Search in sources :

Example 66 with Delete

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

the class JdbcDatabaseMetaData method getColumnPrivileges.

/**
 * Gets the list of column privileges. The result set is sorted by
 * COLUMN_NAME and PRIVILEGE
 *
 * <ol>
 * <li>TABLE_CAT (String) table catalog</li>
 * <li>TABLE_SCHEM (String) table schema</li>
 * <li>TABLE_NAME (String) table name</li>
 * <li>COLUMN_NAME (String) column name</li>
 * <li>GRANTOR (String) grantor of access</li>
 * <li>GRANTEE (String) grantee of access</li>
 * <li>PRIVILEGE (String) SELECT, INSERT, UPDATE, DELETE or REFERENCES
 * (only one per row)</li>
 * <li>IS_GRANTABLE (String) YES means the grantee can grant access to
 * others</li>
 * </ol>
 *
 * @param catalogPattern null (to get all objects) or the catalog name
 * @param schemaPattern null (to get all objects) or a schema name
 *            (uppercase for unquoted names)
 * @param table a table name (uppercase for unquoted names)
 * @param columnNamePattern null (to get all objects) or a column name
 *            (uppercase for unquoted names)
 * @return the list of privileges
 * @throws SQLException if the connection is closed
 */
@Override
public ResultSet getColumnPrivileges(String catalogPattern, String schemaPattern, String table, String columnNamePattern) throws SQLException {
    try {
        if (isDebugEnabled()) {
            debugCode("getColumnPrivileges(" + quote(catalogPattern) + ", " + quote(schemaPattern) + ", " + quote(table) + ", " + quote(columnNamePattern) + ");");
        }
        checkClosed();
        PreparedStatement prep = conn.prepareAutoCloseStatement("SELECT " + "TABLE_CATALOG TABLE_CAT, " + "TABLE_SCHEMA TABLE_SCHEM, " + "TABLE_NAME, " + "COLUMN_NAME, " + "GRANTOR, " + "GRANTEE, " + "PRIVILEGE_TYPE PRIVILEGE, " + "IS_GRANTABLE " + "FROM INFORMATION_SCHEMA.COLUMN_PRIVILEGES " + "WHERE TABLE_CATALOG LIKE ? ESCAPE ? " + "AND TABLE_SCHEMA LIKE ? ESCAPE ? " + "AND TABLE_NAME = ? " + "AND COLUMN_NAME LIKE ? ESCAPE ? " + "ORDER BY COLUMN_NAME, PRIVILEGE");
        prep.setString(1, getCatalogPattern(catalogPattern));
        prep.setString(2, "\\");
        prep.setString(3, getSchemaPattern(schemaPattern));
        prep.setString(4, "\\");
        prep.setString(5, table);
        prep.setString(6, getPattern(columnNamePattern));
        prep.setString(7, "\\");
        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 67 with Delete

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

the class FullText method dropIndex.

/**
 * Drop an existing full text index for a table. This method returns
 * silently if no index for this table exists.
 *
 * @param conn the connection
 * @param schema the schema name of the table (case sensitive)
 * @param table the table name (case sensitive)
 */
public static void dropIndex(Connection conn, String schema, String table) throws SQLException {
    init(conn);
    PreparedStatement prep = conn.prepareStatement("SELECT ID FROM " + SCHEMA + ".INDEXES WHERE SCHEMA=? AND TABLE=?");
    prep.setString(1, schema);
    prep.setString(2, table);
    ResultSet rs = prep.executeQuery();
    if (!rs.next()) {
        return;
    }
    int indexId = rs.getInt(1);
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".INDEXES WHERE ID=?");
    prep.setInt(1, indexId);
    prep.execute();
    createOrDropTrigger(conn, schema, table, false);
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".ROWS WHERE INDEXID=? AND ROWNUM<10000");
    while (true) {
        prep.setInt(1, indexId);
        int deleted = prep.executeUpdate();
        if (deleted == 0) {
            break;
        }
    }
    prep = conn.prepareStatement("DELETE FROM " + SCHEMA + ".MAP " + "WHERE NOT EXISTS (SELECT * FROM " + SCHEMA + ".ROWS R WHERE R.ID=ROWID) AND ROWID<10000");
    while (true) {
        int deleted = prep.executeUpdate();
        if (deleted == 0) {
            break;
        }
    }
}
Also used : SimpleResultSet(org.h2.tools.SimpleResultSet) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 68 with Delete

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

the class PageBtreeLeaf method remove.

@Override
SearchRow remove(SearchRow row) {
    int at = find(row, false, false, true);
    SearchRow delete = getRow(at);
    if (index.compareRows(row, delete) != 0 || delete.getKey() != row.getKey()) {
        throw DbException.get(ErrorCode.ROW_NOT_FOUND_WHEN_DELETING_1, index.getSQL() + ": " + row);
    }
    index.getPageStore().logUndo(this, data);
    if (entryCount == 1) {
        // the page is now empty
        return row;
    }
    removeRow(at);
    memoryChange();
    index.getPageStore().update(this);
    if (at == entryCount) {
        // the last row changed
        return getRow(at - 1);
    }
    // the last row didn't change
    return null;
}
Also used : SearchRow(org.h2.result.SearchRow)

Example 69 with Delete

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

the class PageDataIndex method remove.

@Override
public void remove(Session session, Row row) {
    if (tableData.getContainsLargeObject()) {
        for (int i = 0, len = row.getColumnCount(); i < len; i++) {
            Value v = row.getValue(i);
            if (v.isLinkedToTable()) {
                session.removeAtCommitStop(v);
            }
        }
    }
    if (trace.isDebugEnabled()) {
        trace.debug("{0} remove {1}", getName(), row);
    }
    if (rowCount == 1) {
        removeAllRows();
    } else {
        try {
            long key = row.getKey();
            PageData root = getPage(rootPageId, 0);
            root.remove(key);
            invalidateRowCount();
            rowCount--;
        } finally {
            store.incrementChangeCount();
        }
    }
    if (multiVersion) {
        // if storage is null, the delete flag is not yet set
        row.setDeleted(true);
        if (delta == null) {
            delta = new HashSet<>();
        }
        boolean wasAdded = delta.remove(row);
        if (!wasAdded) {
            delta.add(row);
        }
        incrementRowCount(session.getId(), -1);
    }
    store.logAddOrRemoveRow(session, tableData.getId(), row, false);
}
Also used : Value(org.h2.value.Value)

Example 70 with Delete

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

the class LinkedIndex method remove.

@Override
public void remove(Session session, Row row) {
    ArrayList<Value> params = New.arrayList();
    StatementBuilder buff = new StatementBuilder("DELETE FROM ");
    buff.append(targetTableName).append(" WHERE ");
    for (int i = 0; i < row.getColumnCount(); i++) {
        buff.appendExceptFirst("AND ");
        Column col = table.getColumn(i);
        buff.append(col.getSQL());
        Value v = row.getValue(i);
        if (isNull(v)) {
            buff.append(" IS NULL ");
        } else {
            buff.append('=');
            addParameter(buff, col);
            params.add(v);
            buff.append(' ');
        }
    }
    String sql = buff.toString();
    try {
        PreparedStatement prep = link.execute(sql, params, false);
        int count = prep.executeUpdate();
        link.reusePreparedStatement(prep, sql);
        rowCount -= count;
    } catch (Exception e) {
        throw TableLink.wrapException(sql, e);
    }
}
Also used : Column(org.h2.table.Column) IndexColumn(org.h2.table.IndexColumn) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement) DbException(org.h2.message.DbException)

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