📄 datatablerow.java
字号:
* If countExpression string is null, returns COUNT(*), the whereCondition input string can be null.
* The countExpression string is usually "*" or a combination of "DISTINCT or ALL" and a column name.
* Except for "*" expression, null column values are not included in the count unless the NVL function is used
* in the COUNT expression argument to specify an alternative value string.
* Thus, getRowCount("*", null) and getRowCount("*", "") both return a count of all rows in the table.
* The queried table name is that initialized by the implementing class instance.
* Returns -1 if an error occurs while executing the JDBC query.
* Assumes an existing JDBC Connection class instantiation for the connection to database.
*/
public int getRowCount(String countExpression, String whereCondition) {
return ExecuteSQL.getRowCount(connDB, tableName, countExpression, whereCondition);
}
/** Returns a count of all rows in the table named by this object instance.
*/
public int getRowCount() {
return ExecuteSQL.getRowCount(connDB, tableName, "*", "");
}
/** Returns the array of DataTableRow objects of the invoking class type.
* parses the resultSet returned by the database SQL query specified by the input string argument.
* Returns null if no data satifies specified string query or an error occurs while executing.
* Assumes an existing JDBC Connection class instantiation for the connection to database.
* Does not set the update state flag of the parsed database column data objects (object.isUpdate() == false).
*/
protected Object rowQuery(String sql) {
return rowQuery(connDB, sql);
}
protected Object rowQuery(Connection conn, String sql) {
if (conn == null) {
System.err.println(tableName + " DataTableRow rowQuery: JDBC connection null;" +
" application must first instantiate a connection class" +
"; see JDBConnect(String url, String driverName, String user, String passwd)");
return null;
}
Object rows = null;
Statement sm = null;
ExecuteSQL.setSelectForUpdate(isSelectForUpdate());
try {
sm = conn.createStatement();
// System.out.println("**DataTableRow rowQuery sql:\n" + sql);
ResultSetDb rsdb = new ResultSetDb(ExecuteSQL.rowQuery(sm, sql));
rows = parseResults(rsdb);
sm.close();
}
catch (SQLException ex) {
System.err.println(tableName + " DataTableRow: rowQuery() SQLException");
SQLExceptionHandler.prtSQLException(ex);
try {
if (sm != null) sm.close();
}
catch (SQLException exc) {
SQLExceptionHandler.prtSQLException(exc);
}
return null;
}
return rows;
}
/** Returns String of know column names delimited by ",". */
public String columnNames() {
StringBuffer sb = new StringBuffer(512);
int len = fieldNames.length;
for (int index = 0; index < len; index++) {
sb.append(fieldNames[index]).append(",");
}
return sb.substring(0, sb.length()-1);
}
/** Returns String of table qualified known column names ("tableName.columnName") delimited by ",". */
public String qualifiedColumnNames() {
StringBuffer sb = new StringBuffer(1024);
int len = fieldNames.length;
for (int index = 0; index < len; index++) {
sb.append(tableName).append(".").append(fieldNames[index]).append(",");
}
return sb.substring(0, sb.length()-1);
}
/** Returns DataTableRow "keyFieldName=value AND ..." string for use in SQL WHERE clause.
* Invoked by methods which need to access database record with corresponding key.
* Return string contains only those key column fields where isUpdate() == true.
* Returns a null, if no key fields have isUpdate() == true.
* @see DataObject#setUpdate(boolean)
* @see DataTableRow#setUpdateAllValues(boolean)
* @see DataObject#isUpdate()
* @see DataTableRow#isUpdate()
*/
public String getWhereKeyString() {
StringBuffer retVal = new StringBuffer(512);
retVal.append(" ( ");
int length = retVal.length();
for (int index = 0; index < keyIndexLength; index++) {
DataObject dataObj = (DataObject) fields.get(keyColumnIndex[index]);
if (dataObj == null ) continue;
if (! dataObj.isUpdate() ) continue; // only append if update set for key DataObject
retVal.append(fieldNames[keyColumnIndex[index]]).append(" = ").append(dataObj.toStringSQL()).append(" AND ");
}
if (length >= retVal.length()) return null;
retVal.replace(retVal.length()-5, retVal.length(), " )");
return retVal.toString();
}
/** GetRows a protected wrapper for the private rowQuery(sql) method.
* Used by extending class implementations.
* Returns a new array object of the invoking class type containing the results of the query.
* Returns null if no data or error.
* Does not set the update state flag of the parsed database column data objects (object.isUpdate() == false).
* Requires an active non-null JDBC database Connection reference.
* Method uses the JDBC Connection object assigned with setConnection().
* Appends "FOR UPDATE" to query if isSelectForUpdate() == true.
*/
protected Object getRows(String sql) {
return getRows(connDB, sql);
}
/** GetRows a protected wrapper for the private rowQuery(sql) method.
* Used by extending class implementations.
* Returns a new array object of the invoking class type containing the results of the query.
* Returns null if no data or an error occurs.
* Does not set the update state flag of the parsed database column data objects (object.isUpdate() == false).
* Requires an active non-null JDBC database Connection reference.
* Appends "FOR UPDATE" to query if isSelectForUpdate() == true.
*/
protected Object getRows(Connection conn, String sql) {
String sqltmp = sql;
if (isSelectForUpdate()) {
if (sql.toUpperCase().indexOf(SELECT_FOR_UPDATE) < 0) sqltmp = sql + SELECT_FOR_UPDATE;
}
return rowQuery(conn, sqltmp);
}
/** Returns a new DataTableRow object of the invoking class type containing the results of the query,
* created using the key fields values of this object instance.
* If a non-unique key in the query results in more than one table row, only the first row is returned.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* Returns null if no data satisfy query or an JDBC error occurs.
* Requires an active non-null JDBC database Connection reference.
* Requires getProcessing == SELECT or NONE for the input argument DataTableRow object.
* Method uses the JDBC Connection object assigned with setConnection().
*/
public DataTableRow getRow() {
DataTableRow [] array = (DataTableRow []) getRows();
if (array != null)
return array[0];
else
return null ;
}
/** Returns a new DataTableRow object of the invoking class type containing the results of the query,
* created using the key fields values of input DataTableRow argument.
* However, does a no-op and returns null, if the input row null state is set, row.isNull() == true.
* If a non-unique key in the query results in more than one table row, only the first row is returned.
* Returns null if no data satisfy query, or an JDBC error occurs.
* If the input DataTableRow key is not unique, representing multiple rows,
* only the object for the first of the matching rows is returned.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* Requires an active non-null JDBC database Connection reference.
* Requires getProcessing == SELECT or NONE for the input argument DataTableRow object.
* Method uses the JDBC Connection object assigned with setConnection().
*/
public DataTableRow getRow(DataTableRow row) {
if (row.isNull()) return null;
DataTableRow [] array = (DataTableRow []) getRows(row);
if (array != null)
return array[0];
else
return null ;
}
/** Returns a new array object of the invoking class type containing the results of the query
* created using the key fields values of this object instance.
* Returns null if no data satisfy query or an JDBC error occurs.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* If the input DataTableRow key is not unique, representing multiple rows,
* DataTableRow objects are created for all matching rows.
* Requires an active non-null JDBC database Connection reference.
* Requires getProcessing == SELECT or NONE for this instance object.
* Method uses the JDBC Connection object assigned with setConnection().
*/
public Object getRows() {
String whereCondition = getWhereKeyString();
if (whereCondition == null) return null;
if (getProcessing() != SELECT && getProcessing() != NONE) return null;
String sql = "SELECT " + columnNames() + " FROM " + tableName + " WHERE " + whereCondition;
return getRows(connDB, sql);
}
/** Returns a new array object of the invoking class type containing the results of the query
* created using the key fields values of input DataTableRow object.
* However, does a no-op and returns null, if the input row null state is set, row.isNull() == true.
* Returns null if no data satisfy query or an JDBC error occurs.
* If the input DataTableRow key is not unique, representing multiple rows,
* DataTableRow objects are created for all matching rows.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* Requires an active non-null JDBC database Connection reference.
* Requires getProcessing == SELECT or NONE for the input argument DataTableRow object.
* Method uses the JDBC Connection object assigned with setConnection().
*/
public Object getRows(DataTableRow row) {
if (row.isNull()) return null;
String whereCondition = row.getWhereKeyString();
if (whereCondition == null) return null;
if (row.getProcessing() != SELECT && row.getProcessing() != NONE) return null;
String sql = "SELECT " + columnNames() + " FROM " + tableName + " WHERE " + whereCondition;
return getRows(row.connDB, sql);
}
/** Returns a new array object of the invoking class type containing the results of calling getRows(DataTableRow) for
* each input array element, returns null if an error occurs or no data is found in the database.
* Does a no-op on input rows where, row.isNull() == true.
* If an input element key is not unique, representing multiple rows, all matching DataTableRow objects are created.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* Requires getProcessing == SELECT or NONE for the for each element of the input argument array.
* Requires an active non-null JDBC database Connection reference.
* Method uses the JDBC Connection object assigned with setConnection() in each array object instance.
*/
public Object getRows(DataTableRow [] array) {
Vector vtr = new Vector();
for ( int idx = 0; idx < array.length; idx++) {
DataTableRow [] results = (DataTableRow []) getRows(array[idx]);
if (results == null) continue;
for (int index = 0; index < results.length; index++) {
vtr.add(results[index]);
}
}
/*
if ( vtr.size() > 0) {
DataTableRow [] newArray = new DataTableRow [vtr.size()];
vtr.toArray(newArray);
return newArray;
}
else {
return null;
}
*/
return recast(vtr);
}
/** Returns a new array object of the invoking class type satisfying an SQL table query
* "SELECT DISTINCT * FROM" with specified WHERE clause.
* The query table name is that initialized by the implementing class instance.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* Requires an active non-null JDBC database Connection reference.
* Method uses the JDBC Connection object assigned with setConnection().
* Returns null if no rows satisfy query or an error condition occurs.
*/
public Object getRowsEquals(String whereCondition) {
String sql = "SELECT DISTINCT * FROM " + tableName + " " + whereCondition;
return getRows(connDB, sql);
}
/** A protected wrapper for the getRows(Connection, String) method.
* Used by extending class implementations.
* Returns a new array object of the invoking class type satisfying an SQL table query
* "SELECT * FROM ... WHERE columnName = longValue.
* The query table name is that initialized by the implementing class instance.
* Does not set the update state flag of the parsed database column data objects (isUpdate() == false).
* Requires an active non-null JDBC database Connection reference.
* Method uses the JDBC Connection object assigned with setConnection().
* Returns null if no rows satisfy query or an error condition occurs.
*/
protected Object getRowsEquals(String columnName, long longValue) {
String sql = "SELECT " + columnNames() + " FROM " + tableName + " WHERE " + columnName + " = " + longValue;
return getRows(connDB, sql);
}
// Methods to modify database table rows
/** Executes JDBC update statement for SQL statements of type INSERT, UPDATE, DELETE.
* Requires an active non-null JDBC database Connection reference.
* Method uses the JDBC Connection object assigned with setConnection().
* Returns number of rows modified, a return value of -1 indicates an error condition.
* @see #setConnection(Connection)
* @see #isLockTableForUpdate()
* @see #setLockTableForUpdate(boolean)
* @see ExecuteSQL#lockTableForUpdate(Connection, String, String)
*/
int rowUpdate(String sql) {
return rowUpdate(connDB, sql);
}
/** Executes JDBC update statement for SQL statements of type INSERT, UPDATE, DELETE.
* Requires an active non-null JDBC database Connection reference.
* Returns number of rows modified, a return value of -1 indicates an error condition.
* @see #setConnection(Connection)
* @see #isLockTableForUpdate()
* @see #setLockTableForUpdate(boolean)
* @see ExecuteSQL#lockTableForUpdate(Connection, String, String)
*/
int rowUpdate(Connection conn, String sql) {
if (conn == null) {
System.err.println(tableName + " DataTableRow rowUpdate: JDBC connection null;" +
" application must first instantiate a connection class" +
" (see JDBConn(String url, Strin
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -