Search in sources :

Example 1 with Domain

use of org.h2.schema.Domain in project webpieces by deanhiller.

the class H2DbModule method configure.

@Override
protected void configure() {
    Multibinder<BackendGuiDescriptor> backendBinder = Multibinder.newSetBinder(binder(), BackendGuiDescriptor.class);
    backendBinder.addBinding().to(H2DbGuiDescriptor.class);
    bind(H2DbConfig.class).toInstance(config);
    bind(ServerConfig.class).toInstance(svrConfig);
    try {
        String[] args;
        // if this port1 is 0, server.getPort will get the real port later in this method
        int port1 = config.getPort().get();
        if (config.getConvertDomain() == null) {
            args = new String[] { "-webPort", port1 + "" };
        } else {
            // if we are converting a domain, definitely need to allow other ip addresses in..
            // this is because we are exposing a domain url on the web to hit
            args = new String[] { "-webPort", port1 + "", "-webAllowOthers" };
        }
        log.info("Creating H2 webserver for html GUI interface to serve up as a webpage(for development servers)");
        // start the TCP Server
        Server server = Server.createWebServer(args);
        log.info("Starting H2 webserver");
        server.start();
        int port = server.getPort();
        log.info("H2 webserver started on port=" + port);
        if (config.getConvertDomain() == null) {
            log.info("H2 webserver setting webpage to use=" + port);
            this.svrConfig.setPort(port);
        } else {
            log.info("H2 webserver using the domain converter=" + config.getConvertDomain());
        }
        return;
    } catch (Exception e) {
        throw SneakyThrow.sneak(e);
    }
}
Also used : BackendGuiDescriptor(org.webpieces.plugin.backend.spi.BackendGuiDescriptor) Server(org.h2.tools.Server) SQLException(java.sql.SQLException)

Example 2 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class DataTypeSQLFunction method getValue.

@Override
public Value getValue(SessionLocal session, Value v1, Value v2, Value v3) {
    Schema schema = session.getDatabase().findSchema(v1.getString());
    if (schema == null) {
        return ValueNull.INSTANCE;
    }
    String objectName = v2.getString();
    String objectType = v3.getString();
    String typeIdentifier = args[3].getValue(session).getString();
    if (typeIdentifier == null) {
        return ValueNull.INSTANCE;
    }
    TypeInfo t;
    switch(objectType) {
        case "CONSTANT":
            {
                Constant constant = schema.findConstant(objectName);
                if (constant == null || !typeIdentifier.equals("TYPE")) {
                    return ValueNull.INSTANCE;
                }
                t = constant.getValue().getType();
                break;
            }
        case "DOMAIN":
            {
                Domain domain = schema.findDomain(objectName);
                if (domain == null || !typeIdentifier.equals("TYPE")) {
                    return ValueNull.INSTANCE;
                }
                t = domain.getDataType();
                break;
            }
        case "ROUTINE":
            {
                int idx = objectName.lastIndexOf('_');
                if (idx < 0) {
                    return ValueNull.INSTANCE;
                }
                FunctionAlias function = schema.findFunction(objectName.substring(0, idx));
                if (function == null) {
                    return ValueNull.INSTANCE;
                }
                int ordinal;
                try {
                    ordinal = Integer.parseInt(objectName.substring(idx + 1));
                } catch (NumberFormatException e) {
                    return ValueNull.INSTANCE;
                }
                JavaMethod[] methods;
                try {
                    methods = function.getJavaMethods();
                } catch (DbException e) {
                    return ValueNull.INSTANCE;
                }
                if (ordinal < 1 || ordinal > methods.length) {
                    return ValueNull.INSTANCE;
                }
                FunctionAlias.JavaMethod method = methods[ordinal - 1];
                if (typeIdentifier.equals("RESULT")) {
                    t = method.getDataType();
                } else {
                    try {
                        ordinal = Integer.parseInt(typeIdentifier);
                    } catch (NumberFormatException e) {
                        return ValueNull.INSTANCE;
                    }
                    if (ordinal < 1) {
                        return ValueNull.INSTANCE;
                    }
                    if (!method.hasConnectionParam()) {
                        ordinal--;
                    }
                    Class<?>[] columnList = method.getColumnClasses();
                    if (ordinal >= columnList.length) {
                        return ValueNull.INSTANCE;
                    }
                    t = ValueToObjectConverter2.classToType(columnList[ordinal]);
                }
                break;
            }
        case "TABLE":
            {
                Table table = schema.findTableOrView(session, objectName);
                if (table == null) {
                    return ValueNull.INSTANCE;
                }
                int ordinal;
                try {
                    ordinal = Integer.parseInt(typeIdentifier);
                } catch (NumberFormatException e) {
                    return ValueNull.INSTANCE;
                }
                Column[] columns = table.getColumns();
                if (ordinal < 1 || ordinal > columns.length) {
                    return ValueNull.INSTANCE;
                }
                t = columns[ordinal - 1].getType();
                break;
            }
        default:
            return ValueNull.INSTANCE;
    }
    return ValueVarchar.get(t.getSQL(DEFAULT_SQL_FLAGS));
}
Also used : Table(org.h2.table.Table) FunctionAlias(org.h2.schema.FunctionAlias) Constant(org.h2.schema.Constant) Schema(org.h2.schema.Schema) JavaMethod(org.h2.schema.FunctionAlias.JavaMethod) Domain(org.h2.schema.Domain) TypeInfo(org.h2.value.TypeInfo) DbException(org.h2.message.DbException)

Example 3 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class DropDomain method copyColumn.

private boolean copyColumn(Domain domain, Column targetColumn) {
    Table targetTable = targetColumn.getTable();
    if (dropAction == ConstraintActionType.RESTRICT) {
        throw DbException.get(ErrorCode.CANNOT_DROP_2, domainName, targetTable.getCreateSQL());
    }
    String columnName = targetColumn.getName();
    ArrayList<ConstraintDomain> constraints = domain.getConstraints();
    if (constraints != null && !constraints.isEmpty()) {
        for (ConstraintDomain constraint : constraints) {
            Expression checkCondition = constraint.getCheckConstraint(session, columnName);
            AlterTableAddConstraint check = new AlterTableAddConstraint(session, targetTable.getSchema(), CommandInterface.ALTER_TABLE_ADD_CONSTRAINT_CHECK, false);
            check.setTableName(targetTable.getName());
            check.setCheckExpression(checkCondition);
            check.update();
        }
    }
    copyExpressions(session, domain, targetColumn);
    return true;
}
Also used : ConstraintDomain(org.h2.constraint.ConstraintDomain) Table(org.h2.table.Table) Expression(org.h2.expression.Expression)

Example 4 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class Parser method getColumnWithDomain.

private static Column getColumnWithDomain(String columnName, Domain domain) {
    Column column = new Column(columnName, domain.getDataType());
    column.setComment(domain.getComment());
    column.setDomain(domain);
    return column;
}
Also used : AlterTableRenameColumn(org.h2.command.ddl.AlterTableRenameColumn) IndexColumn(org.h2.table.IndexColumn) ExpressionColumn(org.h2.expression.ExpressionColumn) AlterTableAlterColumn(org.h2.command.ddl.AlterTableAlterColumn) Column(org.h2.table.Column)

Example 5 with Domain

use of org.h2.schema.Domain in project h2database by h2database.

the class Parser method parseCreate.

private Prepared parseCreate() {
    boolean orReplace = false;
    if (readIf(OR)) {
        read("REPLACE");
        orReplace = true;
    }
    boolean force = readIf("FORCE");
    if (readIf("VIEW")) {
        return parseCreateView(force, orReplace);
    } else if (readIf("ALIAS")) {
        return parseCreateFunctionAlias(force);
    } else if (readIf("SEQUENCE")) {
        return parseCreateSequence();
    } else if (readIf(USER)) {
        return parseCreateUser();
    } else if (readIf("TRIGGER")) {
        return parseCreateTrigger(force);
    } else if (readIf("ROLE")) {
        return parseCreateRole();
    } else if (readIf("SCHEMA")) {
        return parseCreateSchema();
    } else if (readIf("CONSTANT")) {
        return parseCreateConstant();
    } else if (readIf("DOMAIN") || readIf("TYPE") || readIf("DATATYPE")) {
        return parseCreateDomain();
    } else if (readIf("AGGREGATE")) {
        return parseCreateAggregate(force);
    } else if (readIf("LINKED")) {
        return parseCreateLinkedTable(false, false, force);
    }
    // tables or linked tables
    boolean memory = false, cached = false;
    if (readIf("MEMORY")) {
        memory = true;
    } else if (readIf("CACHED")) {
        cached = true;
    }
    if (readIf("LOCAL")) {
        read("TEMPORARY");
        if (readIf("LINKED")) {
            return parseCreateLinkedTable(true, false, force);
        }
        read(TABLE);
        return parseCreateTable(true, false, cached);
    } else if (readIf("GLOBAL")) {
        read("TEMPORARY");
        if (readIf("LINKED")) {
            return parseCreateLinkedTable(true, true, force);
        }
        read(TABLE);
        return parseCreateTable(true, true, cached);
    } else if (readIf("TEMP") || readIf("TEMPORARY")) {
        if (readIf("LINKED")) {
            return parseCreateLinkedTable(true, true, force);
        }
        read(TABLE);
        return parseCreateTable(true, true, cached);
    } else if (readIf(TABLE)) {
        if (!cached && !memory) {
            cached = database.getDefaultTableType() == Table.TYPE_CACHED;
        }
        return parseCreateTable(false, false, cached);
    } else if (readIf("SYNONYM")) {
        return parseCreateSynonym(orReplace);
    } else {
        boolean hash = false, primaryKey = false;
        boolean unique = false, spatial = false;
        String indexName = null;
        Schema oldSchema = null;
        boolean ifNotExists = false;
        if (session.isQuirksMode() && readIf(PRIMARY)) {
            read(KEY);
            if (readIf("HASH")) {
                hash = true;
            }
            primaryKey = true;
            if (!isToken(ON)) {
                ifNotExists = readIfNotExists();
                indexName = readIdentifierWithSchema(null);
                oldSchema = getSchema();
            }
        } else {
            if (readIf(UNIQUE)) {
                unique = true;
            }
            if (readIf("HASH")) {
                hash = true;
            } else if (!unique && readIf("SPATIAL")) {
                spatial = true;
            }
            read("INDEX");
            if (!isToken(ON)) {
                ifNotExists = readIfNotExists();
                indexName = readIdentifierWithSchema(null);
                oldSchema = getSchema();
            }
        }
        read(ON);
        String tableName = readIdentifierWithSchema();
        checkSchema(oldSchema);
        String comment = readCommentIf();
        if (!readIf(OPEN_PAREN)) {
            // PostgreSQL compatibility
            if (hash || spatial) {
                throw getSyntaxError();
            }
            read(USING);
            if (readIf("BTREE")) {
            // default
            } else if (readIf("HASH")) {
                hash = true;
            } else {
                read("RTREE");
                spatial = true;
            }
            read(OPEN_PAREN);
        }
        CreateIndex command = new CreateIndex(session, getSchema());
        command.setIfNotExists(ifNotExists);
        command.setPrimaryKey(primaryKey);
        command.setTableName(tableName);
        command.setHash(hash);
        command.setSpatial(spatial);
        command.setIndexName(indexName);
        command.setComment(comment);
        IndexColumn[] columns;
        int uniqueColumnCount = 0;
        if (spatial) {
            columns = new IndexColumn[] { new IndexColumn(readIdentifier()) };
            if (unique) {
                uniqueColumnCount = 1;
            }
            read(CLOSE_PAREN);
        } else {
            columns = parseIndexColumnList();
            if (unique) {
                uniqueColumnCount = columns.length;
                if (readIf("INCLUDE")) {
                    read(OPEN_PAREN);
                    IndexColumn[] columnsToInclude = parseIndexColumnList();
                    int nonUniqueCount = columnsToInclude.length;
                    columns = Arrays.copyOf(columns, uniqueColumnCount + nonUniqueCount);
                    System.arraycopy(columnsToInclude, 0, columns, uniqueColumnCount, nonUniqueCount);
                }
            } else if (primaryKey) {
                uniqueColumnCount = columns.length;
            }
        }
        command.setIndexColumns(columns);
        command.setUniqueColumnCount(uniqueColumnCount);
        return command;
    }
}
Also used : CreateIndex(org.h2.command.ddl.CreateIndex) Schema(org.h2.schema.Schema) DropSchema(org.h2.command.ddl.DropSchema) CreateSchema(org.h2.command.ddl.CreateSchema) ValueBigint(org.h2.value.ValueBigint) AlterDomainRenameConstraint(org.h2.command.ddl.AlterDomainRenameConstraint) AlterTableAddConstraint(org.h2.command.ddl.AlterTableAddConstraint) AlterDomainDropConstraint(org.h2.command.ddl.AlterDomainDropConstraint) AlterTableRenameConstraint(org.h2.command.ddl.AlterTableRenameConstraint) AlterDomainAddConstraint(org.h2.command.ddl.AlterDomainAddConstraint) AlterTableDropConstraint(org.h2.command.ddl.AlterTableDropConstraint) IndexColumn(org.h2.table.IndexColumn)

Aggregations

Domain (org.h2.schema.Domain)22 ConstraintDomain (org.h2.constraint.ConstraintDomain)20 Schema (org.h2.schema.Schema)16 TypeInfo (org.h2.value.TypeInfo)14 ValueBigint (org.h2.value.ValueBigint)10 Constraint (org.h2.constraint.Constraint)8 AlterDomainAddConstraint (org.h2.command.ddl.AlterDomainAddConstraint)6 AlterDomainDropConstraint (org.h2.command.ddl.AlterDomainDropConstraint)6 AlterDomainRenameConstraint (org.h2.command.ddl.AlterDomainRenameConstraint)6 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)6 AlterTableDropConstraint (org.h2.command.ddl.AlterTableDropConstraint)6 AlterTableRenameConstraint (org.h2.command.ddl.AlterTableRenameConstraint)6 Expression (org.h2.expression.Expression)6 DbException (org.h2.message.DbException)6 Constant (org.h2.schema.Constant)6 FunctionAlias (org.h2.schema.FunctionAlias)6 JavaMethod (org.h2.schema.FunctionAlias.JavaMethod)6 Table (org.h2.table.Table)6 DataType (org.h2.value.DataType)6 DropSchema (org.h2.command.ddl.DropSchema)4