Search in sources :

Example 26 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class MetaTable method createColumns.

private Column[] createColumns(String... names) {
    Column[] cols = new Column[names.length];
    for (int i = 0; i < names.length; i++) {
        String nameType = names[i];
        int idx = nameType.indexOf(' ');
        int dataType;
        String name;
        if (idx < 0) {
            dataType = database.getMode().lowerCaseIdentifiers ? Value.STRING_IGNORECASE : Value.STRING;
            name = nameType;
        } else {
            dataType = DataType.getTypeByName(nameType.substring(idx + 1), database.getMode()).type;
            name = nameType.substring(0, idx);
        }
        cols[i] = new Column(name, dataType);
    }
    return cols;
}
Also used : ValueString(org.h2.value.ValueString) Constraint(org.h2.constraint.Constraint)

Example 27 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class Table method compareTypeSafe.

/**
 * Compare two values with the current comparison mode. The values may be of
 * different type.
 *
 * @param a the first value
 * @param b the second value
 * @return 0 if both values are equal, -1 if the first value is smaller, and
 *         1 otherwise
 */
public int compareTypeSafe(Value a, Value b) {
    if (a == b) {
        return 0;
    }
    int dataType = Value.getHigherOrder(a.getType(), b.getType());
    a = a.convertTo(dataType);
    b = b.convertTo(dataType);
    return a.compareTypeSafe(b, compareMode);
}
Also used : Constraint(org.h2.constraint.Constraint)

Example 28 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class Parser method parseValuesTable.

private TableFilter parseValuesTable(int orderInFrom) {
    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.isEmpty()) {
                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 && readIfMore(true));
        rows.add(row);
    } while (readIf(","));
    int columnCount = columns.size();
    int rowCount = rows.size();
    for (ArrayList<Expression> row : rows) {
        if (row.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);
    return new TableFilter(session, table, null, rightsChecked, currentSelect, orderInFrom, null);
}
Also used : RangeTable(org.h2.table.RangeTable) TruncateTable(org.h2.command.ddl.TruncateTable) CreateTable(org.h2.command.ddl.CreateTable) FunctionTable(org.h2.table.FunctionTable) CreateLinkedTable(org.h2.command.ddl.CreateLinkedTable) Table(org.h2.table.Table) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) Schema(org.h2.schema.Schema) ArrayList(java.util.ArrayList) ValueString(org.h2.value.ValueString) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column) ExpressionColumn(org.h2.expression.ExpressionColumn) IndexColumn(org.h2.table.IndexColumn) Expression(org.h2.expression.Expression) ValueExpression(org.h2.expression.ValueExpression) FunctionTable(org.h2.table.FunctionTable) TableFilter(org.h2.table.TableFilter) TableFunction(org.h2.expression.TableFunction) CreateUserDataType(org.h2.command.ddl.CreateUserDataType) DataType(org.h2.value.DataType) DropUserDataType(org.h2.command.ddl.DropUserDataType) UserDataType(org.h2.engine.UserDataType) ExpressionList(org.h2.expression.ExpressionList)

Example 29 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class Parser method parseDrop.

private Prepared parseDrop() {
    if (readIf("TABLE")) {
        boolean ifExists = readIfExists(false);
        String tableName = readIdentifierWithSchema();
        DropTable command = new DropTable(session, getSchema());
        command.setTableName(tableName);
        while (readIf(",")) {
            tableName = readIdentifierWithSchema();
            DropTable next = new DropTable(session, getSchema());
            next.setTableName(tableName);
            command.addNextDropTable(next);
        }
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
            readIf("CONSTRAINTS");
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        } else if (readIf("IGNORE")) {
            command.setDropAction(ConstraintActionType.SET_DEFAULT);
        }
        return command;
    } else if (readIf("INDEX")) {
        boolean ifExists = readIfExists(false);
        String indexName = readIdentifierWithSchema();
        DropIndex command = new DropIndex(session, getSchema());
        command.setIndexName(indexName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        // Support for MySQL: DROP INDEX index_name ON tbl_name
        if (readIf("ON")) {
            readIdentifierWithSchema();
        }
        return command;
    } else if (readIf("USER")) {
        boolean ifExists = readIfExists(false);
        DropUser command = new DropUser(session);
        command.setUserName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        readIf("CASCADE");
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SEQUENCE")) {
        boolean ifExists = readIfExists(false);
        String sequenceName = readIdentifierWithSchema();
        DropSequence command = new DropSequence(session, getSchema());
        command.setSequenceName(sequenceName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("CONSTANT")) {
        boolean ifExists = readIfExists(false);
        String constantName = readIdentifierWithSchema();
        DropConstant command = new DropConstant(session, getSchema());
        command.setConstantName(constantName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("TRIGGER")) {
        boolean ifExists = readIfExists(false);
        String triggerName = readIdentifierWithSchema();
        DropTrigger command = new DropTrigger(session, getSchema());
        command.setTriggerName(triggerName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("VIEW")) {
        boolean ifExists = readIfExists(false);
        String viewName = readIdentifierWithSchema();
        DropView command = new DropView(session, getSchema());
        command.setViewName(viewName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        ConstraintActionType dropAction = parseCascadeOrRestrict();
        if (dropAction != null) {
            command.setDropAction(dropAction);
        }
        return command;
    } else if (readIf("ROLE")) {
        boolean ifExists = readIfExists(false);
        DropRole command = new DropRole(session);
        command.setRoleName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("ALIAS")) {
        boolean ifExists = readIfExists(false);
        String aliasName = readIdentifierWithSchema();
        DropFunctionAlias command = new DropFunctionAlias(session, getSchema());
        command.setAliasName(aliasName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    } else if (readIf("SCHEMA")) {
        boolean ifExists = readIfExists(false);
        DropSchema command = new DropSchema(session);
        command.setSchemaName(readUniqueIdentifier());
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        if (readIf("CASCADE")) {
            command.setDropAction(ConstraintActionType.CASCADE);
        } else if (readIf("RESTRICT")) {
            command.setDropAction(ConstraintActionType.RESTRICT);
        }
        return command;
    } else if (readIf("ALL")) {
        read("OBJECTS");
        DropDatabase command = new DropDatabase(session);
        command.setDropAllObjects(true);
        if (readIf("DELETE")) {
            read("FILES");
            command.setDeleteFiles(true);
        }
        return command;
    } else if (readIf("DOMAIN")) {
        return parseDropUserDataType();
    } else if (readIf("TYPE")) {
        return parseDropUserDataType();
    } else if (readIf("DATATYPE")) {
        return parseDropUserDataType();
    } else if (readIf("AGGREGATE")) {
        return parseDropAggregate();
    } else if (readIf("SYNONYM")) {
        boolean ifExists = readIfExists(false);
        String synonymName = readIdentifierWithSchema();
        DropSynonym command = new DropSynonym(session, getSchema());
        command.setSynonymName(synonymName);
        ifExists = readIfExists(ifExists);
        command.setIfExists(ifExists);
        return command;
    }
    throw getSyntaxError();
}
Also used : DropConstant(org.h2.command.ddl.DropConstant) DropTrigger(org.h2.command.ddl.DropTrigger) DropView(org.h2.command.ddl.DropView) DropUser(org.h2.command.ddl.DropUser) DropSynonym(org.h2.command.ddl.DropSynonym) DropSequence(org.h2.command.ddl.DropSequence) ValueString(org.h2.value.ValueString) DropTable(org.h2.command.ddl.DropTable) DropSchema(org.h2.command.ddl.DropSchema) DropRole(org.h2.command.ddl.DropRole) DropFunctionAlias(org.h2.command.ddl.DropFunctionAlias) DropDatabase(org.h2.command.ddl.DropDatabase) ConstraintActionType(org.h2.constraint.ConstraintActionType) DropIndex(org.h2.command.ddl.DropIndex)

Example 30 with DataType

use of org.h2.mvstore.type.DataType in project h2database by h2database.

the class CreateTable method generateColumnsFromQuery.

private void generateColumnsFromQuery() {
    int columnCount = asQuery.getColumnCount();
    ArrayList<Expression> expressions = asQuery.getExpressions();
    ColumnNamer columnNamer = new ColumnNamer(session);
    for (int i = 0; i < columnCount; i++) {
        Expression expr = expressions.get(i);
        int type = expr.getType();
        String name = columnNamer.getColumnName(expr, i, expr.getAlias());
        long precision = expr.getPrecision();
        int displaySize = expr.getDisplaySize();
        DataType dt = DataType.getDataType(type);
        if (precision > 0 && (dt.defaultPrecision == 0 || (dt.defaultPrecision > precision && dt.defaultPrecision < Byte.MAX_VALUE))) {
            // dont' set precision to MAX_VALUE if this is the default
            precision = dt.defaultPrecision;
        }
        int scale = expr.getScale();
        if (scale > 0 && (dt.defaultScale == 0 || (dt.defaultScale > scale && dt.defaultScale < precision))) {
            scale = dt.defaultScale;
        }
        if (scale > precision) {
            precision = scale;
        }
        String[] enumerators = null;
        if (dt.type == Value.ENUM) {
            /**
             * Only columns of tables may be enumerated.
             */
            if (!(expr instanceof ExpressionColumn)) {
                throw DbException.get(ErrorCode.GENERAL_ERROR_1, "Unable to resolve enumerators of expression");
            }
            enumerators = ((ExpressionColumn) expr).getColumn().getEnumerators();
        }
        Column col = new Column(name, type, precision, scale, displaySize, enumerators);
        addColumn(col);
    }
}
Also used : ColumnNamer(org.h2.util.ColumnNamer) Expression(org.h2.expression.Expression) ExpressionColumn(org.h2.expression.ExpressionColumn) Column(org.h2.table.Column) DataType(org.h2.value.DataType) ExpressionColumn(org.h2.expression.ExpressionColumn)

Aggregations

Value (org.h2.value.Value)16 ValueString (org.h2.value.ValueString)13 DataType (org.h2.value.DataType)12 Expression (org.h2.expression.Expression)6 Column (org.h2.table.Column)6 Constraint (org.h2.constraint.Constraint)5 UserDataType (org.h2.engine.UserDataType)5 UserAggregate (org.h2.engine.UserAggregate)4 Index (org.h2.index.Index)4 Table (org.h2.table.Table)4 ArrayList (java.util.ArrayList)3 IgniteSQLException (org.apache.ignite.internal.processors.query.IgniteSQLException)3 Mode (org.h2.engine.Mode)3 ValueExpression (org.h2.expression.ValueExpression)3 SearchRow (org.h2.result.SearchRow)3 Schema (org.h2.schema.Schema)3 SimpleResultSet (org.h2.tools.SimpleResultSet)3 StatementBuilder (org.h2.util.StatementBuilder)3 CompareMode (org.h2.value.CompareMode)3 IOException (java.io.IOException)2