Search in sources :

Example 41 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class CommandWithColumns method generateSequences.

/**
 * For the given list of columns, create sequences for auto-increment
 * columns (if needed), and then get the list of all sequences of the
 * columns.
 *
 * @param columns the columns
 * @param temporary whether generated sequences should be temporary
 * @return the list of sequences (may be empty)
 */
protected ArrayList<Sequence> generateSequences(ArrayList<Column> columns, boolean temporary) {
    ArrayList<Sequence> sequences = New.arrayList();
    if (columns != null) {
        for (Column c : columns) {
            if (c.isAutoIncrement()) {
                int objId = getObjectId();
                c.convertAutoIncrementToSequence(session, getSchema(), objId, temporary);
                if (!Constants.CLUSTERING_DISABLED.equals(session.getDatabase().getCluster())) {
                    throw DbException.getUnsupportedException("CLUSTERING && auto-increment columns");
                }
            }
            Sequence seq = c.getSequence();
            if (seq != null) {
                sequences.add(seq);
            }
        }
    }
    return sequences;
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) Sequence(org.h2.schema.Sequence)

Example 42 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class JdbcDatabaseMetaData method getExportedKeys.

/**
 * Gets the list of foreign key columns that reference a table. The result
 * set is sorted by FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, FK_NAME,
 * KEY_SEQ.
 *
 * <ol>
 * <li>PKTABLE_CAT (String) primary catalog</li>
 * <li>PKTABLE_SCHEM (String) primary schema</li>
 * <li>PKTABLE_NAME (String) primary table</li>
 * <li>PKCOLUMN_NAME (String) primary column</li>
 * <li>FKTABLE_CAT (String) foreign catalog</li>
 * <li>FKTABLE_SCHEM (String) foreign schema</li>
 * <li>FKTABLE_NAME (String) foreign table</li>
 * <li>FKCOLUMN_NAME (String) foreign column</li>
 * <li>KEY_SEQ (short) sequence number (1,2,...)</li>
 * <li>UPDATE_RULE (short) action on update (see
 * DatabaseMetaData.importedKey...)</li>
 * <li>DELETE_RULE (short) action on delete (see
 * DatabaseMetaData.importedKey...)</li>
 * <li>FK_NAME (String) foreign key name</li>
 * <li>PK_NAME (String) primary key name</li>
 * <li>DEFERRABILITY (short) deferrable or not (always
 * importedKeyNotDeferrable)</li>
 * </ol>
 *
 * @param catalogPattern null or the catalog name
 * @param schemaPattern the schema name of the primary table
 * @param tableName the name of the primary table
 * @return the result set
 * @throws SQLException if the connection is closed
 */
@Override
public ResultSet getExportedKeys(String catalogPattern, String schemaPattern, String tableName) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("getExportedKeys(" + quote(catalogPattern) + ", " + quote(schemaPattern) + ", " + quote(tableName) + ");");
        }
        checkClosed();
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT " + "PKTABLE_CATALOG PKTABLE_CAT, " + "PKTABLE_SCHEMA PKTABLE_SCHEM, " + "PKTABLE_NAME PKTABLE_NAME, " + "PKCOLUMN_NAME, " + "FKTABLE_CATALOG FKTABLE_CAT, " + "FKTABLE_SCHEMA FKTABLE_SCHEM, " + "FKTABLE_NAME, " + "FKCOLUMN_NAME, " + "ORDINAL_POSITION KEY_SEQ, " + "UPDATE_RULE, " + "DELETE_RULE, " + "FK_NAME, " + "PK_NAME, " + "DEFERRABILITY " + "FROM INFORMATION_SCHEMA.CROSS_REFERENCES " + "WHERE PKTABLE_CATALOG LIKE ? ESCAPE ? " + "AND PKTABLE_SCHEMA LIKE ? ESCAPE ? " + "AND PKTABLE_NAME = ? " + "ORDER BY FKTABLE_CAT, FKTABLE_SCHEM, FKTABLE_NAME, FK_NAME, KEY_SEQ");
        prep.setString(1, getCatalogPattern(catalogPattern));
        prep.setString(2, "\\");
        prep.setString(3, getSchemaPattern(schemaPattern));
        prep.setString(4, "\\");
        prep.setString(5, tableName);
        return prep.executeQuery();
    } catch (Exception e) {
        throw logAndConvert(e);
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) DbException(org.h2.message.DbException) SQLException(java.sql.SQLException)

Example 43 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class Schema method removeChildrenAndResources.

@Override
public void removeChildrenAndResources(Session session) {
    while (triggers != null && triggers.size() > 0) {
        TriggerObject obj = (TriggerObject) triggers.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (constraints != null && constraints.size() > 0) {
        Constraint obj = (Constraint) constraints.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    // There can be dependencies between tables e.g. using computed columns,
    // so we might need to loop over them multiple times.
    boolean runLoopAgain = false;
    do {
        runLoopAgain = false;
        if (tablesAndViews != null) {
            // Loop over a copy because the map is modified underneath us.
            for (Table obj : new ArrayList<>(tablesAndViews.values())) {
                // in one go underneath us.
                if (obj.getName() != null) {
                    if (database.getDependentTable(obj, obj) == null) {
                        database.removeSchemaObject(session, obj);
                    } else {
                        runLoopAgain = true;
                    }
                }
            }
        }
    } while (runLoopAgain);
    while (indexes != null && indexes.size() > 0) {
        Index obj = (Index) indexes.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (sequences != null && sequences.size() > 0) {
        Sequence obj = (Sequence) sequences.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (constants != null && constants.size() > 0) {
        Constant obj = (Constant) constants.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    while (functions != null && functions.size() > 0) {
        FunctionAlias obj = (FunctionAlias) functions.values().toArray()[0];
        database.removeSchemaObject(session, obj);
    }
    for (Right right : database.getAllRights()) {
        if (right.getGrantedObject() == this) {
            database.removeDatabaseObject(session, right);
        }
    }
    database.removeMeta(session, getId());
    owner = null;
    invalidate();
}
Also used : RegularTable(org.h2.table.RegularTable) Table(org.h2.table.Table) Constraint(org.h2.constraint.Constraint) FunctionAlias(org.h2.engine.FunctionAlias) ArrayList(java.util.ArrayList) Right(org.h2.engine.Right) Index(org.h2.index.Index)

Example 44 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class TestFileLockSerialized method testSequenceFlush.

private void testSequenceFlush() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized;FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE";
    ResultSet rs;
    Connection conn1 = getConnection(url);
    Statement stat1 = conn1.createStatement();
    stat1.execute("create sequence seq");
    rs = stat1.executeQuery("call seq.nextval");
    rs.next();
    assertEquals(1, rs.getInt(1));
    Connection conn2 = getConnection(url);
    Statement stat2 = conn2.createStatement();
    rs = stat2.executeQuery("call seq.nextval");
    rs.next();
    assertEquals(2, rs.getInt(1));
    conn1.close();
    conn2.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Example 45 with Sequence

use of org.h2.schema.Sequence in project h2database by h2database.

the class TestFileLockSerialized method testSequence.

private void testSequence() throws Exception {
    deleteDb("fileLockSerialized");
    String url = "jdbc:h2:" + getBaseDir() + "/fileLockSerialized" + ";FILE_LOCK=SERIALIZED;OPEN_NEW=TRUE;RECONNECT_CHECK_DELAY=10";
    ResultSet rs;
    Connection conn1 = getConnection(url);
    Statement stat1 = conn1.createStatement();
    stat1.execute("create sequence seq");
    // 5 times RECONNECT_CHECK_DELAY
    Thread.sleep(100);
    rs = stat1.executeQuery("call seq.nextval");
    rs.next();
    conn1.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ResultSet(java.sql.ResultSet) Connection(java.sql.Connection) JdbcConnection(org.h2.jdbc.JdbcConnection)

Aggregations

Sequence (org.h2.schema.Sequence)19 ValueString (org.h2.value.ValueString)14 PreparedStatement (java.sql.PreparedStatement)10 Column (org.h2.table.Column)10 Database (org.h2.engine.Database)9 Expression (org.h2.expression.Expression)8 DbException (org.h2.message.DbException)8 Table (org.h2.table.Table)8 ResultSet (java.sql.ResultSet)7 Statement (java.sql.Statement)7 Constraint (org.h2.constraint.Constraint)7 Index (org.h2.index.Index)7 Connection (java.sql.Connection)6 SQLException (java.sql.SQLException)6 SchemaObject (org.h2.schema.SchemaObject)6 ArrayList (java.util.ArrayList)5 DropSequence (org.h2.command.ddl.DropSequence)5 DbObject (org.h2.engine.DbObject)5 Schema (org.h2.schema.Schema)5 TriggerObject (org.h2.schema.TriggerObject)5