X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract%2FReference.pm;h=4ec6df70970606c64d573fea5e0c57f9a2a6ba04;hb=e376aa8053a377ca85e9a16205132b30436b2ce5;hp=41d95d0b39e52bbd8677f35fe975fbd082fc6001;hpb=5c04ab130a3e1147db69b2f523edea55e52f63ea;p=scpubgit%2FQ-Branch.git diff --git a/lib/SQL/Abstract/Reference.pm b/lib/SQL/Abstract/Reference.pm index 41d95d0..4ec6df7 100644 --- a/lib/SQL/Abstract/Reference.pm +++ b/lib/SQL/Abstract/Reference.pm @@ -744,6 +744,31 @@ treated as rows: VALUES (?, ?), (?, ?) [ 1, 2, 3, 4 ] +=head2 list + +Expects a value or an arrayref of values, expands them, and returns just +the expanded aqt for a single entry or a comma operator for multiple: + + # expr + { -list => [ { -ident => 'foo' } ] } + + # aqt + { -op => [ ',', { -ident => [ 'foo' ] } ] } + + # query + foo + [] + + # expr + { -list => [ { -ident => 'foo' }, { -ident => 'bar' } ] } + + # aqt + { -op => [ ',', { -ident => [ 'foo' ] }, { -ident => [ 'bar' ] } ] } + + # query + foo, bar + [] + =head2 between op The RHS of between must either be a pair of exprs/plain values, or a single @@ -984,6 +1009,69 @@ an order by direction: ORDER BY foo, bar DESC, MAX(baz) [] +=head2 + +An insert node accepts an into/target clause, a fields clause, a values/from +clause, and a returning clause. + +The target clause is expanded with an ident default. + +The fields clause is expanded as a list expression if an arrayref, and +otherwise passed through. + +The from clause may either be an expr, a literal, an arrayref of column +values, or a hashref mapping colum names to values. + +The returning clause is expanded as a list expr with an ident default. + + # expr + { -insert => { + into => 'foo', + returning => 'id', + values => { bar => 'yay', baz => 'argh' }, + } } + + # aqt + { -insert => { + fields => + { -row => [ { -ident => [ 'bar' ] }, { -ident => [ 'baz' ] } ] }, + from => { -values => [ { -row => [ + { -bind => [ 'bar', 'yay' ] }, + { -bind => [ 'baz', 'argh' ] }, + ] } ] }, + returning => { -op => [ ',', { -ident => [ 'id' ] } ] }, + target => { -ident => [ 'foo' ] }, + } } + + # query + INSERT INTO foo (bar, baz) VALUES (?, ?) RETURNING id + [ 'yay', 'argh' ] + + # expr + { -insert => { + fields => [ 'bar', 'baz' ], + from => { -select => { _ => [ 'bar', 'baz' ], from => 'other' } }, + into => 'foo', + } } + + # aqt + { -insert => { + fields => { -row => [ { -op => + [ ',', { -ident => [ 'bar' ] }, { -ident => [ 'baz' ] } ] + } ] }, + from => { -select => { + from => { -ident => [ 'other' ] }, + select => { -op => + [ ',', { -ident => [ 'bar' ] }, { -ident => [ 'baz' ] } ] + }, + } }, + target => { -ident => [ 'foo' ] }, + } } + + # query + INSERT INTO foo (bar, baz) SELECT bar, baz FROM other + [] + =head2 update An update node accepts update/target (either may be used at expansion time), @@ -1022,7 +1110,7 @@ The returning clause is expanded as a list expr with an ident default. ] }, ] }, ] }, - target => { -from_list => [ { -ident => [ 'foo' ] } ] }, + target => { -ident => [ 'foo' ] }, where => { -op => [ 'not', { -ident => [ 'quux' ] } ] }, } } @@ -1030,4 +1118,34 @@ The returning clause is expanded as a list expr with an ident default. UPDATE foo SET bar = ?, baz = baz + ? WHERE (NOT quux) RETURNING id, baz [ 3, 1 ] +=head2 delete + +delete accepts from/target, where, and returning clauses. + +The target clause is expanded with an ident default. + +The where clauses is expanded as a normal expr. + +The returning clause is expanded as a list expr with an ident default. + + # expr + { -delete => { + from => 'foo', + returning => 'id', + where => { bar => { '<' => 10 } }, + } } + + # aqt + { -delete => { + returning => { -op => [ ',', { -ident => [ 'id' ] } ] }, + target => { -op => [ ',', { -ident => [ 'foo' ] } ] }, + where => { -op => + [ '<', { -ident => [ 'bar' ] }, { -bind => [ 'bar', 10 ] } ] + }, + } } + + # query + DELETE FROM foo WHERE bar < ? RETURNING id + [ 10 ] + =cut