📄 table.java
字号:
//-< Table.java >----------------------------------------------------*--------*
// JORA Version 2.0 (c) 1998 GARRET * ? *
// (Java Object Relational Adapter) * /\| *
// * / \ *
// Created: 10-Jun-98 K.A. Knizhnik * / [] \ *
// Last update: 20-Jun-98 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// Class representing database table
//-------------------------------------------------------------------*--------*
package jora;
import java.util.*;
import java.sql.*;
import java.lang.reflect.*;
/** Table class is used to establish mapping between corteges of database
* tables and Java classes. This class is responsible for constructing
* SQL statements for extracting, updating and deleting records of the
* database table.
*/
public class Table {
/** Constructor for table object. Make association between Java class
* and database table.
*
* @param tclassName name of Java class
* @param tableName name of database table mapped on this Java class
* @param key table's primary key. This parameter is used in UPDATE/DELETE
* operations to locate record in the table.
* @s session, which should be opened before first access to the table
*/
public Table(String className, String tableName, Session s, String key) {
String[] keys = {key};
init(className, tableName, s, keys);
}
/** Constructor for table object. Make association between Java class
* and database table.
*
* @param tclassName name of Java class
* @param tableName name of database table mapped on this Java class
* @param keys table primary keys. This parameter is used in UPDATE/DELETE
* operations to locate record in the table.
* @s session, which should be opened before first access to the table
*/
public Table(String className, String tableName, Session s, String[] keys)
{
init(className, tableName, s, keys);
}
/** Constructor for table object. Make association between Java class
* and database table. Name of Java class should be the same as name of
* the database table
*
* @param className name of Java class, which should be (without
* package prefix) be the same as the name of database table.
* @param keys table primary keys. This parameter is used in UPDATE/DELETE
* operations to locate record in the table.
* @s session, which should be opened before first access to the table
*/
public Table(String className, Session s, String[] keys) {
init(className, className.substring(className.lastIndexOf('.')+1),
s, keys);
}
/** Constructor for table object. Make association between Java class
* and database table. Name of Java class should be the same as name of
* the database table
*
* @param className name of Java class, which should be (without
* package prefix) be the same as the name of database table.
* @param key table primary key. This parameter is used in UPDATE/DELETE
* operations to locate record in the table.
* @s session, which should be opened before first access to the table
*/
public Table(String className, Session s, String key) {
String[] keys = {key};
init(className, className.substring(className.lastIndexOf('.')+1),
s, keys);
}
/** Constructor of table without explicit key specification.
* Specification of key is necessary for update/remove operations.
* If key is not specified, it is inherited from base table (if any).
*/
public Table(String className, Session s) {
init(className, className.substring(className.lastIndexOf('.')+1),
s, null);
}
/** Constructor of table with "key" and "session" parameters inherited
* from base table.
*/
public Table(String className) {
init(className, className.substring(className.lastIndexOf('.')+1),
null, null);
}
/** Select records from database table according to search condition
*
* @param condition valid SQL condition expression started with WHERE
* or empty string if all records should be fetched.
*/
public final Cursor select(String condition) {
return new Cursor(this, session, 1, condition);
}
/** Select records from database table according to search condition
*
* @param condition valid SQL condition expression started with WHERE
* or empty string if all records should be fetched.
* @param session user database session
*/
public final Cursor select(String condition, Session session) {
return new Cursor(this, session, 1, condition);
}
/** Select records from specified and derived database tables
*
* @param condition valid SQL condition expression started with WHERE
* or empty string if all records should be fetched.
*/
public final Cursor selectAll(String condition) {
return new Cursor(this, session, nDerived+1, condition);
}
/** Select records from specified and derived database tables
*
* @param condition valid SQL condition expression started with WHERE
* or empty string if all records should be fetched.
* @param session user database session
*/
public final Cursor selectAll(String condition, Session session) {
return new Cursor(this, session, nDerived+1, condition);
}
/** Select records from database table using <I>obj</I> object as
* template for selection. All non-builtin fields of this object,
* which are not null, are compared with correspondent table values.
*
* @param obj object for construction search condition: selected objects
* should match all non-null fields of specified object.
*/
public final Cursor queryByExample(Object obj) {
return new Cursor(this, session, 1, obj);
}
/** Select records from database table using <I>obj</I> object as
* template for selection. All non-builtin fields of this object,
* which are not null, are compared with correspondent table values.
*
* @param obj object for construction search condition: selected objects
* should match all non-null fields of specified object.
* @param session user database session
*/
public final Cursor queryByExample(Object obj, Session session) {
return new Cursor(this, session, 1, obj);
}
/** Select records from specified and derived database tables using
* <I>obj</I> object as template for selection.
* All non-builtin fields of this object,
* which are not null, are compared with correspondent table values.
*
* @param obj object for construction search condition: selected objects
* should match all non-null fields of specified object.
*/
public final Cursor queryAllByExample(Object obj) {
return new Cursor(this, session, nDerived+1, obj);
}
/** Select records from specified and derived database tables using
* <I>obj</I> object as template for selection.
* All non-builtin fields of this object,
* which are not null, are compared with correspondent table values.
*
* @param obj object for construction search condition: selected objects
* should match all non-null fields of specified object.
* @param session user database session
*/
public final Cursor queryAllByExample(Object obj, Session session) {
return new Cursor(this, session, nDerived+1, obj);
}
/** Insert new record in the table. Values of inserted record fields
* are taken from specifed object.
*
* @param obj object specifing values of inserted record fields
*/
public void insert(Object obj) {
insert(obj, session);
}
/** Insert new record in the table using specified database session.
* Values of inserted record fields
* are taken from specifed object.
*
* @param obj object specifing values of inserted record fields
* @param session user database session
*/
public synchronized void insert(Object obj, Session session) {
if (session == null) {
session = ((SessionThread)Thread.currentThread()).session;
}
try {
checkConnection(session);
if (insertStmt == null) {
String sql = "insert into " + name + " ("
+ listOfFields + ") values (?";
for (int i = 1; i < nColumns; i++) {
sql += ",?";
}
sql += ")";
insertStmt = session.connection.prepareStatement(sql);
}
bindUpdateVariables(insertStmt, obj);
insertStmt.executeUpdate();
insertStmt.clearParameters();
} catch(SQLException ex) { session.handleSQLException(ex); }
}
/** Insert several new records in the table. Values of inserted records
* fields are taken from objects of specified array.
*
* @param objects array with objects specifing values of inserted record
* fields
*/
public void insert(Object[] objects) {
insert(objects, session);
}
/** Insert several new records in the table. Values of inserted records
* fields are taken from objects of specified array.
*
* @param objects array with objects specifing values of inserted record
* fields
* @param session user database session
*/
public synchronized void insert(Object[] objects, Session session) {
if (session == null) {
session = ((SessionThread)Thread.currentThread()).session;
}
try {
checkConnection(session);
if (insertStmt == null) {
String sql = "insert into " + name + " ("
+ listOfFields + ") values (?";
for (int i = 1; i < nColumns; i++) {
sql += ",?";
}
sql += ")";
insertStmt = session.connection.prepareStatement(sql);
}
for (int i = 0; i < objects.length; i++) {
bindUpdateVariables(insertStmt, objects[i]);
insertStmt.addBatch();
}
insertStmt.executeBatch();
insertStmt.clearParameters();
} catch(SQLException ex) { session.handleSQLException(ex); }
}
/** Update record in the table using table's primary key to locate
* record in the table and values of fields of specified object <I>obj</I>
* to alter record fields.
*
* @param obj object specifing value of primary key and new values of
* updated record fields
*
* @return number of objects actually updated
*/
public int update(Object obj) {
return update(obj, session);
}
/** Update record in the table using table's primary key to locate
* record in the table and values of fields of specified object <I>obj</I>
* to alter record fields.
*
* @param obj object specifing value of primary key and new values of
* updated record fields
* @param session user database session
*
* @return number of objects actually updated
*/
public synchronized int update(Object obj, Session session) {
if (primaryKeys == null) {
throw new NoPrimaryKeyError(this);
}
if (session == null) {
session = ((SessionThread)Thread.currentThread()).session;
}
int nUpdated = 0;
try {
checkConnection(session);
if (updateStmt == null) {
String sql = "update " + name + " set " + listOfAssignments
+ " where " + primaryKeys[0] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
}
updateStmt = session.connection.prepareStatement(sql);
}
for (int i = 0; i < primaryKeys.length; i++) {
fields[primaryKeyIndices[i]].bindVariable(updateStmt, obj,
nColumns+i+1);
}
bindUpdateVariables(updateStmt, obj);
nUpdated = updateStmt.executeUpdate();
updateStmt.clearParameters();
} catch(SQLException ex) { session.handleSQLException(ex); }
return nUpdated;
}
/** Update set of records in the table using table's primary key to locate
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -