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

📄 basepeer.java

📁 另外一种持久性o/m软件
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
     * 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 table = null;        Iterator keys = criteria.keySet().iterator();        if (keys.hasNext())        {            table = criteria.getTableName((String) keys.next());        }        else        {            throw new TorqueException("Database insert attempted without "                    + "anything specified to insert");        }        String dbName = criteria.getDbName();        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);        TableMap tableMap = dbMap.getTable(table);        Object keyInfo = tableMap.getPrimaryKeyMethodInfo();        IdGenerator keyGen = tableMap.getIdGenerator();        ColumnMap pk = getPrimaryKey(criteria);        // If the keyMethod is SEQUENCE or IDBROKERTABLE, get the id        // before the insert.        if (keyGen != null && keyGen.isPriorToInsert())        {            // 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 '" + table + "' is null");                }                                id = getId(pk, keyGen, con, keyInfo);                criteria.add(pk.getFullyQualifiedName(), id);            }        }        // Use Village to perform the insert.        TableDataSet tds = null;        try        {            String tableName = SQLBuilder.getFullTableName(table, dbName);            tds = new TableDataSet(con, tableName);            Record rec = tds.addRecord();            // not the fully qualified name, insertOrUpdateRecord wants to use table as an index...            BasePeer.insertOrUpdateRecord(rec, table, dbName, criteria);        }        catch (Exception e)        {            throwTorqueException(e);        }        finally        {            VillageUtils.close(tds);        }        // If the primary key column is auto-incremented, get the id        // now.        if (keyGen != null && keyGen.isPostInsert())        {            id = getId(pk, keyGen, con, keyInfo);        }        return id;    }    /**     * Create an Id for insertion in the Criteria     *     * @param pk ColumnMap for the Primary key     * @param keyGen The Id Generator object     * @param con The SQL Connection to run the id generation under     * @param keyInfo KeyInfo Parameter from the Table map     *     * @return A simple Key representing the new Id value     * @throws TorqueException Possible errors get wrapped in here.     */    private static SimpleKey getId(ColumnMap pk, IdGenerator keyGen, Connection con, Object keyInfo)            throws TorqueException    {        SimpleKey id = null;        try        {            if (pk != null && keyGen != null)            {                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);        }        return id;    }    /**     * Grouping of code used in both doInsert() and doUpdate()     * methods.  Sets up a Record for saving.     *     * @param rec A Record.     * @param table Name of table.     * @param criteria A Criteria.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    private static void insertOrUpdateRecord(        Record rec,        String table,        String dbName,        Criteria criteria)        throws TorqueException    {        DatabaseMap dbMap = Torque.getDatabaseMap(dbName);        ColumnMap[] columnMaps = dbMap.getTable(table).getColumns();        boolean shouldSave = false;        for (int j = 0; j < columnMaps.length; j++)        {            ColumnMap colMap = columnMaps[j];            String colName = colMap.getColumnName();            String key = new StringBuffer(colMap.getTableName())                    .append('.')                    .append(colName)                    .toString();            if (criteria.containsKey(key))            {                try                {                    VillageUtils.setVillageValue(criteria, key, rec, colName);                    shouldSave = true;                }                catch (Exception e)                {                    throwTorqueException(e);                }            }        }        if (shouldSave)        {            try            {                rec.save();            }            catch (Exception e)            {                throwTorqueException(e);            }        }        else        {            throw new TorqueException("No changes to save");        }    }    /**     * Method to create an SQL query for display only based on values in a     * Criteria.     *     * @param criteria A Criteria.     * @return the SQL query for display     * @exception TorqueException Trouble creating the query string.     */    static String createQueryDisplayString(Criteria criteria)        throws TorqueException    {        return createQuery(criteria).toString();    }    /**     * Method to create an SQL query for actual execution based on values in a     * Criteria.     *     * @param criteria A Criteria.     * @return the SQL query for actual execution     * @exception TorqueException Trouble creating the query string.     */    public static String createQueryString(Criteria criteria)        throws TorqueException    {        Query query = createQuery(criteria);        return query.toString();    }    /**     * Method to create an SQL query based on values in a Criteria.  Note that     * final manipulation of the limit and offset are performed when the query     * is actually executed.     *     * @param criteria A Criteria.     * @return the sql query     * @exception TorqueException Trouble creating the query string.     */    static Query createQuery(Criteria criteria)        throws TorqueException    {        return SQLBuilder.buildQueryClause(criteria, null, new SQLBuilder.QueryCallback() {                public String process(Criteria.Criterion criterion, List params)                {                    return criterion.toString();                }            });    }    /**     * Returns all results.     *     * @param criteria A Criteria.     * @return List of Record objects.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static List doSelect(Criteria criteria) throws TorqueException    {        Connection con = null;        List results = null;        try        {            con = Transaction.beginOptional(                    criteria.getDbName(),                    criteria.isUseTransaction());            results = doSelect(criteria, con);            Transaction.commit(con);        }        catch (TorqueException e)        {            Transaction.safeRollback(con);            throw e;        }        return results;    }    /**     * Returns all results.     *     * @param criteria A Criteria.     * @param con A Connection.     * @return List of Record objects.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static List doSelect(Criteria criteria, Connection con)        throws TorqueException    {        Query query = createQuery(criteria);        if (query.hasLimit())        {            // We don't need Village to limit the Query            return executeQuery(query.toString(),                    0,                    -1,                    criteria.isSingleRecord(),                    con);        }        else        {            // There is no limit string registered            // with the query. Let Village decide.            return executeQuery(query.toString(),                    criteria.getOffset(),                    criteria.getLimit(),                    criteria.isSingleRecord(),                    con);        }    }    /**     * Utility method which executes a given sql statement.  This     * method should be used for select statements only.  Use     * executeStatement for update, insert, and delete operations.     *     * @param queryString A String with the sql statement to execute.     * @return List of Record objects.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static List executeQuery(String queryString) throws TorqueException    {        return executeQuery(queryString, Torque.getDefaultDB(), false);    }    /**     * Utility method which executes a given sql statement.  This     * method should be used for select statements only.  Use     * executeStatement for update, insert, and delete operations.     *     * @param queryString A String with the sql statement to execute.     * @param dbName The database to connect to.     * @return List of Record objects.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static List executeQuery(String queryString, String dbName)        throws TorqueException    {        return executeQuery(queryString, dbName, false);    }    /**     * Method for performing a SELECT.  Returns all results.     *     * @param queryString A String with the sql statement to execute.     * @param dbName The database to connect to.     * @param singleRecord Whether or not we want to select only a     * single record.     * @return List of Record objects.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static List executeQuery(        String queryString,        String dbName,        boolean singleRecord)        throws TorqueException    {        return executeQuery(queryString, 0, -1, dbName, singleRecord);    }    /**     * Method for performing a SELECT.  Returns all results.     *     * @param queryString A String with the sql statement to execute.     * @param singleRecord Whether or not we want to select only a     * single record.     * @param con A Connection.     * @return List of Record objects.     * @throws TorqueException Any exceptions caught during processing will be     *         rethrown wrapped into a TorqueException.     */    public static List executeQuery(        String queryString,        boolean singleRecord,        Connection con)        throws TorqueException    {        return executeQuery(queryString, 0, -1, singleRecord, con);    }    /**

⌨️ 快捷键说明

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