[Jifty-commit] r2711 - Jifty-DBI/trunk/lib/Jifty/DBI

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Mon Jan 29 04:55:46 EST 2007


Author: audreyt
Date: Mon Jan 29 04:55:46 2007
New Revision: 2711

Modified:
   Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm

Log:
* Rescind __create and instead move it to Handle->_create.
* Also cleans up misleading DBIx::SearchBuilder-era docs in Handle.pm.

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Handle.pm	Mon Jan 29 04:55:46 2007
@@ -61,9 +61,10 @@
     return $self;
 }
 
-=head2 connect PARAMHASH: Driver, Database, Host, User, Password
+=head2 connect PARAMHASH
 
-Takes a paramhash and connects to your DBI datasource. 
+Takes a paramhash and connects to your DBI datasource, with the keys C<driver>,
+C<database>, C<host>, C<user> and C<password>.
 
 If you created the handle with 
      Jifty::DBI::Handle->new
@@ -134,6 +135,8 @@
 
     my $driver = shift;
     my $class  = 'Jifty::DBI::Handle::' . $driver;
+
+    local $@;
     eval "require $class";
     return if $@;
 
@@ -332,6 +335,27 @@
     return ( $DBIHandle{$self} ||= $PrevHandle );
 }
 
+=head2 delete $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 DELETE statement and performs the delete. Returns the row_id of this row.
+
+=cut
+
+sub delete {
+    my ( $self, $table, @pairs ) = @_;
+
+    my @bind  = ();
+    my $where = 'WHERE ';
+    while (my $key = shift @pairs) {
+        $where .= $key . "=?" . " AND ";
+        push( @bind, shift(@pairs) );
+    }
+
+    $where =~ s/AND $//;
+    my $query_string = "DELETE FROM " . $table . ' ' . $where;
+    $self->simple_query( $query_string, @bind );
+}
+
 =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.
@@ -363,13 +387,13 @@
 
 =head2 update_record_value 
 
-Takes a hash with columns: Table, Column, Value PrimaryKeys, and 
-IsSQLFunction.  Table, and Column should be obvious, Value is where you 
-set the new value you want the column to have. The primary_keys column should 
-be the lvalue of Jifty::DBI::Record::PrimaryKeys().  Finally 
-IsSQLFunction is set when the Value is a SQL function.  For example, you 
-might have ('Value'=>'PASSWORD(string)'), by setting IsSQLFunction that 
-string will be inserted into the query directly rather then as a binding. 
+Takes a hash with columns: C<table>, C<column>, C<value>, C<primary_keys>, and
+C<is_sql_function>.  The first two should be obvious; C<value> is where you 
+set the new value you want the column to have. The C<primary_keys> column should 
+be the lvalue of Jifty::DBI::Record::PrimaryKeys().  Finally ,
+C<is_sql_function> is set when the Value is a SQL function.  For example, you 
+might have C<< value => 'PASSWORD(string)' >>, by setting C<is_sql_function> to true,
+that string will be inserted into the query directly rather then as a binding. 
 
 =cut
 
@@ -411,8 +435,9 @@
 
 =head2 update_table_value table COLUMN NEW_value RECORD_ID IS_SQL
 
-Update column COLUMN of table table where the record id = RECORD_ID.  if IS_SQL is set,
-don\'t quote the NEW_VALUE
+Update column COLUMN of table table where the record id = RECORD_ID.
+
+If IS_SQL is set, don't quote the NEW_VALUE.
 
 =cut
 
@@ -480,6 +505,8 @@
         $basetime = Time::HiRes::time();
     }
     my $executed;
+
+    local $@;
     {
         no warnings 'uninitialized';    # undef in bind_values makes DBI sad
         eval { $executed = $sth->execute(@bind_values) };

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm	Mon Jan 29 04:55:46 2007
@@ -969,19 +969,6 @@
         return ($before_ret) unless ($before_ret);
     }
 
-    my $ret = $self->__create(%attribs);
-    $self->after_create( \$ret ) if $self->can('after_create');
-    if ($class) {
-        $self->load_by_cols(id => $ret);
-        return ($self);
-    }
-    else {
-     return ($ret);
-    }
-}
-
-sub __create {
-    my ($self, %attribs) = @_;
     foreach my $column_name ( keys %attribs ) {
         my $column = $self->column($column_name);
         unless ($column) {
@@ -1025,7 +1012,15 @@
         }
     }
 
-    return $self->_handle->insert( $self->table, %attribs );
+    my $ret = $self->_handle->insert( $self->table, %attribs );
+    $self->after_create( \$ret ) if $self->can('after_create');
+    if ($class) {
+        $self->load_by_cols(id => $ret);
+        return ($self);
+    }
+    else {
+     return ($ret);
+    }
 }
 
 =head2 delete
@@ -1074,17 +1069,8 @@
     #TODO Update internal data structure
 
     ## Constructs the where clause.
-    my @bind  = ();
     my %pkeys = $self->primary_keys();
-    my $where = 'WHERE ';
-    foreach my $key ( keys %pkeys ) {
-        $where .= $key . "=?" . " AND ";
-        push( @bind, $pkeys{$key} );
-    }
-
-    $where =~ s/AND\s$//;
-    my $query_string = "DELETE FROM " . $self->table . ' ' . $where;
-    my $return       = $self->_handle->simple_query( $query_string, @bind );
+    my $return       = $self->_handle->delete( $self->table, $self->primary_keys );
 
     if ( UNIVERSAL::isa( 'Class::ReturnValue', $return ) ) {
         return ($return);


More information about the Jifty-commit mailing list