Search in sources :

Example 21 with StatementBuilder

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

the class JavaFunction method getSQL.

@Override
public String getSQL() {
    StatementBuilder buff = new StatementBuilder();
    // TODO always append the schema once FUNCTIONS_IN_SCHEMA is enabled
    if (functionAlias.getDatabase().getSettings().functionsInSchema || !functionAlias.getSchema().getName().equals(Constants.SCHEMA_MAIN)) {
        buff.append(Parser.quoteIdentifier(functionAlias.getSchema().getName())).append('.');
    }
    buff.append(Parser.quoteIdentifier(functionAlias.getName())).append('(');
    for (Expression e : args) {
        buff.appendExceptFirst(", ");
        buff.append(e.getSQL());
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder)

Example 22 with StatementBuilder

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

the class PageStore method addMeta.

/**
 * Add the meta data of an index.
 *
 * @param index the index to add
 * @param session the session
 */
public void addMeta(PageIndex index, Session session) {
    Table table = index.getTable();
    if (SysProperties.CHECK) {
        if (!table.isTemporary()) {
            // the Database lock before we take the PageStore lock
            synchronized (database) {
                synchronized (this) {
                    database.verifyMetaLocked(session);
                }
            }
        }
    }
    synchronized (this) {
        int type = index instanceof PageDataIndex ? META_TYPE_DATA_INDEX : META_TYPE_BTREE_INDEX;
        IndexColumn[] columns = index.getIndexColumns();
        StatementBuilder buff = new StatementBuilder();
        for (IndexColumn col : columns) {
            buff.appendExceptFirst(",");
            int id = col.column.getColumnId();
            buff.append(id);
            int sortType = col.sortType;
            if (sortType != 0) {
                buff.append('/');
                buff.append(sortType);
            }
        }
        String columnList = buff.toString();
        CompareMode mode = table.getCompareMode();
        String options = mode.getName() + "," + mode.getStrength() + ",";
        if (table.isTemporary()) {
            options += "temp";
        }
        options += ",";
        if (index instanceof PageDelegateIndex) {
            options += "d";
        }
        options += "," + mode.isBinaryUnsigned();
        Row row = metaTable.getTemplateRow();
        row.setValue(0, ValueInt.get(index.getId()));
        row.setValue(1, ValueInt.get(type));
        row.setValue(2, ValueInt.get(table.getId()));
        row.setValue(3, ValueInt.get(index.getRootPageId()));
        row.setValue(4, ValueString.get(options));
        row.setValue(5, ValueString.get(columnList));
        row.setKey(index.getId() + 1);
        metaIndex.add(session, row);
    }
}
Also used : RegularTable(org.h2.table.RegularTable) Table(org.h2.table.Table) PageDelegateIndex(org.h2.index.PageDelegateIndex) PageDataIndex(org.h2.index.PageDataIndex) StatementBuilder(org.h2.util.StatementBuilder) CompareMode(org.h2.value.CompareMode) ValueString(org.h2.value.ValueString) Row(org.h2.result.Row) IndexColumn(org.h2.table.IndexColumn)

Example 23 with StatementBuilder

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

the class Trace method formatParams.

/**
 * Format the parameter list.
 *
 * @param parameters the parameter list
 * @return the formatted text
 */
public static String formatParams(ArrayList<? extends ParameterInterface> parameters) {
    if (parameters.isEmpty()) {
        return "";
    }
    StatementBuilder buff = new StatementBuilder();
    int i = 0;
    boolean params = false;
    for (ParameterInterface p : parameters) {
        if (p.isValueSet()) {
            if (!params) {
                buff.append(" {");
                params = true;
            }
            buff.appendExceptFirst(", ");
            Value v = p.getParamValue();
            buff.append(++i).append(": ").append(v.getTraceSQL());
        }
    }
    if (params) {
        buff.append('}');
    }
    return buff.toString();
}
Also used : ParameterInterface(org.h2.expression.ParameterInterface) StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value)

Example 24 with StatementBuilder

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

the class SimpleRow method toString.

@Override
public String toString() {
    StatementBuilder buff = new StatementBuilder("( /* key:");
    buff.append(getKey());
    if (version != 0) {
        buff.append(" v:").append(version);
    }
    buff.append(" */ ");
    for (Value v : data) {
        buff.appendExceptFirst(", ");
        buff.append(v == null ? "null" : v.getTraceSQL());
    }
    return buff.append(')').toString();
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value)

Example 25 with StatementBuilder

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

the class UpdatableRow method insertRow.

/**
 * Insert a new row into the database.
 *
 * @param row the new row
 * @throws SQLException if the row could not be inserted
 */
public void insertRow(Value[] row) throws SQLException {
    StatementBuilder buff = new StatementBuilder("INSERT INTO ");
    appendTableName(buff);
    buff.append('(');
    appendColumnList(buff, false);
    buff.append(")VALUES(");
    buff.resetCount();
    for (int i = 0; i < columnCount; i++) {
        buff.appendExceptFirst(",");
        Value v = row[i];
        if (v == null) {
            buff.append("DEFAULT");
        } else {
            buff.append('?');
        }
    }
    buff.append(')');
    PreparedStatement prep = conn.prepareStatement(buff.toString());
    for (int i = 0, j = 0; i < columnCount; i++) {
        Value v = row[i];
        if (v != null) {
            v.set(prep, j++ + 1);
        }
    }
    int count = prep.executeUpdate();
    if (count != 1) {
        throw DbException.get(ErrorCode.NO_DATA_AVAILABLE);
    }
}
Also used : StatementBuilder(org.h2.util.StatementBuilder) Value(org.h2.value.Value) PreparedStatement(java.sql.PreparedStatement)

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