📄 table.java
字号:
* record in the table and values of fields of objects from sepecifed
* array <I>objects</I> to alter record fields.
*
* @param objects array of objects specifing primiray keys and and new
* values of updated record fields
*
* @return number of objects actually updated
*/
public int update(Object[] objects) {
return update(objects, session);
}
/** Update set of records in the table using table's primary key to locate
* record in the table and values of fields of objects from sepecifed
* array <I>objects</I> to alter record fields.
*
* @param objects array of objects specifing primiray keys and and new
* values of updated record fields
* @param session user database session
*
* @return number of objects actually updated
*/
public synchronized int update(Object[] objects, 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 < objects.length; i++) {
for (int j = 0; j < primaryKeys.length; j++) {
fields[primaryKeyIndices[j]].bindVariable(updateStmt,
objects[i],
nColumns+1+j);
}
bindUpdateVariables(updateStmt, objects[i]);
updateStmt.addBatch();
}
int rc[] = updateStmt.executeBatch();
for (int k = 0; k < rc.length; k++) {
nUpdated += rc[k];
}
updateStmt.clearParameters();
} catch(SQLException ex) { session.handleSQLException(ex); }
return nUpdated;
}
/** Delete record with specified value of primary key from the table.
*
* @param obj object containing value of primary key.
*
* @return number of objects actually deleted
*/
public int delete(Object obj) {
return delete(obj, session);
}
/** Delete record with specified value of primary key from the table.
*
* @param obj object containing value of primary key.
* @param session user database session
*/
public synchronized int delete(Object obj, Session session) {
if (primaryKeys == null) {
throw new NoPrimaryKeyError(this);
}
if (session == null) {
session = ((SessionThread)Thread.currentThread()).session;
}
int nDeleted = 0;
try {
checkConnection(session);
if (deleteStmt == null) {
String sql = "delete from " + name +
" where " + primaryKeys[0] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
}
deleteStmt = session.connection.prepareStatement(sql);
}
for (int i = 0; i < primaryKeys.length; i++) {
fields[primaryKeyIndices[i]].bindVariable(deleteStmt, obj,i+1);
}
nDeleted = deleteStmt.executeUpdate();
deleteStmt.clearParameters();
} catch(SQLException ex) { session.handleSQLException(ex); }
return nDeleted;
}
/** Delete records with specified primary keys from the table.
*
* @param objects array of objects containing values of primary key.
*
* @return number of objects actually deleted
*/
public int delete(Object[] objects) {
return delete(objects, session);
}
/** Delete records with specified primary keys from the table.
*
* @param objects array of objects containing values of primary key.
*
* @return number of objects actually deleted
*/
public synchronized int delete(Object[] objects, Session session) {
if (primaryKeys == null) {
throw new NoPrimaryKeyError(this);
}
if (session == null) {
session = ((SessionThread)Thread.currentThread()).session;
}
int nDeleted = 0;
try {
checkConnection(session);
if (deleteStmt == null) {
String sql = "delete from " + name +
" where " + primaryKeys[0] + " = ?";
for (int i = 1; i < primaryKeys.length; i++) {
sql += " and " + primaryKeys[i] + " = ?";
}
deleteStmt = session.connection.prepareStatement(sql);
}
for (int i = 0; i < objects.length; i++) {
for (int j = 0; j < primaryKeys.length; j++) {
fields[primaryKeyIndices[j]].bindVariable(deleteStmt,
objects[i], j+1);
}
deleteStmt.addBatch();
}
int rc[] = deleteStmt.executeBatch();
for (int k = 0; k < rc.length; k++) {
nDeleted += rc[k];
}
deleteStmt.clearParameters();
} catch(SQLException ex) { session.handleSQLException(ex); }
return nDeleted;
}
/** Spearator of name components of compound field. For example, if Java
* class constains component "location" of Point class, which
* has two components "x" and "y", then database table should
* have columns "location_x" and "location_y" (if '_' is used
* as separator)
*/
public static String fieldSeparator = "_";
/** Some versions of JDBC driver doesn't support
* <code>getBigDecimal(int columnIndex)</code>. Setting this variable to
* <code>true</code> makes JORA to explicitly request scale from result
* set metadata and use deprecated version of <code>getBigDecimal</code>
* with extra <code>scale</code> parameter.
*/
public static boolean useDepricatedGetBigDecimal = true;
// --- Implementation -----------------------------------------
/** Is table abstract - not present in database.
*/
protected boolean isAbstract;
protected Table derived;
protected int nDerived;
protected String name;
protected String listOfFields;
protected String listOfAssignments;
protected Class cls;
protected Session session;
static private Class serializableClass;
private FieldDescriptor[] fields;
private int nFields; // length of "fields" array
private int nColumns; // number of atomic fields in "fields" array
private String primaryKeys[];
private int primaryKeyIndices[];
protected int connectionID;
private PreparedStatement updateStmt;
private PreparedStatement deleteStmt;
private PreparedStatement insertStmt;
private static Table allTables;
private Constructor constructor;
private static Method setBypass;
private static final Object[] bypassFlag = { new Boolean(true) };
private static final Object[] constructorArgs = {};
static {
try {
serializableClass = Class.forName("java.io.Serializable");
Class c = Class.forName("java.lang.reflect.AccessibleObject");
Class[] param = { Boolean.TYPE };
setBypass = c.getMethod("setAccessible", param);
} catch(Exception ex) {}
}
private final void init(String className, String tableName, Session s,
String[] keys)
{
name = tableName;
try {
cls = Class.forName(className);
} catch(ClassNotFoundException ex) {throw new NoClassDefFoundError();}
isAbstract = tableName == null;
session = s;
primaryKeys = keys;
listOfFields = "";
listOfAssignments = "";
connectionID = 0;
Vector fieldsVector = new Vector();
nFields = buildFieldsList(fieldsVector, cls, "");
fields = new FieldDescriptor[nFields];
fieldsVector.copyInto(fields);
try {
constructor = cls.getDeclaredConstructor(new Class[0]);
setBypass.invoke(constructor, bypassFlag);
} catch(Exception ex) {}
if (keys != null) {
if (keys.length == 0) {
throw new NoPrimaryKeyError(this);
}
primaryKeyIndices = new int[keys.length];
for (int j = keys.length; --j >= 0;) {
int i = nFields;
while (--i >= 0) {
if (fields[i].name.equals(keys[j])) {
if (!fields[i].isAtomic()) {
throw new NoPrimaryKeyError(this);
}
primaryKeyIndices[j] = i;
break;
}
}
if (i < 0) {
throw new NoSuchFieldError("No such field '" + keys[j]
+ "' in table " + name);
}
}
}
insertIntoTableHierarchy();
}
private final void insertIntoTableHierarchy()
{
Table t, prev = null;
Table after = null;
int nChilds = 0;
for (t = allTables; t != null; prev = t, t = t.derived) {
if (t.cls.isAssignableFrom(cls)) {
if (primaryKeys == null && t.primaryKeys != null) {
primaryKeys = t.primaryKeys;
primaryKeyIndices = t.primaryKeyIndices;
}
if (session == null) {
session = t.session;
}
t.nDerived += 1;
after = t;
} else if (cls.isAssignableFrom(t.cls)) {
after = prev;
do {
if (cls.isAssignableFrom(t.cls)) {
if (primaryKeys != null && t.primaryKeys == null) {
t.primaryKeys = primaryKeys;
t.primaryKeyIndices = primaryKeyIndices;
}
if (t.session == null) {
t.session = session;
}
nChilds += 1;
}
} while ((t = t.derived) != null);
break;
}
}
if (after == null) {
derived = allTables;
allTables = this;
} else {
derived = after.derived;
after.derived = this;
}
nDerived = nChilds;
}
private final void checkConnection(Session s) throws SQLException {
if (connectionID != s.connectionID) {
if (insertStmt != null) {
insertStmt.close();
insertStmt = null;
}
if (updateStmt != null) {
updateStmt.close();
updateStmt = null;
}
if (deleteStmt != null) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -