From: <chr...@us...> - 2006-05-25 23:39:36
|
Revision: 887 Author: chromatic Date: 2006-05-25 16:39:26 -0700 (Thu, 25 May 2006) ViewCVS: http://svn.sourceforge.net/everydevel/?rev=887&view=rev Log Message: ----------- r17516@windwheel: chromatic | 2006-05-25 16:38:52 -0700 Moved C<lastValue()> method up into Everything::DB. Modified Paths: -------------- trunk/ebase/lib/Everything/DB/Pg.pm trunk/ebase/lib/Everything/DB/mysql.pm trunk/ebase/lib/Everything/DB.pm trunk/ebase/t/DB/mysql.t Property Changed: ---------------- trunk/ebase/ Property changes on: trunk/ebase ___________________________________________________________________ Name: svk:merge - a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17514 + a6810612-c0f9-0310-9d3e-a9e4af8c5745:/ebase/offline:17516 Modified: trunk/ebase/lib/Everything/DB/Pg.pm =================================================================== --- trunk/ebase/lib/Everything/DB/Pg.pm 2006-05-25 22:47:28 UTC (rev 886) +++ trunk/ebase/lib/Everything/DB/Pg.pm 2006-05-25 23:39:26 UTC (rev 887) @@ -39,31 +39,6 @@ } ############################################################################# -# Sub -# lastValue -# -# Purpose -# Return the last sequence/auto_increment value inserted into -# the database. -# -# Parameters -# table - the table (this MUST be the table used in the last query) -# field - the auto_increment field -# -# Returns -# The last sequence/auto_increment value inserted into the -# database by this process/connection. undef if error. -# -sub lastValue -{ - my ( $this, $table, $field ) = @_; - - return $this->{dbh} - ->do( "SELECT currval(\'$table" . "_" . $field . "_seq\')" ) - ->fetchrow(); -} - -############################################################################# # Sub # getFieldsHash # Modified: trunk/ebase/lib/Everything/DB/mysql.pm =================================================================== --- trunk/ebase/lib/Everything/DB/mysql.pm 2006-05-25 22:47:28 UTC (rev 886) +++ trunk/ebase/lib/Everything/DB/mysql.pm 2006-05-25 23:39:26 UTC (rev 887) @@ -50,32 +50,6 @@ or die "Unable to get database connection!"; } -=head2 C<lastValue> - -Returns the last sequence/auto_increment value inserted into the -database. This will return undef on error. - -=over 4 - -=item * $table - -the table (this MUST be the table used in the last query) - -=item * $field - -the auto_increment field - -=back - -=cut - -sub lastValue -{ - my ( $this, $table, $field ) = @_; - - return $this->sqlSelect("LAST_INSERT_ID()"); -} - =head2 C<getFieldsHash> Given a table name, returns the names of fields. If C<$getHash> is true, it Modified: trunk/ebase/lib/Everything/DB.pm =================================================================== --- trunk/ebase/lib/Everything/DB.pm 2006-05-25 22:47:28 UTC (rev 886) +++ trunk/ebase/lib/Everything/DB.pm 2006-05-25 23:39:26 UTC (rev 887) @@ -66,6 +66,32 @@ return $this->{dbh}; } +=head2 C<lastValue> + +Returns the last sequence/auto_increment value inserted into the +database. This will return undef on error. + +=over 4 + +=item * $table + +the table (this MUST be the table used in the last query) + +=item * $field + +the auto_increment field + +=back + +=cut + +sub lastValue +{ + my ( $this, $table, $field ) = @_; + + return $this->getDatabaseHandle()->last_insert_id(); +} + =head2 C<sqlDelete> Quickie wrapper for deleting a row or rows from a specified table. Modified: trunk/ebase/t/DB/mysql.t =================================================================== --- trunk/ebase/t/DB/mysql.t 2006-05-25 22:47:28 UTC (rev 886) +++ trunk/ebase/t/DB/mysql.t 2006-05-25 23:39:26 UTC (rev 887) @@ -3,9 +3,10 @@ use strict; use warnings; -use Test::More tests => 79; +use Test::More tests => 77; use Test::Exception; use Test::MockObject; +use Test::MockObject::Extends; # temporarily avoid sub redefined warnings my $mock = Test::MockObject->new(); @@ -35,13 +36,15 @@ is( $fake->{dbh}, 'new dbh', '... setting dbh field if connection succeeds' ); can_ok( $package, 'lastValue' ); -$mock->set_always( 'sqlSelect', 'insert id' ); -my $result = Everything::DB::mysql::lastValue($mock); -my ( $method, $args ) = $mock->next_call(); -is( $method, 'sqlSelect', 'lastValue() should fetch from the database' ); -is( $args->[1], 'LAST_INSERT_ID()', '... the last inserted id' ); -is( $result, 'insert id', '... returning the results' ); +{ + my $mock = Test::MockObject::Extends->new( Everything::DB::mysql->new() ); + $mock->set_always( getDatabaseHandle => $mock ) + ->set_always( last_insert_id => 'insert id' ); + my $result = $mock->lastValue(); + is( $result, 'insert id', 'lastValue() should fetch the last inserted id' ); +} + my $fields = [ { Field => 'foo', foo => 1 }, { Field => 'bar', bar => 2 } ]; can_ok( $package, 'getFieldsHash' ); @@ -50,7 +53,7 @@ ->set_true('execute')->set_series( 'fetchrow_hashref', @$fields ); my @result = Everything::DB::mysql::getFieldsHash( $mock, 'table' ); -( $method, $args ) = $mock->next_call(); +my ( $method, $args ) = $mock->next_call(); is( $method, 'getNode', 'getFieldsHash() should fetch node' ); is( join( '-', @$args[ 1, 2 ] ), 'table-dbtable', '... by name, of dbtable type' ); @@ -76,7 +79,7 @@ $mock->set_always( prepare => $mock )->set_true('execute') ->set_series( 'fetchrow', 1, 2, 'target' )->set_true('finish'); -$result = Everything::DB::mysql::tableExists( $mock, 'target' ); +my $result = Everything::DB::mysql::tableExists( $mock, 'target' ); ( $method, $args ) = $mock->next_call(); is( $method, 'prepare', 'tableExists should check with the database' ); is( $args->[1], 'show tables', '... fetching available table names' ); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |