@Override public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { checkClosed(); return new PgPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability); }
private static String castToString(final Object in) throws SQLException { try { if (in instanceof String) { return (String) in; } if (in instanceof Clob) { return asString((Clob) in); } // convert any unknown objects to string. return in.toString(); } catch (final Exception e) { throw cannotCastException(in.getClass().getName(), "String", e); } }
public void setDouble(int parameterIndex, double x) throws SQLException { checkClosed(); if (connection.binaryTransferSend(Oid.FLOAT8)) { byte[] val = new byte[8]; ByteConverter.float8(val, 0, x); bindBytes(parameterIndex, val, Oid.FLOAT8); return; } bindLiteral(parameterIndex, Double.toString(x), Oid.FLOAT8); }
public void setBoolean(int parameterIndex, boolean x) throws SQLException { checkClosed(); // The key words TRUE and FALSE are the preferred (SQL-compliant) usage. bindLiteral(parameterIndex, x ? "TRUE" : "FALSE", Oid.BOOL); }
private void setNumber(int parameterIndex, Number x) throws SQLException { checkClosed(); if (x == null) { setNull(parameterIndex, Types.DECIMAL); } else { bindLiteral(parameterIndex, x.toString(), Oid.NUMERIC); } }
protected void setString(int parameterIndex, String x, int oid) throws SQLException { // if the passed string is null, then set this column to null checkClosed(); if (x == null) { preparedParameters.setNull(parameterIndex, oid); } else { bindString(parameterIndex, x, oid); } }
@Override public void setObject(int parameterIndex, Object in, int targetSqlType, int scale) throws SQLException { checkClosed(); setNull(parameterIndex, targetSqlType); return; setUuid(parameterIndex, (UUID) in); return; case Types.SQLXML: if (in instanceof SQLXML) { setSQLXML(parameterIndex, (SQLXML) in); } else { setSQLXML(parameterIndex, new PgSQLXML(connection, in.toString())); setInt(parameterIndex, castToInt(in)); break; case Types.TINYINT: case Types.SMALLINT: setShort(parameterIndex, castToShort(in)); break; case Types.BIGINT: setLong(parameterIndex, castToLong(in)); break; case Types.REAL: setFloat(parameterIndex, castToFloat(in)); break; case Types.DOUBLE:
public void setTime(int i, Time t, java.util.Calendar cal) throws SQLException { checkClosed(); if (t == null) { setNull(i, Types.TIME); return; } int oid = Oid.UNSPECIFIED; // If a PGTime is used, we can define the OID explicitly. if (t instanceof PGTime) { PGTime pgTime = (PGTime) t; if (pgTime.getCalendar() == null) { oid = Oid.TIME; } else { oid = Oid.TIMETZ; cal = pgTime.getCalendar(); } } if (cal == null) { cal = getDefaultCalendar(); } bindString(i, connection.getTimestampUtils().toString(cal, t), oid); }
public void setArray(int i, java.sql.Array x) throws SQLException { checkClosed(); if (null == x) { setNull(i, Types.ARRAY); return; } // This only works for Array implementations that return a valid array // literal from Array.toString(), such as the implementation we return // from ResultSet.getArray(). Eventually we need a proper implementation // here that works for any Array implementation. String typename = x.getBaseTypeName(); int oid = connection.getTypeInfo().getPGArrayType(typename); if (oid == Oid.UNSPECIFIED) { throw new PSQLException(GT.tr("Unknown type {0}.", typename), PSQLState.INVALID_PARAMETER_TYPE); } if (x instanceof PgArray) { PgArray arr = (PgArray) x; if (arr.isBinary()) { bindBytes(i, arr.toBytes(), oid); return; } } setString(i, x.toString(), oid); }
private <A> void setPrimitiveArray(int parameterIndex, A in) throws SQLException { final PrimitiveArraySupport<A> arrayToString = PrimitiveArraySupport.getArraySupport(in); final TypeInfo typeInfo = connection.getTypeInfo(); final int oid = arrayToString.getDefaultArrayTypeOid(typeInfo); if (arrayToString.supportBinaryRepresentation() && connection.getPreferQueryMode() != PreferQueryMode.SIMPLE) { bindBytes(parameterIndex, arrayToString.toBinaryRepresentation(connection, in), oid); } else { final char delim = typeInfo.getArrayDelimiter(oid); setString(parameterIndex, arrayToString.toArrayString(delim, in), oid); } }
private void setUuid(int parameterIndex, UUID uuid) throws SQLException { if (connection.binaryTransferSend(Oid.UUID)) { byte[] val = new byte[16]; ByteConverter.int8(val, 0, uuid.getMostSignificantBits()); ByteConverter.int8(val, 8, uuid.getLeastSignificantBits()); bindBytes(parameterIndex, val, Oid.UUID); } else { bindLiteral(parameterIndex, uuid.toString(), Oid.UUID); } }
private void setTimestamp(int i, OffsetDateTime offsetDateTime) throws SQLException { int oid = Oid.TIMESTAMPTZ; bindString(i, connection.getTimestampUtils().toString(offsetDateTime), oid); } //JCP! endif
private static PSQLException cannotCastException(final String fromType, final String toType) { return cannotCastException(fromType, toType, null); }
public void setTimestamp(int i, Timestamp t, java.util.Calendar cal) throws SQLException { checkClosed(); setNull(i, Types.TIMESTAMP); return; cal = getDefaultCalendar(); bindString(i, connection.getTimestampUtils().toString(cal, t), oid);
private void setMap(int parameterIndex, Map<?, ?> x) throws SQLException { int oid = connection.getTypeInfo().getPGType("hstore"); if (oid == Oid.UNSPECIFIED) { throw new PSQLException(GT.tr("No hstore extension installed."), PSQLState.INVALID_PARAMETER_TYPE); } if (connection.binaryTransferSend(oid)) { byte[] data = HStoreConverter.toBytes(x, connection.getEncoding()); bindBytes(parameterIndex, data, oid); } else { setString(parameterIndex, HStoreConverter.toString(x), oid); } }
private void setTimestamp(int i, LocalDateTime localDateTime) throws SQLException { int oid = Oid.TIMESTAMP; bindString(i, connection.getTimestampUtils().toString(localDateTime), oid); }
public void setDate(int i, java.sql.Date d, java.util.Calendar cal) throws SQLException { checkClosed(); setNull(i, Types.DATE); return; cal = getDefaultCalendar(); bindString(i, connection.getTimestampUtils().toString(cal, d), Oid.UNSPECIFIED);
public void setInt(int parameterIndex, int x) throws SQLException { checkClosed(); if (connection.binaryTransferSend(Oid.INT4)) { byte[] val = new byte[4]; ByteConverter.int4(val, 0, x); bindBytes(parameterIndex, val, Oid.INT4); return; } bindLiteral(parameterIndex, Integer.toString(x), Oid.INT4); }
private static float castToFloat(final Object in) throws SQLException { try { if (in instanceof String) { return Float.parseFloat((String) in); } if (in instanceof Number) { return ((Number) in).floatValue(); } if (in instanceof java.util.Date) { return ((java.util.Date) in).getTime(); } if (in instanceof Boolean) { return (Boolean) in ? 1f : 0f; } if (in instanceof Clob) { return Float.parseFloat(asString((Clob) in)); } if (in instanceof Character) { return Float.parseFloat(in.toString()); } } catch (final Exception e) { throw cannotCastException(in.getClass().getName(), "float", e); } throw cannotCastException(in.getClass().getName(), "float"); }
private void setPGobject(int parameterIndex, PGobject x) throws SQLException { String typename = x.getType(); int oid = connection.getTypeInfo().getPGType(typename); if (oid == Oid.UNSPECIFIED) { throw new PSQLException(GT.tr("Unknown type {0}.", typename), PSQLState.INVALID_PARAMETER_TYPE); } if ((x instanceof PGBinaryObject) && connection.binaryTransferSend(oid)) { PGBinaryObject binObj = (PGBinaryObject) x; byte[] data = new byte[binObj.lengthInBytes()]; binObj.toBytes(data, 0); bindBytes(parameterIndex, data, oid); } else { setString(parameterIndex, x.getValue(), oid); } }