Package org.apache.torque.adapter

Examples of org.apache.torque.adapter.DB


     */
    static Query createQuery(Criteria criteria)
        throws TorqueException
    {
        Query query = new Query();
        DB db = Torque.getDB(criteria.getDbName());
        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());

        UniqueList selectModifiers = query.getSelectModifiers();
        UniqueList selectClause = query.getSelectClause();
        UniqueList fromClause = query.getFromClause();
        UniqueList whereClause = query.getWhereClause();
        UniqueList orderByClause = query.getOrderByClause();
        UniqueList groupByClause = query.getGroupByClause();

        UniqueList orderBy = criteria.getOrderByColumns();
        UniqueList groupBy = criteria.getGroupByColumns();
        UniqueList select = criteria.getSelectColumns();
        Hashtable aliases = criteria.getAsColumns();
        UniqueList modifiers = criteria.getSelectModifiers();

        for (int i = 0; i < modifiers.size(); i++)
        {
            selectModifiers.add(modifiers.get(i));
        }

        for (int i = 0; i < select.size(); i++)
        {
            String columnName = (String) select.get(i);
            if (columnName.indexOf('.') == -&& columnName.indexOf('*') == -1)
            {
                throwMalformedColumnNameException("select", columnName);
            }
            String tableName = null;
            selectClause.add(columnName);
            int parenPos = columnName.indexOf('(');
            if (parenPos == -1)
            {
                tableName = columnName.substring(0, columnName.indexOf('.'));
            }
            else if (columnName.indexOf('.') > -1)
            {
                tableName =
                    columnName.substring(parenPos + 1, columnName.indexOf('.'));
                // functions may contain qualifiers so only take the last
                // word as the table name.
                int lastSpace = tableName.lastIndexOf(' ');
                if (lastSpace != -1)
                {
                    tableName = tableName.substring(lastSpace + 1);
                }
            }
            String tableName2 = criteria.getTableForAlias(tableName);
            if (tableName2 != null)
            {
                fromClause.add(new StringBuffer(
                        tableName.length() + tableName2.length() + 1)
                        .append(tableName2)
                        .append(' ')
                        .append(tableName)
                        .toString());
            }
            else
            {
                fromClause.add(tableName);
            }
        }

        Iterator it = aliases.keySet().iterator();
        while (it.hasNext())
        {
            String key = (String) it.next();
            selectClause.add((String) aliases.get(key) + " AS " + key);
        }

        Iterator critKeys = criteria.keySet().iterator();
        while (critKeys.hasNext())
        {
            String key = (String) critKeys.next();
            Criteria.Criterion criterion = criteria.getCriterion(key);
            Criteria.Criterion[] someCriteria =
                criterion.getAttachedCriterion();
            String table = null;
            for (int i = 0; i < someCriteria.length; i++)
            {
                String tableName = someCriteria[i].getTable();
                table = criteria.getTableForAlias(tableName);
                if (table != null)
                {
                    fromClause.add(new StringBuffer(
                            tableName.length() + table.length() + 1)
                            .append(table)
                            .append(' ')
                            .append(tableName)
                            .toString());
                }
                else
                {
                    fromClause.add(tableName);
                    table = tableName;
                }

                boolean ignorCase = ((criteria.isIgnoreCase()
                        || someCriteria[i].isIgnoreCase())
                        && (dbMap
                            .getTable(table)
                            .getColumn(someCriteria[i].getColumn())
                            .getType()
                            instanceof String));

                someCriteria[i].setIgnoreCase(ignorCase);
            }

            criterion.setDB(db);
            whereClause.add(criterion.toString());
        }

        List join = criteria.getJoinL();
        if (join != null)
        {
            for (int i = 0; i < join.size(); i++)
            {
                String join1 = (String) join.get(i);
                String join2 = (String) criteria.getJoinR().get(i);
                if (join1.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("join", join1);
                }
                if (join2.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("join", join2);
                }

                String tableName = join1.substring(0, join1.indexOf('.'));
                String table = criteria.getTableForAlias(tableName);
                if (table != null)
                {
                    fromClause.add(new StringBuffer(
                            tableName.length() + table.length() + 1)
                            .append(table)
                            .append(' ')
                            .append(tableName)
                            .toString());
                }
                else
                {
                    fromClause.add(tableName);
                }

                int dot = join2.indexOf('.');
                tableName = join2.substring(0, dot);
                table = criteria.getTableForAlias(tableName);
                if (table != null)
                {
                    fromClause.add(new StringBuffer(
                            tableName.length() + table.length() + 1)
                            .append(table)
                            .append(' ')
                            .append(tableName)
                            .toString());
                }
                else
                {
                    fromClause.add(tableName);
                    table = tableName;
                }

                boolean ignorCase = (criteria.isIgnoreCase()
                        && (dbMap
                            .getTable(table)
                            .getColumn(join2.substring(dot + 1, join2.length()))
                            .getType()
                            instanceof String));

                whereClause.add(
                    SqlExpression.buildInnerJoin(join1, join2, ignorCase, db));
            }
        }

        // need to allow for multiple group bys
        if (groupBy != null && groupBy.size() > 0)
        {
            for (int i = 0; i < groupBy.size(); i++)
            {
                String groupByColumn = (String) groupBy.get(i);
                if (groupByColumn.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("group by",
                            groupByColumn);
                }
                groupByClause.add(groupByColumn);
            }
        }

        Criteria.Criterion having = criteria.getHaving();
        if (having != null)
        {
            //String groupByString = null;
            query.setHaving(having.toString());
        }

        if (orderBy != null && orderBy.size() > 0)
        {
            // Check for each String/Character column and apply
            // toUpperCase().
            for (int i = 0; i < orderBy.size(); i++)
            {
                String orderByColumn = (String) orderBy.get(i);
                if (orderByColumn.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("order by",
                            orderByColumn);
                }
                String tableName =
                    orderByColumn.substring(0, orderByColumn.indexOf('.'));
                String table = criteria.getTableForAlias(tableName);
                if (table == null)
                {
                    table = tableName;
                }

                // See if there's a space (between the column list and sort
                // order in ORDER BY table.column DESC).
                int spacePos = orderByColumn.indexOf(' ');
                String columnName;
                if (spacePos == -1)
                {
                    columnName =
                        orderByColumn.substring(orderByColumn.indexOf('.') + 1);
                }
                else
                {
                    columnName = orderByColumn.substring(
                            orderByColumn.indexOf('.') + 1, spacePos);
                }
                ColumnMap column = dbMap.getTable(table).getColumn(columnName);
                if (column.getType() instanceof String)
                {
                    if (spacePos == -1)
                    {
                        orderByClause.add(
                                db.ignoreCaseInOrderBy(orderByColumn));
                    }
                    else
                    {
                        orderByClause.add(db.ignoreCaseInOrderBy(
                                orderByColumn.substring(0, spacePos))
                                + orderByColumn.substring(spacePos));
                    }
                    selectClause.add(
                        db.ignoreCaseInOrderBy(table + '.' + columnName));
                }
                else
                {
                    orderByClause.add(orderByColumn);
                }
View Full Code Here


        Criteria selectCriteria,
        Criteria updateValues,
        Connection con)
        throws TorqueException
    {
        DB db = Torque.getDB(selectCriteria.getDbName());
        DatabaseMap dbMap = Torque.getDatabaseMap(selectCriteria.getDbName());

        // Set up a list of required tables. StringStack.add()
        // only adds element if it is unique.
        HashSet tables = new HashSet();
View Full Code Here

        Criteria criteria,
        StringBuffer queryString,
        List params)
        throws TorqueException
    {
        DB db = Torque.getDB(criteria.getDbName());
        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());

        Query query = new Query();

        UniqueList selectModifiers = query.getSelectModifiers();
        UniqueList selectClause = query.getSelectClause();
        UniqueList fromClause = query.getFromClause();
        UniqueList whereClause = query.getWhereClause();
        UniqueList orderByClause = query.getOrderByClause();

        UniqueList orderBy = criteria.getOrderByColumns();
        UniqueList select = criteria.getSelectColumns();
        Hashtable aliases = criteria.getAsColumns();
        UniqueList modifiers = criteria.getSelectModifiers();

        for (int i = 0; i < modifiers.size(); i++)
        {
            selectModifiers.add(modifiers.get(i));
        }

        for (int i = 0; i < select.size(); i++)
        {
            String columnName = (String) select.get(i);
            if (columnName.indexOf('.') == -1)
            {
                throwMalformedColumnNameException("select", columnName);
            }
            String tableName = null;
            selectClause.add(columnName);
            int parenPos = columnName.indexOf('(');
            if (parenPos == -1)
            {
                tableName = columnName.substring(0, columnName.indexOf('.'));
            }
            else
            {
                tableName =
                    columnName.substring(parenPos + 1, columnName.indexOf('.'));
                // functions may contain qualifiers so only take the last
                // word as the table name.
                int lastSpace = tableName.lastIndexOf(' ');
                if (lastSpace != -1)
                {
                    tableName = tableName.substring(lastSpace + 1);
                }
            }
            String tableName2 = criteria.getTableForAlias(tableName);
            if (tableName2 != null)
            {
                fromClause.add(new StringBuffer(tableName.length()
                        + tableName2.length() + 1)
                        .append(tableName2)
                        .append(' ')
                        .append(tableName)
                        .toString());
            }
            else
            {
                fromClause.add(tableName);
            }
        }

        Iterator it = aliases.keySet().iterator();
        while (it.hasNext())
        {
            String key = (String) it.next();
            selectClause.add((String) aliases.get(key) + " AS " + key);
        }

        Iterator critKeys = criteria.keySet().iterator();
        while (critKeys.hasNext())
        {
            String key = (String) critKeys.next();
            Criteria.Criterion criterion = criteria.getCriterion(key);
            Criteria.Criterion[] someCriteria =
                criterion.getAttachedCriterion();

            String table = null;
            for (int i = 0; i < someCriteria.length; i++)
            {
                String tableName = someCriteria[i].getTable();
                table = criteria.getTableForAlias(tableName);
                if (table != null)
                {
                    fromClause.add(new StringBuffer(tableName.length()
                            + table.length() + 1)
                            .append(table)
                            .append(' ')
                            .append(tableName)
                            .toString());
                }
                else
                {
                    fromClause.add(tableName);
                    table = tableName;
                }

                boolean ignorCase = ((criteria.isIgnoreCase()
                        || someCriteria[i].isIgnoreCase())
                        && (dbMap
                            .getTable(table)
                            .getColumn(someCriteria[i].getColumn())
                            .getType()
                            instanceof String));

                someCriteria[i].setIgnoreCase(ignorCase);
            }

            criterion.setDB(db);
            StringBuffer sb = new StringBuffer();
            criterion.appendPsTo(sb, params);
            whereClause.add(sb.toString());
        }

        List join = criteria.getJoinL();
        if (join != null)
        {
            for (int i = 0; i < join.size(); i++)
            {
                String join1 = (String) join.get(i);
                String join2 = (String) criteria.getJoinR().get(i);
                if (join1.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("join", join1);
                }
                if (join2.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("join", join2);
                }

                String tableName = join1.substring(0, join1.indexOf('.'));
                String table = criteria.getTableForAlias(tableName);
                if (table != null)
                {
                    fromClause.add(new StringBuffer(tableName.length()
                            + table.length() + 1)
                            .append(table)
                            .append(' ')
                            .append(tableName)
                            .toString());
                }
                else
                {
                    fromClause.add(tableName);
                }

                int dot = join2.indexOf('.');
                tableName = join2.substring(0, dot);
                table = criteria.getTableForAlias(tableName);
                if (table != null)
                {
                    fromClause.add(new StringBuffer(tableName.length()
                            + table.length() + 1)
                            .append(table)
                            .append(' ')
                            .append(tableName)
                            .toString());
                }
                else
                {
                    fromClause.add(tableName);
                    table = tableName;
                }

                boolean ignorCase = (criteria.isIgnoreCase()
                        && (dbMap
                            .getTable(table)
                            .getColumn(join2.substring(dot + 1, join2.length()))
                            .getType()
                            instanceof String));

                whereClause.add(
                    SqlExpression.buildInnerJoin(join1, join2, ignorCase, db));
            }
        }

        if (orderBy != null && orderBy.size() > 0)
        {
            // Check for each String/Character column and apply
            // toUpperCase().
            for (int i = 0; i < orderBy.size(); i++)
            {
                String orderByColumn = (String) orderBy.get(i);
                if (orderByColumn.indexOf('.') == -1)
                {
                    throwMalformedColumnNameException("order by",
                        orderByColumn);
                }
                String table =
                    orderByColumn.substring(0, orderByColumn.indexOf('.'));
                // See if there's a space (between the column list and sort
                // order in ORDER BY table.column DESC).
                int spacePos = orderByColumn.indexOf(' ');
                String columnName;
                if (spacePos == -1)
                {
                    columnName =
                        orderByColumn.substring(orderByColumn.indexOf('.') + 1);
                }
                else
                {
                    columnName = orderByColumn.substring(
                            orderByColumn.indexOf('.') + 1,
                            spacePos);
                }
                ColumnMap column = dbMap.getTable(table).getColumn(columnName);
                if (column.getType() instanceof String)
                {
                    if (spacePos == -1)
                    {
                        orderByClause.add(
                            db.ignoreCaseInOrderBy(orderByColumn));
                    }
                    else
                    {
                        orderByClause.add(db.ignoreCaseInOrderBy(
                                orderByColumn.substring(0, spacePos))
                                + orderByColumn.substring(spacePos));
                    }
                    selectClause.add(
                        db.ignoreCaseInOrderBy(table + '.' + columnName));
                }
                else
                {
                    orderByClause.add(orderByColumn);
                }
View Full Code Here

                    String key = (String) it.next();
                    if (key.endsWith("adapter"))
                    {
                        String adapter = c.getString(key);
                        String handle = key.substring(0, key.indexOf('.'));
                        DB db = DBFactory.create(adapter);
                        // register the adapter for this name
                        adapterMap.put(handle, db);
                        log.debug("Adding " + adapter + " -> " + handle + " as Adapter");
                        foundAdapters = true;
                    }
View Full Code Here

            String key = getDatabaseProperty(name, "adapter");
            if (StringUtils.isEmpty(key))
            {
                key = getDatabaseProperty(name, "driver");
            }
            DB db = DBFactory.create(key);
            for (int i = 0; i < IDGeneratorFactory.ID_GENERATOR_METHODS.length;
                 i++)
            {
                map.addIdGenerator(IDGeneratorFactory.ID_GENERATOR_METHODS[i],
                        IDGeneratorFactory.create(db));
View Full Code Here

     */
    public static List doSelect(Criteria criteria, Connection con)
        throws TorqueException
    {
        Query query = createQuery(criteria);
        DB dbadapter = Torque.getDB(criteria.getDbName());

        // Call Village depending on the capabilities of the DB
        return executeQuery(query.toString(),
                dbadapter.supportsNativeOffset() ? 0 : criteria.getOffset(),
                dbadapter.supportsNativeLimit() ? -1 : criteria.getLimit(),
                criteria.isSingleRecord(),
                con);
    }
View Full Code Here

     */
    private static void processTables(Criteria crit, Set tables, Connection con, ProcessCallback pc)
            throws Exception
    {
        String dbName = crit.getDbName();
        DB db = Torque.getDB(dbName);
        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);

        // create the statements for the tables
        for (Iterator it = tables.iterator(); it.hasNext();)
        {
View Full Code Here

                        || key.endsWith(DB.DRIVER_KEY))
                {
                    String adapter = c.getString(key);
                    String handle = key.substring(0, key.indexOf('.'));

                    DB db;

                    db = DBFactory.create(adapter);

                    // Not supported, try manually defined adapter class
                    if (db == null)
View Full Code Here

         * variations of sql.
         * @return value of db.
         */
        public DB getDb()
        {
            DB db = null;
            if (this.db == null)
            {
                // db may not be set if generating preliminary sql for
                // debugging.
                try
View Full Code Here

            if (column == null || value == null)
            {
                return;
            }

            DB db = getDb();

            for (int j = 0; j < this.clauses.size(); j++)
            {
                sb.append('(');
            }
            if (CUSTOM == comparison)
            {
                if (!"".equals(value))
                {
                    sb.append((String) value);
                }
            }
            else
            {
                String field = null;
                if (table == null)
                {
                    field = column;
                }
                else
                {
                    field = new StringBuffer(
                        table.length() + 1 + column.length())
                        .append(table).append('.').append(column)
                        .toString();
                }

                if (comparison.equals(Criteria.IN)
                     || comparison.equals(Criteria.NOT_IN))
                {
                    sb.append(field)
                      .append(comparison);

                    UniqueList inClause = new UniqueList();

                    if (value instanceof List)
                    {
                        value = ((List) value).toArray (new Object[0]);
                    }

                    for (int i = 0; i < Array.getLength(value); i++)
                    {
                        Object item = Array.get(value, i);

                        inClause.add(SqlExpression.processInValue(item,
                                                                  ignoreCase,
                                                                  db));
                    }

                    StringBuffer inString = new StringBuffer();
                    inString.append('(').append(StringUtils.join(
                            inClause.iterator(), (","))).append(')');
                    sb.append(inString.toString());
                }
                else
                {
                    if (ignoreCase)
                    {
                        sb.append(db.ignoreCase(field))
                          .append(comparison)
                          .append(db.ignoreCase("?"));
                    }
                    else
                    {
                        sb.append(field)
                          .append(comparison)
View Full Code Here

TOP

Related Classes of org.apache.torque.adapter.DB

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.