+ public void setBinaryField(String aFieldName, String anObjectId, byte aData[]) throws DatabaseFailure, SQLException {
+ PreparedStatement statement = null;
+ Connection connection = obtainConnection();
+
+ try {
+ connection.setAutoCommit(false);
+ try {
+ // are we using bytea ?
+ if (getFieldType(aFieldName) == java.sql.Types.BINARY) {
+ statement = connection.prepareStatement(
+ "update " + mainTable + " set " + aFieldName + " = ? where " + getIdFieldName() + "=" + Integer.parseInt(anObjectId));
+ statement.setBytes(1, aData);
+ statement.execute();
+ connection.commit();
+ }
+ // or the old oid's
+ else {
+ PGConnection postgresqlConnection = (org.postgresql.PGConnection) ((DelegatingConnection) connection).getDelegate();
+ LargeObjectManager lobManager = postgresqlConnection.getLargeObjectAPI();
+ int oid = lobManager.create(LargeObjectManager.READ | LargeObjectManager.WRITE);
+ LargeObject obj = lobManager.open(oid, LargeObjectManager.WRITE); // Now open the file File file =
+ obj.write(aData);
+ obj.close();
+ statement = connection.prepareStatement(
+ "update " + mainTable + " set " + aFieldName + " = ? where " + getIdFieldName() + "=" + Integer.parseInt(anObjectId));
+ statement.setInt(1, oid);
+ statement.execute();
+ connection.commit();
+ }
+ }
+ finally {
+ connection.setAutoCommit(true);
+ }
+ }
+ finally {
+ freeConnection(connection, statement);
+ }
+ }
+
+ /**
+ * Can be overridden to specify a primary key sequence name not named according to
+ * the convention (tablename _id_seq)
+ */
+ protected String getPrimaryKeySequence() {
+ return mainTable+"_id_seq";
+ }
+
+ /**
+ * Can be called by subclasses to specify fields that are binary, and that shouldn't
+ * be updated outside of {@link #setBinaryField}
+ *
+ * @param aBinaryField The field name of the binary field
+ */
+ protected void markBinaryField(String aBinaryField) {
+ binaryFields.add(aBinaryField);
+ }
+
+ private void logQueryBefore(String aQuery) {
+ logger.debug("about to perform QUERY " + aQuery);
+// (new Throwable()).printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
+ }
+
+ private void logQueryAfter(String aQuery, long aTime) {
+ logger.info("QUERY " + aQuery + " took " + aTime + "ms.");
+ }
+
+ private void logQueryError(String aQuery, long aTime, Throwable anException) {
+ logger.error("QUERY " + aQuery + " took " + aTime + "ms, but threw exception " + anException.toString());
+ }
+
+ private int getFieldType(String aFieldName) {
+ if (fieldNameToType == null) {
+ acquireMetaData();
+ }
+
+ return ((Integer) fieldNameToType.get(aFieldName)).intValue();