[Rt-commit] r5723 - in DBIx-SearchBuilder/trunk: . SearchBuilder/Handle

ruz at bestpractical.com ruz at bestpractical.com
Tue Aug 8 20:38:16 EDT 2006


Author: ruz
Date: Tue Aug  8 20:38:16 2006
New Revision: 5723

Modified:
   DBIx-SearchBuilder/trunk/   (props changed)
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
   DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm

Log:
 r8659 at cubic-pc:  cubic | 2006-08-09 00:25:04 +0400
 ::Handle and ::Handle::Pg
 * move query generation code from Insert to InsertQueryString
   so subclasses may change query
 * as Pg doesn't like "INSERT INTO MyTable;" syntax we subclass
   InsertQueryString method and add 'DEFAULT VALUES' parameter


Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle.pm	Tue Aug  8 20:38:16 2006
@@ -339,32 +339,47 @@
 
 =head2 Insert $TABLE_NAME @KEY_VALUE_PAIRS
 
-Takes a table name and a set of key-value pairs in an array. splits the key value pairs, constructs an INSERT statement and performs the insert. Returns the row_id of this row.
+Takes a table name and a set of key-value pairs in an array.
+Splits the key value pairs, constructs an INSERT statement
+and performs the insert.
+
+Base class return statement handle object, while DB specific
+subclass should return row id.
 
 =cut
 
 sub Insert {
+    my $self = shift;
+    return $self->SimpleQuery( $self->InsertQueryString(@_) );
+}
+
+=head2 InsertQueryString $TABLE_NAME @KEY_VALUE_PAIRS
+
+Takes a table name and a set of key-value pairs in an array.
+Splits the key value pairs, constructs an INSERT statement
+and returns query string and set of bind values.
+
+This method is more useful for subclassing in DB specific
+handles. L</Insert> method is prefered for end users.
+
+=cut
+
+sub InsertQueryString {
     my($self, $table, @pairs) = @_;
     my(@cols, @vals, @bind);
 
-    #my %seen; #only the *first* value is used - allows drivers to specify default
     while ( my $key = shift @pairs ) {
-        my $value = shift @pairs;
-        # next if $seen{$key}++;
         push @cols, $key;
         push @vals, '?';
-        push @bind, $value;  
+        push @bind, shift @pairs;
     }
 
-    my $QueryString =
-        "INSERT INTO $table (". join(", ", @cols). ") VALUES ".
-        "(". join(", ", @vals). ")";
-
-    my $sth = $self->SimpleQuery($QueryString, @bind);
-    return $sth;
+    my $QueryString = "INSERT INTO $table";
+    $QueryString .= " (". join(", ", @cols) .")" if @cols;
+    $QueryString .= " VALUES (". join(", ", @vals). ")" if @vals;
+    return ($QueryString, @bind);
 }
 
-
 =head2 UpdateRecordValue 
 
 Takes a hash with fields: Table, Column, Value PrimaryKeys, and 

Modified: DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm
==============================================================================
--- DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm	(original)
+++ DBIx-SearchBuilder/trunk/SearchBuilder/Handle/Pg.pm	Tue Aug  8 20:38:16 2006
@@ -48,11 +48,12 @@
 
 =head2 Insert
 
-Takes a table name as the first argument and assumes that the rest of the arguments
-are an array of key-value pairs to be inserted.
+Takes a table name as the first argument and assumes
+that the rest of the arguments are an array of key-value
+pairs to be inserted.
 
-In case of isnert failure, returns a Class::ReturnValue object preloaded
-with error info
+In case of insert failure, returns a L<Class::ReturnValue>
+object preloaded with error info.
 
 =cut
 
@@ -61,11 +62,9 @@
     my $self  = shift;
     my $table = shift;
     my %args  = (@_);
-    my $sth   = $self->SUPER::Insert( $table, %args );
 
-    unless ($sth) {
-        return ($sth);
-    }
+    my $sth = $self->SUPER::Insert( $table, %args );
+    return $sth unless $sth;
 
     if ( $args{'id'} || $args{'Id'} ) {
         $self->{'id'} = $args{'id'} || $args{'Id'};
@@ -82,6 +81,19 @@
     return ( $self->{'id'} );
 }
 
+=head2 InsertQueryString
+
+Postgres sepcific overriding method for
+L<DBIx::SearchBuilder::Handle/InsertQueryString>.
+
+=cut
+
+sub InsertQueryString {
+    my $self = shift;
+    my ($query_string, @bind) = $self->SUPER::InsertQueryString( @_ );
+    $query_string .= ' DEFAULT VALUES' unless $query_string =~ /\bVALUES\s+\(/i;
+    return ($query_string, @bind);
+}
 
 =head2 IdSequenceName TABLE
 


More information about the Rt-commit mailing list