Package org.apache.openjpa.jdbc.schema

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


            || cols.size() == icols.size())) {
            if (cols.isEmpty())
                cols = new ArrayList(icols.size());
            for (int i = 0; i < icols.size(); i++) {
                if (cols.size() == i)
                    cols.add(new Column());
                ((Column) cols.get(i)).copy((Column) icols.get(i));
            }
            setColumns(cols);
        }
    }
View Full Code Here


     * Assert that the user did not try to join.
     */
    public void assertNoJoin(MetaDataContext context, boolean die) {
        boolean join = false;
        if (_cols != null) {
            Column col;
            for (int i = 0; !join && i < _cols.size(); i++) {
                col = (Column) _cols.get(i);
                if (col.getTarget() != null)
                    join = true;
            }
        }
        if (!join)
            return;
View Full Code Here

                context, String.valueOf(tmplates.length),
                String.valueOf(given.size())));

        Column[] cols = new Column[tmplates.length];
        _io = null;
        Column col;
        for (int i = 0; i < tmplates.length; i++) {
            col = (given.isEmpty()) ? null : (Column) given.get(i);
            cols[i] = mergeColumn(context, prefix, tmplates[i], true, col,
                table, adapt, fill);
            setIOFromColumnFlags(col, i);
View Full Code Here

                null, null);
            colName = colName.substring(dotIdx + 1);
        }

        // find existing column
        Column col = table.getColumn(colName);
        if (col == null && !adapt)
            throw new MetaDataException(_loc.get(prefix + "-bad-col-name",
                context, colName, table));

        MappingRepository repos = (MappingRepository) context.getRepository();
        DBDictionary dict = repos.getDBDictionary();

        // use information from template column by default, allowing any
        // user-given specifics to override it
        int type = tmplate.getType();
        int size = tmplate.getSize();
        if (type == Types.OTHER)
            type = dict.getJDBCType(tmplate.getJavaType(), size == -1);
        boolean ttype = true;
        int otype = type;
        String typeName = tmplate.getTypeName();
        Boolean notNull = null;
        if (tmplate.isNotNullExplicit())
            notNull = (tmplate.isNotNull()) ? Boolean.TRUE : Boolean.FALSE;
        int decimals = tmplate.getDecimalDigits();
        String defStr = tmplate.getDefaultString();
        boolean autoAssign = tmplate.isAutoAssigned();
        boolean relationId = tmplate.isRelationId();
        String targetField = tmplate.getTargetField();
        if (given != null) {
            // use given type if provided, but warn if it isn't compatible with
            // the expected column type
            if (given.getType() != Types.OTHER) {
                ttype = false;
                if (compat && !given.isCompatible(type, typeName, size,
                    decimals)) {
                    Log log = repos.getLog();
                    if (log.isWarnEnabled())
                        log.warn(_loc.get(prefix + "-incompat-col",
                            context, colName, Schemas.getJDBCName(type)));
                }
                otype = given.getType();
                type = dict.getPreferredType(otype);
            }
            typeName = given.getTypeName();
            size = given.getSize();
            decimals = given.getDecimalDigits();

            // leave this info as the template defaults unless the user
            // explicitly turns it on in the given column
            if (given.isNotNullExplicit())
                notNull = (given.isNotNull()) ? Boolean.TRUE : Boolean.FALSE;
            if (given.getDefaultString() != null)
                defStr = given.getDefaultString();
            if (given.isAutoAssigned())
                autoAssign = true;
            if (given.isRelationId())
                relationId = true;
        }

        // default char column size if original type is char (test original
        // type rather than final type because orig might be clob, translated
        // to an unsized varchar, which is supported by some dbs)
        if (size == 0 && (otype == Types.VARCHAR || otype == Types.CHAR))
            size = dict.characterColumnSize;

        // create column, or make sure existing column matches expected type
        if (col == null) {
            col = table.addColumn(colName);
            col.setType(type);
        } else if ((compat || !ttype) && !col.isCompatible(type, typeName,
            size, decimals)) {
            // if existing column isn't compatible with desired type, die if
            // can't adapt, else warn and change the existing column type
            Message msg = _loc.get(prefix + "-bad-col", context,
                Schemas.getJDBCName(type), col.getDescription());
            if (!adapt)
                throw new MetaDataException(msg);
            Log log = repos.getLog();
            if (log.isWarnEnabled())
                log.warn(msg);

            col.setType(type);
        } else if (given != null && given.getType() != Types.OTHER) {
            // as long as types are compatible, set column to expected type
            col.setType(type);
        }

        // always set the java type and autoassign to expected values, even on
        // an existing column, since we don't get this from the DB
        if (compat)
            col.setJavaType(tmplate.getJavaType());
        else if (col.getJavaType() == JavaTypes.OBJECT) {
            if (given != null && given.getJavaType() != JavaTypes.OBJECT)
                col.setJavaType(given.getJavaType());
            else
                col.setJavaType(JavaTypes.getTypeCode
                    (Schemas.getJavaType(col.getType(), col.getSize(),
                        col.getDecimalDigits())));
        }
        col.setAutoAssigned(autoAssign);
        col.setRelationId(relationId);
        col.setTargetField(targetField);

        // we need this for runtime, and the dynamic schema factory might
        // not know it, so set it even if not adapting
        if (defStr != null)
            col.setDefaultString(defStr);
        if (notNull != null)
            col.setNotNull(notNull.booleanValue());

        // add other details if adapting
        if (adapt) {
            if (typeName != null)
                col.setTypeName(typeName);
            if (size != 0)
                col.setSize(size);
            if (decimals != 0)
                col.setDecimalDigits(decimals);
        }
        return col;
    }
View Full Code Here

        fk.setDeleteAction(delAction);
        fk.setUpdateAction(upAction);
        fk.setDeferred(deferred);

        // add joins to key
        Column col;
        for (int i = 0; i < joins.length; i++) {
            col = (Column) joins[i][0];
            if (joins[i][1]instanceof Column)
                fk.join(col, (Column) joins[i][1]);
            else if ((joins[i][2] == Boolean.TRUE) != (_join == JOIN_INVERSE))
View Full Code Here

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

            Column[] targets = rel.getPrimaryKeyColumns();
            joins = new Object[targets.length][3];
            Column tmplate;
            for (int i = 0; i < targets.length; i++) {
                tmplate = new Column();
                tmplate.setName(targets[i].getName());
                tmplate.setJavaType(targets[i].getJavaType());
                tmplate.setType(targets[i].getType());
                tmplate.setTypeName(targets[i].getTypeName());
                tmplate.setSize(targets[i].getSize());
                tmplate.setDecimalDigits(targets[i].getDecimalDigits());

                if (def != null)
                    def.populate(table, rel.getTable(), tmplate, targets[i],
                        false, i, targets.length);
                joins[i][0] = mergeColumn(context, prefix, tmplate, true,
                    null, table, adapt, fill);
                joins[i][1] = targets[i];
            }
            return joins;
        }

        // use given columns to create join.  we don't try to use any of the
        // template columns, even if the user doesn't give a column linking to
        // every primary key of the target type -- users are allowed to create
        // partial joins.  this means, though, that if a user wants to specify
        // info for one join column, he has to at least create elements for
        // all of them

        joins = new Object[given.size()][3];
        Column col;
        for (int i = 0; i < joins.length; i++) {
            col = (Column) given.get(i);
            mergeJoinColumn(context, prefix, col, joins, i, table, cls, rel,
                def, inversable && !col.getFlag(Column.FLAG_PK_JOIN), adapt,
                fill);
        }
        return joins;
    }
View Full Code Here

                    + "-no-fkcol-target-adapt", context, name));
        }

        // find the target column, and create template for local column based
        // on it
        Column tmplate = new Column();
        tmplate.setName(name);
        if (!constant) {
            Column tcol = foreign.getColumn(targetName);
            if (tcol == null)
                throw new MetaDataException(_loc.get(prefix + "-bad-fktarget",
                    new Object[]{ context, targetName, name, foreign }));

            if (name == null)
                tmplate.setName(tcol.getName());
            tmplate.setJavaType(tcol.getJavaType());
            tmplate.setType(tcol.getType());
            tmplate.setTypeName(tcol.getTypeName());
            tmplate.setSize(tcol.getSize());
            tmplate.setDecimalDigits(tcol.getDecimalDigits());
            target = tcol;
        } else if (target instanceof String)
            tmplate.setJavaType(JavaTypes.STRING);
        else if (target instanceof Integer)
            tmplate.setJavaType(JavaTypes.INT);
        else if (target instanceof Double)
            tmplate.setJavaType(JavaTypes.DOUBLE);

        // populate template, but let user-given name override default name
        if (def != null)
            def.populate(local, foreign, tmplate, target, inverse, idx,
                joins.length);
        if (name != null)
            tmplate.setName(name);

        // create or merge local column
        Column col = mergeColumn(context, prefix, tmplate, true, given, local,
            adapt, fill);

        joins[idx][0] = col;
        joins[idx][1] = target;
        if (inverse)
View Full Code Here

        boolean forceJDBCType) {
        if (cols == null || cols.length == 0)
            _cols = null;
        else {
            _cols = new ArrayList(cols.length);
            Column col;
            for (int i = 0; i < cols.length; i++) {
                col = syncColumn(context, cols[i], cols.length,
                    forceJDBCType, cols[i].getTable(), null, null, false);
                setColumnFlagsFromIO(col, i);
                _cols.add(col);
View Full Code Here

        Column[] cpkCols = fk.getConstantPrimaryKeyColumns();
        Object[] cpks = fk.getPrimaryKeyConstants();

        int size = cols.length + ccols.length + cpkCols.length;
        _cols = new ArrayList(size);
        Column col;
        for (int i = 0; i < cols.length; i++) {
            col = syncColumn(context, cols[i], size, false, local,
                target, pkCols[i], _join == JOIN_INVERSE);
            setColumnFlagsFromIO(col, i);
            _cols.add(col);
View Full Code Here

        Object target, boolean inverse) {
        // use full name for cols that aren't in the expected table, or that
        // are inverse joins
        DBDictionary dict = ((MappingRepository) context.getRepository()).
            getDBDictionary();
        Column copy = new Column();
        if (col.getTable() != colTable || inverse)
            copy.setName(dict.getFullName(col.getTable(), true)
                + "." + col.getName());
        else
            copy.setName(col.getName());

        // set target if not default
        if (target != null) {
            if (target == NULL)
                copy.setTarget("null");
            else if (target instanceof Column) {
                Column tcol = (Column) target;
                if ((!inverse && tcol.getTable() != targetTable)
                    || (inverse && tcol.getTable() != colTable))
                    copy.setTarget(dict.getFullName(tcol.getTable(), true)
                        + "." + tcol.getName());
                else if (!defaultTarget(col, tcol, num))
                    copy.setTarget(tcol.getName());
            } else if (target instanceof Number)
                copy.setTarget(target.toString());
            else
                copy.setTarget("'" + target + "'");
        } else if (num > 1)
View Full Code Here

TOP

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

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.