Search in sources :

Example 81 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class Select method init.

@Override
public void init() {
    if (SysProperties.CHECK && checkInit) {
        DbException.throwInternalError();
    }
    expandColumnList();
    visibleColumnCount = expressions.size();
    ArrayList<String> expressionSQL;
    if (orderList != null || group != null) {
        expressionSQL = New.arrayList();
        for (int i = 0; i < visibleColumnCount; i++) {
            Expression expr = expressions.get(i);
            expr = expr.getNonAliasExpression();
            String sql = expr.getSQL();
            expressionSQL.add(sql);
        }
    } else {
        expressionSQL = null;
    }
    if (orderList != null) {
        initOrder(session, expressions, expressionSQL, orderList, visibleColumnCount, distinct, filters);
    }
    distinctColumnCount = expressions.size();
    if (having != null) {
        expressions.add(having);
        havingIndex = expressions.size() - 1;
        having = null;
    } else {
        havingIndex = -1;
    }
    Database db = session.getDatabase();
    // and 'GROUP BY' expressions at the end
    if (group != null) {
        int size = group.size();
        int expSize = expressionSQL.size();
        groupIndex = new int[size];
        for (int i = 0; i < size; i++) {
            Expression expr = group.get(i);
            String sql = expr.getSQL();
            int found = -1;
            for (int j = 0; j < expSize; j++) {
                String s2 = expressionSQL.get(j);
                if (db.equalsIdentifiers(s2, sql)) {
                    found = j;
                    break;
                }
            }
            if (found < 0) {
                // special case: GROUP BY a column alias
                for (int j = 0; j < expSize; j++) {
                    Expression e = expressions.get(j);
                    if (db.equalsIdentifiers(sql, e.getAlias())) {
                        found = j;
                        break;
                    }
                    sql = expr.getAlias();
                    if (db.equalsIdentifiers(sql, e.getAlias())) {
                        found = j;
                        break;
                    }
                }
            }
            if (found < 0) {
                int index = expressions.size();
                groupIndex[i] = index;
                expressions.add(expr);
            } else {
                groupIndex[i] = found;
            }
        }
        groupByExpression = new boolean[expressions.size()];
        for (int gi : groupIndex) {
            groupByExpression[gi] = true;
        }
        group = null;
    }
    // map columns in select list and condition
    for (TableFilter f : filters) {
        mapColumns(f, 0);
    }
    if (havingIndex >= 0) {
        Expression expr = expressions.get(havingIndex);
        SelectListColumnResolver res = new SelectListColumnResolver(this);
        expr.mapColumns(res, 0);
    }
    checkInit = true;
}
Also used : Database(org.h2.engine.Database)

Example 82 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class SessionRemote method initTransfer.

private Transfer initTransfer(ConnectionInfo ci, String db, String server) throws IOException {
    Socket socket = NetUtils.createSocket(server, Constants.DEFAULT_TCP_PORT, ci.isSSL());
    Transfer trans = new Transfer(this, socket);
    trans.setSSL(ci.isSSL());
    trans.init();
    trans.writeInt(Constants.TCP_PROTOCOL_VERSION_MIN_SUPPORTED);
    trans.writeInt(Constants.TCP_PROTOCOL_VERSION_MAX_SUPPORTED);
    trans.writeString(db);
    trans.writeString(ci.getOriginalURL());
    trans.writeString(ci.getUserName());
    trans.writeBytes(ci.getUserPasswordHash());
    trans.writeBytes(ci.getFilePasswordHash());
    String[] keys = ci.getKeys();
    trans.writeInt(keys.length);
    for (String key : keys) {
        trans.writeString(key).writeString(ci.getProperty(key));
    }
    try {
        done(trans);
        clientVersion = trans.readInt();
        trans.setVersion(clientVersion);
        if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_14) {
            if (ci.getFileEncryptionKey() != null) {
                trans.writeBytes(ci.getFileEncryptionKey());
            }
        }
        trans.writeInt(SessionRemote.SESSION_SET_ID);
        trans.writeString(sessionId);
        done(trans);
        if (clientVersion >= Constants.TCP_PROTOCOL_VERSION_15) {
            autoCommit = trans.readBoolean();
        } else {
            autoCommit = true;
        }
        return trans;
    } catch (DbException e) {
        trans.close();
        throw e;
    }
}
Also used : Transfer(org.h2.value.Transfer) Socket(java.net.Socket) DbException(org.h2.message.DbException)

Example 83 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class Comparison method getAdditional.

/**
 * Get an additional condition if possible. Example: given two conditions
 * A=B AND B=C, the new condition A=C is returned. Given the two conditions
 * A=1 OR A=2, the new condition A IN(1, 2) is returned.
 *
 * @param session the session
 * @param other the second condition
 * @param and true for AND, false for OR
 * @return null or the third condition
 */
Expression getAdditional(Session session, Comparison other, boolean and) {
    if (compareType == other.compareType && compareType == EQUAL) {
        boolean lc = left.isConstant();
        boolean rc = right.isConstant();
        boolean l2c = other.left.isConstant();
        boolean r2c = other.right.isConstant();
        String l = left.getSQL();
        String l2 = other.left.getSQL();
        String r = right.getSQL();
        String r2 = other.right.getSQL();
        if (and) {
            // must not compare constants. example: NOT(B=2 AND B=3)
            if (!(rc && r2c) && l.equals(l2)) {
                return new Comparison(session, EQUAL, right, other.right);
            } else if (!(rc && l2c) && l.equals(r2)) {
                return new Comparison(session, EQUAL, right, other.left);
            } else if (!(lc && r2c) && r.equals(l2)) {
                return new Comparison(session, EQUAL, left, other.right);
            } else if (!(lc && l2c) && r.equals(r2)) {
                return new Comparison(session, EQUAL, left, other.left);
            }
        } else {
            // a=b OR a=c
            Database db = session.getDatabase();
            if (rc && r2c && l.equals(l2)) {
                return new ConditionIn(db, left, new ArrayList<>(Arrays.asList(right, other.right)));
            } else if (rc && l2c && l.equals(r2)) {
                return new ConditionIn(db, left, new ArrayList<>(Arrays.asList(right, other.left)));
            } else if (lc && r2c && r.equals(l2)) {
                return new ConditionIn(db, right, new ArrayList<>(Arrays.asList(left, other.right)));
            } else if (lc && l2c && r.equals(r2)) {
                return new ConditionIn(db, right, new ArrayList<>(Arrays.asList(left, other.left)));
            }
        }
    }
    return null;
}
Also used : Database(org.h2.engine.Database) ArrayList(java.util.ArrayList)

Example 84 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class MetaRecord method execute.

/**
 * Execute the meta data statement.
 *
 * @param db the database
 * @param systemSession the system session
 * @param listener the database event listener
 */
void execute(Database db, Session systemSession, DatabaseEventListener listener) {
    try {
        Prepared command = systemSession.prepare(sql);
        command.setObjectId(id);
        command.update();
    } catch (DbException e) {
        e = e.addSQL(sql);
        SQLException s = e.getSQLException();
        db.getTrace(Trace.DATABASE).error(s, sql);
        if (listener != null) {
            listener.exceptionThrown(s, sql);
        // continue startup in this case
        } else {
            throw e;
        }
    }
}
Also used : SQLException(java.sql.SQLException) Prepared(org.h2.command.Prepared) DbException(org.h2.message.DbException)

Example 85 with Db

use of org.h2.jaqu.Db in project h2database by h2database.

the class TableView method createShadowTableForRecursiveTableExpression.

/**
 * Create a table for a recursive query.
 *
 * @param isPersistent whether the table is persisted
 * @param targetSession the session
 * @param cteViewName the name
 * @param schema the schema
 * @param columns the columns
 * @param db the database
 * @return the table
 */
public static Table createShadowTableForRecursiveTableExpression(boolean isPersistent, Session targetSession, String cteViewName, Schema schema, List<Column> columns, Database db) {
    // create table data object
    CreateTableData recursiveTableData = new CreateTableData();
    recursiveTableData.id = db.allocateObjectId();
    recursiveTableData.columns = new ArrayList<>(columns);
    recursiveTableData.tableName = cteViewName;
    recursiveTableData.temporary = !isPersistent;
    recursiveTableData.persistData = true;
    recursiveTableData.persistIndexes = isPersistent;
    recursiveTableData.create = true;
    recursiveTableData.session = targetSession;
    // this gets a meta table lock that is not released
    Table recursiveTable = schema.createTable(recursiveTableData);
    if (isPersistent) {
        // this unlock is to prevent lock leak from schema.createTable()
        db.unlockMeta(targetSession);
        synchronized (targetSession) {
            db.addSchemaObject(targetSession, recursiveTable);
        }
    } else {
        targetSession.addLocalTempTable(recursiveTable);
    }
    return recursiveTable;
}
Also used : CreateTableData(org.h2.command.ddl.CreateTableData)

Aggregations

Database (org.h2.engine.Database)70 Connection (java.sql.Connection)31 Statement (java.sql.Statement)20 Table (org.h2.table.Table)19 PreparedStatement (java.sql.PreparedStatement)18 ResultSet (java.sql.ResultSet)13 SQLException (java.sql.SQLException)13 Column (org.h2.table.Column)12 JdbcDataSource (org.h2.jdbcx.JdbcDataSource)9 StatementBuilder (org.h2.util.StatementBuilder)9 DbObject (org.h2.engine.DbObject)8 File (java.io.File)7 IOException (java.io.IOException)7 ArrayList (java.util.ArrayList)7 DbException (org.h2.message.DbException)7 Schema (org.h2.schema.Schema)7 Before (org.junit.Before)7 InputStream (java.io.InputStream)6 ExpressionColumn (org.h2.expression.ExpressionColumn)6 JdbcConnection (org.h2.jdbc.JdbcConnection)6