Search in sources :

Example 76 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.

the class ForeignKeySQLTest method createTableWithOnUpdateSetNull.

@Test
public void createTableWithOnUpdateSetNull() throws Exception {
    assumeTrue("only jsqlparser 4.x support ON UPDATE set NULL", false);
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.parent (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.child (k2 string primary key,n2 int," + "s2 string, " + "CONSTRAINT fk1 FOREIGN KEY (s2,n2) REFERENCES parent(k1,n1) ON DELETE NO ACTION ON UPDATE SET NULL)", Collections.emptyList());
        Table parentTable = manager.getTableSpaceManager("tblspace1").getTableManager("parent").getTable();
        Table childTable = manager.getTableSpaceManager("tblspace1").getTableManager("child").getTable();
        assertEquals(1, childTable.foreignKeys.length);
        assertEquals("fk1", childTable.foreignKeys[0].name);
        assertEquals(ForeignKeyDef.ACTION_SETNULL, childTable.foreignKeys[0].onUpdateAction);
        assertEquals(ForeignKeyDef.ACTION_NO_ACTION, childTable.foreignKeys[0].onDeleteAction);
        assertEquals(parentTable.uuid, childTable.foreignKeys[0].parentTableId);
        assertArrayEquals(new String[] { "s2", "n2" }, childTable.foreignKeys[0].columns);
        assertArrayEquals(new String[] { "k1", "n1" }, childTable.foreignKeys[0].parentTableColumns);
        // test without transaction
        testChildSideOfForeignKey(manager, TransactionContext.NOTRANSACTION_ID, "fk1");
        execute(manager, "DELETE FROM tblspace1.child", Collections.emptyList());
        execute(manager, "DELETE FROM tblspace1.parent", Collections.emptyList());
        long tx = beginTransaction(manager, "tblspace1");
        // test with transaction
        testChildSideOfForeignKey(manager, tx, "fk1");
        TestUtils.commitTransaction(manager, "tblspace1", tx);
        execute(manager, "DELETE FROM tblspace1.child", Collections.emptyList());
        execute(manager, "DELETE FROM tblspace1.parent", Collections.emptyList());
        // test without transaction
        testServerSideOfForeignKey(manager, TransactionContext.NOTRANSACTION_ID, "fk1", "SET NULL", "NO ACTION");
        execute(manager, "DELETE FROM tblspace1.child", Collections.emptyList());
        execute(manager, "DELETE FROM tblspace1.parent", Collections.emptyList());
        tx = beginTransaction(manager, "tblspace1");
        // test with transaction
        testServerSideOfForeignKey(manager, tx, "fk1", "SET NULL", "NO ACTION");
        TestUtils.commitTransaction(manager, "tblspace1", tx);
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 77 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.

the class ForeignKeySQLTest method alterTableCannotAddViolatedForeignKey.

@Test
public void alterTableCannotAddViolatedForeignKey() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.parent (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.child (k2 string primary key,n2 int,s2 string)", Collections.emptyList());
        execute(manager, "INSERT INTO tblspace1.parent(k1,n1,s1) values('a',2,'pvalue')", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
        // insert a record that could violates the new FK1
        execute(manager, "INSERT INTO tblspace1.child(k2,n2,s2) values('no',10,'a')", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
        // add the FK, it must fail
        ForeignKeyViolationException err = expectThrows(ForeignKeyViolationException.class, () -> {
            execute(manager, "ALTER TABLE tblspace1.`CHILD` Add CONSTRAINT `fk1` FOREIGN KEY (s2,n2) REFERENCES parent(k1,n1) ON DELETE RESTRICT", Collections.emptyList());
        });
        assertEquals("fk1", err.getForeignKeyName());
        // check that the fk is not added
        Table parentTable = manager.getTableSpaceManager("tblspace1").getTableManager("parent").getTable();
        Table childTable = manager.getTableSpaceManager("tblspace1").getTableManager("child").getTable();
        assertNull(childTable.foreignKeys);
        // fix the record, in transaction
        long tx = beginTransaction(manager, "tblspace1");
        execute(manager, "UPDATE tblspace1.child set s2='a',n2=2", Collections.emptyList(), new TransactionContext(tx));
        assertNotNull(manager.getTableSpaceManager("tblspace1").getTransaction(tx));
        execute(manager, "ALTER TABLE tblspace1.`CHILD` Add CONSTRAINT `fk1` FOREIGN KEY (s2,n2) REFERENCES parent(k1,n1) ON DELETE RESTRICT", Collections.emptyList(), new TransactionContext(tx));
        // transaction is committed automatically after an ALTER TABLE operation
        assertNull(manager.getTableSpaceManager("tblspace1").getTransaction(tx));
        // check that the fk is not present
        childTable = manager.getTableSpaceManager("tblspace1").getTableManager("child").getTable();
        assertEquals(1, childTable.foreignKeys.length);
        assertEquals("fk1", childTable.foreignKeys[0].name);
        assertEquals(ForeignKeyDef.ACTION_NO_ACTION, childTable.foreignKeys[0].onUpdateAction);
        assertEquals(ForeignKeyDef.ACTION_NO_ACTION, childTable.foreignKeys[0].onDeleteAction);
        assertEquals(parentTable.uuid, childTable.foreignKeys[0].parentTableId);
        assertArrayEquals(new String[] { "s2", "n2" }, childTable.foreignKeys[0].columns);
        assertArrayEquals(new String[] { "k1", "n1" }, childTable.foreignKeys[0].parentTableColumns);
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) ForeignKeyViolationException(herddb.model.ForeignKeyViolationException) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 78 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.

the class ForeignKeySQLTest method cannotAlterColumnsWithChildTableRefs.

@Test
public void cannotAlterColumnsWithChildTableRefs() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.parent (k1 string primary key,n1 int,s1 string)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.child (k2 string primary key,n2 int," + "s2 string, " + "CONSTRAINT fk1 FOREIGN KEY (s2,n2) REFERENCES parent(k1,n1) ON DELETE NO ACTION ON UPDATE NO ACTION)", Collections.emptyList());
        Table childTable = manager.getTableSpaceManager("tblspace1").getTableManager("child").getTable();
        assertEquals(1, childTable.foreignKeys.length);
        StatementExecutionException errCannotDrop = expectThrows(StatementExecutionException.class, () -> {
            execute(manager, "DROP TABLE tblspace1.parent", Collections.emptyList());
        });
        assertEquals("Cannot drop table tblspace1.parent because it has children tables: child", errCannotDrop.getMessage());
        StatementExecutionException errCannotDropColumn = expectThrows(StatementExecutionException.class, () -> {
            execute(manager, "ALTER TABLE tblspace1.parent DROP COLUMN n1", Collections.emptyList());
        });
        assertEquals("Cannot drop column parent.n1 because of foreign key constraint fk1 on table child", errCannotDropColumn.getMessage());
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) Table(herddb.model.Table) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 79 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.

the class SimpleSubqueryTest method subQueryOnWhereTest.

@Test
public void subQueryOnWhereTest() throws Exception {
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.table1 (k1 string primary key,n1 int)", Collections.emptyList());
        execute(manager, "CREATE TABLE tblspace1.table2 (k2 string primary key,fk string)", Collections.emptyList());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey2", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey3", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table1(k1,n1) values(?,?)", Arrays.asList("mykey4", Integer.valueOf(1234))).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey1", "mykey2")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey2", "mykey2")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey3", "mykey3")).getUpdateCount());
        assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.table2(k2,fk) values(?,?)", Arrays.asList("subkey4", "mykey4")).getUpdateCount());
        assertEquals(2, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1 in ('mykey','mykey3')" + "", Collections.emptyList()).consumeAndClose().size());
        if (manager.isFullSQLSupportEnabled()) {
            assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2='subkey4')" + "", Collections.emptyList()).consumeAndClose().size());
            assertEquals(1, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.n1 = ? and t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2=?)" + "", Arrays.asList(1234, "subkey4")).consumeAndClose().size());
            assertEquals(0, scan(manager, "SELECT * " + "FROM tblspace1.table1 t1 " + "WHERE t1.n1 = ? and t1.k1 in (SELECT fk FROM tblspace1.table2 WHERE k2=?)" + "", Arrays.asList(1234, "subkey5")).consumeAndClose().size());
        }
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Example 80 with MemoryMetadataStorageManager

use of herddb.mem.MemoryMetadataStorageManager in project herddb by diennea.

the class UpdateTest method updateMultiRowsWithValidationError.

@Test
public void updateMultiRowsWithValidationError() throws Exception {
    final int inserts = 10;
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new MemoryMetadataStorageManager(), new MemoryDataStorageManager(), new MemoryCommitLogManager(), null, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), NO_TRANSACTION);
        manager.waitForTablespace("tblspace1", 10000);
        execute(manager, "CREATE TABLE tblspace1.tsql (k1 int, n1 int not null, primary key(k1))", Collections.emptyList());
        for (int i = 0; i < inserts; i++) {
            assertEquals(1, executeUpdate(manager, "INSERT INTO tblspace1.tsql(k1,n1) values(?,?)", Arrays.asList(Integer.valueOf(i), Integer.valueOf(1))).getUpdateCount());
        }
        long tx = beginTransaction(manager, "tblspace1");
        TransactionContext ctx = new TransactionContext(tx);
        // single record failed update
        StatementExecutionException error = herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
            executeUpdate(manager, "UPDATE tblspace1.tsql set n1 = null WHERE n1=1", Collections.emptyList(), ctx);
        });
        assertEquals("error on column n1 (integer not null):Cannot have null value in non-NULL type integer", error.getMessage());
        // multi record failed update
        StatementExecutionException errors = herddb.utils.TestUtils.expectThrows(StatementExecutionException.class, () -> {
            executeUpdate(manager, "UPDATE tblspace1.tsql set n1 = null", Collections.emptyList(), ctx);
        });
        assertEquals("error on column n1 (integer not null):Cannot have null value in non-NULL type integer", errors.getMessage());
        commitTransaction(manager, "tblspace1", tx);
    }
}
Also used : CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) DBManager(herddb.core.DBManager) MemoryDataStorageManager(herddb.mem.MemoryDataStorageManager) TransactionContext(herddb.model.TransactionContext) MemoryCommitLogManager(herddb.mem.MemoryCommitLogManager) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) StatementExecutionException(herddb.model.StatementExecutionException) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) Test(org.junit.Test)

Aggregations

MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)257 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)256 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)252 Test (org.junit.Test)247 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)240 DataScanner (herddb.model.DataScanner)156 RawString (herddb.utils.RawString)138 DBManager (herddb.core.DBManager)133 DataAccessor (herddb.utils.DataAccessor)83 Table (herddb.model.Table)80 ScanStatement (herddb.model.commands.ScanStatement)65 CreateTableStatement (herddb.model.commands.CreateTableStatement)57 TranslatedQuery (herddb.sql.TranslatedQuery)55 List (java.util.List)54 Index (herddb.model.Index)40 TransactionContext (herddb.model.TransactionContext)39 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)37 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)30 RuntimeProjectedDataAccessor (herddb.model.planner.ProjectOp.ZeroCopyProjection.RuntimeProjectedDataAccessor)28 StatementExecutionException (herddb.model.StatementExecutionException)26