Search in sources :

Example 61 with Delete

use of org.h2.command.dml.Delete in project h2database by h2database.

the class Recover method dumpPageLogStream.

private void dumpPageLogStream(PrintWriter writer, int logKey, int logFirstTrunkPage, int logFirstDataPage, long pageCount) throws IOException {
    Data s = Data.create(this, pageSize);
    DataReader in = new DataReader(new PageInputStream(writer, this, store, logKey, logFirstTrunkPage, logFirstDataPage, pageSize));
    writer.println("---- Transaction log ----");
    CompressLZF compress = new CompressLZF();
    while (true) {
        int x = in.readByte();
        if (x < 0) {
            break;
        }
        if (x == PageLog.NOOP) {
        // ignore
        } else if (x == PageLog.UNDO) {
            int pageId = in.readVarInt();
            int size = in.readVarInt();
            byte[] data = new byte[pageSize];
            if (size == 0) {
                in.readFully(data, pageSize);
            } else if (size == 1) {
            // empty
            } else {
                byte[] compressBuffer = new byte[size];
                in.readFully(compressBuffer, size);
                try {
                    compress.expand(compressBuffer, 0, size, data, 0, pageSize);
                } catch (ArrayIndexOutOfBoundsException e) {
                    throw DbException.convertToIOException(e);
                }
            }
            String typeName = "";
            int type = data[0];
            boolean last = (type & Page.FLAG_LAST) != 0;
            type &= ~Page.FLAG_LAST;
            switch(type) {
                case Page.TYPE_EMPTY:
                    typeName = "empty";
                    break;
                case Page.TYPE_DATA_LEAF:
                    typeName = "data leaf " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_DATA_NODE:
                    typeName = "data node " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_DATA_OVERFLOW:
                    typeName = "data overflow " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_BTREE_LEAF:
                    typeName = "b-tree leaf " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_BTREE_NODE:
                    typeName = "b-tree node " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_FREE_LIST:
                    typeName = "free list " + (last ? "(last)" : "");
                    break;
                case Page.TYPE_STREAM_TRUNK:
                    typeName = "log trunk";
                    break;
                case Page.TYPE_STREAM_DATA:
                    typeName = "log data";
                    break;
                default:
                    typeName = "ERROR: unknown type " + type;
                    break;
            }
            writer.println("-- undo page " + pageId + " " + typeName);
            if (trace) {
                Data d = Data.create(null, data);
                dumpPage(writer, d, pageId, pageCount);
            }
        } else if (x == PageLog.ADD) {
            int sessionId = in.readVarInt();
            setStorage(in.readVarInt());
            Row row = PageLog.readRow(RowFactory.DEFAULT, in, s);
            writer.println("-- session " + sessionId + " table " + storageId + " + " + row.toString());
            if (transactionLog) {
                if (storageId == 0 && row.getColumnCount() >= 4) {
                    int tableId = (int) row.getKey();
                    String sql = row.getValue(3).getString();
                    String name = extractTableOrViewName(sql);
                    if (row.getValue(2).getInt() == DbObject.TABLE_OR_VIEW) {
                        tableMap.put(tableId, name);
                    }
                    writer.println(sql + ";");
                } else {
                    String tableName = tableMap.get(storageId);
                    if (tableName != null) {
                        StatementBuilder buff = new StatementBuilder();
                        buff.append("INSERT INTO ").append(tableName).append(" VALUES(");
                        for (int i = 0; i < row.getColumnCount(); i++) {
                            buff.appendExceptFirst(", ");
                            buff.append(row.getValue(i).getSQL());
                        }
                        buff.append(");");
                        writer.println(buff.toString());
                    }
                }
            }
        } else if (x == PageLog.REMOVE) {
            int sessionId = in.readVarInt();
            setStorage(in.readVarInt());
            long key = in.readVarLong();
            writer.println("-- session " + sessionId + " table " + storageId + " - " + key);
            if (transactionLog) {
                if (storageId == 0) {
                    int tableId = (int) key;
                    String tableName = tableMap.get(tableId);
                    if (tableName != null) {
                        writer.println("DROP TABLE IF EXISTS " + tableName + ";");
                    }
                } else {
                    String tableName = tableMap.get(storageId);
                    if (tableName != null) {
                        String sql = "DELETE FROM " + tableName + " WHERE _ROWID_ = " + key + ";";
                        writer.println(sql);
                    }
                }
            }
        } else if (x == PageLog.TRUNCATE) {
            int sessionId = in.readVarInt();
            setStorage(in.readVarInt());
            writer.println("-- session " + sessionId + " table " + storageId + " truncate");
            if (transactionLog) {
                writer.println("TRUNCATE TABLE " + storageId);
            }
        } else if (x == PageLog.COMMIT) {
            int sessionId = in.readVarInt();
            writer.println("-- commit " + sessionId);
        } else if (x == PageLog.ROLLBACK) {
            int sessionId = in.readVarInt();
            writer.println("-- rollback " + sessionId);
        } else if (x == PageLog.PREPARE_COMMIT) {
            int sessionId = in.readVarInt();
            String transaction = in.readString();
            writer.println("-- prepare commit " + sessionId + " " + transaction);
        } else if (x == PageLog.NOOP) {
        // nothing to do
        } else if (x == PageLog.CHECKPOINT) {
            writer.println("-- checkpoint");
        } else if (x == PageLog.FREE_LOG) {
            int size = in.readVarInt();
            StringBuilder buff = new StringBuilder("-- free");
            for (int i = 0; i < size; i++) {
                buff.append(' ').append(in.readVarInt());
            }
            writer.println(buff);
        } else {
            writer.println("-- ERROR: unknown operation " + x);
            break;
        }
    }
}
Also used : DataReader(org.h2.store.DataReader) CompressLZF(org.h2.compress.CompressLZF) StatementBuilder(org.h2.util.StatementBuilder) Data(org.h2.store.Data) Row(org.h2.result.Row) SimpleRow(org.h2.result.SimpleRow)

Example 62 with Delete

use of org.h2.command.dml.Delete in project h2database by h2database.

the class Parser method parseReferences.

private void parseReferences(AlterTableAddConstraint command, Schema schema, String tableName) {
    if (readIf("(")) {
        command.setRefTableName(schema, tableName);
        command.setRefIndexColumns(parseIndexColumnList());
    } else {
        String refTableName = readIdentifierWithSchema(schema.getName());
        command.setRefTableName(getSchema(), refTableName);
        if (readIf("(")) {
            command.setRefIndexColumns(parseIndexColumnList());
        }
    }
    if (readIf("INDEX")) {
        String indexName = readIdentifierWithSchema();
        command.setRefIndex(getSchema().findIndex(session, indexName));
    }
    while (readIf("ON")) {
        if (readIf("DELETE")) {
            command.setDeleteAction(parseAction());
        } else {
            read("UPDATE");
            command.setUpdateAction(parseAction());
        }
    }
    if (readIf("NOT")) {
        read("DEFERRABLE");
    } else {
        readIf("DEFERRABLE");
    }
}
Also used : ValueString(org.h2.value.ValueString)

Example 63 with Delete

use of org.h2.command.dml.Delete in project h2database by h2database.

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf("TABLE")) {
        boolean ifExists = readIfExists(false);
        String tableName = readIdentifierWithSchema();
        DropTable command = new DropTable(session, getSchema());
        command.setTableName(tableName);
        while (readIf(",")) {
            tableName = readIdentifierWithSchema();
            DropTable next = new DropTable(session, getSchema());
            next.setTableName(tableName);
            command.addNextDropTable(next);
        }
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
            readIf("CONSTRAINTS");
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        } else if (readIf("IGNORE")) {
            command.setDropAction(ConstraintActionType.SET_DEFAULT);
        }
        return command;
    } else if (readIf("INDEX")) {
        boolean ifExists = readIfExists(false);
        String indexName = readIdentifierWithSchema();
        DropIndex command = new DropIndex(session, getSchema());
        command.setIndexName(indexName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        // Support for MySQL: DROP INDEX index_name ON tbl_name
        if (readIf("ON")) {
            readIdentifierWithSchema();
        }
        return command;
    } else if (readIf("USER")) {
        boolean ifExists = readIfExists(false);
        DropUser command = new DropUser(session);
        command.setUserName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        readIf("CASCADE");
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SEQUENCE")) {
        boolean ifExists = readIfExists(false);
        String sequenceName = readIdentifierWithSchema();
        DropSequence command = new DropSequence(session, getSchema());
        command.setSequenceName(sequenceName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("CONSTANT")) {
        boolean ifExists = readIfExists(false);
        String constantName = readIdentifierWithSchema();
        DropConstant command = new DropConstant(session, getSchema());
        command.setConstantName(constantName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("TRIGGER")) {
        boolean ifExists = readIfExists(false);
        String triggerName = readIdentifierWithSchema();
        DropTrigger command = new DropTrigger(session, getSchema());
        command.setTriggerName(triggerName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("VIEW")) {
        boolean ifExists = readIfExists(false);
        String viewName = readIdentifierWithSchema();
        DropView command = new DropView(session, getSchema());
        command.setViewName(viewName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        return command;
    } else if (readIf("ROLE")) {
        boolean ifExists = readIfExists(false);
        DropRole command = new DropRole(session);
        command.setRoleName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("ALIAS")) {
        boolean ifExists = readIfExists(false);
        String aliasName = readIdentifierWithSchema();
        DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
        command.setAliasName(aliasName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SCHEMA")) {
        boolean ifExists = readIfExists(false);
        DropSchema command = new DropSchema(session);
        command.setSchemaName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        }
        return command;
    } else if (readIf("ALL")) {
        read("OBJECTS");
        DropDatabase command = new DropDatabase(session);
        command.setDropAllObjects(true);
        if (readIf("DELETE")) {
            read("FILES");
            command.setDeleteFiles(true);
        }
        return command;
    } else if (readIf("DOMAIN")) {
        return parseDropUserDataType();
    } else if (readIf("TYPE")) {
        return parseDropUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseDropUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseDropAggregate();
    } else if (readIf("SYNONYM")) {
        boolean ifExists = readIfExists(false);
        String synonymName = readIdentifierWithSchema();
        DropSynonym command = new DropSynonym(session, getSchema());
        command.setSynonymName(synonymName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    }
    throw getSyntaxError();
}
Also used : DropConstant(org.h2.command.ddl.DropConstant) DropTrigger(org.h2.command.ddl.DropTrigger) DropView(org.h2.command.ddl.DropView) DropUser(org.h2.command.ddl.DropUser) DropSynonym(org.h2.command.ddl.DropSynonym) DropSequence(org.h2.command.ddl.DropSequence) ValueString(org.h2.value.ValueString) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) DropRole(org.h2.command.ddl.DropRole) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) DropDatabase(org.h2.command.ddl.DropDatabase) ConstraintActionType(org.h2.constraint.ConstraintActionType) DropIndex(org.h2.command.ddl.DropIndex)

Example 64 with Delete

use of org.h2.command.dml.Delete in project h2database by h2database.

the class Parser method parseExplain.

private Explain parseExplain() {
    Explain command = new Explain(session);
    if (readIf("ANALYZE")) {
        command.setExecuteCommand(true);
    } else {
        if (readIf("PLAN")) {
            readIf("FOR");
        }
    }
    if (isToken("SELECT") || isToken("FROM") || isToken("(") || isToken("WITH")) {
        Query query = parseSelect();
        query.setNeverLazy(true);
        command.setCommand(query);
    } else if (readIf("DELETE")) {
        command.setCommand(parseDelete());
    } else if (readIf("UPDATE")) {
        command.setCommand(parseUpdate());
    } else if (readIf("INSERT")) {
        command.setCommand(parseInsert());
    } else if (readIf("MERGE")) {
        command.setCommand(parseMerge());
    } else {
        throw getSyntaxError();
    }
    return command;
}
Also used : Query(org.h2.command.dml.Query) Explain(org.h2.command.dml.Explain)

Example 65 with Delete

use of org.h2.command.dml.Delete 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)

Aggregations

Connection (java.sql.Connection)40 PreparedStatement (java.sql.PreparedStatement)39 Statement (java.sql.Statement)38 ResultSet (java.sql.ResultSet)36 JdbcConnection (org.h2.jdbc.JdbcConnection)25 SQLException (java.sql.SQLException)17 SimpleResultSet (org.h2.tools.SimpleResultSet)14 Savepoint (java.sql.Savepoint)13 StatementBuilder (org.h2.util.StatementBuilder)9 DbException (org.h2.message.DbException)8 Column (org.h2.table.Column)8 ValueString (org.h2.value.ValueString)7 Random (java.util.Random)6 Expression (org.h2.expression.Expression)5 ValueExpression (org.h2.expression.ValueExpression)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ArrayList (java.util.ArrayList)4 GridH2Table (org.apache.ignite.internal.processors.query.h2.opt.GridH2Table)4 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)4 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)4