⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basepeer.java

📁 torque服务器源代码
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            throw new TorqueException(e);        }        finally        {            if (statement != null)            {                try                {                    statement.close();                }                catch (SQLException ignored)                {                }            }        }    }    /**     * Convenience method that uses straight JDBC to delete multiple     * rows.  Village throws an Exception when multiple rows are     * deleted.  This method attempts to get the default database from     * the pool.     *     * @param table The table to delete records from.     * @param column The column in the where clause.     * @param value The value of the column.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static void deleteAll(String table, String column, int value)        throws TorqueException    {        Connection con = null;        try        {            // Get a connection to the db.            con = Torque.getConnection("default");            deleteAll(con, table, column, value);        }        finally        {            Torque.closeConnection(con);        }    }    /**     * Method to perform deletes based on values and keys in a     * Criteria.     *     * @param criteria The criteria to use.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static void doDelete(Criteria criteria) throws TorqueException    {        Connection con = null;        try        {            con = Transaction.beginOptional(                    criteria.getDbName(),                    criteria.isUseTransaction());            doDelete(criteria, con);            Transaction.commit(con);        }        catch (TorqueException e)        {            Transaction.safeRollback(con);            throw e;        }    }    /**     * Method to perform deletes based on values and keys in a Criteria.     *     * @param criteria The criteria to use.     * @param con A Connection.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static void doDelete(Criteria criteria, Connection con)        throws TorqueException    {        DB db = Torque.getDB(criteria.getDbName());        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());        // Set up a list of required tables and add extra entries to        // criteria if directed to delete all related records.        // StringStack.add() only adds element if it is unique.        HashSet tables = new HashSet();        Iterator it = criteria.keySet().iterator();        while (it.hasNext())        {            String key = (String) it.next();            Criteria.Criterion c = criteria.getCriterion(key);            List tableNames = c.getAllTables();            for (int i = 0; i < tableNames.size(); i++)            {                String name = (String) tableNames.get(i);                String tableName2 = criteria.getTableForAlias(name);                if (tableName2 != null)                {                    tables.add(new StringBuffer(                            name.length() + tableName2.length() + 1)                            .append(tableName2)                            .append(' ')                            .append(name)                            .toString());                }                else                {                    tables.add(name);                }            }            if (criteria.isCascade())            {                // This steps thru all the columns in the database.                TableMap[] tableMaps = dbMap.getTables();                for (int i = 0; i < tableMaps.length; i++)                {                    ColumnMap[] columnMaps = tableMaps[i].getColumns();                    for (int j = 0; j < columnMaps.length; j++)                    {                        // Only delete rows where the foreign key is                        // also a primary key.  Other rows need                        // updating, but that is not implemented.                        if (columnMaps[j].isForeignKey()                            && columnMaps[j].isPrimaryKey()                            && key.equals(columnMaps[j].getRelatedName()))                        {                            tables.add(tableMaps[i].getName());                            criteria.add(columnMaps[j].getFullyQualifiedName(),                                criteria.getValue(key));                        }                    }                }            }        }        Iterator tabIt = tables.iterator();        while (tabIt.hasNext())        {            String tab = (String) tabIt.next();            KeyDef kd = new KeyDef();            HashSet whereClause = new HashSet();            ColumnMap[] columnMaps = dbMap.getTable(tab).getColumns();            for (int j = 0; j < columnMaps.length; j++)            {                ColumnMap colMap = columnMaps[j];                if (colMap.isPrimaryKey())                {                    kd.addAttrib(colMap.getColumnName());                }                String key = new StringBuffer(colMap.getTableName())                        .append('.')                        .append(colMap.getColumnName())                        .toString();                if (criteria.containsKey(key))                {                    if (criteria.getComparison(key).equals(Criteria.CUSTOM))                    {                        whereClause.add(criteria.getString(key));                    }                    else                    {                        whereClause.add(SqlExpression.build(                                colMap.getColumnName(),                                criteria.getValue(key),                                criteria.getComparison(key),                                criteria.isIgnoreCase(),                                db));                    }                }            }            // Execute the statement.            TableDataSet tds = null;            try            {                tds = new TableDataSet(con, tab, kd);                String sqlSnippet = StringUtils.join(whereClause.iterator(), " AND ");                if (log.isDebugEnabled())                {                    log.debug("BasePeer.doDelete: whereClause=" + sqlSnippet);                }                tds.where(sqlSnippet);                tds.fetchRecords();                if (tds.size() > 1 && criteria.isSingleRecord())                {                    handleMultipleRecords(tds);                }                for (int j = 0; j < tds.size(); j++)                {                    Record rec = tds.getRecord(j);                    rec.markToBeDeleted();                    rec.save();                }            }            catch (Exception e)            {                throwTorqueException(e);            }            finally            {                if (tds != null)                {                    try                    {                        tds.close();                    }                    catch (Exception ignored)                    {                    }                }            }        }    }    /**     * Method to perform inserts based on values and keys in a     * Criteria.     * <p>     * If the primary key is auto incremented the data in Criteria     * will be inserted and the auto increment value will be returned.     * <p>     * If the primary key is included in Criteria then that value will     * be used to insert the row.     * <p>     * If no primary key is included in Criteria then we will try to     * figure out the primary key from the database map and insert the     * row with the next available id using util.db.IDBroker.     * <p>     * If no primary key is defined for the table the values will be     * inserted as specified in Criteria and -1 will be returned.     *     * @param criteria Object containing values to insert.     * @return An Object which is the id of the row that was inserted     * (if the table has a primary key) or null (if the table does not     * have a primary key).     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static ObjectKey doInsert(Criteria criteria) throws TorqueException    {        Connection con = null;        ObjectKey id = null;        try        {            con = Transaction.beginOptional(                    criteria.getDbName(),                    criteria.isUseTransaction());            id = doInsert(criteria, con);            Transaction.commit(con);        }        catch (TorqueException e)        {            Transaction.safeRollback(con);            throw e;        }        return id;    }    /**     * Method to perform inserts based on values and keys in a     * Criteria.     * <p>     * If the primary key is auto incremented the data in Criteria     * will be inserted and the auto increment value will be returned.     * <p>     * If the primary key is included in Criteria then that value will     * be used to insert the row.     * <p>     * If no primary key is included in Criteria then we will try to     * figure out the primary key from the database map and insert the     * row with the next available id using util.db.IDBroker.     * <p>     * If no primary key is defined for the table the values will be     * inserted as specified in Criteria and null will be returned.     *     * @param criteria Object containing values to insert.     * @param con A Connection.     * @return An Object which is the id of the row that was inserted     * (if the table has a primary key) or null (if the table does not     * have a primary key).     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static ObjectKey doInsert(Criteria criteria, Connection con)        throws TorqueException    {        SimpleKey id = null;        // Get the table name and method for determining the primary        // key value.        String tableName = null;        Iterator keys = criteria.keySet().iterator();        if (keys.hasNext())        {            tableName = criteria.getTableName((String) keys.next());        }        else        {            throw new TorqueException("Database insert attempted without "                    + "anything specified to insert");        }        DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName());        TableMap tableMap = dbMap.getTable(tableName);        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();        IdGenerator keyGen = tableMap.getIdGenerator();        ColumnMap pk = getPrimaryKey(criteria);        // pk will be null if there is no primary key defined for the table        // we're inserting into.        if (pk != null && !criteria.containsKey(pk.getFullyQualifiedName()))        {            if (keyGen == null)            {                throw new TorqueException(                    "IdGenerator for table '" + tableName + "' is null");            }            // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id            // before the insert.            if (keyGen.isPriorToInsert())            {                try                {                    if (pk.getType() instanceof Number)                    {                        id = new NumberKey(                                keyGen.getIdAsBigDecimal(con, keyInfo));                    }                    else                    {                        id = new StringKey(keyGen.getIdAsString(con, keyInfo));                    }                }                catch (Exception e)                {                    throwTorqueException(e);                }                criteria.add(pk.getFullyQualifiedName(), id);            }        }        // Use Village to perform the insert.        TableDataSet tds = null;        try        {            tds = new TableDataSet(con, tableName);            Record rec = tds.addRecord();            BasePeer.insertOrUpdateRecord(rec, tableName, criteria);        }        catch (Exception e)        {            throwTorqueException(e);        }        finally        {            if (tds != null)            {                try                {                    tds.close();                }                catch (Exception e)                {                    throwTorqueException(e);                }            }        }        // If the primary key column is auto-incremented, get the id        // now.        if (pk != null && keyGen != null && keyGen.isPostInsert())        {            try            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -