bliako has asked for the wisdom of the Perl Monks concerning the following question:
Hello Monks
I am trying to parse a (My)SQL INSERT statement's VALUES section and convert it to a Perl Data Structure. Ideally into an array of hashes, each hash being a table row. My data is produced by mysqldump --no-tablespaces.
The order of data in each of the VALUES row (within a pair of brackets) is the same as the order of columns specified in the CREATE just above. So I parse the CREATE (lamely) in order to find the column names. Then I must parse, edit: and convert into a Perl array, the VALUES section which looks like a Perl array, (1,2,3),(4,5,6), and so, lazily, I do an eval which relieves me from dealing with what's inside the VALUES quoted strings (which can be anything, html etc.)
So, my question is what is the best way to parse (My)SQL and create an array of hashes for each table's data+structure. Ideally I would prefer a CPAN module (I tried SQL::Parser but it failed to read the whole SQL dump, complained about "---"). But my eval works with my dump (so far).
Here is a gist of what I do in a large scale:
my $tablename = 'mytable'; my $sqlcontent = <<'EOS'; /* create statement before the insert */ CREATE TABLE `mytable` ( `id` int, `content` varchar(1000) ) ENGINE=InnoDB; INSERT INTO `mytable` VALUES (1,'abc\"xyz\"'),(2,'blah') ; UNLOCK TABLES; EOS my ($statements, $values) = $sqlcontent =~ /CREATE\s+TABLE\s+`${tablename}`\s+(\(.+\))\s+ENGINE=.+?;\s+ .+?INSERT\s+INTO\s+`${tablename}`\s+VALUES\s+(.+?);\s+ .*?UNLOCK\s+TABLES; /sx ; # Below is problematic but works for restricted cases my @values = eval $values; die $@ if $@; print join("\n", @values)."\n";
This small issue is part of my efforts to upgrade a DB schema (by adding or removing table columns), processing the database data and inserting it back into the DB
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Convert an SQL insert into a Perl data structure
by hippo (Archbishop) on Apr 05, 2023 at 11:46 UTC | |
by bliako (Abbot) on Apr 05, 2023 at 12:01 UTC | |
|
Re: Convert an SQL insert into a Perl data structure
by LanX (Saint) on Apr 05, 2023 at 11:24 UTC | |
by bliako (Abbot) on Apr 05, 2023 at 11:57 UTC |