[svn:DBD-Pg] r13758 - in DBD-Pg/trunk: . t

[email protected]
Newsgroups gmane.comp.db.postgresql.dbdpg.cvs
Message-ID <[email protected]>
Author: turnstep
Date: Wed Jan 20 13:14:28 2010
New Revision: 13758

Modified:
   DBD-Pg/trunk/Changes
   DBD-Pg/trunk/Pg.pm
   DBD-Pg/trunk/Pg.xs
   DBD-Pg/trunk/dbdimp.c
   DBD-Pg/trunk/dbdimp.h
   DBD-Pg/trunk/t/03dbmethod.t

Log:
First pass at lo_import_with_oid support


Modified: DBD-Pg/trunk/Changes
==============================================================================
--- DBD-Pg/trunk/Changes	(original)
+++ DBD-Pg/trunk/Changes	Wed Jan 20 13:14:28 2010
@@ -1,5 +1,10 @@
 ('GSM' is Greg Sabino Mullane, greg-738XdyZ4GzZWk0Htik3J/[email protected])
 
+2.17.0
+
+  - Added support for new lo_import_with_oid function.
+    [GSM] (CPAN bug #53835)
+
 2.16.1 Released January 20, 2010 (subversion r13756)
 
   - Output error messages in UTF-8 as needed. Reported by 

Modified: DBD-Pg/trunk/Pg.pm
==============================================================================
--- DBD-Pg/trunk/Pg.pm	(original)
+++ DBD-Pg/trunk/Pg.pm	Wed Jan 20 13:14:28 2010
@@ -152,6 +152,7 @@
 		DBD::Pg::db->install_method('pg_lo_close');
 		DBD::Pg::db->install_method('pg_lo_unlink');
 		DBD::Pg::db->install_method('pg_lo_import');
+		DBD::Pg::db->install_method('pg_lo_import_with_oid');
 		DBD::Pg::db->install_method('pg_lo_export');
 
 		return $drh;
@@ -2148,6 +2149,18 @@
 Imports a Unix file as a large object and returns the object id of the new
 object or C<undef> upon failure.
 
+=item lo_import_with_oid
+
+
+  $lobjId = $dbh->pg_lo_import($filename, $OID);
+
+Same as lo_import, but attempts to use the supplied OID as the 
+large object number. If this number is 0, it falls back to the 
+behavior of lo_import (which assigns the next available OID).
+
+This is only available when DBD::Pg is compiled against a Postgres 
+server version 8.4 or later.
+
 =item lo_export
 
   $ret = $dbh->pg_lo_export($lobjId, $filename);

Modified: DBD-Pg/trunk/Pg.xs
==============================================================================
--- DBD-Pg/trunk/Pg.xs	(original)
+++ DBD-Pg/trunk/Pg.xs	Wed Jan 20 13:14:28 2010
@@ -481,6 +481,17 @@
 
 
 void
+pg_lo_import_with_oid(dbh, filename, lobjId)
+	SV * dbh
+	char * filename
+	unsigned int lobjId
+	CODE:
+		const unsigned int ret = (lobjId==0) ? pg_db_lo_import(dbh, filename)
+			: pg_db_lo_import_with_oid(dbh, filename, lobjId);
+		ST(0) = (ret > 0) ? sv_2mortal(newSVuv(ret)) : &PL_sv_undef;
+
+
+void
 pg_lo_export(dbh, lobjId, filename)
 	SV * dbh
 	unsigned int lobjId

Modified: DBD-Pg/trunk/dbdimp.c
==============================================================================
--- DBD-Pg/trunk/dbdimp.c	(original)
+++ DBD-Pg/trunk/dbdimp.c	Wed Jan 20 13:14:28 2010
@@ -4414,6 +4414,34 @@
 }
 
 /* ================================================================== */
+unsigned int pg_db_lo_import_with_oid (SV * dbh, char * filename, unsigned int lobjId)
+{
+
+	Oid loid;
+	dTHX;
+	D_imp_dbh(dbh);
+
+	if (TSTART) TRC(DBILOGFP, "%sBegin pg_db_lo_import_with_oid (filename: %s, oid: %d)\n",
+					THEADER, filename, lobjId);
+
+	if (!pg_db_start_txn(aTHX_ dbh,imp_dbh))
+		return 0; /* No other option, because lo_import* returns an Oid */
+
+	if (TLIBPQ) {
+		TRC(DBILOGFP, "%slo_import_with_oid\n", THEADER);
+	}
+	loid = lo_import_with_oid(imp_dbh->conn, filename, lobjId); /* 0 on error */
+
+	if (DBIc_has(imp_dbh, DBIcf_AutoCommit)) {
+		if (!pg_db_end_txn(aTHX_ dbh, imp_dbh, 0==loid ? 0 : 1))
+			return 0;
+	}
+
+	return loid;
+
+}
+
+/* ================================================================== */
 int pg_db_lo_export (SV * dbh, unsigned int lobjId, char * filename)
 {
 

Modified: DBD-Pg/trunk/dbdimp.h
==============================================================================
--- DBD-Pg/trunk/dbdimp.h	(original)
+++ DBD-Pg/trunk/dbdimp.h	Wed Jan 20 13:14:28 2010
@@ -231,6 +231,8 @@
 
 unsigned int pg_db_lo_import (SV *dbh, char *filename);
 
+unsigned int pg_db_lo_import_with_oid (SV *dbh, char *filename, unsigned int lobjId);
+
 int pg_db_lo_export (SV *dbh, unsigned int lobjId, char *filename);
 
 int pg_db_result (SV *h, imp_dbh_t *imp_dbh);

Modified: DBD-Pg/trunk/t/03dbmethod.t
==============================================================================
--- DBD-Pg/trunk/t/03dbmethod.t	(original)
+++ DBD-Pg/trunk/t/03dbmethod.t	Wed Jan 20 13:14:28 2010
@@ -26,7 +26,7 @@
 if (! defined $dbh) {
 	plan skip_all => 'Connection to database failed, cannot continue testing';
 }
-plan tests => 524;
+plan tests => 529;
 
 isnt ($dbh, undef, 'Connect to database for database handle method testing');
 
@@ -1400,12 +1400,14 @@
 ok (!$result, $t);
 $dbh->rollback();
 
+my $abctext = $pgversion >= 80500 ? 'x6162630a646566' : "abc\ndef";
+
 SKIP: {
 
 	eval {
 		require File::Temp;
 	};
-	$@ and skip ('Must have File::Temp to test pg_lo_import and pg_lo_export', 8);
+	$@ and skip ('Must have File::Temp to test pg_lo_import* and pg_lo_export', 8);
 
 	$t='DB handle method "pg_lo_import" works';
 	my ($fh,$filename) = File::Temp::tmpnam();
@@ -1414,12 +1416,46 @@
 	$handle = $dbh->pg_lo_import($filename);
 	my $objid = $handle;
 	ok ($handle, $t);
-	unlink $filename;
 
 	$t='DB handle method "pg_lo_import" inserts correct data';
 	$SQL = "SELECT data FROM pg_largeobject where loid = $handle";
 	$info = $dbh->selectall_arrayref($SQL)->[0][0];
-	is_deeply ($info, "abc\ndef", $t);
+	is_deeply ($info, $abctext, $t);
+	$dbh->commit();
+
+  SKIP: {
+		if ($pglibversion < 80400) {
+			skip ('Cannot test pg_lo_import_with_oid unless compiled against 8.4 or better server', 5);
+		}
+
+		$t='DB handle method "pg_lo_import_with_oid" works with high number';
+		my $highnumber = 345167;
+		$dbh->pg_lo_unlink($highnumber);
+		$dbh->commit();
+		my $thandle = $dbh->pg_lo_import_with_oid($filename, $highnumber);
+		is ($thandle, $highnumber, $t);
+		ok ($thandle, $t);
+
+		$t='DB handle method "pg_lo_import_with_oid" inserts correct data';
+		$SQL = "SELECT data FROM pg_largeobject where loid = $thandle";
+		$info = $dbh->selectall_arrayref($SQL)->[0][0];
+		is_deeply ($info, $abctext, $t);
+
+		$t='DB handle method "pg_lo_import_with_oid" fails when given already used number';
+		eval {
+			$thandle = $dbh->pg_lo_import_with_oid($filename, $objid);
+		};
+		is ($thandle, undef, $t);
+		$dbh->rollback();
+
+		$t='DB handle method "pg_lo_import_with_oid" falls back to lo_import when number is 0';
+		eval {
+			$thandle = $dbh->pg_lo_import_with_oid($filename, 0);
+		};
+		ok ($thandle, $t);
+	}
+
+	unlink $filename;
 
 	$t='DB handle method "pg_lo_open" works after "pg_lo_insert"';
 	$handle = $dbh->pg_lo_open($handle, $R);
@@ -1517,7 +1553,7 @@
 	$sth = $dbh->prepare($SQL);
 	$sth->execute($handle);
 	$info = $sth->fetchall_arrayref()->[0][0];
-	is_deeply ($info, "abc\ndef", $t);
+	is_deeply ($info, $abctext, $t);
 
 	$t='DB handle method "pg_lo_import" works (AutoCommit on, begin_work called, no command)';
 	$dbh->begin_work();
@@ -1525,7 +1561,7 @@
 	ok ($handle, $t);
 	$sth->execute($handle);
 	$info = $sth->fetchall_arrayref()->[0][0];
-	is_deeply ($info, "abc\ndef", $t);
+	is_deeply ($info, $abctext, $t);
 	$dbh->rollback();
 
 	$t='DB handle method "pg_lo_import" works (AutoCommit on, begin_work called, no command, rollback)';
@@ -1544,7 +1580,7 @@
 	ok ($handle, $t);
 	$sth->execute($handle);
 	$info = $sth->fetchall_arrayref()->[0][0];
-	is_deeply ($info, "abc\ndef", $t);
+	is_deeply ($info, $abctext, $t);
 	$dbh->rollback();
 
 	$t='DB handle method "pg_lo_import" works (AutoCommit on, begin_work called, second command, rollback)';
@@ -1564,7 +1600,7 @@
 	ok ($handle, $t);
 	$sth->execute($handle);
 	$info = $sth->fetchall_arrayref()->[0][0];
-	is_deeply ($info, "abc\ndef", $t);
+	is_deeply ($info, $abctext, $t);
 
 	$t='DB handle method "pg_lo_import" works (AutoCommit not on, second command)';
 	$dbh->rollback();
@@ -1573,7 +1609,7 @@
 	ok ($handle, $t);
 	$sth->execute($handle);
 	$info = $sth->fetchall_arrayref()->[0][0];
-	is_deeply ($info, "abc\ndef", $t);
+	is_deeply ($info, $abctext, $t);
 
 	unlink $filename;
 	$dbh->{AutoCommit} = 1;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.