📄 basepeer.java
字号:
// 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 { if (qds != null) { try { qds.close(); } catch (Exception ignored) { } } } 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 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. * @param con A Connection. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static void doUpdate( 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(); Iterator it = selectCriteria.keySet().iterator(); while (it.hasNext()) { tables.add(selectCriteria.getTableName((String) it.next())); } Iterator tabIt = tables.iterator(); while (tabIt.hasNext()) { String tab = (String) tabIt.next(); KeyDef kd = new KeyDef(); HashSet whereClause = new HashSet(); DatabaseMap tempDbMap = dbMap; ColumnMap[] columnMaps = tempDbMap.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 (selectCriteria.containsKey(key)) { if (selectCriteria .getComparison(key) .equals(Criteria.CUSTOM)) { whereClause.add(selectCriteria.getString(key)); } else { whereClause.add( SqlExpression.build( colMap.getColumnName(), selectCriteria.getValue(key), selectCriteria.getComparison(key), selectCriteria.isIgnoreCase(),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -