Search in sources :

Example 1 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class Analyze method analyzeTable.

/**
 * Analyze this table.
 *
 * @param session the session
 * @param table the table
 * @param sample the number of sample rows
 * @param manual whether the command was called by the user
 */
public static void analyzeTable(Session session, Table table, int sample, boolean manual) {
    if (table.getTableType() != TableType.TABLE || table.isHidden() || session == null) {
        return;
    }
    if (!manual) {
        if (session.getDatabase().isSysTableLocked()) {
            return;
        }
        if (table.hasSelectTrigger()) {
            return;
        }
    }
    if (table.isTemporary() && !table.isGlobalTemporary() && session.findLocalTempTable(table.getName()) == null) {
        return;
    }
    if (table.isLockedExclusively() && !table.isLockedExclusivelyBy(session)) {
        return;
    }
    if (!session.getUser().hasRight(table, Right.SELECT)) {
        return;
    }
    if (session.getCancel() != 0) {
        // if the connection is closed and there is something to undo
        return;
    }
    Column[] columns = table.getColumns();
    if (columns.length == 0) {
        return;
    }
    Database db = session.getDatabase();
    StatementBuilder buff = new StatementBuilder("SELECT ");
    for (Column col : columns) {
        buff.appendExceptFirst(", ");
        int type = col.getType();
        if (type == Value.BLOB || type == Value.CLOB) {
            // can not index LOB columns, so calculating
            // the selectivity is not required
            buff.append("MAX(NULL)");
        } else {
            buff.append("SELECTIVITY(").append(col.getSQL()).append(')');
        }
    }
    buff.append(" FROM ").append(table.getSQL());
    if (sample > 0) {
        buff.append(" LIMIT ? SAMPLE_SIZE ? ");
    }
    String sql = buff.toString();
    Prepared command = session.prepare(sql);
    if (sample > 0) {
        ArrayList<Parameter> params = command.getParameters();
        params.get(0).setValue(ValueInt.get(1));
        params.get(1).setValue(ValueInt.get(sample));
    }
    ResultInterface result = command.query(0);
    result.next();
    for (int j = 0; j < columns.length; j++) {
        Value v = result.currentRow()[j];
        if (v != ValueNull.INSTANCE) {
            int selectivity = v.getInt();
            columns[j].setSelectivity(selectivity);
        }
    }
    db.updateMeta(session, table);
}
Also used : ResultInterface(org.h2.result.ResultInterface) Column(org.h2.table.Column) StatementBuilder(org.h2.util.StatementBuilder) Database(org.h2.engine.Database) Prepared(org.h2.command.Prepared) Value(org.h2.value.Value) Parameter(org.h2.expression.Parameter)

Example 2 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class FunctionAlias method getMethodSignature.

private static String getMethodSignature(Method m) {
    StatementBuilder buff = new StatementBuilder(m.getName());
    buff.append('(');
    for (Class<?> p : m.getParameterTypes()) {
        // do not use a space here, because spaces are removed
        // in CreateFunctionAlias.setJavaClassMethod()
        buff.appendExceptFirst(",");
        if (p.isArray()) {
            buff.append(p.getComponentType().getName()).append("[]");
        } else {
            buff.append(p.getName());
        }
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 3 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class Merge method getPlanSQL.

@Override
public String getPlanSQL() {
    StatementBuilder buff = new StatementBuilder("MERGE INTO ");
    buff.append(targetTable.getSQL()).append('(');
    for (Column c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(')');
    if (keys != null) {
        buff.append(" KEY(");
        buff.resetCount();
        for (Column c : keys) {
            buff.appendExceptFirst(", ");
            buff.append(c.getSQL());
        }
        buff.append(')');
    }
    buff.append('\n');
    if (!valuesExpressionList.isEmpty()) {
        buff.append("VALUES ");
        int row = 0;
        for (Expression[] expr : valuesExpressionList) {
            if (row++ > 0) {
                buff.append(", ");
            }
            buff.append('(');
            buff.resetCount();
            for (Expression e : expr) {
                buff.appendExceptFirst(", ");
                if (e == null) {
                    buff.append("DEFAULT");
                } else {
                    buff.append(e.getSQL());
                }
            }
            buff.append(')');
        }
    } else {
        buff.append(query.getPlanSQL());
    }
    return buff.toString();
}
Also used : Column(org.h2.table.Column) Expression(org.h2.expression.Expression) StatementBuilder(org.h2.util.StatementBuilder)

Example 4 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class DropTable method prepareDrop.

private void prepareDrop() {
    table = getSchema().findTableOrView(session, tableName);
    if (table == null) {
        if (!ifExists) {
            throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableName);
        }
    } else {
        session.getUser().checkRight(table, Right.ALL);
        if (!table.canDrop()) {
            throw DbException.get(ErrorCode.CANNOT_DROP_TABLE_1, tableName);
        }
        if (dropAction == ConstraintActionType.RESTRICT) {
            StatementBuilder buff = new StatementBuilder();
            CopyOnWriteArrayList<TableView> dependentViews = table.getDependentViews();
            if (dependentViews != null && !dependentViews.isEmpty()) {
                for (TableView v : dependentViews) {
                    buff.appendExceptFirst(", ");
                    buff.append(v.getName());
                }
            }
            if (session.getDatabase().getSettings().standardDropTableRestrict) {
                final List<Constraint> constraints = table.getConstraints();
                if (constraints != null && !constraints.isEmpty()) {
                    for (Constraint c : constraints) {
                        if (c.getTable() != table) {
                            buff.appendExceptFirst(", ");
                            buff.append(c.getName());
                        }
                    }
                }
            }
            if (buff.length() > 0) {
                throw DbException.get(ErrorCode.CANNOT_DROP_2, tableName, buff.toString());
            }
        }
        table.lock(session, true, true);
    }
    if (next != null) {
        next.prepareDrop();
    }
}
Also used : Constraint(org.h2.constraint.Constraint) StatementBuilder(org.h2.util.StatementBuilder) TableView(org.h2.table.TableView)

Example 5 with StatementBuilder

use of org.h2.util.StatementBuilder in project h2database by h2database.

the class ConstraintReferential method checkExistingData.

@Override
public void checkExistingData(Session session) {
    if (session.getDatabase().isStarting()) {
        // don't check at startup
        return;
    }
    session.startStatementWithinTransaction();
    StatementBuilder buff = new StatementBuilder("SELECT 1 FROM (SELECT ");
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(" FROM ").append(table.getSQL()).append(" WHERE ");
    buff.resetCount();
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(" AND ");
        buff.append(c.getSQL()).append(" IS NOT NULL ");
    }
    buff.append(" ORDER BY ");
    buff.resetCount();
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(", ");
        buff.append(c.getSQL());
    }
    buff.append(") C WHERE NOT EXISTS(SELECT 1 FROM ").append(refTable.getSQL()).append(" P WHERE ");
    buff.resetCount();
    int i = 0;
    for (IndexColumn c : columns) {
        buff.appendExceptFirst(" AND ");
        buff.append("C.").append(c.getSQL()).append('=').append("P.").append(refColumns[i++].getSQL());
    }
    buff.append(')');
    String sql = buff.toString();
    ResultInterface r = session.prepare(sql).query(1);
    if (r.next()) {
        throw DbException.get(ErrorCode.REFERENTIAL_INTEGRITY_VIOLATED_PARENT_MISSING_1, getShortDescription(null, null));
    }
}
Also used : ResultInterface(org.h2.result.ResultInterface) StatementBuilder(org.h2.util.StatementBuilder) IndexColumn(org.h2.table.IndexColumn)

Aggregations

StatementBuilder (org.h2.util.StatementBuilder)82 Value (org.h2.value.Value)16 Column (org.h2.table.Column)15 IndexColumn (org.h2.table.IndexColumn)11 PreparedStatement (java.sql.PreparedStatement)9 Expression (org.h2.expression.Expression)9 ValueString (org.h2.value.ValueString)7 Index (org.h2.index.Index)6 DbException (org.h2.message.DbException)6 ResultSet (java.sql.ResultSet)5 Row (org.h2.result.Row)4 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Constraint (org.h2.constraint.Constraint)3 Database (org.h2.engine.Database)3 ExpressionColumn (org.h2.expression.ExpressionColumn)3 HashMap (java.util.HashMap)2 SelectOrderBy (org.h2.command.dml.SelectOrderBy)2 ResultInterface (org.h2.result.ResultInterface)2 SearchRow (org.h2.result.SearchRow)2