Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf478692

Browse files
committed
The attached file: SerializePatch2.tgz, contains a patch for
org.postgresql.util.Serialize and org.postgresql.jdbc2.PreparedStatementthat fixes the ability to "serialize" a simple java class into apostgres table.The current cvs seems completely broken in this support, so the patchputs it into working condition, granted that there are many limitationswith serializing java classes into Postgres.The code to do serialize appears to have been in the driver sincePostgres 6.4, according to some comments in the source. My code is notadding any totally new ability to the driver, rather just fixing whatis there so that it actually is usable. I do not think that it shouldaffect any existing functions of the driver that people regularlydepend on.The code is activated if you use jdbc2.PreparedStatement and try tosetObject some java class type that is unrecognized, like not String ornot some other primitive type. This will cause a sequence of functioncalls that results in an instance of Serialize being instantiated forthe class type passed. The Serialize constructor will query pg_classto see if it can find an existing table that matches the name of thejava class. If found, it will continue and try to use the table tostore the object, otherwise an SQL exception is thrown and no harm isdone. Serialize.create() has to be used to setup the table for a javaclass before anything can really happen with this code other than anSQLException (unless by some freak chance a table exists that it thinksit can use).I saw a difference in Serialize.java between 7.1.3 and 7.2devel that Ididn't notice before, so I had to redo my changes from the 7.2develversion (why I had to resend this patch now). I was missing thefixString stuff, which is nice and is imporant to ensure the insertswill not fail due to embedded single quote or unescaped backslashes. Ichanged that fixString function in Serialize just a little since thereis no need to muddle with escaping newlines: only escaping single quoteand literal backslashes is needed. Postgres appears to insert newlineswithin strings without trouble.
1 parentca86791 commitf478692

File tree

2 files changed

+360
-252
lines changed

2 files changed

+360
-252
lines changed

‎src/interfaces/jdbc/org/postgresql/jdbc2/PreparedStatement.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,8 @@ else if (x instanceof Boolean)
613613
elseif (xinstanceofPGobject)
614614
setString(parameterIndex, ((PGobject)x).getValue());
615615
else
616-
setLong(parameterIndex,connection.putObject(x));
616+
// Try to store java object in database
617+
setSerialize(parameterIndex,connection.putObject(x),x.getClass().getName() );
617618
}
618619

619620
/**
@@ -674,6 +675,29 @@ private void set(int paramIndex, String s) throws SQLException
674675
inStrings[paramIndex -1] =s;
675676
}
676677

678+
/**
679+
* Set a parameter to a tablerow-type oid reference.
680+
*
681+
* @param parameterIndex the first parameter is 1...
682+
* @param x the oid of the object from org.postgresql.util.Serialize.store
683+
* @param classname the classname of the java object x
684+
* @exception SQLException if a database access error occurs
685+
*/
686+
privatevoidsetSerialize(intparameterIndex,longx,Stringclassname)throwsSQLException
687+
{
688+
// converts . to _, toLowerCase, and ensures length<32
689+
Stringtablename =Serialize.toPostgreSQL(classname );
690+
DriverManager.println("setSerialize: setting " +x +"::" +tablename );
691+
692+
// OID reference to tablerow-type must be cast like: <oid>::<tablename>
693+
// Note that postgres support for tablerow data types is incomplete/broken.
694+
// This cannot be just a plain OID because then there would be ambiguity
695+
// between when you want the oid itself and when you want the object
696+
// an oid references.
697+
set(parameterIndex,Long.toString(x) +"::" +tablename );
698+
}
699+
700+
677701
// ** JDBC 2 Extensions **
678702

679703
/**
@@ -803,3 +827,4 @@ public void setTimestamp(int i,Timestamp t,java.util.Calendar cal) throws SQLExc
803827
}
804828

805829
}
830+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp