Search in sources :

Example 86 with Timestamp

use of java.sql.Timestamp in project jforum2 by rafaelsteil.

the class GenericPostDAO method updatePostsTable.

protected void updatePostsTable(Post post) {
    PreparedStatement p = null;
    try {
        p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("PostModel.updatePost"));
        p.setInt(1, post.getTopicId());
        p.setInt(2, post.getForumId());
        p.setInt(3, post.isBbCodeEnabled() ? 1 : 0);
        p.setInt(4, post.isHtmlEnabled() ? 1 : 0);
        p.setInt(5, post.isSmiliesEnabled() ? 1 : 0);
        p.setInt(6, post.isSignatureEnabled() ? 1 : 0);
        p.setTimestamp(7, new Timestamp(System.currentTimeMillis()));
        p.setInt(8, post.getEditCount() + 1);
        p.setString(9, post.getUserIp());
        p.setInt(10, post.getId());
        p.executeUpdate();
    } catch (SQLException e) {
        throw new DatabaseException(e);
    } finally {
        DbUtils.close(p);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp) DatabaseException(net.jforum.exceptions.DatabaseException)

Example 87 with Timestamp

use of java.sql.Timestamp in project presto by prestodb.

the class TestShardCleaner method testAbortOldTransactions.

@Test
public void testAbortOldTransactions() throws Exception {
    TestingDao dao = dbi.onDemand(TestingDao.class);
    long now = System.currentTimeMillis();
    long txn1 = dao.insertTransaction(new Timestamp(now - HOURS.toMillis(26)));
    long txn2 = dao.insertTransaction(new Timestamp(now - HOURS.toMillis(25)));
    long txn3 = dao.insertTransaction(new Timestamp(now));
    ShardDao shardDao = dbi.onDemand(ShardDao.class);
    assertEquals(shardDao.finalizeTransaction(txn1, true), 1);
    assertQuery("SELECT transaction_id, successful FROM transactions", row(txn1, true), row(txn2, null), row(txn3, null));
    cleaner.abortOldTransactions();
    assertQuery("SELECT transaction_id, successful FROM transactions", row(txn1, true), row(txn2, false), row(txn3, null));
}
Also used : Timestamp(java.sql.Timestamp) Test(org.testng.annotations.Test)

Example 88 with Timestamp

use of java.sql.Timestamp in project presto by prestodb.

the class TestShardCleaner method testDeleteOldCompletedTransactions.

@Test
public void testDeleteOldCompletedTransactions() throws Exception {
    TestingDao dao = dbi.onDemand(TestingDao.class);
    ShardDao shardDao = dbi.onDemand(ShardDao.class);
    long now = System.currentTimeMillis();
    Timestamp yesterdayStart = new Timestamp(now - HOURS.toMillis(27));
    Timestamp yesterdayEnd = new Timestamp(now - HOURS.toMillis(26));
    Timestamp todayEnd = new Timestamp(now - HOURS.toMillis(1));
    long txn1 = dao.insertTransaction(yesterdayStart);
    long txn2 = dao.insertTransaction(yesterdayStart);
    long txn3 = dao.insertTransaction(yesterdayStart);
    long txn4 = dao.insertTransaction(yesterdayStart);
    long txn5 = dao.insertTransaction(new Timestamp(now));
    long txn6 = dao.insertTransaction(new Timestamp(now));
    assertEquals(shardDao.finalizeTransaction(txn1, true), 1);
    assertEquals(shardDao.finalizeTransaction(txn2, false), 1);
    assertEquals(shardDao.finalizeTransaction(txn3, false), 1);
    assertEquals(shardDao.finalizeTransaction(txn5, true), 1);
    assertEquals(shardDao.finalizeTransaction(txn6, false), 1);
    assertEquals(dao.updateTransactionEndTime(txn1, yesterdayEnd), 1);
    assertEquals(dao.updateTransactionEndTime(txn2, yesterdayEnd), 1);
    assertEquals(dao.updateTransactionEndTime(txn3, yesterdayEnd), 1);
    assertEquals(dao.updateTransactionEndTime(txn5, todayEnd), 1);
    assertEquals(dao.updateTransactionEndTime(txn6, todayEnd), 1);
    shardDao.insertCreatedShard(randomUUID(), txn2);
    shardDao.insertCreatedShard(randomUUID(), txn2);
    assertQuery("SELECT transaction_id, successful, end_time FROM transactions", // old successful
    row(txn1, true, yesterdayEnd), // old failed, shards present
    row(txn2, false, yesterdayEnd), // old failed, no referencing shards
    row(txn3, false, yesterdayEnd), // old not finished
    row(txn4, null, null), // new successful
    row(txn5, true, todayEnd), // new failed, no referencing shards
    row(txn6, false, todayEnd));
    cleaner.deleteOldCompletedTransactions();
    assertQuery("SELECT transaction_id, successful, end_time FROM transactions", row(txn2, false, yesterdayEnd), row(txn4, null, null), row(txn5, true, todayEnd), row(txn6, false, todayEnd));
}
Also used : Timestamp(java.sql.Timestamp) Test(org.testng.annotations.Test)

Example 89 with Timestamp

use of java.sql.Timestamp in project presto by prestodb.

the class H2QueryRunner method rowMapper.

private static ResultSetMapper<MaterializedRow> rowMapper(final List<? extends Type> types) {
    return new ResultSetMapper<MaterializedRow>() {

        @Override
        public MaterializedRow map(int index, ResultSet resultSet, StatementContext ctx) throws SQLException {
            int count = resultSet.getMetaData().getColumnCount();
            checkArgument(types.size() == count, "type does not match result");
            List<Object> row = new ArrayList<>(count);
            for (int i = 1; i <= count; i++) {
                Type type = types.get(i - 1);
                if (BOOLEAN.equals(type)) {
                    boolean booleanValue = resultSet.getBoolean(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(booleanValue);
                    }
                } else if (TINYINT.equals(type)) {
                    byte byteValue = resultSet.getByte(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(byteValue);
                    }
                } else if (SMALLINT.equals(type)) {
                    short shortValue = resultSet.getShort(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(shortValue);
                    }
                } else if (INTEGER.equals(type)) {
                    int intValue = resultSet.getInt(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(intValue);
                    }
                } else if (BIGINT.equals(type)) {
                    long longValue = resultSet.getLong(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(longValue);
                    }
                } else if (REAL.equals(type)) {
                    float floatValue = resultSet.getFloat(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(floatValue);
                    }
                } else if (DOUBLE.equals(type)) {
                    double doubleValue = resultSet.getDouble(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(doubleValue);
                    }
                } else if (isVarcharType(type)) {
                    String stringValue = resultSet.getString(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(stringValue);
                    }
                } else if (isCharType(type)) {
                    String stringValue = resultSet.getString(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(padEnd(stringValue, ((CharType) type).getLength(), ' '));
                    }
                } else if (DATE.equals(type)) {
                    Date dateValue = resultSet.getDate(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(dateValue);
                    }
                } else if (TIME.equals(type) || TIME_WITH_TIME_ZONE.equals(type)) {
                    Time timeValue = resultSet.getTime(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(timeValue);
                    }
                } else if (TIMESTAMP.equals(type) || TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
                    Timestamp timestampValue = resultSet.getTimestamp(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(timestampValue);
                    }
                } else if (UNKNOWN.equals(type)) {
                    Object objectValue = resultSet.getObject(i);
                    checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
                    row.add(null);
                } else if (type instanceof DecimalType) {
                    BigDecimal decimalValue = resultSet.getBigDecimal(i);
                    if (resultSet.wasNull()) {
                        row.add(null);
                    } else {
                        row.add(decimalValue);
                    }
                } else {
                    throw new AssertionError("unhandled type: " + type);
                }
            }
            return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
        }
    };
}
Also used : ResultSetMapper(org.skife.jdbi.v2.tweak.ResultSetMapper) ArrayList(java.util.ArrayList) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date) BigDecimal(java.math.BigDecimal) StatementContext(org.skife.jdbi.v2.StatementContext) DecimalType(com.facebook.presto.spi.type.DecimalType) Varchars.isVarcharType(com.facebook.presto.spi.type.Varchars.isVarcharType) Type(com.facebook.presto.spi.type.Type) CharType(com.facebook.presto.spi.type.CharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) ResultSet(java.sql.ResultSet) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType) Chars.isCharType(com.facebook.presto.spi.type.Chars.isCharType) MaterializedRow(com.facebook.presto.testing.MaterializedRow)

Example 90 with Timestamp

use of java.sql.Timestamp in project dropwizard by dropwizard.

the class JodaDateTimeArgument method apply.

@Override
public void apply(final int position, final PreparedStatement statement, final StatementContext ctx) throws SQLException {
    if (value != null) {
        if (calendar.isPresent()) {
            // We need to make a clone, because Calendar is not thread-safe
            // and some JDBC drivers mutate it during time calculations
            final Calendar calendarClone = (Calendar) calendar.get().clone();
            statement.setTimestamp(position, new Timestamp(value.getMillis()), calendarClone);
        } else {
            statement.setTimestamp(position, new Timestamp(value.getMillis()));
        }
    } else {
        statement.setNull(position, Types.TIMESTAMP);
    }
}
Also used : Calendar(java.util.Calendar) Timestamp(java.sql.Timestamp)

Aggregations

Timestamp (java.sql.Timestamp)1539 PreparedStatement (java.sql.PreparedStatement)265 Test (org.junit.Test)245 SQLException (java.sql.SQLException)236 ResultSet (java.sql.ResultSet)209 BigDecimal (java.math.BigDecimal)204 Date (java.util.Date)165 Date (java.sql.Date)122 Connection (java.sql.Connection)117 ArrayList (java.util.ArrayList)109 Calendar (java.util.Calendar)76 Test (org.testng.annotations.Test)71 GregorianCalendar (java.util.GregorianCalendar)68 Time (java.sql.Time)65 SimpleDateFormat (java.text.SimpleDateFormat)62 Change (com.google.gerrit.reviewdb.client.Change)59 IOException (java.io.IOException)51 BaseTest (util.BaseTest)49 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)45 Properties (java.util.Properties)41