Search in sources :

Example 36 with UpdateStatement

use of herddb.model.commands.UpdateStatement in project herddb by diennea.

the class ColumnTimestampTest method test2.

@Test
public void test2() throws Exception {
    String tableName2 = "t2";
    Table table2 = Table.builder().tablespace("tblspace1").name(tableName2).tablespace(tableSpace).column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).column("ts1", ColumnTypes.TIMESTAMP).primaryKey("id").build();
    CreateTableStatement st2 = new CreateTableStatement(table2);
    manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    java.sql.Timestamp ts1 = new java.sql.Timestamp(System.currentTimeMillis());
    {
        Map<String, Object> bean = new HashMap<>();
        bean.put("id", "key1");
        bean.put("ts1", ts1);
        Record record = RecordSerializer.toRecord(bean, table2);
        InsertStatement st = new InsertStatement(tableSpace, "t2", record);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName2, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        Map<String, Object> resultbean = result.getRecord().toBean(table2);
        assertEquals(Bytes.from_string("key1"), result.getRecord().key);
        assertEquals(2, resultbean.entrySet().size());
        assertEquals(RawString.of("key1"), resultbean.get("id"));
        assertEquals(ts1, resultbean.get("ts1"));
    }
    {
        Map<String, Object> bean = new HashMap<>();
        bean.put("id", "key1");
        bean.put("ts1", null);
        Record record = RecordSerializer.toRecord(bean, table2);
        UpdateStatement st = new UpdateStatement(tableSpace, tableName2, record, null);
        assertEquals(1, manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).getUpdateCount());
    }
    {
        GetResult result = manager.get(new GetStatement(tableSpace, tableName2, Bytes.from_string("key1"), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(result.found());
        Map<String, Object> resultbean = result.getRecord().toBean(table2);
        assertEquals(Bytes.from_string("key1"), result.getRecord().key);
        assertEquals(1, resultbean.entrySet().size());
        assertEquals(RawString.of("key1"), resultbean.get("id"));
        assertEquals(null, resultbean.get("ts1"));
    }
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) RawString(herddb.utils.RawString) InsertStatement(herddb.model.commands.InsertStatement) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Aggregations

UpdateStatement (herddb.model.commands.UpdateStatement)36 InsertStatement (herddb.model.commands.InsertStatement)31 Record (herddb.model.Record)29 Test (org.junit.Test)29 Table (herddb.model.Table)28 GetResult (herddb.model.GetResult)26 GetStatement (herddb.model.commands.GetStatement)26 TransactionContext (herddb.model.TransactionContext)23 Bytes (herddb.utils.Bytes)22 CreateTableStatement (herddb.model.commands.CreateTableStatement)21 TransactionResult (herddb.model.TransactionResult)14 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)14 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)14 Path (java.nio.file.Path)14 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)13 DeleteStatement (herddb.model.commands.DeleteStatement)12 ConstValueRecordFunction (herddb.model.ConstValueRecordFunction)10 DMLStatementExecutionResult (herddb.model.DMLStatementExecutionResult)10 ArrayList (java.util.ArrayList)8 FileCommitLogManager (herddb.file.FileCommitLogManager)7