Search in sources :

Example 56 with Timestamp

use of java.sql.Timestamp in project binnavi by google.

the class PostgreSQLViewCreator method createView.

/**
   * Inserts a new view in the database by copying an existing view.
   * 
   * @param provider The connection to the database.
   * @param containerId The ID of the container where the view is created.
   * @param view The view to be copied.
   * @param name The name of the new view.
   * @param description The description of the new view.
   * @param containerTable Name of the view container table.
   * @param viewContainerTable Name of the view container views table.
   * @param generator Generates the view.
   * @return The created view.
   * @throws CouldntSaveDataException Thrown if the view could not be created.
   */
private static CView createView(final AbstractSQLProvider provider, final int containerId, final INaviView view, final String name, final String description, final String containerTable, final String viewContainerTable, final ViewGenerator generator) throws CouldntSaveDataException {
    final CConnection connection = provider.getConnection();
    try {
        PostgreSQLHelpers.beginTransaction(connection);
        final int viewId = insertView(connection, name, description);
        // Mark the view as a module view
        connection.executeUpdate("INSERT INTO " + viewContainerTable + " VALUES(" + containerId + ", " + viewId + ")", true);
        final List<INaviViewNode> nodes = view.getGraph().getNodes();
        final List<INaviEdge> edges = view.getGraph().getEdges();
        // Store all nodes
        PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
        // Store all edges
        PostgreSQLEdgeSaver.writeEdges(provider, edges);
        PostgreSQLHelpers.endTransaction(connection);
        final String query = "SELECT creation_date, modification_date FROM " + CTableNames.VIEWS_TABLE + " WHERE id = " + viewId;
        final ResultSet resultSet = connection.executeQuery(query, true);
        try {
            while (resultSet.next()) {
                final Timestamp creationDate = resultSet.getTimestamp("creation_date");
                final Timestamp modificationDate = resultSet.getTimestamp("modification_date");
                PostgreSQLHelpers.updateModificationDate(connection, containerTable, containerId);
                return generator.generate(viewId, name, description, ViewType.NonNative, view.getGraphType(), creationDate, modificationDate, view.getNodeCount(), view.getEdgeCount(), new HashSet<CTag>(), new HashSet<CTag>(), false);
            }
            throw new CouldntSaveDataException("Error: Couldnt't load the created view");
        } finally {
            resultSet.close();
        }
    } catch (final SQLException exception) {
        CUtilityFunctions.logException(exception);
        try {
            PostgreSQLHelpers.rollback(connection);
        } catch (final SQLException e) {
            CUtilityFunctions.logException(e);
        }
        throw new CouldntSaveDataException(exception);
    }
}
Also used : SQLException(java.sql.SQLException) CouldntSaveDataException(com.google.security.zynamics.binnavi.Database.Exceptions.CouldntSaveDataException) CTag(com.google.security.zynamics.binnavi.Tagging.CTag) INaviEdge(com.google.security.zynamics.binnavi.disassembly.INaviEdge) Timestamp(java.sql.Timestamp) CConnection(com.google.security.zynamics.binnavi.Database.CConnection) ResultSet(java.sql.ResultSet) INaviViewNode(com.google.security.zynamics.binnavi.disassembly.INaviViewNode)

Example 57 with Timestamp

use of java.sql.Timestamp in project jdbc-shards by wplatform.

the class PreparedStatementTestCase method testDate.

private void testDate(Connection conn) throws SQLException {
    PreparedStatement prep = conn.prepareStatement("SELECT ?");
    Timestamp ts = Timestamp.valueOf("2001-02-03 04:05:06");
    prep.setObject(1, new java.util.Date(ts.getTime()));
    ResultSet rs = prep.executeQuery();
    rs.next();
    Timestamp ts2 = rs.getTimestamp(1);
    assertEquals(ts.toString(), ts2.toString());
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Timestamp(java.sql.Timestamp)

Example 58 with Timestamp

use of java.sql.Timestamp in project jdbc-shards by wplatform.

the class PreparedStatementTestCase method testCoalesce.

private static void testCoalesce(Connection conn) throws SQLException {
    Statement stat = conn.createStatement();
    stat.executeUpdate("create table test(tm timestamp)");
    stat.executeUpdate("insert into test values(current_timestamp)");
    PreparedStatement prep = conn.prepareStatement("update test set tm = coalesce(?,tm)");
    prep.setTimestamp(1, new java.sql.Timestamp(System.currentTimeMillis()));
    prep.executeUpdate();
    stat.executeUpdate("drop table test");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Timestamp(java.sql.Timestamp) PreparedStatement(java.sql.PreparedStatement)

Example 59 with Timestamp

use of java.sql.Timestamp in project hibernate-orm by hibernate.

the class DefaultGeneratedValueTest method testUpdateTimestampGeneration.

@Test
@TestForIssue(jiraKey = "HHH-2907")
public void testUpdateTimestampGeneration() {
    Session s = openSession();
    s.beginTransaction();
    TheEntity theEntity = new TheEntity(1);
    assertNull(theEntity.updated);
    s.save(theEntity);
    //TODO: Actually the value should be non-null afterQuery save
    assertNull(theEntity.updated);
    s.getTransaction().commit();
    s.close();
    Timestamp created = theEntity.vmCreatedSqlTimestamp;
    Timestamp updated = theEntity.updated;
    assertNotNull(updated);
    assertNotNull(created);
    s = openSession();
    s.beginTransaction();
    theEntity = (TheEntity) s.get(TheEntity.class, 1);
    theEntity.lastName = "Smith";
    try {
        Thread.sleep(1);
    } catch (InterruptedException ignore) {
    }
    s.getTransaction().commit();
    s.close();
    s = openSession();
    s.beginTransaction();
    theEntity = (TheEntity) s.get(TheEntity.class, 1);
    assertEquals("Creation timestamp should not change on update", created, theEntity.vmCreatedSqlTimestamp);
    assertTrue("Update timestamp should have changed due to update", theEntity.updated.after(updated));
    s.delete(theEntity);
    s.getTransaction().commit();
    s.close();
}
Also used : CreationTimestamp(org.hibernate.annotations.CreationTimestamp) UpdateTimestamp(org.hibernate.annotations.UpdateTimestamp) Timestamp(java.sql.Timestamp) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 60 with Timestamp

use of java.sql.Timestamp in project hibernate-orm by hibernate.

the class ASTParserLoadingTest method testStandardFunctions.

@Test
public void testStandardFunctions() throws Exception {
    Session session = openSession();
    Transaction t = session.beginTransaction();
    Product p = new Product();
    p.setDescription("a product");
    p.setPrice(new BigDecimal(1.0));
    p.setProductId("abc123");
    session.persist(p);
    Object[] result = (Object[]) session.createQuery("select current_time(), current_date(), current_timestamp() from Product").uniqueResult();
    assertTrue(result[0] instanceof Time);
    assertTrue(result[1] instanceof Date);
    assertTrue(result[2] instanceof Timestamp);
    assertNotNull(result[0]);
    assertNotNull(result[1]);
    assertNotNull(result[2]);
    session.delete(p);
    t.commit();
    session.close();
}
Also used : Transaction(org.hibernate.Transaction) Product(org.hibernate.test.cid.Product) Time(java.sql.Time) Timestamp(java.sql.Timestamp) BigDecimal(java.math.BigDecimal) Date(java.sql.Date) Session(org.hibernate.Session) Test(org.junit.Test)

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