Search in sources :

Example 16 with Schema

use of com.wplatform.ddal.dbobject.schema.Schema in project jdbc-shards by wplatform.

the class Parser method parseValuesTable.

private TableFilter parseValuesTable() {
    Schema mainSchema = database.getSchema(Constants.SCHEMA_MAIN);
    TableFunction tf = (TableFunction) Function.getFunction(database, "TABLE");
    ArrayList<Column> columns = New.arrayList();
    ArrayList<ArrayList<Expression>> rows = New.arrayList();
    do {
        int i = 0;
        ArrayList<Expression> row = New.arrayList();
        boolean multiColumn = readIf("(");
        do {
            Expression expr = readExpression();
            expr = expr.optimize(session);
            int type = expr.getType();
            long prec;
            int scale, displaySize;
            Column column;
            String columnName = "C" + (i + 1);
            if (rows.size() == 0) {
                if (type == Value.UNKNOWN) {
                    type = Value.STRING;
                }
                DataType dt = DataType.getDataType(type);
                prec = dt.defaultPrecision;
                scale = dt.defaultScale;
                displaySize = dt.defaultDisplaySize;
                column = new Column(columnName, type, prec, scale, displaySize);
                columns.add(column);
            }
            prec = expr.getPrecision();
            scale = expr.getScale();
            displaySize = expr.getDisplaySize();
            if (i >= columns.size()) {
                throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
            }
            Column c = columns.get(i);
            type = Value.getHigherOrder(c.getType(), type);
            prec = Math.max(c.getPrecision(), prec);
            scale = Math.max(c.getScale(), scale);
            displaySize = Math.max(c.getDisplaySize(), displaySize);
            column = new Column(columnName, type, prec, scale, displaySize);
            columns.set(i, column);
            row.add(expr);
            i++;
        } while (multiColumn && readIf(","));
        if (multiColumn) {
            read(")");
        }
        rows.add(row);
    } while (readIf(","));
    int columnCount = columns.size();
    int rowCount = rows.size();
    for (int i = 0; i < rowCount; i++) {
        if (rows.get(i).size() != columnCount) {
            throw DbException.get(ErrorCode.COLUMN_COUNT_DOES_NOT_MATCH);
        }
    }
    for (int i = 0; i < columnCount; i++) {
        Column c = columns.get(i);
        if (c.getType() == Value.UNKNOWN) {
            c = new Column(c.getName(), Value.STRING, 0, 0, 0);
            columns.set(i, c);
        }
        Expression[] array = new Expression[rowCount];
        for (int j = 0; j < rowCount; j++) {
            array[j] = rows.get(j).get(i);
        }
        ExpressionList list = new ExpressionList(array);
        tf.setParameter(i, list);
    }
    tf.setColumns(columns);
    tf.doneWithParameters();
    Table table = new FunctionTable(mainSchema, session, tf, tf);
    TableFilter filter = new TableFilter(session, table, null, rightsChecked, currentSelect);
    return filter;
}
Also used : Schema(com.wplatform.ddal.dbobject.schema.Schema) ArrayList(java.util.ArrayList)

Aggregations

Schema (com.wplatform.ddal.dbobject.schema.Schema)16 TableMate (com.wplatform.ddal.dbobject.table.TableMate)2 Parser (com.wplatform.ddal.command.Parser)1 SchemaConfig (com.wplatform.ddal.config.SchemaConfig)1 TableConfig (com.wplatform.ddal.config.TableConfig)1 Role (com.wplatform.ddal.dbobject.Role)1 User (com.wplatform.ddal.dbobject.User)1 Constant (com.wplatform.ddal.dbobject.schema.Constant)1 Sequence (com.wplatform.ddal.dbobject.schema.Sequence)1 Table (com.wplatform.ddal.dbobject.table.Table)1 Database (com.wplatform.ddal.engine.Database)1 Mode (com.wplatform.ddal.engine.Mode)1 ArrayList (java.util.ArrayList)1