📄 basepeer.java
字号:
package org.apache.torque.util;/* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact apache@apache.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. */import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;import java.math.BigDecimal;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Collections;import java.util.HashSet;import java.util.Hashtable;import java.util.Iterator;import java.util.List;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.apache.torque.Torque;import org.apache.torque.TorqueException;import org.apache.torque.adapter.DB;import org.apache.torque.map.ColumnMap;import org.apache.torque.map.DatabaseMap;import org.apache.torque.map.MapBuilder;import org.apache.torque.map.TableMap;import org.apache.torque.oid.IdGenerator;import org.apache.torque.om.NumberKey;import org.apache.torque.om.ObjectKey;import org.apache.torque.om.SimpleKey;import org.apache.torque.om.StringKey;import com.workingdogs.village.Column;import com.workingdogs.village.DataSet;import com.workingdogs.village.KeyDef;import com.workingdogs.village.QueryDataSet;import com.workingdogs.village.Record;import com.workingdogs.village.Schema;import com.workingdogs.village.TableDataSet;/** * This is the base class for all Peer classes in the system. Peer * classes are responsible for isolating all of the database access * for a specific business object. They execute all of the SQL * against the database. Over time this class has grown to include * utility methods which ease execution of cross-database queries and * the implementation of concrete Peers. * * @author <a href="mailto:frank.kim@clearink.com">Frank Y. Kim</a> * @author <a href="mailto:jmcnally@collab.net">John D. McNally</a> * @author <a href="mailto:bmclaugh@algx.net">Brett McLaughlin</a> * @author <a href="mailto:stephenh@chase3000.com">Stephen Haberman</a> * @author <a href="mailto:mpoeschl@marmot.at">Martin Poeschl</a> * @version $Id: BasePeer.java,v 1.76 2003/08/25 16:33:22 henning Exp $ */public abstract class BasePeer implements java.io.Serializable{ /** Constant criteria key to reference ORDER BY columns. */ public static final String ORDER_BY = "ORDER BY"; /** * Constant criteria key to remove Case Information from * search/ordering criteria. */ public static final String IGNORE_CASE = "IgNOrE cAsE"; /** Classes that implement this class should override this value. */ public static final String TABLE_NAME = "TABLE_NAME"; /** * The Torque default MapBuilder. * * @deprecated there is no default map builder! */ public static final String DEFAULT_MAP_BUILDER = "org.apache.torque.util.db.map.TurbineMapBuilder"; /** Hashtable that contains the cached mapBuilders. */ private static Hashtable mapBuilders = new Hashtable(5); /** the log */ protected static Log log = LogFactory.getLog(BasePeer.class); /** * Converts a hashtable to a byte array for storage/serialization. * * @param hash The Hashtable to convert. * @return A byte[] with the converted Hashtable. * @throws TorqueException Any exceptions caught during processing will be * rethrown wrapped into a TorqueException. */ public static byte[] hashtableToByteArray(Hashtable hash) throws TorqueException { Hashtable saveData = new Hashtable(hash.size()); String key = null; Object value = null; byte[] byteArray = null; Iterator keys = hash.keySet().iterator(); while (keys.hasNext()) { key = (String) keys.next(); value = hash.get(key); if (value instanceof Serializable) { saveData.put(key, value); } } ByteArrayOutputStream baos = null; BufferedOutputStream bos = null; ObjectOutputStream out = null; try { // These objects are closed in the finally. baos = new ByteArrayOutputStream(); bos = new BufferedOutputStream(baos); out = new ObjectOutputStream(bos); out.writeObject(saveData); out.flush(); bos.flush(); byteArray = baos.toByteArray(); } catch (Exception e) { throwTorqueException(e); } finally { if (out != null) { try { out.close(); } catch (IOException ignored) { } } if (bos != null) { try { bos.close(); } catch (IOException ignored) { } } if (baos != null) { try { baos.close(); } catch (IOException ignored) { } } } return byteArray; } private static void throwTorqueException(Exception e) throws TorqueException { if (e instanceof TorqueException) { throw (TorqueException)e; } else { throw new TorqueException(e); } } /** * Sets up a Schema for a table. This schema is then normally * used as the argument for initTableColumns(). * * @param tableName The name of the table. * @return A Schema. */ public static Schema initTableSchema(String tableName) { return initTableSchema(tableName, Torque.getDefaultDB()); } /** * Sets up a Schema for a table. This schema is then normally * used as the argument for initTableColumns * * @param tableName The propery name for the database in the * configuration file. * @param dbName The name of the database. * @return A Schema. */ public static Schema initTableSchema(String tableName, String dbName) { Schema schema = null; Connection con = null; try { con = Torque.getConnection(dbName); schema = new Schema().schema(con, tableName); } catch (Exception e) { log.error(e); throw new Error("Error in BasePeer.initTableSchema(" + tableName + "): " + e.getMessage()); } finally { Torque.closeConnection(con); } return schema; } /** * Creates a Column array for a table based on its Schema. * * @param schema A Schema object. * @return A Column[]. */ public static Column[] initTableColumns(Schema schema) { Column[] columns = null; try { int numberOfColumns = schema.numberOfColumns(); columns = new Column[numberOfColumns]; for (int i = 0; i < numberOfColumns; i++) { columns[i] = schema.column(i + 1); } } catch (Exception e) { log.error(e); throw new Error( "Error in BasePeer.initTableColumns(): " + e.getMessage()); } return columns; } /** * Convenience method to create a String array of column names. * * @param columns A Column[]. * @return A String[]. */ public static String[] initColumnNames(Column[] columns) { String[] columnNames = null; columnNames = new String[columns.length]; for (int i = 0; i < columns.length; i++) { columnNames[i] = columns[i].name().toUpperCase(); } return columnNames; } /** * Convenience method to create a String array of criteria keys. * * @param tableName Name of table. * @param columnNames A String[]. * @return A String[]. */ public static String[] initCriteriaKeys( String tableName, String[] columnNames) { String[] keys = new String[columnNames.length]; for (int i = 0; i < columnNames.length; i++) { keys[i] = tableName + "." + columnNames[i].toUpperCase(); } return keys; } /** * Convenience method that uses straight JDBC to delete multiple * rows. Village throws an Exception when multiple rows are * deleted. * * @param con A Connection. * @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( Connection con, String table, String column, int value) throws TorqueException { Statement statement = null; try { statement = con.createStatement(); StringBuffer query = new StringBuffer(); query.append("DELETE FROM ") .append(table) .append(" WHERE ") .append(column) .append(" = ") .append(value); statement.executeUpdate(query.toString()); } catch (SQLException e) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -