This should show you how to store any complex structure into an SQL table (I used SQLite in the example). I chose a hash as the top structure:
#!/usr/bin/perl use warnings; use strict; use DBI; use Data::Dumper; use constant { KEY => 0, VALUE => 1, WHERE => 2, NEW => 3, }; sub store { my ($db, $struct) = @_; $db->do('DROP TABLE IF EXISTS t'); $db->do('CREATE TABLE t (key VARCHAR, value VARCHAR, ref INT)'); my $store = $db->prepare('INSERT INTO t VALUES (?, ?, ?)'); while (my ($key, $value) = each %$struct) { _store($db, $store, $key, $value); } } sub _store { my ($db, $store, $key, $value, $where) = @_; if (my $ref = ref $value) { $store->execute($key, $ref, $where); my $new = $db->last_insert_id((q()) x 4); if ('HASH' eq $ref) { while (my ($k, $v) = each %$value) { _store($db, $store, $k, $v, $new); } } elsif ('ARRAY' eq $ref) { _store($db, $store, $_, $value->[$_], $new) for 0 .. $#{$v +alue}; } } else { $store->execute($key, $value, $where); } } sub retrieve { my ($db) = @_; my %hash; my $select = $db->prepare('SELECT ROWID, key, value, ref FROM t OR +DER BY ROWID'); $select->execute; while (my @row = $select->fetchrow_array) { $hash{ $row[0] } = [ @row[1 .. 3] ]; } my %return; for my $key (sort { $b <=> $a } keys %hash) { my $new = defined $hash{$key}[NEW] ? $hash{$key}[NEW] : $hash{ +$key}[VALUE]; if (defined $hash{$key}[WHERE]) { my $where = $hash{ $hash{$key}[WHERE] }; if ('ARRAY' eq $where->[VALUE]) { unshift @{ $where->[NEW] }, $new; } else { $where->[NEW]{ $hash{$key}[KEY] } = $new; } } else { $return{$hash{$key}[KEY]} = $new; } } return %return; } my %struct = ( sv0 => 100, sv1 => 'string', hv => { l1sv => 'deeper', l1hv => { x => 10, y => 20 }, l1av => [qw/1 2 3 A B C/] }, av => [ 5, 'e', { k0 => 'v0', k1 => 'v1' }, [qw/Mercury Venus Mars/] ], ); my $db = 'DBI'->connect('dbi:SQLite:dbname=1.db', q(), q()); store($db, \%struct); %struct = retrieve($db); print Dumper \%struct;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

In reply to Re: mapping complex structures to mysql tables by choroba
in thread mapping complex structures to mysql tables by niubi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.