Search in sources :

Example 1 with Database

use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.

the class TableFunction method getTable.

private ValueResultSet getTable(Session session, Expression[] argList, boolean onlyColumnList, boolean distinctRows) {
    int len = columnList.length;
    Expression[] header = new Expression[len];
    Database db = session.getDatabase();
    for (int i = 0; i < len; i++) {
        Column c = columnList[i];
        ExpressionColumn col = new ExpressionColumn(db, c);
        header[i] = col;
    }
    LocalResult result = new LocalResult(session, header, len);
    if (distinctRows) {
        result.setDistinct();
    }
    if (!onlyColumnList) {
        Value[][] list = new Value[len][];
        int rows = 0;
        for (int i = 0; i < len; i++) {
            Value v = argList[i].getValue(session);
            if (v == ValueNull.INSTANCE) {
                list[i] = new Value[0];
            } else {
                ValueArray array = (ValueArray) v.convertTo(Value.ARRAY);
                Value[] l = array.getList();
                list[i] = l;
                rows = Math.max(rows, l.length);
            }
        }
        for (int row = 0; row < rows; row++) {
            Value[] r = new Value[len];
            for (int j = 0; j < len; j++) {
                Value[] l = list[j];
                Value v;
                if (l.length <= row) {
                    v = ValueNull.INSTANCE;
                } else {
                    Column c = columnList[j];
                    v = l[row];
                    v = c.convert(v);
                    v = v.convertPrecision(c.getPrecision(), false);
                    v = v.convertScale(true, c.getScale());
                }
                r[j] = v;
            }
            result.addRow(r);
        }
    }
    result.done();
    ValueResultSet vr = ValueResultSet.get(getSimpleResultSet(result, Integer.MAX_VALUE));
    return vr;
}
Also used : LocalResult(com.wplatform.ddal.result.LocalResult) Column(com.wplatform.ddal.dbobject.table.Column) Database(com.wplatform.ddal.engine.Database)

Example 2 with Database

use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.

the class Select method expandColumnList.

private void expandColumnList() {
    Database db = session.getDatabase();
    // the expressions may change within the loop
    for (int i = 0; i < expressions.size(); i++) {
        Expression expr = expressions.get(i);
        if (!expr.isWildcard()) {
            continue;
        }
        String schemaName = expr.getSchemaName();
        String tableAlias = expr.getTableAlias();
        if (tableAlias == null) {
            int temp = i;
            expressions.remove(i);
            for (TableFilter filter : filters) {
                Wildcard c2 = new Wildcard(filter.getTable().getSchema().getName(), filter.getTableAlias());
                expressions.add(i++, c2);
            }
            i = temp - 1;
        } else {
            TableFilter filter = null;
            for (TableFilter f : filters) {
                if (db.equalsIdentifiers(tableAlias, f.getTableAlias())) {
                    if (schemaName == null || db.equalsIdentifiers(schemaName, f.getSchemaName())) {
                        filter = f;
                        break;
                    }
                }
            }
            if (filter == null) {
                throw DbException.get(ErrorCode.TABLE_OR_VIEW_NOT_FOUND_1, tableAlias);
            }
            Table t = filter.getTable();
            String alias = filter.getTableAlias();
            expressions.remove(i);
            Column[] columns = t.getColumns();
            for (Column c : columns) {
                if (filter.isNaturalJoinColumn(c)) {
                    continue;
                }
                ExpressionColumn ec = new ExpressionColumn(session.getDatabase(), null, alias, c.getName());
                expressions.add(i++, ec);
            }
            i--;
        }
    }
}
Also used : Expression(com.wplatform.ddal.command.expression.Expression) Wildcard(com.wplatform.ddal.command.expression.Wildcard) ExpressionColumn(com.wplatform.ddal.command.expression.ExpressionColumn) Database(com.wplatform.ddal.engine.Database) ExpressionColumn(com.wplatform.ddal.command.expression.ExpressionColumn)

Example 3 with Database

use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.

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 : Expression(com.wplatform.ddal.command.expression.Expression) Database(com.wplatform.ddal.engine.Database)

Example 4 with Database

use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.

the class Expression method getExpressionColumns.

/**
 * Extracts expression columns from the given result set.
 *
 * @param session the session
 * @param rs      the result set
 * @return an array of expression columns
 */
public static Expression[] getExpressionColumns(Session session, ResultSet rs) {
    try {
        ResultSetMetaData meta = rs.getMetaData();
        int columnCount = meta.getColumnCount();
        Expression[] expressions = new Expression[columnCount];
        Database db = session == null ? null : session.getDatabase();
        for (int i = 0; i < columnCount; i++) {
            String name = meta.getColumnLabel(i + 1);
            int type = DataType.getValueTypeFromResultSet(meta, i + 1);
            int precision = meta.getPrecision(i + 1);
            int scale = meta.getScale(i + 1);
            int displaySize = meta.getColumnDisplaySize(i + 1);
            Column col = new Column(name, type, precision, scale, displaySize);
            Expression expr = new ExpressionColumn(db, col);
            expressions[i] = expr;
        }
        return expressions;
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Column(com.wplatform.ddal.dbobject.table.Column) SQLException(java.sql.SQLException) Database(com.wplatform.ddal.engine.Database)

Example 5 with Database

use of com.wplatform.ddal.engine.Database in project jdbc-shards by wplatform.

the class SetExecutor method executeUpdate.

@Override
public int executeUpdate() {
    Database database = session.getDatabase();
    String stringValue = prepared.getStringValue();
    int type = prepared.getSetType();
    switch(type) {
        case SetTypes.QUERY_TIMEOUT:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("QUERY_TIMEOUT", getIntValue());
                }
                int value = getIntValue();
                session.setQueryTimeout(value);
                break;
            }
        case SetTypes.ALLOW_LITERALS:
            {
                session.getUser().checkAdmin();
                int value = getIntValue();
                if (value < 0 || value > 2) {
                    throw DbException.getInvalidValueException("ALLOW_LITERALS", getIntValue());
                }
                database.setAllowLiterals(value);
                break;
            }
        case SetTypes.MAX_MEMORY_ROWS:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("MAX_MEMORY_ROWS", getIntValue());
                }
                session.getUser().checkAdmin();
                database.setMaxMemoryRows(getIntValue());
                break;
            }
        case SetTypes.MODE:
            Mode mode = Mode.getInstance(stringValue);
            if (mode == null) {
                throw DbException.get(ErrorCode.UNKNOWN_MODE_1, stringValue);
            }
            if (database.getMode() != mode) {
                session.getUser().checkAdmin();
                database.setMode(mode);
            }
            break;
        case SetTypes.SCHEMA:
            {
                Schema schema = database.getSchema(stringValue);
                session.setCurrentSchema(schema);
                break;
            }
        case SetTypes.TRACE_LEVEL_FILE:
            session.getUser().checkAdmin();
            database.getTraceSystem().setLevelFile(getIntValue());
            break;
        case SetTypes.TRACE_LEVEL_SYSTEM_OUT:
            session.getUser().checkAdmin();
            database.getTraceSystem().setLevelSystemOut(getIntValue());
            break;
        case SetTypes.THROTTLE:
            {
                if (getIntValue() < 0) {
                    throw DbException.getInvalidValueException("THROTTLE", getIntValue());
                }
                session.setThrottle(getIntValue());
                break;
            }
        case SetTypes.CACHE_SIZE:
        case SetTypes.CLUSTER:
        case SetTypes.COLLATION:
        case SetTypes.BINARY_COLLATION:
        case SetTypes.COMPRESS_LOB:
        case SetTypes.CREATE_BUILD:
        case SetTypes.DATABASE_EVENT_LISTENER:
        case SetTypes.DB_CLOSE_DELAY:
        case SetTypes.DEFAULT_LOCK_TIMEOUT:
        case SetTypes.DEFAULT_TABLE_TYPE:
        case SetTypes.EXCLUSIVE:
        case SetTypes.JAVA_OBJECT_SERIALIZER:
        case SetTypes.IGNORECASE:
        case SetTypes.LOCK_MODE:
        case SetTypes.LOCK_TIMEOUT:
        case SetTypes.LOG:
        case SetTypes.MAX_LENGTH_INPLACE_LOB:
        case SetTypes.MAX_LOG_SIZE:
        case SetTypes.MAX_MEMORY_UNDO:
        case SetTypes.MAX_OPERATION_MEMORY:
        case SetTypes.MULTI_THREADED:
        case SetTypes.MVCC:
        case SetTypes.OPTIMIZE_REUSE_RESULTS:
        case SetTypes.REDO_LOG_BINARY:
        case SetTypes.REFERENTIAL_INTEGRITY:
        case SetTypes.QUERY_STATISTICS:
        case SetTypes.SCHEMA_SEARCH_PATH:
        case SetTypes.TRACE_MAX_FILE_SIZE:
        case SetTypes.UNDO_LOG:
        case SetTypes.VARIABLE:
        case SetTypes.WRITE_DELAY:
        case SetTypes.RETENTION_TIME:
        default:
            DbException.throwInternalError("type=" + type);
    }
    return 0;
}
Also used : Mode(com.wplatform.ddal.engine.Mode) Schema(com.wplatform.ddal.dbobject.schema.Schema) Database(com.wplatform.ddal.engine.Database)

Aggregations

Database (com.wplatform.ddal.engine.Database)12 Expression (com.wplatform.ddal.command.expression.Expression)3 Column (com.wplatform.ddal.dbobject.table.Column)3 SQLException (java.sql.SQLException)3 ExpressionColumn (com.wplatform.ddal.command.expression.ExpressionColumn)2 DbException (com.wplatform.ddal.message.DbException)2 LocalResult (com.wplatform.ddal.result.LocalResult)2 Wildcard (com.wplatform.ddal.command.expression.Wildcard)1 Configuration (com.wplatform.ddal.config.Configuration)1 XmlConfigParser (com.wplatform.ddal.config.parser.XmlConfigParser)1 Schema (com.wplatform.ddal.dbobject.schema.Schema)1 TableFilter (com.wplatform.ddal.dbobject.table.TableFilter)1 Mode (com.wplatform.ddal.engine.Mode)1 Session (com.wplatform.ddal.engine.Session)1 ValueString (com.wplatform.ddal.value.ValueString)1 InputStream (java.io.InputStream)1 ResultSetMetaData (java.sql.ResultSetMetaData)1