Java Code Examples for org.postgresql.util.PGobject

The following examples show how to use org.postgresql.util.PGobject. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: xyz-hub   Source File: DatabaseWriter.java    License: Apache License 2.0 6 votes vote down vote up
protected static PGobject featureToPGobject(final Feature feature) throws SQLException {
    final Geometry geometry = feature.getGeometry();
    feature.setGeometry(null); // Do not serialize the geometry in the JSON object

    final String json;

    try {
        json = feature.serialize();
    } finally {
        feature.setGeometry(geometry);
    }

    final PGobject jsonbObject = new PGobject();
    jsonbObject.setType("jsonb");
    jsonbObject.setValue(json);
    return jsonbObject;
}
 
Example 2
@Override
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
    try (Connection c = postgreSQLHolder.getConnection();
         PreparedStatement s = c.prepareStatement(updateAuthSQL)) {
        s.setString(1, username); // Username case
        s.setString(2, accessToken);

        PGobject uuidObject = new PGobject();
        uuidObject.setType("uuid");
        uuidObject.setValue(uuid.toString());
        s.setObject(3, uuidObject);

        // Execute update
        s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT);
        return s.executeUpdate() > 0;
    } catch (SQLException e) {
        throw new IOException(e);
    }
}
 
Example 3
@Override
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
    try (Connection c = postgreSQLHolder.getConnection();
         PreparedStatement s = c.prepareStatement(updateServerIDSQL)) {
        s.setString(1, serverID);

        PGobject uuidObject = new PGobject();
        uuidObject.setType("uuid");
        uuidObject.setValue(uuid.toString());
        s.setObject(2, uuidObject);

        // Execute update
        s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT);
        return s.executeUpdate() > 0;
    } catch (SQLException e) {
        throw new IOException(e);
    }
}
 
Example 4
private Entry query(String sql, UUID value) throws IOException {
    try (Connection c = postgreSQLHolder.getConnection();
         PreparedStatement s = c.prepareStatement(sql)) {
        PGobject uuidObject = new PGobject();
        uuidObject.setType("uuid");
        uuidObject.setValue(value.toString());

        s.setObject(1, uuidObject);

        // Execute query
        s.setQueryTimeout(PostgreSQLSourceConfig.TIMEOUT);
        try (ResultSet set = s.executeQuery()) {
            return constructEntry(set);
        }
    } catch (SQLException e) {
        throw new IOException(e);
    }
}
 
Example 5
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    if("int4range".equals(type)) {
        return integerRange(value);
    } else if("int8range".equals(type)) {
        return longRange(value);
    } else if("numrange".equals(type)) {
        return bigDecimalRange(value);
    } else {
        throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 6
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    if("int4range".equals(type)) {
        return Range.integerRange(value);
    } else if("int8range".equals(type)) {
        return Range.longRange(value);
    } else if("numrange".equals(type)) {
        return Range.bigDecimalRange(value);
    } else {
        throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 7
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    if("int4range".equals(type)) {
        return integerRange(value);
    } else if("int8range".equals(type)) {
        return longRange(value);
    } else if("numrange".equals(type)) {
        return bigDecimalRange(value);
    } else {
        throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 8
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    if("int4range".equals(type)) {
        return Range.integerRange(value);
    } else if("int8range".equals(type)) {
        return Range.longRange(value);
    } else if("numrange".equals(type)) {
        return Range.bigDecimalRange(value);
    } else {
        throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 9
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    if("int4range".equals(type)) {
        return integerRange(value);
    } else if("int8range".equals(type)) {
        return longRange(value);
    } else if("numrange".equals(type)) {
        return bigDecimalRange(value);
    } else {
        throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 10
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    if("int4range".equals(type)) {
        return Range.integerRange(value);
    } else if("int8range".equals(type)) {
        return Range.longRange(value);
    } else if("numrange".equals(type)) {
        return Range.bigDecimalRange(value);
    } else {
        throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 11
Source Project: hibernate-postgresql   Source File: StringArrayType.java    License: Apache License 2.0 6 votes vote down vote up
@Override
   public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException {
	String[] strings = (String[]) value;

	if (value == null) {
		preparedStatement.setNull(i, java.sql.Types.OTHER);
	} else {
		StringBuffer buffer = new StringBuffer();
		for (int j = 0; j < strings.length; j++) {
			buffer.append("\"" + strings[j] + "\"");
			if (j < strings.length - 1)
				buffer.append(",");
		}

		PGobject object = new PGobject();
		object.setValue("{" + buffer + "}");
		preparedStatement.setObject(i, object);
	}
}
 
Example 12
@Override
public Map<String, String> doNullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
		throws HibernateException, SQLException {

	Object identifier = rs.getObject(names[0]);

	if (rs.wasNull()) {
		return null;
	}

	final String jsonText;
	if (identifier instanceof PGobject) {
		PGobject pg = (PGobject) identifier;
		jsonText = pg.getValue();
	} else if (identifier instanceof String) { // some PostgreSQL Dialects /
												// Versions return String
												// not PGObject
		jsonText = (String) identifier;
	} else {
		throw new IllegalArgumentException("PersistentJsonObjectAsPostgreSQLJson type expected PGobject, received "
				+ identifier.getClass().getName() + " with value of '" + identifier + "'");
	}

	return toMap(jsonText);
}
 
Example 13
@Override
public void doNullSafeSet(PreparedStatement preparedStatement, Map<String, String> value, int index,
		SharedSessionContractImplementor session) throws SQLException {

	if (value == null) {
		preparedStatement.setNull(index, Types.NULL);
	} else {

		String identifier = toString(value);
		;

		PGobject jsonObject = new PGobject();
		jsonObject.setType("json");
		jsonObject.setValue(identifier);

		preparedStatement.setObject(index, jsonObject);
	}
}
 
Example 14
Source Project: jadira   Source File: PersistentEnumAsPostgreSQLEnum.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object doNullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {

	if (!HAS_POSTGRES_DRIVER) {
		return super.doNullSafeGet(rs, names, session, owner);
	}
	
    Object identifier = rs.getObject(names[0]);
    
    if (rs.wasNull()) {
        return null;
    }

    if (identifier instanceof PGobject) {
        PGobject pg = (PGobject) identifier;
        return getValueOfMethod().invoke(getMappedClass(), new Object[] { pg.getValue() });
    } else if (identifier instanceof String) { // some PostgreSQL Dialects / Versions return String not PGObject
        return getValueOfMethod().invoke(getMappedClass(), new Object[] { (String) identifier });
    } else {
        throw new IllegalArgumentException("PersistentEnum type expected PGobject, received " + identifier.getClass().getName() + " with value of '" + identifier + "'");
    }
}
 
Example 15
Source Project: presto   Source File: TypeUtils.java    License: Apache License 2.0 5 votes vote down vote up
static PGobject toPgTimestamp(LocalDateTime localDateTime)
        throws SQLException
{
    PGobject pgObject = new PGobject();
    pgObject.setType("timestamp");
    pgObject.setValue(localDateTime.toString());
    return pgObject;
}
 
Example 16
Source Project: presto   Source File: PostgreSqlClient.java    License: Apache License 2.0 5 votes vote down vote up
private static SliceWriteFunction typedVarcharWriteFunction(String jdbcTypeName)
{
    return (statement, index, value) -> {
        PGobject pgObject = new PGobject();
        pgObject.setType(jdbcTypeName);
        pgObject.setValue(value.toStringUtf8());
        statement.setObject(index, pgObject);
    };
}
 
Example 17
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    switch (type) {
        case "int4range":
            return integerRange(value);
        case "int8range":
            return longRange(value);
        case "numrange":
            return bigDecimalRange(value);
        case "tsrange":
            return localDateTimeRange(value);
        case "tstzrange":
            return zonedDateTimeRange(value);
        case "daterange":
            return localDateRange(value);
        default:
            throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 18
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SharedSessionContractImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(asString(range));

        st.setObject(index, object);
    }
}
 
Example 19
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected Range get(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws SQLException {
    PGobject pgObject = (PGobject) rs.getObject(names[0]);

    if (pgObject == null) {
        return null;
    }

    String type = pgObject.getType();
    String value = pgObject.getValue();

    switch (type) {
        case "int4range":
            return Range.integerRange(value);
        case "int8range":
            return Range.longRange(value);
        case "numrange":
            return Range.bigDecimalRange(value);
        case "tsrange":
            return Range.localDateTimeRange(value);
        case "tstzrange":
            return Range.zonedDateTimeRange(value);
        case "daterange":
            return Range.localDateRange(value);
        default:
            throw new IllegalStateException("The range type [" + type + "] is not supported!");
    }
}
 
Example 20
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SharedSessionContractImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(range.asString());

        st.setObject(index, object);
    }
}
 
Example 21
Source Project: hibernate-types   Source File: PostgreSQLInetType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(PreparedStatement st, Inet value, int index, SharedSessionContractImplementor session) throws SQLException {
    if (value == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject holder = new PGobject();
        holder.setType("inet");
        holder.setValue(value.getAddress());
        st.setObject(index, holder);
    }
}
 
Example 22
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SessionImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(asString(range));

        st.setObject(index, object);
    }
}
 
Example 23
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SessionImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(range.asString());

        st.setObject(index, object);
    }
}
 
Example 24
Source Project: hibernate-types   Source File: PostgreSQLInetType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(PreparedStatement st, Inet value, int index, SessionImplementor session) throws SQLException {
    if (value == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject holder = new PGobject();
        holder.setType("inet");
        holder.setValue(value.getAddress());
        st.setObject(index, holder);
    }
}
 
Example 25
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SessionImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(asString(range));

        st.setObject(index, object);
    }
}
 
Example 26
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SessionImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(range.asString());

        st.setObject(index, object);
    }
}
 
Example 27
Source Project: hibernate-types   Source File: PostgreSQLInetType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(PreparedStatement st, Inet value, int index, SessionImplementor session) throws SQLException {
    if (value == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject holder = new PGobject();
        holder.setType("inet");
        holder.setValue(value.getAddress());
        st.setObject(index, holder);
    }
}
 
Example 28
Source Project: hibernate-types   Source File: PostgreSQLGuavaRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SessionImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(asString(range));

        st.setObject(index, object);
    }
}
 
Example 29
Source Project: hibernate-types   Source File: PostgreSQLRangeType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void set(PreparedStatement st, Range range, int index, SessionImplementor session) throws SQLException {

    if (range == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject object = new PGobject();
        object.setType(determineRangeType(range));
        object.setValue(range.asString());

        st.setObject(index, object);
    }
}
 
Example 30
Source Project: hibernate-types   Source File: PostgreSQLInetType.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void set(PreparedStatement st, Inet value, int index, SessionImplementor session) throws SQLException {
    if (value == null) {
        st.setNull(index, Types.OTHER);
    } else {
        PGobject holder = new PGobject();
        holder.setType("inet");
        holder.setValue(value.getAddress());
        st.setObject(index, holder);
    }
}