You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tv...@apache.org on 2007/10/31 13:50:47 UTC
svn commit: r590654 [1/3] - in /db/torque/village/trunk: src/ src/java/
src/java/com/ src/java/com/workingdogs/ src/java/com/workingdogs/village/
src/test/ src/test/com/ src/test/com/workingdogs/ xdocs/
Author: tv
Date: Wed Oct 31 05:50:45 2007
New Revision: 590654
URL: http://svn.apache.org/viewvc?rev=590654&view=rev
Log:
Initial import according to vote in
<47...@backstagetech.com.au>
Added:
db/torque/village/trunk/src/
db/torque/village/trunk/src/java/
db/torque/village/trunk/src/java/com/
db/torque/village/trunk/src/java/com/workingdogs/
db/torque/village/trunk/src/java/com/workingdogs/village/
db/torque/village/trunk/src/java/com/workingdogs/village/Column.java
db/torque/village/trunk/src/java/com/workingdogs/village/DataSet.java
db/torque/village/trunk/src/java/com/workingdogs/village/DataSetException.java
db/torque/village/trunk/src/java/com/workingdogs/village/Enums.java
db/torque/village/trunk/src/java/com/workingdogs/village/KeyDef.java
db/torque/village/trunk/src/java/com/workingdogs/village/QueryDataSet.java
db/torque/village/trunk/src/java/com/workingdogs/village/Record.java
db/torque/village/trunk/src/java/com/workingdogs/village/Schema.java
db/torque/village/trunk/src/java/com/workingdogs/village/SelectStmt.java
db/torque/village/trunk/src/java/com/workingdogs/village/TableDataSet.java
db/torque/village/trunk/src/java/com/workingdogs/village/Value.java
db/torque/village/trunk/src/test/
db/torque/village/trunk/src/test/com/
db/torque/village/trunk/src/test/com/workingdogs/
db/torque/village/trunk/xdocs/
db/torque/village/trunk/xdocs/changes.xml
db/torque/village/trunk/xdocs/index.xml
db/torque/village/trunk/xdocs/navigation.xml
Added: db/torque/village/trunk/src/java/com/workingdogs/village/Column.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/Column.java?rev=590654&view=auto
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/Column.java (added)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/Column.java Wed Oct 31 05:50:45 2007
@@ -0,0 +1,556 @@
+package com.workingdogs.village;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.sql.ResultSetMetaData;
+import java.sql.SQLException;
+import java.sql.Types;
+
+/**
+ * This class represents a Column in the database and its associated meta information. A <a href="Record.html">Record</A> is a
+ * collection of columns.
+ *
+ * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
+ * @version $Revision: 568 $
+ */
+public class Column
+{
+ /** column number in a schema object */
+ private int columnNumber = -1;
+
+ /** name of the column */
+ private String name = "";
+
+ /** example: this column is of type "String" */
+ private String columnTypeName = "";
+
+ /** what java.sql.Type is this column? */
+ private int columnType = Types.LONGVARCHAR;
+
+ /** name of table that this column belongs to */
+ private String tableName = "";
+
+ /** is null allowed for this column? */
+ private boolean nullAllowed = false;
+
+ /** is this an auto increment column? */
+ private boolean autoIncrement = false;
+
+ /** is this a read only column? */
+ private boolean readOnly = false;
+
+ /** is this a searchable column? */
+ private boolean searchable = false;
+
+ /** what is the scale of this column? */
+ private int scale = -1;
+
+ /** what is the precision of this column? */
+ private int precision = -1;
+
+ /** what is the length of this column? */
+ private int length = -1;
+
+ /**
+ * constructor
+ */
+ public Column()
+ {
+ this.columnNumber = -1;
+ this.name = "";
+ this.columnTypeName = "";
+ this.tableName = "";
+ this.columnType = Types.LONGVARCHAR;
+ this.nullAllowed = false;
+ this.autoIncrement = false;
+ this.readOnly = false;
+ this.searchable = false;
+ this.scale = -1;
+ this.precision = -1;
+ this.length = -1;
+ }
+
+ /**
+ * internal package method for populating a Column instance
+ *
+ * @param rsmd TODO: DOCUMENT ME!
+ * @param colNum TODO: DOCUMENT ME!
+ * @param tableName TODO: DOCUMENT ME!
+ *
+ * @throws SQLException TODO: DOCUMENT ME!
+ */
+ void populate(ResultSetMetaData rsmd, int colNum, String tableName)
+ throws SQLException
+ {
+ this.columnNumber = colNum;
+ this.name = rsmd.getColumnName(columnNumber);
+
+ // Workaround for Sybase jConnect 5.2 and older.
+ try
+ {
+ this.tableName = rsmd.getTableName(columnNumber);
+
+ // ResultSetMetaData may report table name as the empty
+ // string when a database-specific function has been
+ // called to generate a Column.
+ if ((this.tableName == null) || this.tableName.equals(""))
+ {
+ if (tableName != null)
+ {
+ this.tableName = tableName;
+ }
+ else
+ {
+ this.tableName = "";
+ }
+ }
+ }
+ catch (RuntimeException e)
+ {
+ if (tableName != null)
+ {
+ this.tableName = tableName;
+ }
+ else
+ {
+ this.tableName = "";
+ }
+ }
+
+ this.columnTypeName = rsmd.getColumnTypeName(columnNumber);
+ this.columnType = rsmd.getColumnType(columnNumber);
+ this.nullAllowed = rsmd.isNullable(columnNumber) == 1;
+ this.autoIncrement = rsmd.isAutoIncrement(columnNumber);
+ this.readOnly = rsmd.isReadOnly(columnNumber);
+ this.searchable = rsmd.isSearchable(columnNumber);
+ this.scale = rsmd.getScale(columnNumber);
+
+ try
+ {
+ this.precision = rsmd.getPrecision(columnNumber);
+ }
+ catch (NumberFormatException assumedTooLarge)
+ {
+ // This may happen if the precision is too large for an
+ // int, with column types such as MySQL BIGINT, Oracle
+ // BLOB, etc.. See bug #4625851 at the JDC for details.
+ this.precision = Integer.MAX_VALUE;
+ }
+
+ this.length = rsmd.getColumnDisplaySize(columnNumber);
+ }
+
+ /**
+ * the name of the column
+ *
+ * @return the name of the column
+ */
+ public String name()
+ {
+ return this.name;
+ }
+
+ /**
+ * the data type of a column
+ *
+ * @return the java.sql.Types String
+ */
+ public String dbType()
+ {
+ return this.columnTypeName;
+ }
+
+ /**
+ * the data type of a column
+ *
+ * @return the java.sql.Types enum
+ */
+ public int typeEnum()
+ {
+ return this.columnType;
+ }
+
+ /**
+ * does this column allow null?
+ *
+ * @return whether or not the column has null Allowed
+ */
+ public boolean nullAllowed()
+ {
+ return this.nullAllowed;
+ }
+
+ /**
+ * does this column auto increment?
+ *
+ * @return whether or not this column auto increments
+ */
+ public boolean autoIncrement()
+ {
+ return this.autoIncrement;
+ }
+
+ /**
+ * is this column read only?
+ *
+ * @return whether or not this column is read only
+ */
+ public boolean readOnly()
+ {
+ return this.readOnly;
+ }
+
+ /**
+ * is this column searchable?
+ *
+ * @return true if this column is searchable
+ */
+ public boolean searchable()
+ {
+ return this.searchable;
+ }
+
+ /**
+ * the scale of the column
+ *
+ * @return the scale of the column
+ */
+ public int scale()
+ {
+ return this.scale;
+ }
+
+ /**
+ * the precision of the column
+ *
+ * @return the precision of the column
+ */
+ public int precision()
+ {
+ return this.precision;
+ }
+
+ /**
+ * the storage length of a column
+ *
+ * @return the storage length of a column
+ */
+ public int length()
+ {
+ return this.length;
+ }
+
+ /**
+ * the type of the column as a string
+ *
+ * @return the type of the column as a string
+ */
+ public String type()
+ {
+ if (isBoolean())
+ {
+ return "BOOLEAN";
+ }
+ else if (isByte())
+ {
+ return "BYTE";
+ }
+ else if (isShort())
+ {
+ return "SHORT";
+ }
+ else if (isInt())
+ {
+ return "INTEGER";
+ }
+ else if (isLong())
+ {
+ return "LONG";
+ }
+ else if (isFloat())
+ {
+ return "FLOAT";
+ }
+ else if (isDouble())
+ {
+ return "DOUBLE";
+ }
+ else if (isBigDecimal())
+ {
+ return "BIGDECIMAL";
+ }
+ else if (isDate())
+ {
+ return "DATE";
+ }
+ else if (isTime())
+ {
+ return "TIME";
+ }
+ else if (isTimestamp())
+ {
+ return "TIMESTAMP";
+ }
+ else if (isString())
+ {
+ return "STRING";
+ }
+ else if (isBinary())
+ {
+ return "BINARY";
+ }
+ else if (isVarBinary())
+ {
+ return "VARBINARY";
+ }
+ else if (isLongVarBinary())
+ {
+ return "LONGVARBINARY";
+ }
+
+ return "UNKNOWN TYPE: " + typeEnum();
+ }
+
+ /**
+ * column isBoolean: -7
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isBoolean()
+ {
+ return this.typeEnum() == Types.BIT;
+ }
+
+ /**
+ * column isBigDecimal: 2 || 3
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isBigDecimal()
+ {
+ return (this.typeEnum() == Types.NUMERIC) || (this.typeEnum() == Types.DECIMAL);
+ }
+
+ /**
+ * column isBinary: -2
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isBinary()
+ {
+ return this.typeEnum() == Types.BINARY;
+ }
+
+ /**
+ * column isByte: -6
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isByte()
+ {
+ return this.typeEnum() == Types.TINYINT;
+ }
+
+ /**
+ * column isBytes: -4 || -3 || -2
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isBytes()
+ {
+ return (this.typeEnum() == Types.LONGVARBINARY)
+ || (this.typeEnum() == Types.VARBINARY)
+ || (this.columnType == Types.BINARY);
+ }
+
+ /**
+ * column isBytes: 91
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isDate()
+ {
+ return this.typeEnum() == Types.DATE;
+ }
+
+ /**
+ * column isDouble: 6 || 8
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isDouble()
+ {
+ return (this.typeEnum() == Types.FLOAT) || (this.typeEnum() == Types.DOUBLE);
+ }
+
+ /**
+ * column isFloat: 7
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isFloat()
+ {
+ return this.typeEnum() == Types.REAL;
+ }
+
+ /**
+ * column isInt: 4
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isInt()
+ {
+ return this.typeEnum() == Types.INTEGER;
+ }
+
+ /**
+ * column isLong: -5
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isLong()
+ {
+ return this.typeEnum() == Types.BIGINT;
+ }
+
+ /**
+ * column isShort: 5
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isShort()
+ {
+ return this.typeEnum() == Types.SMALLINT;
+ }
+
+ /**
+ * column isString: -1 || -11 || 12
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isString()
+ {
+ return (this.typeEnum() == Types.LONGVARCHAR)
+ || (this.typeEnum() == Types.VARCHAR)
+ || (this.typeEnum() == 11);
+ }
+
+ /**
+ * column isTime: 92
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isTime()
+ {
+ return this.typeEnum() == Types.TIME;
+ }
+
+ /**
+ * column isTimestamp: 93
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isTimestamp()
+ {
+ return this.typeEnum() == Types.TIMESTAMP;
+ }
+
+ /**
+ * column isVarBinary: -3
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isVarBinary()
+ {
+ return this.typeEnum() == Types.VARBINARY;
+ }
+
+ /**
+ * column isLongVarBinary: -4
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public boolean isLongVarBinary()
+ {
+ return this.typeEnum() == Types.LONGVARBINARY;
+ }
+
+ /**
+ * unknown use
+ *
+ * @return TODO: DOCUMENT ME!
+ *
+ * @throws DataSetException TODO: DOCUMENT ME!
+ */
+ public String dbKonaMethod()
+ throws DataSetException
+ {
+ throw new DataSetException("Method not implemented: Unknown use!");
+ }
+
+ /**
+ * unknown use
+ *
+ * @return TODO: DOCUMENT ME!
+ *
+ * @throws DataSetException TODO: DOCUMENT ME!
+ */
+ public String javaType()
+ throws DataSetException
+ {
+ throw new DataSetException("Method not implemented: Unknown use!");
+ }
+
+ /**
+ * unknown use
+ *
+ * @return TODO: DOCUMENT ME!
+ *
+ * @throws DataSetException TODO: DOCUMENT ME!
+ */
+ public final String preparedStatemntBindMethod()
+ throws DataSetException
+ {
+ throw new DataSetException("Method not implemented: Unknown use!");
+ }
+
+ /**
+ * unknown use
+ *
+ * @return TODO: DOCUMENT ME!
+ *
+ * @throws DataSetException TODO: DOCUMENT ME!
+ */
+ public final String resultSetMethod()
+ throws DataSetException
+ {
+ throw new DataSetException("Method not implemented: Unknown use!");
+ }
+
+ /**
+ * TODO: DOCUMENT ME!
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public String getTableName()
+ {
+ return tableName;
+ }
+}
Added: db/torque/village/trunk/src/java/com/workingdogs/village/DataSet.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/DataSet.java?rev=590654&view=auto
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/DataSet.java (added)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/DataSet.java Wed Oct 31 05:50:45 2007
@@ -0,0 +1,729 @@
+package com.workingdogs.village;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.io.ByteArrayOutputStream;
+import java.io.PrintWriter;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import java.util.Vector;
+
+/**
+ * The DataSet represents a table in the database. It is extended by <a href="QueryDataSet.html">QueryDataSet</a> and <a
+ * href="TableDataSet.html">TableDataSet</a> and should not be used directly. A DataSet contains a <a
+ * href="Schema.html">Schema</a> and potentially a collection of <a href="Record.html">Records</a>.
+ *
+ * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
+ * @version $Revision: 568 $
+ */
+public abstract class DataSet
+{
+ /** indicates that all records should be retrieved during a fetch */
+ protected static final int ALL_RECORDS = -1;
+
+ /** this DataSet's schema object */
+ protected Schema schema;
+
+ /** this DataSet's collection of Record objects */
+ protected Vector records = null;
+
+ /** this DataSet's connection object */
+ protected Connection conn;
+
+ /** have all records been retrieved with the fetchRecords? */
+ private boolean allRecordsRetrieved = false;
+
+ /** number of records retrieved */
+ private int recordRetrievedCount = 0;
+
+ /** number of records that were last fetched */
+ private int lastFetchSize = 0;
+
+ /** the columns in the SELECT statement for this DataSet */
+ private String columns;
+
+ /** the select string that was used to build this DataSet */
+ protected StringBuffer selectString;
+
+ /** the KeyDef for this DataSet */
+ private KeyDef keyDefValue;
+
+ /** the result set for this DataSet */
+ protected ResultSet resultSet;
+
+ /** the Statement for this DataSet */
+ protected Statement stmt;
+
+ /**
+ * Private, not used
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ public DataSet()
+ throws DataSetException, SQLException
+ {
+ }
+
+ /**
+ * Create a new DataSet with a connection and a Table name
+ *
+ * @param conn
+ * @param tableName
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ DataSet(Connection conn, String tableName)
+ throws DataSetException, SQLException
+ {
+ this.conn = conn;
+ this.columns = "*";
+ this.schema = new Schema().schema(conn, tableName);
+ }
+
+ /**
+ * Create a new DataSet with a connection, schema and KeyDef
+ *
+ * @param conn
+ * @param schema
+ * @param keydef
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ DataSet(Connection conn, Schema schema, KeyDef keydef)
+ throws DataSetException, SQLException
+ {
+ if (conn == null)
+ {
+ throw new SQLException("Database connection could not be established!");
+ }
+ else if (schema == null)
+ {
+ throw new DataSetException("You need to specify a valid schema!");
+ }
+ else if (keydef == null)
+ {
+ throw new DataSetException("You need to specify a valid KeyDef!");
+ }
+
+ this.conn = conn;
+ this.schema = schema;
+ this.columns = "*";
+
+ this.keyDefValue = keydef;
+ }
+
+ /**
+ * Create a new DataSet with a connection, tablename and KeyDef
+ *
+ * @param conn
+ * @param tableName
+ * @param keydef
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ DataSet(Connection conn, String tableName, KeyDef keydef)
+ throws SQLException, DataSetException
+ {
+ this.conn = conn;
+ this.keyDefValue = keydef;
+ this.columns = "*";
+ this.schema = new Schema().schema(conn, tableName);
+ }
+
+ /**
+ * Create a new DataSet with a connection, tablename and list of columns
+ *
+ * @param conn
+ * @param tableName
+ * @param columns
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ DataSet(Connection conn, String tableName, String columns)
+ throws SQLException, DataSetException
+ {
+ this.conn = conn;
+ this.columns = columns;
+ this.schema = new Schema().schema(conn, tableName, columns);
+ }
+
+ /**
+ * Create a new DataSet with a connection, tableName, columns and a KeyDef
+ *
+ * @param conn
+ * @param tableName
+ * @param columns
+ * @param keyDef
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ DataSet(Connection conn, String tableName, String columns, KeyDef keyDef)
+ throws SQLException, DataSetException
+ {
+ this.conn = conn;
+ this.columns = columns;
+ this.keyDefValue = keyDef;
+ this.schema = new Schema().schema(conn, tableName, columns);
+ }
+
+ /**
+ * Gets the ResultSet for this DataSet
+ *
+ * @return the result set for this DataSet
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public ResultSet resultSet()
+ throws SQLException, DataSetException
+ {
+ if (this.resultSet == null)
+ {
+ throw new DataSetException("ResultSet is null.");
+ }
+
+ return this.resultSet;
+ }
+
+ /**
+ * Calls addRecord(DataSet)
+ *
+ * @return the added record
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ public Record addRecord()
+ throws DataSetException, SQLException
+ {
+ return addRecord(this);
+ }
+
+ /**
+ * Creates a new Record within this DataSet
+ *
+ * @param ds
+ *
+ * @return the added record
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ public Record addRecord(DataSet ds)
+ throws DataSetException, SQLException
+ {
+ if (ds instanceof QueryDataSet)
+ {
+ throw new DataSetException("You cannot add records to a QueryDataSet.");
+ }
+
+ if (records == null)
+ {
+ records = new Vector(10);
+ }
+
+ Record rec = new Record(ds, true);
+ rec.markForInsert();
+ records.addElement(rec);
+
+ return rec;
+ }
+
+ /**
+ * Check if all the records have been retrieve
+ *
+ * @return true if all records have been retrieved
+ */
+ public boolean allRecordsRetrieved()
+ {
+ return this.allRecordsRetrieved;
+ }
+
+ /**
+ * Set all records retrieved
+ *
+ * @param set TODO: DOCUMENT ME!
+ */
+ void setAllRecordsRetrieved(boolean set)
+ {
+ this.allRecordsRetrieved = set;
+ }
+
+ /**
+ * Remove a record from the DataSet's internal storage
+ *
+ * @param rec
+ *
+ * @return the record removed
+ *
+ * @exception DataSetException
+ */
+ public Record removeRecord(Record rec)
+ throws DataSetException
+ {
+ Record removeRec = null;
+
+ try
+ {
+ int loc = this.records.indexOf(rec);
+ removeRec = (Record) this.records.elementAt(loc);
+ this.records.removeElementAt(loc);
+ }
+ catch (Exception e)
+ {
+ throw new DataSetException("Record could not be removed!");
+ }
+
+ return removeRec;
+ }
+
+ /**
+ * Remove all records from the DataSet and nulls those records out and close() the DataSet.
+ *
+ * @return an instance of myself
+ */
+ public DataSet clearRecords()
+ {
+ this.records.removeAllElements();
+ this.records = null;
+
+ return this;
+ }
+
+ /**
+ * Removes the records from the DataSet, but does not null the records out
+ *
+ * @return an instance of myself
+ */
+ public DataSet releaseRecords()
+ {
+ this.records = null;
+ this.recordRetrievedCount = 0;
+ this.lastFetchSize = 0;
+ setAllRecordsRetrieved(false);
+
+ return this;
+ }
+
+ /**
+ * Releases the records, closes the ResultSet and the Statement, and nulls the Schema and Connection references.
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public void close()
+ throws SQLException, DataSetException
+ {
+ releaseRecords();
+ this.schema = null;
+
+ if ((this.resultSet != null) && !(this instanceof QueryDataSet))
+ {
+ resultSet().close();
+ }
+
+ this.resultSet = null;
+
+ if (this.stmt != null)
+ {
+ this.stmt.close();
+ }
+
+ this.conn = null;
+ }
+
+ /**
+ * Essentially the same as releaseRecords, but it won't work on a QueryDataSet that has been created with a ResultSet
+ *
+ * @return an instance of myself
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ public DataSet reset()
+ throws DataSetException, SQLException
+ {
+ if (!((resultSet() != null) && (this instanceof QueryDataSet)))
+ {
+ return releaseRecords();
+ }
+ else
+ {
+ throw new DataSetException("You cannot call reset() on a QueryDataSet.");
+ }
+ }
+
+ /**
+ * Gets the current database connection
+ *
+ * @return a database connection
+ *
+ * @exception SQLException
+ */
+ public Connection connection()
+ throws SQLException
+ {
+ return this.conn;
+ }
+
+ /**
+ * Gets the Schema for this DataSet
+ *
+ * @return the Schema for this DataSet
+ */
+ public Schema schema()
+ {
+ return this.schema;
+ }
+
+ /**
+ * Get Record at 0 based index position
+ *
+ * @param pos
+ *
+ * @return an instance of the found Record
+ *
+ * @exception DataSetException
+ */
+ public Record getRecord(int pos)
+ throws DataSetException
+ {
+ if (containsRecord(pos))
+ {
+ Record rec = (Record) this.records.elementAt(pos);
+
+ if (this instanceof TableDataSet)
+ {
+ rec.markForUpdate();
+ }
+
+ recordRetrievedCount++;
+
+ return rec;
+ }
+
+ throw new DataSetException("Record not found at index: " + pos);
+ }
+
+ /**
+ * Find Record at 0 based index position. This is an internal alternative to getRecord which tries to be smart about the type
+ * of record it is.
+ *
+ * @param pos
+ *
+ * @return an instance of the found Record
+ *
+ * @exception DataSetException
+ */
+ Record findRecord(int pos)
+ throws DataSetException
+ {
+ if (containsRecord(pos))
+ {
+ return (Record) this.records.elementAt(pos);
+ }
+
+ throw new DataSetException("Record not found at index: " + pos);
+ }
+
+ /**
+ * Check to see if the DataSet contains a Record at 0 based position
+ *
+ * @param pos
+ *
+ * @return true if record exists
+ */
+ public boolean containsRecord(int pos)
+ {
+ try
+ {
+ if (this.records.elementAt(pos) != null)
+ {
+ return true;
+ }
+ }
+ catch (Exception e)
+ {
+ return false;
+ }
+
+ return false;
+ }
+
+ /**
+ * Causes the DataSet to hit the database and fetch all the records.
+ *
+ * @return an instance of myself
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public DataSet fetchRecords()
+ throws SQLException, DataSetException
+ {
+ return fetchRecords(ALL_RECORDS);
+ }
+
+ /**
+ * Causes the DataSet to hit the database and fetch max records.
+ *
+ * @param max
+ *
+ * @return an instance of myself
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public DataSet fetchRecords(int max)
+ throws SQLException, DataSetException
+ {
+ return fetchRecords(0, max);
+ }
+
+ /**
+ * Causes the DataSet to hit the database and fetch max records, starting at start. Record count begins at 0.
+ *
+ * @param start
+ * @param max
+ *
+ * @return an instance of myself
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public DataSet fetchRecords(int start, int max)
+ throws SQLException, DataSetException
+ {
+ if (max == 0)
+ {
+ throw new DataSetException("Max is 1 based and must be greater than 0!");
+ }
+ else if ((lastFetchSize() > 0) && (this.records != null))
+ {
+ throw new DataSetException("You must call DataSet.clearRecords() before executing DataSet.fetchRecords() again!");
+ }
+
+ if (selectString == null)
+ {
+ selectString = new StringBuffer(256);
+ selectString.append("SELECT ");
+ selectString.append(schema().attributes());
+ selectString.append(" FROM ");
+ selectString.append(schema().tableName());
+ }
+
+ try
+ {
+ if ((stmt == null) && (this.resultSet == null))
+ {
+ stmt = connection().createStatement();
+ this.resultSet = stmt.executeQuery(selectString.toString());
+ }
+
+ if (this.resultSet != null)
+ {
+ if ((this.records == null) && (max > 0))
+ {
+ this.records = new Vector(max);
+ }
+ else
+ {
+ this.records = new Vector();
+ }
+
+ int startCounter = 0;
+ int fetchCount = 0;
+
+ while (!allRecordsRetrieved())
+ {
+ if (fetchCount == max)
+ {
+ break;
+ }
+
+ if (this.resultSet.next())
+ {
+ if (startCounter >= start)
+ {
+ Record rec = new Record(this);
+ records.addElement(rec);
+ fetchCount++;
+ }
+ else
+ {
+ startCounter++;
+ }
+ }
+ else
+ {
+ setAllRecordsRetrieved(true);
+
+ break;
+ }
+ }
+
+ lastFetchSize = fetchCount;
+ }
+ }
+ catch (SQLException e)
+ {
+ if (stmt != null)
+ {
+ stmt.close();
+ }
+
+ throw new SQLException(e.getMessage());
+ }
+
+ return this;
+ }
+
+ /**
+ * The number of records that were fetched with the last fetchRecords.
+ *
+ * @return int
+ */
+ public int lastFetchSize()
+ {
+ return lastFetchSize;
+ }
+
+ /**
+ * gets the KeyDef object for this DataSet
+ *
+ * @return the keydef for this DataSet, this value can be null
+ */
+ public KeyDef keydef()
+ {
+ return this.keyDefValue;
+ }
+
+ /**
+ * This returns a represention of this DataSet
+ *
+ * @return TODO: DOCUMENT ME!
+ */
+ public String toString()
+ {
+ try
+ {
+ ByteArrayOutputStream bout = new ByteArrayOutputStream();
+ PrintWriter out = new PrintWriter(bout);
+
+ if (schema != null)
+ {
+ out.println(schema.toString());
+ }
+
+ for (int i = 0; i < size(); i++)
+ {
+ out.println(findRecord(i));
+ }
+
+ out.flush();
+
+ return bout.toString();
+ }
+ catch (DataSetException e)
+ {
+ return "{}";
+ }
+ }
+
+ /**
+ * Gets the tableName defined in the schema
+ *
+ * @return string
+ *
+ * @throws DataSetException TODO: DOCUMENT ME!
+ */
+ public String tableName()
+ throws DataSetException
+ {
+ return schema().tableName();
+ }
+
+ /**
+ * Calculates the maxColumnWidths for the column in a DataSet I really don't know what this is used for so it isn't
+ * implemented.
+ *
+ * @param with_heading
+ *
+ * @return int
+ *
+ * @exception DataSetException
+ * @exception SQLException
+ */
+ public int [] maxColumnWidths(boolean with_heading)
+ throws DataSetException, SQLException
+ {
+ if (schema() == null)
+ {
+ throw new DataSetException("Schema is null!");
+ }
+
+ throw new DataSetException("Not yet implemented!");
+ }
+
+ /**
+ * Classes extending this class must implement this method.
+ *
+ * @return the select string
+ *
+ * @throws DataSetException TODO: DOCUMENT ME!
+ */
+ public abstract String getSelectString()
+ throws DataSetException;
+
+ /**
+ * Returns the columns attribute for the DataSet
+ *
+ * @return the columns attribute for the DataSet
+ */
+ String getColumns()
+ {
+ return this.columns;
+ }
+
+ /**
+ * Gets the number of Records in this DataSet. It is 0 based.
+ *
+ * @return number of Records in this DataSet
+ */
+ public int size()
+ {
+ if (this.records == null)
+ {
+ return 0;
+ }
+
+ return this.records.size();
+ }
+}
Added: db/torque/village/trunk/src/java/com/workingdogs/village/DataSetException.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/DataSetException.java?rev=590654&view=auto
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/DataSetException.java (added)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/DataSetException.java Wed Oct 31 05:50:45 2007
@@ -0,0 +1,51 @@
+package com.workingdogs.village;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * A DataSetException is thrown if there is an error.
+ *
+ * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
+ * @version $Revision: 568 $
+ */
+public class DataSetException
+ extends Exception
+{
+ /** Serial Version UID */
+ private static final long serialVersionUID = -1898644287113084556L;
+
+ /**
+ * Creates a new DataSetException object.
+ */
+ public DataSetException()
+ {
+ super();
+ }
+
+ /**
+ * Creates a new DataSetException object.
+ *
+ * @param s TODO: DOCUMENT ME!
+ */
+ public DataSetException(String s)
+ {
+ super(s);
+ }
+}
Added: db/torque/village/trunk/src/java/com/workingdogs/village/Enums.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/Enums.java?rev=590654&view=auto
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/Enums.java (added)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/Enums.java Wed Oct 31 05:50:45 2007
@@ -0,0 +1,78 @@
+package com.workingdogs.village;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/**
+ * A class for constants.
+ *
+ * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
+ * @version $Revision: 567 $
+ */
+public class Enums
+{
+ /**
+ * Doesn't do anything
+ */
+ Enums()
+ {
+ }
+
+ /** DataSet record that has been deleted but not removed from the DataSet */
+ public static final int ZOMBIE = 1;
+
+ /** A record marked for delete */
+ public static final int DELETE = 2;
+
+ /** A record marked for update */
+ public static final int UPDATE = 3;
+
+ /** A record marked for insert */
+ public static final int INSERT = 4;
+
+ /** trigger state before a delete is run */
+ public static final int BEFOREDELETE = 5;
+
+ /** trigger state after a delete is run */
+ public static final int AFTERDELETE = 6;
+
+ /** trigger state before a insert is run */
+ public static final int BEFOREINSERT = 7;
+
+ /** trigger state after a insert is run */
+ public static final int AFTERINSERT = 8;
+
+ /** trigger state before a update is run */
+ public static final int BEFOREUPDATE = 9;
+
+ /** trigger state after a update is run */
+ public static final int AFTERUPDATE = 10;
+
+ /** an unknown type */
+ public static final int UNKNOWN = 11;
+
+ /** an oracle type */
+ public static final int ORACLE = 12;
+
+ /** an sybase type */
+ public static final int SYBASE = 13;
+
+ /** an sqlserver type */
+ public static final int SQLSERVER = 14;
+}
Added: db/torque/village/trunk/src/java/com/workingdogs/village/KeyDef.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/KeyDef.java?rev=590654&view=auto
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/KeyDef.java (added)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/KeyDef.java Wed Oct 31 05:50:45 2007
@@ -0,0 +1,126 @@
+package com.workingdogs.village;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Vector;
+
+/**
+ * A KeyDef is a way to define the key columns in a table. The KeyDef is generally used in conjunction with a <a
+ * href="TableDataSet.html">TableDataSet</a>. Essentially a KeyDef is what forms the WHERE clause for an UPDATE or DELETE.
+ *
+ * <P>
+ * In order to use the KeyDef, you simply use it like this:
+ * <PRE>
+ * KeyDef kd = new KeyDef().addAttrib("key_column_a");
+ * TableDataSet tds = new TableDataSet ( connection, "table", kd );
+ * tds.fetchRecords();
+ * Record rec = tds.getRecord(0);
+ * rec.setValue("column_name", "new value" );
+ * rec.save();
+ * tds.close();
+ * </PRE>
+ * In the above example, Record 0 is retrieved from the database table and the following update statement is generated:
+ * </p>
+ *
+ * <P>
+ * UPDATE table SET column_name=? WHERE key_column_a=?
+ * </p>
+ *
+ * <P></p>
+ *
+ * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
+ * @version $Revision: 568 $
+ */
+public class KeyDef
+{
+ /** TODO: DOCUMENT ME! */
+ private Vector mySelf = null;
+
+ /**
+ * Constructor for KeyDef. Make sure to always initialize KeyDef with an initial element because it is 1 based.
+ */
+ public KeyDef()
+ {
+ mySelf = new Vector();
+ mySelf.addElement("");
+ }
+
+ /**
+ * Adds the named attribute to the KeyDef.
+ *
+ * @param name TODO: DOCUMENT ME!
+ *
+ * @return a copy of itself
+ */
+ public KeyDef addAttrib(String name)
+ {
+ mySelf.addElement(name);
+
+ return this;
+ }
+
+ /**
+ * Determines if the KeyDef contains the requested Attribute.
+ *
+ * @param name TODO: DOCUMENT ME!
+ *
+ * @return true if the attribute has been defined. false otherwise.
+ */
+ public boolean containsAttrib(String name)
+ {
+ return (mySelf.indexOf((Object) name) == -1) ? false : true;
+ }
+
+ /**
+ * getAttrib is 1 based. Setting pos to 0 will attempt to return pos 1.
+ *
+ * @param pos TODO: DOCUMENT ME!
+ *
+ * @return value of Attribute at pos as String. null if value is not found.
+ */
+ public String getAttrib(int pos)
+ {
+ if (pos == 0)
+ {
+ pos = 1;
+ }
+
+ try
+ {
+ return (String) mySelf.elementAt(pos);
+ }
+ catch (ArrayIndexOutOfBoundsException e)
+ {
+ return null;
+ }
+ }
+
+ /**
+ * KeyDef's are 1 based, returns size - 1
+ *
+ * @return the number of elements in the KeyDef that were set by addAttrib()
+ *
+ * @see #addAttrib(java.lang.String)
+ */
+ public int size()
+ {
+ return mySelf.size() - 1;
+ }
+}
Added: db/torque/village/trunk/src/java/com/workingdogs/village/QueryDataSet.java
URL: http://svn.apache.org/viewvc/db/torque/village/trunk/src/java/com/workingdogs/village/QueryDataSet.java?rev=590654&view=auto
==============================================================================
--- db/torque/village/trunk/src/java/com/workingdogs/village/QueryDataSet.java (added)
+++ db/torque/village/trunk/src/java/com/workingdogs/village/QueryDataSet.java Wed Oct 31 05:50:45 2007
@@ -0,0 +1,110 @@
+package com.workingdogs.village;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * This class is used for doing SQL select statements on the database. It should not be used for doing modifications via
+ * update/delete/insert statements. If you would like to perform those functions, please use a <a
+ * href="TableDataSet.html">TableDataSet</a>.
+ *
+ * <P>
+ * Here is some example code for using a QueryDataSet.
+ * <PRE>
+ * QueryDataSet qds = new QueryDataSet ( connection, "SELECT * from my_table" );
+ * qds.fetchRecords(10); // fetch the first 10 records
+ * for ( int i = 0; i < qds.size(); i++ )
+ * {
+ * Record rec = qds.getRecord(i);
+ * int value = rec.getValue("column").asInt();
+ * System.out.println ( "The value is: " + value );
+ * }
+ * qds.close();
+ * </PRE>
+ * It is important to always remember to close() a QueryDataSet in order to free the allocated resources.
+ * </p>
+ *
+ * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
+ * @version $Revision: 564 $
+ */
+public class QueryDataSet
+ extends DataSet
+{
+ /**
+ * Private...does nothing.
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public QueryDataSet()
+ throws SQLException, DataSetException
+ {
+ }
+
+ /**
+ * Creates a new QueryDataSet based on a connection and a select string
+ *
+ * @param conn
+ * @param selectStmt
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public QueryDataSet(Connection conn, String selectStmt)
+ throws SQLException, DataSetException
+ {
+ this.conn = conn;
+
+ selectString = new StringBuffer(selectStmt);
+ stmt = conn.createStatement();
+ resultSet = stmt.executeQuery(selectStmt);
+ schema = new Schema();
+ schema.populate(resultSet.getMetaData(), null);
+ }
+
+ /**
+ * Create a new QueryDataSet based on an existing resultSet
+ *
+ * @param resultSet
+ *
+ * @exception SQLException
+ * @exception DataSetException
+ */
+ public QueryDataSet(ResultSet resultSet)
+ throws SQLException, DataSetException
+ {
+ this.resultSet = resultSet;
+ schema = new Schema();
+ schema.populate(resultSet.getMetaData(), null);
+ }
+
+ /**
+ * get the Select String that was used to create this QueryDataSet
+ *
+ * @return a select string
+ */
+ public String getSelectString()
+ {
+ return this.selectString.toString();
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org