Package org.apache.openjpa.jdbc.schema

Examples of org.apache.openjpa.jdbc.schema.Table


        DBDictionary dict = new DBDictionary();
        dict.setConfiguration(mockConfiguration);
        dict.tableLengthIncludesSchema=false;
        dict.maxTableNameLength = 12;

        Table table = new Table();
        table.setIdentifier(DBIdentifier.newTable("NameIsRight"));
        table.setSchemaIdentifier(DBIdentifier.newSchema("IAmASchema"));
       
        String[] sqls = dict.getCreateTableSQL(table);
        assertEquals(1, sqls.length);
        assertTrue(sqls[0].contains("NameIsRight"));
        assertTrue(sqls[0].contains("IAmASchema"));
View Full Code Here


    /**
     * Return the alias for the give column
     */
    public String getColumnAlias(Column col, Object path) {
        Table table = col.getTable();
        String tableAlias = null;
        Iterator itr = getJoinIterator();
        while (itr.hasNext()) {
            Join join = (Join) itr.next();
            if (join != null) {
                if (join.getTable1() == table)
                    tableAlias = join.getAlias1();
                else if (join.getTable2() == table)
                    tableAlias = join.getAlias2();
                if (tableAlias != null)
                    return new StringBuilder(tableAlias).append(".").
                        append(_dict.getNamingUtil().toDBName(col.getIdentifier())).toString();
            }
        }
        throw new InternalException("Can not resolve alias for field: " +
            path.toString() + " mapped to column: " + col.getIdentifier().getName() +
            " table: "+table.getIdentifier().getName());
    }
View Full Code Here

            schema = null;
        }

        // look for named table using full name and findTable, which allows
        // the dynamic schema factory to create the table if needed
        Table table = group.findTable(path);
        if (table != null)
            return table;
        if (!adapt)
            throw new MetaDataException(_loc.get("bad-table", given, context));
View Full Code Here

            // are kept up-to-date.
            //
            if ((colName.getName().length() > dict.maxColumnNameLength) ||
               dict.getInvalidColumnWordSet().contains(DBIdentifier.toUpper(colName).getName()) &&
              !(table.getClass().getName().contains("DynamicTable"))) {
                colName=dict.getValidColumnName(colName, new Table());
                col = table.getColumn(colName);
                if (col == null && !adapt) {
                    throw new MetaDataException(_loc.
                        get(prefix + "-bad-col-name", context, colName, table));
                }
View Full Code Here

                    + "-no-index-cols", context));
            return null;
        }

        // look for an existing index on these columns
        Table table = cols[0].getTable();
        Index[] idxs = table.getIndexes();
        Index exist = null;
        for (int i = 0; i < idxs.length; i++) {
            if (idxs[i].columnsMatch(cols)) {
                exist = idxs[i];
                break;
            }
        }

        // remove existing index?
        if (!_canIdx) {
            if (exist == null)
                return null;
            if (!adapt)
                throw new MetaDataException(_loc.get(prefix + "-index-exists",
                    context));
            table.removeIndex(exist);
            return null;
        }

        // if we have an existing index, merge given info into it
        if (exist != null) {
            if (_idx != null && _idx.isUnique() && !exist.isUnique()) {
                if (!adapt)
                    throw new MetaDataException(_loc.get(prefix
                        + "-index-not-unique", context));
                exist.setUnique(true);
            }
            return exist;
        }

        // if no defaults return null
        MappingRepository repos = (MappingRepository) context.getRepository();
        boolean fill = repos.getMappingDefaults().defaultMissingInfo();
        if (_idx == null && (tmplate == null || (!adapt && !fill)))
            return null;

        DBIdentifier name = DBIdentifier.NULL;
        boolean unq;
        if (_idx != null) {
            name = _idx.getIdentifier();
            unq = _idx.isUnique();
            // preserve multiple columns if they are specified in the index
            if (_idx.getColumns() != null && _idx.getColumns().length > 1)
                cols = _idx.getColumns();
        } else
            unq = tmplate.isUnique();

        // if no name provided by user info, make one
        if (DBIdentifier.isNull(name)) {
            if (tmplate != null)
                name = tmplate.getIdentifier();
            else {
                name = cols[0].getIdentifier();
                name = repos.getDBDictionary().getValidIndexName(name, table);
            }
        }

        Index idx = table.addIndex(name);
        idx.setUnique(unq);
        idx.setColumns(cols);
        return idx;
    }
View Full Code Here

                    + "-no-unique-cols", context));
            return null;
        }

        // look for an existing constraint on these columns
        Table table = cols[0].getTable();
        Unique[] unqs = table.getUniques();
        Unique exist = null;
        for (int i = 0; i < unqs.length; i++) {
            if (unqs[i].columnsMatch(cols)) {
                exist = unqs[i];
                break;
            }
        }

        // remove existing unique?
        if (!_canUnq) {
            if (exist == null)
                return null;
            if (!adapt)
                throw new MetaDataException(_loc.get(prefix
                    + "-unique-exists", context));
            table.removeUnique(exist);
            return null;
        }

        // no defaults; return existing constraint (if any)
        if (tmplate == null && _unq == null)
            return exist;

        MappingRepository repos = (MappingRepository) context.getRepository();
        if (exist != null) {
            if (_unq != null && _unq.isDeferred() && !exist.isDeferred()) {
                Log log = repos.getLog();
                if (log.isWarnEnabled())
                    log.warn(_loc.get(prefix + "-defer-unique", context));
            }
            return exist;
        }

        // dict can't handle unique constraints?
        DBDictionary dict = repos.getDBDictionary();
        if (_unq != null && !dict.supportsUniqueConstraints) {
            Log log = repos.getLog();
            if (log.isWarnEnabled())
                log.warn(_loc.get(prefix + "-unique-support", context));
            return null;
        }

        boolean fill = repos.getMappingDefaults().defaultMissingInfo();
        if (!adapt && !fill && _unq == null)
            return null;

        DBIdentifier name = DBIdentifier.NULL;
        boolean deferred;
        if (_unq != null) {
            name = _unq.getIdentifier();
            deferred = _unq.isDeferred();
        } else {
            name = tmplate.getIdentifier();
            deferred = tmplate.isDeferred();
        }

        if (deferred && !dict.supportsDeferredConstraints) {
            Log log = repos.getLog();
            if (log.isWarnEnabled())
                log.warn(_loc.get(prefix + "-create-defer-unique",
                    context, dict.platform));
            deferred = false;
        }
       
        if (DBIdentifier.isEmpty(name)) {
          name = cols[0].getIdentifier();
          name = repos.getDBDictionary().getValidUniqueName(name, table);
        }
       
        Unique unq = table.addUnique(name);
        unq.setDeferred(deferred);
        unq.setColumns(cols);
        return unq;
    }
View Full Code Here

            given, def, inversable, adapt);
        _join = JOIN_FORWARD;

        // establish local table using any join between two columns; if we only
        // find constant joins, then keep default local table (directionless)
        Table local = table;
        Table foreign = rel.getTable();
        Table tmp;
        boolean constant = false;
        boolean localSet = false;
        for (int i = 0; i < joins.length; i++) {
            if (joins[i][1]instanceof Column) {
                tmp = ((Column) joins[i][0]).getTable();
View Full Code Here

            throw new MetaDataException(_loc.get(prefix + "-no-fkcol-name",
                context));

        // check to see if the column isn't in the expected table; it might
        // be an inverse join or a join to a base class of the target type
        Table local = table;
        Table foreign = rel.getTable();
        boolean fullName = false;
        boolean inverse = false;
        if (!DBIdentifier.isNull(name)) {
            QualifiedDBIdentifier path = QualifiedDBIdentifier.getPath(name);
            if (!DBIdentifier.isNull(path.getObjectTableName())) {
                if (DBIdentifier.isEmpty(path.getObjectTableName()))
                    local = foreign;
                else
                    local = findTable(context, path.getObjectTableName(),
                        local, foreign, null);
                fullName = true;
                name = path.getIdentifier().getUnqualifiedName();

                // if inverse join, then swap local and foreign tables
                if (local != table) {
                    foreign = table;
                    inverse = true;
                }
            }
        }
        boolean forceInverse = !fullName && _join == JOIN_INVERSE;
        if (forceInverse) {
            local = foreign;
            foreign = table;
            inverse = true;
        }

        // determine target
        DBIdentifier targetName = given.getTargetIdentifier();
        Object target = null;
        Table ttable = null;
        boolean constant = false;
        boolean fullTarget = false;
        if (DBIdentifier.isNull(targetName) && given.getTargetField() != null) {
            ClassMapping tcls = (inverse) ? cls : rel;
            String fieldName = given.getTargetField();
View Full Code Here

        return getTableIdentifier(fm, schema).getName();
    }

    public DBIdentifier getTableIdentifier(FieldMapping fm, Schema schema) {
        DBIdentifier sName = DBIdentifier.newTable(fm.getName());
        Table table = fm.getDefiningMapping().getTable();
        if (table != null) {
            DBIdentifier tableName = DBIdentifier.truncate(table.getIdentifier(),5);
            sName = DBIdentifier.append(tableName, fm.getName());
        }
        if (!_defMissing)
            sName = dict.getValidTableName(sName, schema);
        return sName;
View Full Code Here

        // differentiate between secondary table joins and relations built
        // around an inverse key: check to see if we're mapped as a secondary
        // table join but we're in the table of the related type, and if so
        // switch our join mapping info to our value mapping info
        DBIdentifier tableName = field.getMappingInfo().getTableIdentifier();
        Table table = field.getTypeMapping().getTable();
        ValueMappingInfo vinfo = field.getValueInfo();
        if (!DBIdentifier.isNull(tableName) && table != null
            && (tableName.equals(table.getIdentifier())
            || tableName.equals(table.getFullIdentifier()))) {
            vinfo.setJoinDirection(MappingInfo.JOIN_INVERSE);
            vinfo.setColumns(field.getMappingInfo().getColumns());
            field.getMappingInfo().setTableIdentifier(DBIdentifier.NULL);
            field.getMappingInfo().setColumns(null);
        }
View Full Code Here

TOP

Related Classes of org.apache.openjpa.jdbc.schema.Table

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.