[Dbix-class] Inflating/delfating $random objects to/from Database (tested and a bit specific toPostgresql).
Jesper Krogh
jesper at krogh.cc
Sun Apr 30 21:25:53 CEST 2006
Hi.
I just want to thank all involved in DBIx::Class for the impressive work
done.
I've made this small "hack" that basically allows to serialize any
perl-object that YAML can handle properly into the database.
1) Create a new datatype that the inflate/deflate code from:
http://dbix-class.shadowcatsystems.co.uk/index.cgi?TypeBasedInflation
can match up on:
== SQL ==
create domain yaml as text;
== END ==
Attached in init.sql
1a) Create some table with the "yaml-datatype".
create table yamltest ( id serial, data yaml);
2) Create inflate code:
Attached in DB.pm
This code uses the ->column_info magic to extract the datatype from the
database and test if it is the type "yaml".
3) Create the Table-code.
Attached in Yamltest.pm
Testscripts:
#!/usr/bin/perl
#
use strict;
use warnings;
use lib ".";
use DB;
use DateTime;
my $s = DB->connect("dbi:Pg:dbname=jesper","jesper","jesper",{AutoCommit
=> 0});
$s->txn_begin;
$s->resultset("Yamltest")->create({data => DateTime->now()});
$s->txn_commit;
.... also in test.pl ..
And This works... if you apply the YAML-patch attached.
jesper at ubuntulap:~/yamltest$ cat test-dump.pl
#!/usr/bin/perl
#
use strict;
use warnings;
use lib ".";
use DB;
my $s = DB->connect("dbi:Pg:dbname=jesper","jesper","jesper",{AutoCommit
=> 0});
my $rs = $s->resultset("DB::Yamltest")->search({});
while(my $obj = $rs->next()){
print $obj->data()->ymd() . " " . $obj->data() . "\n";
}
.. Also in test-dump.pl ...
Not that in the dump-code I don't have to load the "DateTime" module to
call functions on the returned object.
Well perhaps it's not a big invention, but stuff that automatically
works are always nice :-)
Would it make sense to have a SVN-repo somewhere with test-snippets like
this one?
Jesper
--
Jesper Krogh, jesper at krogh.cc
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DB.pm
Type: application/x-perl
Size: 1655 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060430/c3409285/attachment.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: init.sql
Type: text/x-sql
Size: 119 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060430/c3409285/attachment-0001.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Yamltest.pm
Type: application/x-perl
Size: 210 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060430/c3409285/attachment-0002.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: yaml.patch
Type: text/x-patch
Size: 557 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060430/c3409285/attachment-0003.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-dump.pl
Type: application/x-perl
Size: 290 bytes
Desc: not available
Url : http://lists.rawmode.org/pipermail/dbix-class/attachments/20060430/c3409285/attachment-0004.bin
More information about the Dbix-class
mailing list