[Dbix-class] first Muldis DB example code (was Re: SQL
Generation with SQL::DB)
Darren Duncan
darren at DarrenDuncan.net
Mon Sep 17 10:58:14 GMT 2007
At 2:48 AM -0700 9/17/07, Darren Duncan wrote:
> my $r1_ast = [ 'Relation', 'sys.Core.Relation.Relation', [
> {
> 'x' => [ 'PInt', 'perl_pint', 4 ],
> 'y' => [ 'PInt', 'perl_pint', 7 ],
> },
<snip>
> my $r2_ast = [ 'Relation', 'sys.Core.Relation.Relation', [
> {
> 'y' => [ 'PInt', 'perl_pint', 5 ],
> 'z' => [ 'PInt', 'perl_pint', 6 ],
> },
<snip>
> my $r3 = $dbms->call_func( 'sys.Core.Relation.join', {
> 'topic' => [ 'QuasiSet', 'sys.Core.Spec.QuasiSetOfRelation', [
> $r1, # or $r1_ast
> $r2, # or $r2_ast
> ],
> } );
> my $r3_ast = $r3->fetch();
>
> # Then $r3_ast contains:
> # [ 'Relation', 'sys.Core.Relation.Relation', [
> # {
> # 'x' => [ 'PInt', 'perl_pint', 3 ],
> # 'y' => [ 'PInt', 'perl_pint', 2 ],
> # 'z' => [ 'PInt', 'perl_pint', 1 ],
> # },
<snip>
>That example declares two Perl variables containing rowset/table
>data, then does a (N-ary) relational join (natural inner join) on
>them, producing a third Perl variable holding the rowset/table data
>of the result.
As an addendum, if you couldn't figure it out for yourself ...
If this example were against actual SQL tables mirroring $r1 and $r2,
then the generated SQL for the above join would look like:
SELECT DISTINCT r1.x AS x, r1.y AS y, r2.z AS z
FROM r1 INNER JOIN r2 USING (y)
-- Darren Duncan
More information about the DBIx-Class
mailing list