📄 basepeer.java
字号:
* Method for performing a SELECT. * * @param queryString A String with the sql statement to execute. * @param start The first row to return. * @param numberOfResults The number of rows to return. * @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, int start, int numberOfResults, String dbName, boolean singleRecord) throws TorqueException { Connection con = null; List results = null; try { con = Torque.getConnection(dbName); // execute the query results = executeQuery( queryString, start, numberOfResults, singleRecord, con); } finally { Torque.closeConnection(con); } return results; } /** * Method for performing a SELECT. Returns all results. * * @param queryString A String with the sql statement to execute. * @param start The first row to return. * @param numberOfResults The number of rows to return. * @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, int start, int numberOfResults, boolean singleRecord, Connection con) throws TorqueException { QueryDataSet qds = null; List results = Collections.EMPTY_LIST; try { // execute the query long startTime = System.currentTimeMillis(); qds = new QueryDataSet(con, queryString); if (log.isDebugEnabled()) { log.debug("Elapsed time=" + (System.currentTimeMillis() - startTime) + " ms"); } results = getSelectResults( qds, start, numberOfResults, singleRecord); } catch (Exception e) { throwTorqueException(e); } finally { VillageUtils.close(qds); } return results; } /** * Returns all records in a QueryDataSet as a List of Record * objects. Used for functionality like util.LargeSelect. * * @see #getSelectResults(QueryDataSet, int, int, boolean) * @param qds the QueryDataSet * @return a List of Record objects * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static List getSelectResults(QueryDataSet qds) throws TorqueException { return getSelectResults(qds, 0, -1, false); } /** * Returns all records in a QueryDataSet as a List of Record * objects. Used for functionality like util.LargeSelect. * * @see #getSelectResults(QueryDataSet, int, int, boolean) * @param qds the QueryDataSet * @param singleRecord * @return a List of Record objects * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static List getSelectResults(QueryDataSet qds, boolean singleRecord) throws TorqueException { return getSelectResults(qds, 0, -1, singleRecord); } /** * Returns numberOfResults records in a QueryDataSet as a List * of Record objects. Starting at record 0. Used for * functionality like util.LargeSelect. * * @see #getSelectResults(QueryDataSet, int, int, boolean) * @param qds the QueryDataSet * @param numberOfResults * @param singleRecord * @return a List of Record objects * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static List getSelectResults( QueryDataSet qds, int numberOfResults, boolean singleRecord) throws TorqueException { List results = null; if (numberOfResults != 0) { results = getSelectResults(qds, 0, numberOfResults, singleRecord); } return results; } /** * Returns numberOfResults records in a QueryDataSet as a List * of Record objects. Starting at record start. Used for * functionality like util.LargeSelect. * * @param qds The <code>QueryDataSet</code> to extract results * from. * @param start The index from which to start retrieving * <code>Record</code> objects from the data set. * @param numberOfResults The number of results to return (or * <code> -1</code> for all results). * @param singleRecord Whether or not we want to select only a * single record. * @return A <code>List</code> of <code>Record</code> objects. * @exception TorqueException If any <code>Exception</code> occurs. */ public static List getSelectResults( QueryDataSet qds, int start, int numberOfResults, boolean singleRecord) throws TorqueException { List results = null; try { if (numberOfResults <= 0) { results = new ArrayList(); qds.fetchRecords(); } else { results = new ArrayList(numberOfResults); qds.fetchRecords(start, numberOfResults); } if (qds.size() > 1 && singleRecord) { handleMultipleRecords(qds); } int startRecord = 0; //Offset the correct number of people if (start > 0 && numberOfResults <= 0) { startRecord = start; } // Return a List of Record objects. for (int i = startRecord; i < qds.size(); i++) { Record rec = qds.getRecord(i); results.add(rec); } } catch (Exception e) { throwTorqueException(e); } return results; } /** * Helper method which returns the primary key contained * in the given Criteria object. * * @param criteria A Criteria. * @return ColumnMap if the Criteria object contains a primary * key, or null if it doesn't. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ private static ColumnMap getPrimaryKey(Criteria criteria) throws TorqueException { // Assume all the keys are for the same table. String key = (String) criteria.keys().nextElement(); String table = criteria.getTableName(key); ColumnMap pk = null; if (!table.equals("")) { DatabaseMap dbMap = Torque.getDatabaseMap(criteria.getDbName()); if (dbMap == null) { throw new TorqueException("dbMap is null"); } if (dbMap.getTable(table) == null) { throw new TorqueException("dbMap.getTable() is null"); } ColumnMap[] columns = dbMap.getTable(table).getColumns(); for (int i = 0; i < columns.length; i++) { if (columns[i].isPrimaryKey()) { pk = columns[i]; break; } } } return pk; } /** * Convenience method used to update rows in the DB. Checks if a * <i>single</i> int primary key is specified in the Criteria * object and uses it to perform the udpate. If no primary key is * specified an Exception will be thrown. * <p> * Use this method for performing an update of the kind: * <p> * "WHERE primary_key_id = an int" * <p> * To perform an update with non-primary key fields in the WHERE * clause use doUpdate(criteria, criteria). * * @param updateValues A Criteria object containing values used in * set clause. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static void doUpdate(Criteria updateValues) throws TorqueException { Connection con = null; try { con = Transaction.beginOptional( updateValues.getDbName(), updateValues.isUseTransaction()); doUpdate(updateValues, con); Transaction.commit(con); } catch (TorqueException e) { Transaction.safeRollback(con); throw e; } } /** * Convenience method used to update rows in the DB. Checks if a * <i>single</i> int primary key is specified in the Criteria * object and uses it to perform the udpate. If no primary key is * specified an Exception will be thrown. * <p> * Use this method for performing an update of the kind: * <p> * "WHERE primary_key_id = an int" * <p> * To perform an update with non-primary key fields in the WHERE * clause use doUpdate(criteria, criteria). * * @param updateValues A Criteria object containing values used in * set clause. * @param con A Connection. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static void doUpdate(Criteria updateValues, Connection con) throws TorqueException { ColumnMap pk = getPrimaryKey(updateValues); Criteria selectCriteria = null; if (pk != null && updateValues.containsKey(pk.getFullyQualifiedName())) { selectCriteria = new Criteria(2); selectCriteria.put(pk.getFullyQualifiedName(), updateValues.remove(pk.getFullyQualifiedName())); } else { throw new TorqueException("No PK specified for database update"); } doUpdate(selectCriteria, updateValues, con); } /** * Method used to update rows in the DB. Rows are selected based * on selectCriteria and updated using values in updateValues. * <p> * Use this method for performing an update of the kind: * <p> * WHERE some_column = some value AND could_have_another_column = * another value AND so on... * * @param selectCriteria A Criteria object containing values used in where * clause. * @param updateValues A Criteria object containing values used in set * clause. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static void doUpdate(Criteria selectCriteria, Criteria updateValues) throws TorqueException { Connection con = null; try { con = Transaction.beginOptional( selectCriteria.getDbName(), updateValues.isUseTransaction()); doUpdate(selectCriteria, updateValues, con); Transaction.commit(con); } catch (TorqueException e) { Transaction.safeRollback(con); throw e; } } /** * Method used to update rows in the DB. Rows are selected based * on criteria and updated using values in updateValues. * <p> * Use this method for performing an update of the kind: * <p> * WHERE some_column = some value AND could_have_another_column = * another value AND so on. * * @param criteria A Criteria object containing values used in where * clause. * @param updateValues A Criteria object containing values used in set * clause. * @param con A Connection. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static void doUpdate( Criteria criteria, final Criteria updateValues, Connection con) throws TorqueException { String dbName = criteria.getDbName(); DB db = Torque.getDB(dbName); DatabaseMap dbMap = Torque.getDatabaseMap(dbName); Set tables = SQLBuilder.getTableSet(criteria, null); try { processTables(criteria, tables, con, new ProcessCallback() { public void process (String table, String dbName, Record rec) throws Exception { // Callback must be called with table name without Schema! BasePeer.insertOrUpdateRecord(rec, table, dbName, updateValues); } }); } catch (Exception e) { throwTorqueException(e); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -