Package org.xorm.datastore

Examples of org.xorm.datastore.Table


    private boolean distinct;
    private List parameters = new ArrayList();

    public SQLQuery(Selector selector) {
        this.selector = selector;
        Table table = selector.getTable();
        Alias alias = makeAlias(selector);
        parse(alias, selector);
    }
View Full Code Here


        fromClause = new StringBuffer();

        Iterator i = condition.getTables().iterator();
        boolean first = true;
        while (i.hasNext()) {
            Table t = (Table) i.next();
            Alias alias;
            if (first) {
                alias = new Alias(t.getName(), t, selector.getFetchColumns(), t.getPrimaryKey());
                first = false;
            else {
                alias = new Alias(t.getName(), t, null, t.getPrimaryKey());
                fromClause.append(",");
            }
            aliases.add(alias);
            fromClause.append(t.getName());
        }
        whereClause.append(condition.getRawQuery());
    }
View Full Code Here

        // Examine all selectors for fetchColumns
        Iterator i = aliases.iterator();
        boolean seenAny = false;
        while (i.hasNext()) {
            Alias alias = (Alias) i.next();
            Table t = alias.getTable();
            Set s = alias.getFetchColumns();
            Iterator j = s.iterator();
            while (j.hasNext()) {
                if (seenAny) {
                    sql.append(", ");
View Full Code Here

    public Table describeTable(String tableName) {
        logger.fine("Attempting to load table from metadata: " + tableName);
        Connection connection = null;
        DatabaseMetaData metadata = null;
        ResultSet results = null;
        Table table = null;
        try {
            connection = getDataSource().getConnection();
            metadata = connection.getMetaData();
            results = metadata.getTables(null,
                                         null,
                                         tableName,
                                         new String[] { "TABLE" });
            // It's possible that multiple tables match the pattern that
            // tableName defines.  Iterate through until we find the right one.
            boolean found = false;
            while (!found && results.next()) {
                found = results.getString("TABLE_NAME").equals(tableName);
            }
            if (!found) {
                // The table doesn't exist.
                logger.warning("Table doesn't exist in metadata: " + tableName);
                throw new JDOFatalUserException(I18N.msg("E_no_table_in_db", tableName));
            }

            // Snag all of the primary key column names, which we'll use
            // as we iterate through the tables columns.
            results = metadata.getPrimaryKeys(null, null, tableName);
            ArrayList pkColumnNames = new ArrayList();
            while (results.next()) {
                String pkColumnName = results.getString("COLUMN_NAME");
                logger.fine("Primary key column: " + pkColumnName);
                pkColumnNames.add(pkColumnName);
            }
            results.close();

            table = new Table(tableName);

            // Iterate through all of the columns
            results = metadata.getColumns(null, null, tableName, null);
            while (results.next()) {
                String columnName = results.getString("COLUMN_NAME");
                int dataType = results.getInt("DATA_TYPE");
                String typeName = SQLType.nameFor(dataType);
                boolean isNullable =
                    results.getString("IS_NULLABLE").equalsIgnoreCase("YES");
                boolean isPrimaryKey = pkColumnNames.contains(columnName);
                logger.fine("Column: " + columnName +
                            ", type: " + typeName + " (" + dataType +
                            "), nullable: " + isNullable +
                            (isPrimaryKey ? ", primary key" : ""));
               
                Column column = new Column(table, columnName);
                if (isPrimaryKey) {
                    table.setPrimaryKey(column);
                    if (autoIncrementPrimaryKeys) {
                        logger.fine("Setting " + columnName +
                                    " column to auto-increment");
                        column.setAutoIncremented(true);
                    }
View Full Code Here

     */
    public void update(Row row) throws DriverException {
        if (readOnly) {
            throw new DriverException(I18N.msg("E_read_only_txn", "update"));
        }
        Table table = row.getTable();
        Column primaryKey = table.getPrimaryKey();

        // Cannot update rows without a primary key
        if (primaryKey == null) {
            return;
        }

        StringBuffer sql = new StringBuffer();
        sql.append("UPDATE ")
            .append(table.getName())
            .append(" SET ");
        Iterator it = table.getColumns().iterator();
        boolean seenOne = false;
        while (it.hasNext()) {
            Column c = (Column) it.next();
            if ((c == primaryKey && primaryKey.isAutoIncremented())
                || c.isReadOnly()) {
                continue;
            }
            if (row.isDirty(c)) {
                if (seenOne) {
                    sql.append(", ");
                } else {
                    seenOne = true;
                }
                sql.append(c.getName())
                    .append(" = ?");
            }
        }
        // No need to do anything if nothing has changed.
        if (!seenOne) {
            return;
        }
        sql.append(" WHERE ")
            .append(table.getPrimaryKey().getName())
            .append(" = ?");
        String statement = sql.toString();
        logger.info(statement);
        logger.info(row.toString());
        try {
View Full Code Here

        }
    }

    public int count(Selector selector) throws DriverException {
        SQLQuery query = new SQLQuery(selector);
        Table table = query.getTargetTable();
        // Similar to a select for the class
        String statement = query.toCountSQL();
        logger.info(statement);
        int size = 0;
        try {
View Full Code Here

     
            while (rs.next()) {
                Row row;
                Set columns;
                SQLQuery.Alias alias;
                Table table;

                pos = 0;
                Iterator i = aliases.iterator();
                while (i.hasNext()) {
                    alias = (SQLQuery.Alias) i.next();
                    table = alias.getTable();
                    Set fcs = alias.getFetchColumns();
                    if (!fcs.isEmpty()) {
                        row = new Row(table);
                        Iterator fci = fcs.iterator();
                        while (fci.hasNext()) {
                            Column c = (Column) fci.next();
       
                            // TODO will this be flexible enough?
                            row.setValue(c, rs.getObject(++pos));
                        }
                        if (table.equals(query.getTargetTable())) {
                            list.add(row);
                        } else {
                            extraRows.add(row);
                        }
                    } // non-empty fetchgroup
View Full Code Here

                }

                Iterator exts = jdoClass.getExtensions().iterator();
                JDOExtension extension;
                Class dsi = null;
                Table t = null;
                while (exts.hasNext()) {
                    extension = (JDOExtension) exts.next();
                    if (XORM_VENDOR_NAME.equals(extension.getVendorName())) {
                        String key = extension.getKey();
                        if ("datastore-identity-type".equals(key)) {
View Full Code Here

     */
    private void parseClass(JDOClass jdoClass, String defaultPackage) throws ClassNotFoundException {
        String className = getPackagedClassName(jdoClass.getName(), defaultPackage);
        Class c = Class.forName(className);
        ClassMapping classMapping = getClassMapping(c);
        Table t = classMapping.getTable();
 
        Iterator exts;
        JDOExtension extension;
        // read fields
        Iterator j = jdoClass.getFields().iterator();
        while (j.hasNext()) {
            JDOField field = (JDOField) j.next();
            exts = field.getExtensions().iterator();
            while (exts.hasNext()) {
                extension = (JDOExtension) exts.next();
                if (XORM_VENDOR_NAME.equals(extension.getVendorName())) {
                    String key = extension.getKey();
                    if ("column".equals(key)) {
                        Column c2 = t.getColumnByName(extension.getValue());
                        if (c2 == null) {
                            throw new JDOFatalUserException(I18N.msg("E_no_column", extension.getValue(), t.getName(), jdoClass.getName()));
                        }
                        classMapping.setColumn(field.getName(), c2, field.isDefaultFetchGroup());
                        if (field.getNullValue().equals(JDONullValue.EXCEPTION)) {
                            // TODO: should this be done here?
                            c2.setNonNull(true);
View Full Code Here

        RelationshipMapping.Endpoint target = new RelationshipMapping.Endpoint();
        target.setCollectionType(target.SET);
        relationship.setTarget(target);

        Table table = null;
        Iterator i = root.getExtensions().iterator();
        String sourceStr = null;
        String targetStr = null;
        String indexStr = null;
        // Added support for filtered collections (Dan Checkoway, 6/26/03)
        String filterStr = null;
        String parametersStr = null;
        String variablesStr = null;
        String importsStr = null;
        while (i.hasNext()) {
            JDOExtension element = (JDOExtension) i.next();
            if (XORM_VENDOR_NAME.equals(element.getVendorName())) {
                String key = element.getKey();
                String value = element.getValue();
                boolean redefined = false;
                if ("table".equals(key)) {
                    redefined = table != null;
                    table = getTable(value);
                    if (table == null) {
                        throw new JDOFatalUserException(I18N.msg("E_unknown_table", value));
                    }
                } else if (ATTR_SOURCE.equals(key)) {
                    redefined = sourceStr != null;
                    sourceStr = value;
                } else if (ATTR_TARGET.equals(key)) {
                    redefined = targetStr != null;
                    targetStr = value;
                } else if (ATTR_ORDER_BY.equals(key)) {
                    redefined = relationship.getOrderBy() != null;
                    relationship.setOrderBy(value);
                } else if (ATTR_INDEX.equals(key)) {
                    redefined = indexStr != null;
                    indexStr = value;
                } else if (ATTR_FILTER.equals(key)) {
                    // Filtered collection query
                    redefined = filterStr != null;
                    filterStr = value;
                } else if (ATTR_PARAMETERS.equals(key)) {
                    // Filtered collection query parameters
                    redefined = parametersStr != null;
                    parametersStr = value;
                } else if (ATTR_VARIABLES.equals(key)) {
                    // Filtered collection query variables
                    redefined = variablesStr != null;
                    variablesStr = value;
                } else if (ATTR_ORDERING.equals(key)) {
                    // JDO-style ordering
                    redefined = relationship.getOrdering() != null;
                    relationship.setOrdering(value);
                } else if (ATTR_IMPORTS.equals(key)) {
                    redefined = importsStr != null;
                    importsStr = value;
                }
                if (redefined) {
                    throw new JDOFatalUserException(I18N.msg("E_collection_redefinition", key, field.getName(), jdoClass.getName()));
                }
            }
        } // for each "extension" element

        if (sourceStr == null && filterStr == null) {
            if (useDefaultMapping) {
                sourceStr = "source_" + ownerClass.getName();
            } else {
                // You have to specify either a source or a filter or both.
                throw new JDOFatalUserException(I18N.msg("E_collection_no_source", field.getName(), jdoClass.getName()));
            }
        }
       
        if (table == null) {
            // The table wasn't explicitly specified.  We can safely
            // assume that if a collection is specified without an explicit
            // table, then the table is the collection element type's table.
            ClassMapping mapping = getClassMapping(elementClass);
            if ((table = mapping.getTable()) == null) {
                // Not much we can do about this.  I don't think it will ever
                // happen, but just in case...
                throw new JDOFatalUserException(I18N.msg("E_collection_no_table", field.getName(), jdoClass.getName()));
            }
            logger.fine("No table specified for collection field \"" +
                        field.getName() +
                        "\"...assuming element type table: " +
                        table.getName());
        }

        if (relationship.getOrderBy() != null &&
            relationship.getOrdering() != null) {
            // Don't even bother trying to interpret precedence
            throw new JDOFatalUserException(I18N.msg("E_collection_order_by_and_ordering"));
        }

        if (relationship.getOrdering() != null && filterStr == null) {
            throw new JDOFatalUserException(I18N.msg("E_collection_ordering_without_filter", field.getName(), jdoClass.getName()));
        }
       
        if (sourceStr != null) {
            source.setColumn(table.getColumnByName(sourceStr));
        }

        if (targetStr == null) {
            target.setColumn(table.getPrimaryKey());
        } else {
            // It's many-to-many.  We used to set the target's elementClass
            // to signify this, but that seemed like a hack to me.  And since
            // the target's elementClass was set to the owner class, which
            // didn't seem to apply (if anything the target element class
            // would be the collection's element class, not the owner's class),
            // I added this boolean setter/getter instead.
            relationship.setMToN(true);
            target.setColumn(table.getColumnByName(targetStr));
        }

        if (indexStr != null) {
            Column c = table.getColumnByName(indexStr);
            // Set the column to managed mode; we don't want
            // indices to be included in Row.equals() operations
            c.setManaged(true);
            relationship.setIndexColumn(c);
        }
View Full Code Here

TOP

Related Classes of org.xorm.datastore.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.