Java Code Examples for org.postgresql.util.PGobject#setValue()

The following examples show how to use org.postgresql.util.PGobject#setValue() . 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 File: DatabaseWriter.java    From xyz-hub with Apache License 2.0 7 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
Source File: PostgreSQLAuthHandler.java    From Launcher with GNU General Public License v3.0 6 votes vote down vote up
@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
Source File: PostgreSQLAuthHandler.java    From Launcher with GNU General Public License v3.0 6 votes vote down vote up
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 4
Source File: PersistentStringMapAsPostgreSQLJson.java    From jadira with Apache License 2.0 6 votes vote down vote up
@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 5
Source File: PostgreSQLRangeType.java    From hibernate-types with 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 6
Source File: CockroachdbDialect.java    From sqlg with MIT License 5 votes vote down vote up
@Override
public void setJson(PreparedStatement preparedStatement, int parameterStartIndex, JsonNode json) {
    PGobject jsonObject = new PGobject();
    jsonObject.setType("jsonb");
    try {
        jsonObject.setValue(json.toString());
        preparedStatement.setObject(parameterStartIndex, jsonObject);
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}
 
Example 7
Source File: JsonBinaryPlainStringType.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void nullSafeSet( PreparedStatement ps, Object value, int idx, SharedSessionContractImplementor session ) throws HibernateException, SQLException
{
    if ( value == null )
    {
        ps.setObject( idx, null );
        return;
    }

    PGobject pg = new PGobject();
    pg.setType( "jsonb" );
    pg.setValue( value.toString() );

    ps.setObject( idx, pg );
}
 
Example 8
Source File: PostgreSQLInetType.java    From hibernate-types with 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 9
Source File: PostgreSQLRangeType.java    From hibernate-types with 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 10
Source File: DaoOperations.java    From kaif with Apache License 2.0 5 votes vote down vote up
default PGobject pgJson(final String rawJson) {
  final PGobject jsonObject = new PGobject();
  jsonObject.setType("json");
  try {
    jsonObject.setValue(rawJson);
    return jsonObject;
  } catch (final SQLException e) {
    // see NamedParameterJdbcTemplate for source code
    throw ((JdbcTemplate) jdbc()).getExceptionTranslator()
        .translate("not valid json: " + rawJson, null, e);
  }
}
 
Example 11
Source File: PostgresqlDAO.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void storeGrade(PreparedStatement pst, int position, Grading grd) throws SQLException {
	PGobject jsonObject = new PGobject();
	jsonObject.setType("json");
	jsonObject.setValue(serialiser.toJsonElement(grd).toString());
	pst.setObject(position, jsonObject);
}
 
Example 12
Source File: PostgreSQLGuavaRangeType.java    From hibernate-types with 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 13
Source File: InetAddressType.java    From hibernate-postgresql with Apache License 2.0 5 votes vote down vote up
@Override
   public void nullSafeSet(PreparedStatement preparedStatement, Object value, int i, SharedSessionContractImplementor sessionImplementor) throws HibernateException, SQLException {
	if (value == null) {
		preparedStatement.setNull(i, java.sql.Types.OTHER);
	} else {
		PGobject object = new PGobject();
		object.setValue(((InetAddress) value).getHostAddress());
		object.setType("inet");
		preparedStatement.setObject(i, object);
	}
}
 
Example 14
Source File: TypeUtils.java    From presto with 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 15
Source File: PostgreSQLGuavaRangeType.java    From hibernate-types with 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 16
Source File: SqlRunner.java    From yawp with MIT License 5 votes vote down vote up
protected final PGobject createJsonObject(String json) {
    try {
        PGobject jsonObject = new PGobject();
        jsonObject.setType("jsonb");
        jsonObject.setValue(json);
        return jsonObject;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 17
Source File: V2_35_2__Update_data_sync_job_parameters_with_system_setting_value.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void updateJobParamaters( final Context context, int dataValuesPageSize )
    throws JsonProcessingException, SQLException
{
    if ( dataValuesPageSize > 0 )
    {
        Map<Integer, JobParameters> updatedJobParameters = new HashMap<>();

        String sql = "SELECT jobconfigurationid, jsonbjobparameters FROM jobconfiguration " +
            "WHERE jobtype = '" + JobType.DATA_SYNC.name() + "';";
        try ( Statement stmt = context.getConnection().createStatement();
              ResultSet rs = stmt.executeQuery( sql ); )
        {
            while ( rs.next())
            {
                DataSynchronizationJobParameters jobparams = reader
                    .readValue( rs.getString( "jsonbjobparameters" ) );
                jobparams.setPageSize( dataValuesPageSize );

                updatedJobParameters.put( rs.getInt( "jobconfigurationid" ), jobparams );
            }
        }

        for ( Map.Entry<Integer, JobParameters> jobParams : updatedJobParameters.entrySet() )
        {
            try ( PreparedStatement ps = context.getConnection()
                .prepareStatement( "UPDATE jobconfiguration SET jsonbjobparameters = ? where  jobconfigurationid = ?;" ) )
            {
                PGobject pg = new PGobject();
                pg.setType( "jsonb" );
                pg.setValue( writer.writeValueAsString( jobParams.getValue() ) );

                ps.setObject( 1, pg );
                ps.setInt( 2, jobParams.getKey() );

                ps.execute();
            }
        }
    }
}
 
Example 18
Source File: JSONTypeHandler.java    From mmpt with MIT License 5 votes vote down vote up
@Override
public void setNonNullParameter(PreparedStatement ps, int i,
        String parameter, JdbcType jdbcType) throws SQLException {
    PGobject jsonObject = new PGobject();
    jsonObject.setType("json");
    jsonObject.setValue(parameter);
    ps.setObject(i, jsonObject);
}
 
Example 19
Source File: PostgresqlDAO.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void storeTiersApps(PreparedStatement pst, int i, Map<String, Object> tiersAppIds) throws SQLException {
	
	PGobject jsonObject = new PGobject();
	jsonObject.setType("json");
	jsonObject.setValue(serialiser.toJsonElement(tiersAppIds).toString());
	pst.setObject(i, jsonObject);
}
 
Example 20
Source File: V2_34_7__Convert_push_analysis_job_parameters_into_list_of_string.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void migrate( Context context ) throws Exception
{
    String pushAnalysisUid = null;

    try ( Statement statement = context.getConnection().createStatement() )
    {
        ResultSet resultSet = statement.executeQuery( "select jsonbjobparameters->1->'pushAnalysis' from public.jobconfiguration where jobtype = '" +
            JobType.PUSH_ANALYSIS.name() + "';" );

        if ( resultSet.next() )
        {
            pushAnalysisUid = resultSet.getString( 1 );
            pushAnalysisUid = StringUtils.strip( pushAnalysisUid, "\"" );
        }
    }

    if ( pushAnalysisUid != null )
    {
        ObjectMapper mapper = new ObjectMapper();
        mapper.activateDefaultTyping( BasicPolymorphicTypeValidator.builder().allowIfBaseType( JobParameters.class ).build() );
        mapper.setSerializationInclusion( JsonInclude.Include.NON_NULL );

        JavaType resultingJavaType = mapper.getTypeFactory().constructType( JobParameters.class );
        ObjectWriter writer = mapper.writerFor( resultingJavaType );

        try ( PreparedStatement ps = context.getConnection().prepareStatement( "UPDATE jobconfiguration SET jsonbjobparameters = ? where  jobtype = ?;" ) )
        {
            PushAnalysisJobParameters jobParameters = new PushAnalysisJobParameters( pushAnalysisUid );

            PGobject pg = new PGobject();
            pg.setType( "jsonb" );
            pg.setValue( writer.writeValueAsString( jobParameters ) );

            ps.setObject( 1, pg );
            ps.setString( 2, JobType.PUSH_ANALYSIS.name() );

            ps.execute();

            log.info( "JobType " + JobType.PUSH_ANALYSIS.name() + " has been updated." );
        }
    }
}