Search in sources :

Example 41 with PSQLException

use of org.postgresql.util.PSQLException in project questdb by bluestreak01.

the class PGJobContextTest method testPreparedStatementInsertSelectNullDesignatedColumn.

@Test
public void testPreparedStatementInsertSelectNullDesignatedColumn() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(2);
            final Connection connection = getConnection(false, false);
            final Statement statement = connection.createStatement();
            final PreparedStatement insert = connection.prepareStatement("insert into tab(ts, value) values(?, ?)")) {
            statement.execute("create table tab(ts timestamp, value double) timestamp(ts) partition by MONTH");
            // Null is not allowed
            insert.setNull(1, Types.NULL);
            insert.setNull(2, Types.NULL);
            try {
                insert.executeUpdate();
                fail("cannot insert null when the column is designated");
            } catch (PSQLException expected) {
                Assert.assertEquals("ERROR: timestamp before 1970-01-01 is not allowed", expected.getMessage());
            }
            // Insert a dud
            insert.setString(1, "1970-01-01 00:11:22.334455");
            insert.setNull(2, Types.NULL);
            insert.executeUpdate();
            try (ResultSet rs = statement.executeQuery("select null, ts, value from tab where value = null")) {
                StringSink sink = new StringSink();
                String expected = "null[VARCHAR],ts[TIMESTAMP],value[DOUBLE]\n" + "null,1970-01-01 00:11:22.334455,null\n";
                assertResultSet(expected, sink, rs);
            }
            statement.execute("drop table tab");
        }
    });
}
Also used : PSQLException(org.postgresql.util.PSQLException) BaseConnection(org.postgresql.core.BaseConnection) StringSink(io.questdb.std.str.StringSink) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 42 with PSQLException

use of org.postgresql.util.PSQLException in project questdb by bluestreak01.

the class PGJobContextTest method testBlobOverLimit.

@Test
public void testBlobOverLimit() throws Exception {
    PGWireConfiguration configuration = new DefaultPGWireConfiguration() {

        @Override
        public int getMaxBlobSizeOnQuery() {
            return 150;
        }
    };
    TestUtils.assertMemoryLeak(() -> {
        try (final PGWireServer ignored = createPGServer(configuration);
            final Connection connection = getConnection(false, true)) {
            Statement statement = connection.createStatement();
            statement.executeQuery("select " + "rnd_str(4,4,4) s, " + "rnd_int(0, 256, 4) i, " + "rnd_double(4) d, " + "timestamp_sequence(0,10000) t, " + "rnd_float(4) f, " + "rnd_short() _short, " + "rnd_long(0, 10000000, 5) l, " + "rnd_timestamp(to_timestamp('2015','yyyy'),to_timestamp('2016','yyyy'),2) ts2, " + "rnd_byte(0,127) bb, " + "rnd_boolean() b, " + "rnd_symbol(4,4,4,2), " + "rnd_date(to_date('2015', 'yyyy'), to_date('2016', 'yyyy'), 2)," + "rnd_bin(1024,2048,2) " + "from long_sequence(50)");
            Assert.fail();
        } catch (PSQLException e) {
            TestUtils.assertContains(e.getServerErrorMessage().getMessage(), "blob is too large");
        }
    });
}
Also used : PSQLException(org.postgresql.util.PSQLException) BaseConnection(org.postgresql.core.BaseConnection) AbstractGriffinTest(io.questdb.griffin.AbstractGriffinTest) Test(org.junit.Test)

Example 43 with PSQLException

use of org.postgresql.util.PSQLException in project cloudbreak by hortonworks.

the class RdsConnectionBuilder method createDb.

private void createDb(Connection conn, String clusterName, String service) {
    String createSQL = "CREATE DATABASE ?";
    try (PreparedStatement preparedStatement = conn.prepareStatement(createSQL)) {
        preparedStatement.setString(1, clusterName + service);
        preparedStatement.executeUpdate(createSQL);
    } catch (PSQLException ex) {
        if ("42P04".equals(ex.getSQLState())) {
            LOGGER.warn("The expected database already exist");
        } else {
            throw new BadRequestException("Failed to create database in RDS: " + ex.getMessage(), ex);
        }
    } catch (SQLException e) {
        throw new BadRequestException("Failed to connect to RDS: " + e.getMessage(), e);
    }
}
Also used : SQLException(java.sql.SQLException) PSQLException(org.postgresql.util.PSQLException) PSQLException(org.postgresql.util.PSQLException) BadRequestException(com.sequenceiq.cloudbreak.controller.BadRequestException) PreparedStatement(java.sql.PreparedStatement)

Example 44 with PSQLException

use of org.postgresql.util.PSQLException in project cloudbreak by hortonworks.

the class SqlUtil method getProperSqlErrorMessage.

public static String getProperSqlErrorMessage(DataIntegrityViolationException ex) {
    Throwable cause = ex.getCause();
    while (cause.getCause() != null || cause instanceof PSQLException) {
        if (cause instanceof PSQLException && !((SQLException) cause).getSQLState().isEmpty()) {
            PSQLException e = (PSQLException) cause;
            String[] split = e.getLocalizedMessage().split("\\n");
            if (split.length > 0) {
                return split[0];
            }
        }
        cause = cause.getCause();
    }
    return ex.getLocalizedMessage();
}
Also used : PSQLException(org.postgresql.util.PSQLException)

Example 45 with PSQLException

use of org.postgresql.util.PSQLException in project midpoint by Evolveum.

the class SqaleServiceBase method recordFatalError.

protected void recordFatalError(@NotNull OperationResult operationResult, @NotNull Throwable t) {
    String exceptionMessage = t.toString();
    if (t instanceof com.querydsl.core.QueryException) {
        PSQLException psqlException = ExceptionUtil.findCause(t, PSQLException.class);
        // PSQLException message is lost in QueryException and it may be handy
        if (psqlException != null) {
            exceptionMessage += "\n" + psqlException.getMessage();
        }
    }
    logger.debug("Unexpected exception (will be rethrown and handled higher): {}\n" + "  OPERATION RESULT: {}", exceptionMessage, operationResult.debugDump());
    operationResult.recordFatalError(t);
}
Also used : PSQLException(org.postgresql.util.PSQLException)

Aggregations

PSQLException (org.postgresql.util.PSQLException)47 ServerErrorMessage (org.postgresql.util.ServerErrorMessage)28 Test (org.testng.annotations.Test)26 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)24 Test (org.junit.Test)6 SQLException (java.sql.SQLException)4 AbstractGriffinTest (io.questdb.griffin.AbstractGriffinTest)3 Connection (java.sql.Connection)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 IOException (java.io.IOException)2 BatchUpdateException (java.sql.BatchUpdateException)2 Properties (java.util.Properties)2 BaseConnection (org.postgresql.core.BaseConnection)2 PGExceptionSorter (com.alibaba.druid.pool.vendor.PGExceptionSorter)1 JdbcSession (com.evolveum.midpoint.repo.sqlbase.JdbcSession)1 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)1 QueryException (com.querydsl.core.QueryException)1 BadRequestException (com.sequenceiq.cloudbreak.controller.BadRequestException)1 StringSink (io.questdb.std.str.StringSink)1 String.format (java.lang.String.format)1