Skip to content
Browse files

Add relation types: default to multiple (1:m), accepts single (1:1)

Signed-off-by: Pedro Melo <melo@simplicidade.org>
  • Loading branch information...
1 parent 59b357f commit 62dbc8619230b46ea94b1ff2e2242f0ed3166780 @melo committed Jun 1, 2012
Showing with 50 additions and 10 deletions.
  1. +19 −4 lib/DBIx/Nesting.pm
  2. +31 −6 t/10-compiler.t
View
23 lib/DBIx/Nesting.pm
@@ -40,7 +40,7 @@ sub _emit_meta_block {
$p_o_var = '@res' unless $p_o_var;
$p_s_var = '$seen' unless $p_s_var;
- my ($id, $flds, $key, $nest, $prfx) = @{$meta}{qw(id fields key nest prefix)};
+ my ($id, $flds, $key, $type, $nest, $prfx) = @{$meta}{qw(id fields key type nest prefix)};
my $o_var = "\$o$id";
my $s_var = "\$s$id";
my $f_var = "\$f$id";
@@ -69,7 +69,6 @@ sub _emit_meta_block {
$p .= "unless (\%$s_var) {";
## Not seen yet, so prep our o_var
- $p_o_var = "\@{$p_o_var}" unless substr($p_o_var, 0, 1) eq '@';
if ($flds) {
$p .= "$o_var = {";
$p .= "'$_->{name}'=>$r_var\->{'$_->{col}'}," for @$flds;
@@ -83,8 +82,17 @@ sub _emit_meta_block {
. "$o_var\->{$loop_var\->{name}} = $r_var\->{$loop_var\->{col}};" . '}';
}
- ## per relation-type manipulation: 1:m only for now
- $p .= "push $p_o_var, $s_var\->{o} = $o_var;";
+ ## per relation-type manipulation
+ if ($type eq 'multiple') {
+ $p_o_var = "\@{$p_o_var}" unless substr($p_o_var, 0, 1) eq '@';
+ $p .= "push $p_o_var, $s_var\->{o} = $o_var;";
+ }
+ elsif ($type eq 'single') {
+ $p .= "$p_o_var = $s_var\->{o} = $o_var;";
+ }
+ else {
+ die "Unkonwn relation type '$type'";
+ }
## .. and o_var is set now, so make sure we are using the correct one
$p .= '}' # ends the unless (%$s_var)
@@ -143,6 +151,13 @@ sub _expand_meta_with_defaults {
}
$cm{key} = $key if $key;
+ # Relation type
+ my $type = 'multiple';
+ if (exists $meta->{type}) {
+ $type = $meta->{type};
+ }
+ $cm{type} = $type;
+
# Cleanup nested meta
$cm{nest} = {};
if (exists $meta->{nest}) {
View
37 t/10-compiler.t
@@ -53,6 +53,7 @@ __DATA__
{ fields => [{ name => 'k', col => 'k' }, { name => 'n', col => 'n' }],
key => [{ name => 'k', col => 'k' }, { name => 'n', col => 'n' }],
id => 1,
+ type => 'multiple',
nest => {},
},
@@ -70,6 +71,7 @@ __DATA__
{ fields => [{ name => 'k', col => 'k' }, { name => 'n', col => 'n', label => 'N' }],
key => [{ name => 'k', col => 'k' }, { name => 'n', col => 'n', label => 'N' }],
id => 1,
+ type => 'multiple',
nest => {},
}
@@ -87,6 +89,7 @@ __DATA__
{ fields => [{ name => 'k', col => 'k' }, { name => 'n', col => 'n', label => 'N' }],
key => [{ name => 'k', col => 'k' }],
id => 1,
+ type => 'multiple',
nest => {},
}
@@ -108,6 +111,7 @@ __DATA__
],
key => [{ name => 'k', col => 'k' }, { name => 's', col => 's' }],
id => 1,
+ type => 'multiple',
nest => {},
}
@@ -126,6 +130,7 @@ __DATA__
key => [{ name => 'k', col => 'p_k' }],
id => 1,
prefix => 'p_',
+ type => 'multiple',
nest => {},
}
@@ -143,6 +148,7 @@ __DATA__
{ key => [{ name => 'k', col => 'p1_k' }],
id => 1,
prefix => 'p1_',
+ type => 'multiple',
nest => {},
}
@@ -159,6 +165,7 @@ __DATA__
{ id => 1,
prefix => 'p1_',
+ type => 'multiple',
nest => {},
}
@@ -175,10 +182,12 @@ __DATA__
{ id => 1,
prefix => 'p1_',
+ type => 'multiple',
nest => {
x => {
id => 2,
prefix => 'p2_',
+ type => 'multiple',
nest => {},
},
},
@@ -190,7 +199,7 @@ __DATA__
### _expand_meta_with_defaults complex
> for _expand_meta_with_defaults
-> msg two col set, 1:M relation, explicit pk, automatic prefix
+> msg two col set, multiple relation, explicit pk, automatic prefix
>+ meta
{ fields => [qw(k n)],
@@ -209,12 +218,14 @@ __DATA__
key => [{ name => 'k', col => 'p1_k' }],
id => 1,
prefix => 'p1_',
+ type => 'multiple',
nest => {
s => {
fields => [{ name => 'k', col => 'p2_k' }, { name => 't', col => 'p2_t' }],
key => [{ name => 'k', col => 'p2_k' }],
id => 2,
prefix => 'p2_',
+ type => 'multiple',
nest => {},
},
},
@@ -224,7 +235,7 @@ __DATA__
> for _expand_meta_with_defaults
-> msg several col set, 1:M relations, several levels, explicit pk, automatic prefix
+> msg several col set, multiple relations, several levels, explicit pk, automatic prefix
>+ meta
{ fields => [qw(k n)],
@@ -255,45 +266,52 @@ __DATA__
key => [{ name => 'k', col => 'p1_k' }],
id => 1,
prefix => 'p1_',
+ type => 'multiple',
nest => {
s => {
fields => [{ name => 'k', col => 'p2_k' }, { name => 's', col => 'p2_s' }],
key => [{ name => 'k', col => 'p2_k' }],
id => 2,
prefix => 'p2_',
+ type => 'multiple',
nest => {},
},
t => {
fields => [{ name => 'tid', col => 'p3_tid' }, { name => 't', col => 'p3_t' }],
key => [{ name => 'tid', col => 'p3_tid' }],
id => 3,
prefix => 'p3_',
+ type => 'multiple',
nest => {
x => {
fields => [{ name => 'xid', col => 'p4_xid' }, { name => 'x', col => 'p4_x' }],
key => [{ name => 'xid', col => 'p4_xid' }, { name => 'x', col => 'p4_x' }],
id => 4,
prefix => 'p4_',
+ type => 'multiple',
nest => {},
},
y => {
fields => [{ name => 'yid', col => 'p5_yid' }, { name => 'y', col => 'p5_y' }],
key => [{ name => 'yid', col => 'p5_yid' }, { name => 'y', col => 'p5_y' }],
id => 5,
prefix => 'p5_',
+ type => 'multiple',
nest => {},
},
z => {
fields => [{ name => 'zid', col => 'p6_zid' }, { name => 'z', col => 'p6_z' }],
key => [{ name => 'zid', col => 'p6_zid' }, { name => 'z', col => 'p6_z' }],
id => 6,
prefix => 'p6_',
+ type => 'multiple',
nest => {
w => {
fields => [{ name => 'wid', col => 'p7_wid' }, { name => 'w', col => 'p7_w' }],
key => [{ name => 'wid', col => 'p7_wid' }, { name => 'w', col => 'p7_w' }],
id => 7,
prefix => 'p7_',
+ type => 'multiple',
nest => {},
},
},
@@ -307,7 +325,7 @@ __DATA__
> for _expand_meta_with_defaults
-> msg several col set, 1:M relations, several levels, no fields, some pk, automatic prefix
+> msg several col set, multiple relations, several levels, no fields, some pk, automatic prefix
>+ meta
{ key => 'k',
@@ -329,35 +347,42 @@ __DATA__
{ key => [{ name => 'k', col => 'p1_k' }],
id => 1,
prefix => 'p1_',
+ type => 'multiple',
nest => {
s => {
key => [{ name => 'k', col => 'p2_k' }],
id => 2,
prefix => 'p2_',
+ type => 'multiple',
nest => {},
},
t => {
key => [{ name => 'tid', col => 'p3_tid' }],
id => 3,
prefix => 'p3_',
+ type => 'multiple',
nest => {
x => {
id => 4,
prefix => 'p4_',
+ type => 'multiple',
nest => {},
},
y => {
id => 5,
prefix => 'p5_',
+ type => 'multiple',
nest => {},
},
z => {
id => 6,
prefix => 'p6_',
+ type => 'multiple',
nest => {
w => {
id => 7,
prefix => 'p7_',
+ type => 'multiple',
nest => {},
},
},
@@ -586,12 +611,12 @@ __DATA__
> for _emit_code
-> msg complex multi-nested meta, without fields
+> msg complex multi-nested meta, without fields, mixed relation types
>+ meta
{ key => 'k',
nest => {
- s => { key => 'k' },
+ s => { type => 'single', key => 'k' },
t => {
key => 'tid',
nest => {
@@ -636,7 +661,7 @@ __DATA__
for my $f (@$f2) {\
$o2->{$f->{name}} = $r->{$f->{col}};\
}\
- push @{$o1->{'s'}}, $s2->{o} = $o2;\
+ $o1->{'s'} = $s2->{o} = $o2;\
} \
$o2 = $s2->{o};\
\

0 comments on commit 62dbc86

Please sign in to comment.
Something went wrong with that request. Please try again.