Package com.foundationdb.ais.model

Examples of com.foundationdb.ais.model.AISBuilder


           skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) {
            return;
        }

        TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator();
        AISBuilder builder = new AISBuilder();
        builder.table(schemaName, tableName);
        Table table = builder.akibanInformationSchema().getTable(schemaName, tableName);
        ResultColumnList resultColumns = null;
        if(createTable != null)
            resultColumns = createTable.getResultColumns();
        String newColumnName;
        ResultColumn resultColumn;
        if(resultColumns != null && resultColumns.size() > descriptors.size())
            throw new InvalidCreateAsException("More columns names in create than in select query");
        int colpos = 0;
        for (DataTypeDescriptor descriptor : descriptors) {
            if ((resultColumns != null) && (resultColumns.size() > colpos)){
                    resultColumn = resultColumns.getResultColumn(colpos+ 1);
                    if(resultColumn != null) {
                        newColumnName = resultColumn.getName();
                    }else {
                        newColumnName = columnNames.get(colpos);
                     }

            } else {
                newColumnName = columnNames.get(colpos);
            }
            addColumn(builder, schemaName, tableName, colpos++, newColumnName,typesTranslator,  descriptor);
        }
        builder.basicSchemaIsComplete();
        builder.groupingIsComplete();
        setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName);
        if(createTable.isWithData()) {
            ddlFunctions.createTable(session, table, createTable.getCreateAsQuery().toLowerCase(), context, server);
            return;
        }
View Full Code Here


        }
       
        TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator();
        AISViewDefinition viewdef = binderContext.getViewDefinition(createView);
        Map<TableName,Collection<String>> tableColumnReferences = viewdef.getTableColumnReferences();
        AISBuilder builder = new AISBuilder();
        builder.view(schemaName, viewName, viewdef.getQueryExpression(),
                     binderContext.getParserProperties(schemaName), tableColumnReferences);
        int colpos = 0;
        for (ResultColumn rc : viewdef.getResultColumns()) {
            DataTypeDescriptor type = rc.getType();
            if (type == null) {
                if (rc.getExpression().getNodeType() != NodeTypes.UNTYPED_NULL_CONSTANT_NODE)
                    throw new AkibanInternalException(rc.getName() + " has unknown type");
                type = new DataTypeDescriptor(TypeId.CHAR_ID, true, 0);
            }
            TableDDL.addColumn(builder, typesTranslator,
                               schemaName, viewName, rc.getName(), colpos++,
                               type, null, null);
        }
        View view = builder.akibanInformationSchema().getView(schemaName, viewName);
        ddlFunctions.createView(session, view);
    }
View Full Code Here

           skipOrThrow(context, createTable.getExistenceCheck(), curTable, new DuplicateTableNameException(fullName))) {
            return;
        }

        TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator();
        AISBuilder builder = new AISBuilder();
        builder.getNameGenerator().mergeAIS(ais);
        builder.table(schemaName, tableName);
        Table table = builder.akibanInformationSchema().getTable(schemaName, tableName);
        IndexNameGenerator namer = DefaultIndexNameGenerator.forTable(table);

        cloneReferencedTables(defaultSchemaName,
                              ddlFunctions.getAISCloner(),
                              ais,
                              builder.akibanInformationSchema(),
                              createTable.getTableElementList());

        int colpos = 0;
        // first loop through table elements, add the columns
        for (TableElementNode tableElement : createTable.getTableElementList()) {
            if (tableElement instanceof ColumnDefinitionNode) {
                addColumn (builder, typesTranslator,
                           (ColumnDefinitionNode)tableElement, schemaName, tableName, colpos++);
            }
        }
        // second pass get the constraints (primary, FKs, and other keys)
        // This needs to be done in two passes as the parser may put the
        // constraint before the column definition. For example:
        // CREATE TABLE t1 (c1 INT PRIMARY KEY) produces such a result.
        // The Builder complains if you try to do such a thing.
        for (TableElementNode tableElement : createTable.getTableElementList()) {
            if (tableElement instanceof FKConstraintDefinitionNode) {
                FKConstraintDefinitionNode fkdn = (FKConstraintDefinitionNode)tableElement;
                if (fkdn.isGrouping()) {
                    addJoin (builder, fkdn, defaultSchemaName, schemaName, tableName);
                } else {
                    addForeignKey(builder, ddlFunctions.getAIS(session), fkdn, defaultSchemaName, schemaName, tableName);
                }
            }
            else if (tableElement instanceof ConstraintDefinitionNode) {
                addIndex (namer, builder, (ConstraintDefinitionNode)tableElement, schemaName, tableName, context);
            } else if (tableElement instanceof IndexDefinitionNode) {
                addIndex (namer, builder, (IndexDefinitionNode)tableElement, schemaName, tableName, context, ddlFunctions);
            } else if (!(tableElement instanceof ColumnDefinitionNode)) {
                throw new UnsupportedSQLException("Unexpected TableElement", tableElement);
            }
        }
        setTableStorage(ddlFunctions, createTable, builder, tableName, table, schemaName);
        builder.basicSchemaIsComplete();
        builder.groupingIsComplete();
        ddlFunctions.createTable(session, table);
    }
View Full Code Here

        final AkibanInformationSchema origAIS = origTable.getAIS();
        final Table tableCopy = copyTable(ddl.getAISCloner(), origTable, columnChanges);
        final AkibanInformationSchema aisCopy = tableCopy.getAIS();
        TableDDL.cloneReferencedTables(defaultSchema, ddl.getAISCloner(), origAIS, aisCopy, elements);
        final TypesTranslator typesTranslator = ddl.getTypesTranslator();
        final AISBuilder builder = new AISBuilder(aisCopy);
        builder.getNameGenerator().mergeAIS(origAIS);

        int pos = tableCopy.getColumnsIncludingInternal().size();
        for(ColumnDefinitionNode cdn : columnDefNodes) {
            if(cdn instanceof ModifyColumnNode) {
                ModifyColumnNode modNode = (ModifyColumnNode) cdn;
                handleModifyColumnNode(modNode, builder, tableCopy, typesTranslator);
            } else {
                TableDDL.addColumn(builder, typesTranslator, cdn, origTable.getName().getSchemaName(), origTable.getName().getTableName(), pos++);
            }
        }
        // Ensure that internal columns stay at the end
        // because there's a bunch of places that assume that they are
        // (e.g. they assume getColumns() have indexes (1...getColumns().size()))
        // If the original table had a primary key, the hidden pk is added a bit farther down
       
        for (Column origColumn : origTable.getColumnsIncludingInternal()) {
            if (origColumn.isInternalColumn()) {
                String newName = findNewName(columnChanges, origColumn.getName());
                if (newName != null) {
                    Column.create(tableCopy, origColumn, newName, pos++);
                }
            }
        }
        copyTableIndexes(origTable, tableCopy, columnChanges, indexChanges);

        IndexNameGenerator indexNamer = DefaultIndexNameGenerator.forTable(tableCopy);
        TableName newName = tableCopy.getName();
        for(ConstraintDefinitionNode cdn : conDefNodes) {
            assert cdn.getConstraintType() != ConstraintType.DROP : cdn;
            String name = TableDDL.addIndex(indexNamer, builder, cdn, newName.getSchemaName(), newName.getTableName(), context);
            indexChanges.add(TableChange.createAdd(name));
            // This is required as the addIndex() for a primary key constraint
            // *may* alter the NULL->NOT NULL status
            // of the columns in the primary key
            if (name.equals(Index.PRIMARY)) {
                for (IndexColumn col : tableCopy.getIndex(name).getKeyColumns())
                {
                    String columnName = col.getColumn().getName();
                   
                    // Check if the column was added in the same alter as creating the index:
                    // ALTER TABLE c ADD COLUMN n SERIAL PRIMARY KEY
                    // You can't add and modify the column, so assume the add does the correct thing.
                    boolean columnAdded = false;
                    for (TableChange change : columnChanges) {
                        if (change.getChangeType() ==  ChangeType.ADD && columnName.equals(change.getNewName()))
                            columnAdded = true;
                    }
                    if (!columnAdded)
                        columnChanges.add(TableChange.createModify(columnName, columnName));
                }
            }
        }

        for(IndexDefinitionNode idn : indexDefNodes) {
            String name = TableDDL.addIndex(indexNamer, builder, idn, newName.getSchemaName(), newName.getTableName(), context, ddl);
            indexChanges.add(TableChange.createAdd(name));
        }

        // Correctly adds the Hidden PK (including sequence).
        if (tableCopy.getPrimaryKeyIncludingInternal() == null) {
            if (origTable.getPrimaryKeyIncludingInternal().isAkibanPK()) {
                Column origColumn = origTable.getPrimaryKeyIncludingInternal().getColumns().get(0);
                Column.create(tableCopy, origColumn, Column.ROW_ID_NAME, tableCopy.getColumns().size());
            } else {
                tableCopy.addHiddenPrimaryKey(builder.getNameGenerator());
                columnChanges.add(TableChange.createAdd(Column.ROW_ID_NAME));
            }
        }
       
        for(FKConstraintDefinitionNode fk : fkDefNodes) {
View Full Code Here

                throw new InvalidRoutineException(schemaName, routineName, "must have EXTERNAL NAME function_name");
            }
        }

        TypesTranslator typesTranslator = ddlFunctions.getTypesTranslator();
        AISBuilder builder = new AISBuilder();
        builder.routine(schemaName, routineName,
                        language, callingConvention);
       
        for (int i = 0; i < aliasInfo.getParameterCount(); i++) {
            String parameterName = aliasInfo.getParameterNames()[i];
            Parameter.Direction direction;
            switch (aliasInfo.getParameterModes()[i]) {
            case ParameterMetaData.parameterModeIn:
            default:
                direction = Parameter.Direction.IN;
                break;
            case ParameterMetaData.parameterModeOut:
                direction = Parameter.Direction.OUT;
                break;
            case ParameterMetaData.parameterModeInOut:
                direction = Parameter.Direction.INOUT;
                break;
            }
            TInstance type = typesTranslator.typeForSQLType(aliasInfo.getParameterTypes()[i],
                    schemaName, routineName, parameterName);
            builder.parameter(schemaName, routineName, parameterName,
                              direction, type);
        }
       
        if (aliasInfo.getReturnType() != null) {
            TInstance type = typesTranslator.typeForSQLType(aliasInfo.getReturnType(),
                    schemaName, routineName, "return value");
            builder.parameter(schemaName, routineName, null,
                              Parameter.Direction.RETURN, type);
        }

        if (createAlias.getExternalName() != null) {
            String className, methodName;
            boolean checkJarName;
            if (callingConvention == Routine.CallingConvention.JAVA) {
                className = createAlias.getJavaClassName();
                methodName = createAlias.getMethodName();
                checkJarName = true;
            }
            else if (callingConvention == Routine.CallingConvention.LOADABLE_PLAN) {
                // The whole class implements a standard interface.
                className = createAlias.getExternalName();
                methodName = null;
                checkJarName = true;
            }
            else {
                className = null;
                methodName = createAlias.getExternalName();
                checkJarName = false;
            }
            String jarSchema = null;
            String jarName = null;
            if (checkJarName) {
                int idx = className.indexOf(':');
                if (idx >= 0) {
                    jarName = className.substring(0, idx);
                    className = className.substring(idx + 1);
                    if (jarName.equals("thisjar")) {
                        TableName thisJar = (TableName)createAlias.getUserData();
                        jarSchema = thisJar.getSchemaName();
                        jarName = thisJar.getTableName();
                    }
                    else {
                        idx = jarName.indexOf('.');
                        if (idx < 0) {
                            jarSchema = defaultSchemaName;
                        }
                        else {
                            jarSchema = jarName.substring(0, idx);
                            jarName = jarName.substring(idx + 1);
                        }
                    }
                }
            }
            if (jarName != null) {
                AkibanInformationSchema ais = ddlFunctions.getAIS(session);
                SQLJJar sqljJar = ais.getSQLJJar(jarSchema, jarName);
                if (sqljJar == null)
                    throw new NoSuchSQLJJarException(jarSchema, jarName);
                builder.sqljJar(jarSchema, jarName, sqljJar.getURL());
            }
            builder.routineExternalName(schemaName, routineName,
                                        jarSchema, jarName,
                                        className, methodName);
        }
        if (createAlias.getDefinition() != null) {
            builder.routineDefinition(schemaName, routineName,
                                      createAlias.getDefinition());
        }

        if (aliasInfo.getSQLAllowed() != null) {
            Routine.SQLAllowed sqlAllowed;
            switch (aliasInfo.getSQLAllowed()) {
            case MODIFIES_SQL_DATA:
                sqlAllowed = Routine.SQLAllowed.MODIFIES_SQL_DATA;
                break;
            case READS_SQL_DATA:
                sqlAllowed = Routine.SQLAllowed.READS_SQL_DATA;
                break;
            case CONTAINS_SQL:
                sqlAllowed = Routine.SQLAllowed.CONTAINS_SQL;
                break;
            case NO_SQL:
                sqlAllowed = Routine.SQLAllowed.NO_SQL;
                break;
            default:
                throw new InvalidRoutineException(schemaName, routineName, "unsupported " + aliasInfo.getSQLAllowed().getSQL());
            }
            builder.routineSQLAllowed(schemaName, routineName, sqlAllowed);
        }
        builder.routineDynamicResultSets(schemaName, routineName,
                                         aliasInfo.getMaxDynamicResultSets());
        builder.routineDeterministic(schemaName, routineName,
                                     aliasInfo.isDeterministic());
        builder.routineCalledOnNullInput(schemaName, routineName,
                                         aliasInfo.calledOnNullInput());
       
        Routine routine = builder.akibanInformationSchema().getRoutine(tableName);
        boolean replaceExisting = createAlias.isCreateOrReplace();
        ddlFunctions.createRoutine(session, routine, replaceExisting);
        if (replaceExisting)
            routineLoader.checkUnloadRoutine(session, tableName);
    }
View Full Code Here

            } else if(typeId == TypeId.INTEGER_ID) {
                maxValue = Integer.MAX_VALUE;
            }
            // else keep long max
        }
        AISBuilder builder = new AISBuilder();
        builder.sequence(seqName.getSchemaName(), seqName.getTableName(), startWith, incBy, minValue, maxValue, isCycle);
        Sequence sequence = builder.akibanInformationSchema().getSequence(seqName);
        if (node.getStorageFormat() != null) {
            TableDDL.setStorage(ddlFunctions, sequence, node.getStorageFormat());
        }
        ddlFunctions.createSequence(session, sequence);
    }
View Full Code Here

        nameChanger.setSchemaName(newName.getSchemaName());
        nameChanger.setNewTableName(newName.getTableName());
        nameChanger.doChange();

        // AISTableNameChanger doesn't bother with group names or group tables, fix them with the builder
        AISBuilder builder = new AISBuilder(newAIS);
        builder.basicSchemaIsComplete();
        builder.groupingIsComplete();

        renamingTable(session, currentName, newName);
        final String curSchema = currentName.getSchemaName();
        final String newSchema = newName.getSchemaName();
        final List<String> schemaNames;
View Full Code Here

        }
    }

    public void createJarAndRoutine(String jarSchema, String jarName,
                                    String routineSchema, String routineName) throws MalformedURLException {
        AISBuilder builder = new AISBuilder();
        builder.sqljJar(jarSchema, jarName, new URL("file://ajar.jar"));
        ddl().createSQLJJar(session(), builder.akibanInformationSchema().getSQLJJar(jarSchema, jarName));
        builder.routine(routineSchema, routineName, "java", Routine.CallingConvention.JAVA);
        builder.routineExternalName(routineSchema, routineName, jarSchema, jarName, "className", "method");
        ddl().createRoutine(session(), builder.akibanInformationSchema().getRoutine(routineSchema, routineName), true);
    }
View Full Code Here

        expectNotViews("test", "v1", "v2", "v3");
    }

    @Test
    public void dropRoutine() throws Exception {
        AISBuilder builder = new AISBuilder();
        builder.routine("drop", "f", "javascript", Routine.CallingConvention.SCRIPT_FUNCTION_JSON);
        builder.routineDefinition("drop", "f", "function f() { return 3; }");
        builder.routine("keep", "p", "javascript", Routine.CallingConvention.SCRIPT_FUNCTION_JSON);
        builder.routineDefinition("keep", "p", "function f() { return 8; }");
        ddl().createRoutine(session(), builder.akibanInformationSchema().getRoutine("drop", "f"), true);
        ddl().createRoutine(session(), builder.akibanInformationSchema().getRoutine("keep", "p"), true);
        expectRoutines("drop", "f");
        expectRoutines("keep", "p");
        ddl().dropSchema(session(), "drop");
        expectNotRoutines("drop", "f");
        expectRoutines("keep", "p");
View Full Code Here

        expectNotRoutines("drop", "peanuts");
    }

    @Test
    public void dropSqljJar() throws Exception {
        AISBuilder builder = new AISBuilder();
        builder.sqljJar("drop", "grapeJelly", new URL("file://boo.jar"));
        ddl().createSQLJJar(session(), builder.akibanInformationSchema().getSQLJJar("drop", "grapeJelly"));
        builder.sqljJar("keep", "strawberryJam", new URL("file://far.jar"));
        ddl().createSQLJJar(session(), builder.akibanInformationSchema().getSQLJJar("keep", "strawberryJam"));
        expectSqljJars("drop", "grapeJelly");
        expectSqljJars("keep", "strawberryJam");
        ddl().dropSchema(session(), "drop");
        expectNotSqljJars("drop", "grapeJelly");
        expectSqljJars("keep", "strawberryJam");
View Full Code Here

TOP

Related Classes of com.foundationdb.ais.model.AISBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.