From 40a9d99496e098562f090fb7ffce9e749011b131 Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Mon, 20 May 2024 17:58:16 -0400 Subject: Formatting pass --- .../java/net/wotonomy/jdbcadaptor/JDBCChannel.java | 230 ++++++++++++--------- 1 file changed, 134 insertions(+), 96 deletions(-) (limited to 'projects/net.wotonomy.persistence.adapter.jdbc/src/main/java/net/wotonomy/jdbcadaptor/JDBCChannel.java') diff --git a/projects/net.wotonomy.persistence.adapter.jdbc/src/main/java/net/wotonomy/jdbcadaptor/JDBCChannel.java b/projects/net.wotonomy.persistence.adapter.jdbc/src/main/java/net/wotonomy/jdbcadaptor/JDBCChannel.java index c62c463..64e4572 100644 --- a/projects/net.wotonomy.persistence.adapter.jdbc/src/main/java/net/wotonomy/jdbcadaptor/JDBCChannel.java +++ b/projects/net.wotonomy.persistence.adapter.jdbc/src/main/java/net/wotonomy/jdbcadaptor/JDBCChannel.java @@ -41,12 +41,12 @@ import net.wotonomy.foundation.NSMutableDictionary; import net.wotonomy.foundation.NSTimestamp; /** -* Concrete implementation of EOAdaptorChannel for use with JDBC. -* -* @author ezamudio@nasoft.com -* @author $Author: cgruber $ -* @version $Revision: 903 $ -*/ + * Concrete implementation of EOAdaptorChannel for use with JDBC. + * + * @author ezamudio@nasoft.com + * @author $Author: cgruber $ + * @version $Revision: 903 $ + */ public class JDBCChannel extends EOAdaptorChannel { protected boolean _fetchInProgress; @@ -60,6 +60,7 @@ public class JDBCChannel extends EOAdaptorChannel { /** * Creates a new JDBCChannel. + * * @param context The JDBCContext this channel belongs to. */ public JDBCChannel(JDBCContext context) { @@ -67,24 +68,31 @@ public class JDBCChannel extends EOAdaptorChannel { } protected JDBCContext _context() { - return (JDBCContext)adaptorContext(); + return (JDBCContext) adaptorContext(); } - /* Sets the attributes to be fetched from the database. - * @see net.wotonomy.access.EOAdaptorChannel#setAttributesToFetch(net.wotonomy.foundation.NSArray) + /* + * Sets the attributes to be fetched from the database. + * + * @see net.wotonomy.access.EOAdaptorChannel#setAttributesToFetch(net.wotonomy. + * foundation.NSArray) */ public void setAttributesToFetch(NSArray atts) { _attsToFetch = atts; } - /* Returns an array with the attributes that will be fetched. + /* + * Returns an array with the attributes that will be fetched. + * * @see net.wotonomy.access.EOAdaptorChannel#attributesToFetch() */ public NSArray attributesToFetch() { return _attsToFetch; } - /* Cancels the fetch, rolling back the transaction. + /* + * Cancels the fetch, rolling back the transaction. + * * @see net.wotonomy.access.EOAdaptorChannel#cancelFetch() */ public void cancelFetch() { @@ -98,7 +106,9 @@ public class JDBCChannel extends EOAdaptorChannel { } } - /* Closes the jdbc channel. + /* + * Closes the jdbc channel. + * * @see net.wotonomy.access.EOAdaptorChannel#closeChannel() */ public void closeChannel() { @@ -111,9 +121,11 @@ public class JDBCChannel extends EOAdaptorChannel { } } - /* If the fetch was done with an array of EOAttributes, returns - * that same array; otherwise it creates an array of EOAttributes - * based on the column names that will be fetched. + /* + * If the fetch was done with an array of EOAttributes, returns that same array; + * otherwise it creates an array of EOAttributes based on the column names that + * will be fetched. + * * @see net.wotonomy.access.EOAdaptorChannel#describeResults() */ public NSArray describeResults() { @@ -134,7 +146,7 @@ public class JDBCChannel extends EOAdaptorChannel { a.setAllowsNull(_rsmeta.isNullable(i) == ResultSetMetaData.columnNullable); a.setWidth(_rsmeta.getColumnDisplaySize(i)); a.setReadOnly(_rsmeta.isReadOnly(i)); - attarr[i-1] = a; + attarr[i - 1] = a; } _resultAttributes = new NSArray(attarr); } catch (SQLException ex) { @@ -144,9 +156,12 @@ public class JDBCChannel extends EOAdaptorChannel { return _resultAttributes; } - /* Deletes from the database the rows described by the qualifier, - * in the specified entity. - * @see net.wotonomy.access.EOAdaptorChannel#deleteRowsDescribedByQualifier(net.wotonomy.control.EOQualifier, net.wotonomy.access.EOEntity) + /* + * Deletes from the database the rows described by the qualifier, in the + * specified entity. + * + * @see net.wotonomy.access.EOAdaptorChannel#deleteRowsDescribedByQualifier(net. + * wotonomy.control.EOQualifier, net.wotonomy.access.EOEntity) */ public int deleteRowsDescribedByQualifier(EOQualifier q, EOEntity entity) { EOSQLExpression exp = adaptorContext().adaptor().expressionFactory().createExpression(entity); @@ -155,11 +170,14 @@ public class JDBCChannel extends EOAdaptorChannel { return _resultCount; } - /* Creates a java.sql.Statement object and executes it. - * If there is an open transaction, the statement is executed inside it; - * otherwise a transaction is started, the statement executed, and - * the transaction is committed. - * @see net.wotonomy.access.EOAdaptorChannel#evaluateExpression(net.wotonomy.access.EOSQLExpression) + /* + * Creates a java.sql.Statement object and executes it. If there is an open + * transaction, the statement is executed inside it; otherwise a transaction is + * started, the statement executed, and the transaction is committed. + * + * @see + * net.wotonomy.access.EOAdaptorChannel#evaluateExpression(net.wotonomy.access. + * EOSQLExpression) */ public void evaluateExpression(EOSQLExpression sql) { if (!isOpen()) @@ -173,20 +191,20 @@ public class JDBCChannel extends EOAdaptorChannel { boolean isQuery = false; String text = sql.statement(); try { - //run an executeUpdate with these prefixes + // run an executeUpdate with these prefixes if (text.startsWith("INSERT") || text.startsWith("DELETE") || text.startsWith("UPDATE")) { conditionalBeginTransaction(); _resultCount = _statement.executeUpdate(text); conditionalCommitTransaction(); return; } else if (text.startsWith("SELECT")) { - //run an executeQuery with SELECT + // run an executeQuery with SELECT if (_resultCount > 0) _statement.setMaxRows(_resultCount); _resultSet = _statement.executeQuery(text); _fetchInProgress = true; return; - } else { //just plain execute + } else { // just plain execute conditionalBeginTransaction(); isQuery = _statement.execute(text); } @@ -207,80 +225,83 @@ public class JDBCChannel extends EOAdaptorChannel { } } - /* Executes a stored procedure with the specified parameters. - * Any results that the procedure returns should be obtained - * by calling returnValuesForLastStoredProcedureInvocation. - * @see net.wotonomy.access.EOAdaptorChannel#executeStoredProcedure(net.wotonomy.access.EOStoredProcedure, net.wotonomy.foundation.NSDictionary) + /* + * Executes a stored procedure with the specified parameters. Any results that + * the procedure returns should be obtained by calling + * returnValuesForLastStoredProcedureInvocation. + * + * @see + * net.wotonomy.access.EOAdaptorChannel#executeStoredProcedure(net.wotonomy. + * access.EOStoredProcedure, net.wotonomy.foundation.NSDictionary) */ - public void executeStoredProcedure( - EOStoredProcedure proc, NSDictionary values) { + public void executeStoredProcedure(EOStoredProcedure proc, NSDictionary values) { if (!isOpen()) throw new EOGeneralAdaptorException("Attempt to execute a stored procedure on a closed channel."); conditionalBeginTransaction(); try { - //Assemble the procedure call + // Assemble the procedure call StringBuffer buf = new StringBuffer("{ call "); buf.append(proc.externalName()); NSArray args = proc.arguments(); if (args != null && args.count() > 0) { buf.append("["); for (int i = 0; i < args.count(); i++) { - EOAttribute a = (EOAttribute)args.objectAtIndex(i); + EOAttribute a = (EOAttribute) args.objectAtIndex(i); if (a.parameterDirection() != EOAttribute.OutParameter) { buf.append('?'); buf.append(", "); } } - buf.delete(buf.length()-2, buf.length()); + buf.delete(buf.length() - 2, buf.length()); buf.append("]"); } buf.append(" }"); - //get the callable statement + // get the callable statement CallableStatement sp = _context().connection().prepareCall(buf.toString()); if (args != null && args.count() > 0) { int pos = 1; - //set the in and inOut parameters + // set the in and inOut parameters for (int i = 0; i < args.count(); i++) { - EOAttribute a = (EOAttribute)args.objectAtIndex(i); + EOAttribute a = (EOAttribute) args.objectAtIndex(i); if (a.parameterDirection() != EOAttribute.OutParameter) { Object val = values.objectForKey(a.name()); if (val == NSKeyValueCoding.NullValue) - sp.setNull(pos, 0); //TODO: check sql type + sp.setNull(pos, 0); // TODO: check sql type if (val instanceof String) - sp.setString(pos, (String)val); + sp.setString(pos, (String) val); else if (val instanceof BigDecimal) - sp.setBigDecimal(pos, (BigDecimal)val); + sp.setBigDecimal(pos, (BigDecimal) val); else if (val instanceof NSTimestamp) - sp.setTimestamp(pos, (NSTimestamp)val); + sp.setTimestamp(pos, (NSTimestamp) val); else if (val instanceof NSData) - sp.setBytes(pos, ((NSData)val).bytes()); + sp.setBytes(pos, ((NSData) val).bytes()); else if (val instanceof Integer) - sp.setInt(pos, ((Integer)val).intValue()); + sp.setInt(pos, ((Integer) val).intValue()); else if (val instanceof Long) - sp.setLong(pos, ((Long)val).longValue()); + sp.setLong(pos, ((Long) val).longValue()); else sp.setObject(pos, val); pos++; } } } - //run the procedure + // run the procedure sp.execute(); - //get the return values + // get the return values if (args != null && args.count() > 0) { int pos = 1; NSMutableDictionary retvals = new NSMutableDictionary(); for (int i = 0; i < args.count(); i++) { - EOAttribute a = (EOAttribute)args.objectAtIndex(i); + EOAttribute a = (EOAttribute) args.objectAtIndex(i); if (a.parameterDirection() != EOAttribute.InParameter) { Object val = sp.getObject(pos); if (val == null) retvals.setObjectForKey(NSKeyValueCoding.NullValue, a.name()); else if (val instanceof Blob) { try { - retvals.setObjectForKey(new NSData(((Blob)val).getBinaryStream(), 1024), a.name()); + retvals.setObjectForKey(new NSData(((Blob) val).getBinaryStream(), 1024), a.name()); } catch (java.io.IOException ex) { - //what should I do here? + // what should I do here? retvals.setObjectForKey(NSData.EmptyData, a.name()); } } else @@ -296,7 +317,9 @@ public class JDBCChannel extends EOAdaptorChannel { conditionalCommitTransaction(); } - /* Fetches one row from the database + /* + * Fetches one row from the database + * * @see net.wotonomy.access.EOAdaptorChannel#fetchRow() */ public NSMutableDictionary fetchRow() { @@ -306,7 +329,7 @@ public class JDBCChannel extends EOAdaptorChannel { if (attributesToFetch() == null) throw new EOGeneralAdaptorException("Attempt to fetchRow without setting attributes to fetch first."); try { - //If the current result set ends, there may be another one + // If the current result set ends, there may be another one if (!_resultSet.next()) { _resultSet.close(); _resultAttributes = null; @@ -319,12 +342,12 @@ public class JDBCChannel extends EOAdaptorChannel { throw new JDBCAdaptorException("While trying to fetch row.", ex); } - //Assemble the dictionary + // Assemble the dictionary NSMutableDictionary dict = new NSMutableDictionary(attributesToFetch().count()); try { for (int i = 0; i < attributesToFetch().count(); i++) { - EOAttribute a = (EOAttribute)attributesToFetch().objectAtIndex(i); - Object o = _resultSet.getObject(i+1); + EOAttribute a = (EOAttribute) attributesToFetch().objectAtIndex(i); + Object o = _resultSet.getObject(i + 1); if (o == null) o = NSKeyValueCoding.NullValue; dict.setObjectForKey(o, a.name()); @@ -335,8 +358,11 @@ public class JDBCChannel extends EOAdaptorChannel { return dict; } - /* Inserts a row into a table in the database. - * @see net.wotonomy.access.EOAdaptorChannel#insertRow(net.wotonomy.foundation.NSDictionary, net.wotonomy.access.EOEntity) + /* + * Inserts a row into a table in the database. + * + * @see net.wotonomy.access.EOAdaptorChannel#insertRow(net.wotonomy.foundation. + * NSDictionary, net.wotonomy.access.EOEntity) */ public void insertRow(NSDictionary row, EOEntity entity) { EOSQLExpression exp = adaptorContext().adaptor().expressionFactory().createExpression(entity); @@ -344,15 +370,19 @@ public class JDBCChannel extends EOAdaptorChannel { evaluateExpression(exp); } - /* Indicates if a fetch is in progress; that is, if a SELECT statement - * was executed and there are still rows to be fetched. + /* + * Indicates if a fetch is in progress; that is, if a SELECT statement was + * executed and there are still rows to be fetched. + * * @see net.wotonomy.access.EOAdaptorChannel#isFetchInProgress() */ public boolean isFetchInProgress() { return _fetchInProgress; } - /* Indicates if the channel is open. + /* + * Indicates if the channel is open. + * * @see net.wotonomy.access.EOAdaptorChannel#isOpen() */ public boolean isOpen() { @@ -365,9 +395,10 @@ public class JDBCChannel extends EOAdaptorChannel { return open; } - /* Opens the channel. If the adaptor context has not yet made - * a connection to the database, this forces the context to - * connect. + /* + * Opens the channel. If the adaptor context has not yet made a connection to + * the database, this forces the context to connect. + * * @see net.wotonomy.access.EOAdaptorChannel#openChannel() */ public void openChannel() { @@ -379,36 +410,46 @@ public class JDBCChannel extends EOAdaptorChannel { } } - /* Returns the values obtained from the last stored procedure executed. - * @see net.wotonomy.access.EOAdaptorChannel#returnValuesForLastStoredProcedureInvocation() + /* + * Returns the values obtained from the last stored procedure executed. + * + * @see net.wotonomy.access.EOAdaptorChannel# + * returnValuesForLastStoredProcedureInvocation() */ public NSDictionary returnValuesForLastStoredProcedureInvocation() { return _spReturnValues; } - /* Creates a SELECT expression and executes it. If the attribute array is null, - * then the result's metadata is used to dynamically create an array - * of attributes. - * @see net.wotonomy.access.EOAdaptorChannel#selectAttributes(net.wotonomy.foundation.NSArray, net.wotonomy.control.EOFetchSpecification, boolean, net.wotonomy.access.EOEntity) + /* + * Creates a SELECT expression and executes it. If the attribute array is null, + * then the result's metadata is used to dynamically create an array of + * attributes. + * + * @see + * net.wotonomy.access.EOAdaptorChannel#selectAttributes(net.wotonomy.foundation + * .NSArray, net.wotonomy.control.EOFetchSpecification, boolean, + * net.wotonomy.access.EOEntity) */ - public void selectAttributes( - NSArray atts, EOFetchSpecification fspec, - boolean lock, EOEntity entity) { + public void selectAttributes(NSArray atts, EOFetchSpecification fspec, boolean lock, EOEntity entity) { _resultAttributes = atts; EOSQLExpression expr = adaptorContext().adaptor().expressionFactory().createExpression(entity); _fetchInProgress = true; expr.prepareSelectExpressionWithAttributes(atts, lock, fspec); - //for now we store the fetch limit here + // for now we store the fetch limit here if (fspec != null) _resultCount = fspec.fetchLimit(); evaluateExpression(expr); } - /* Creates and executes an UPDATE statement. - * @see net.wotonomy.access.EOAdaptorChannel#updateValuesInRowsDescribedByQualifier(net.wotonomy.foundation.NSDictionary, net.wotonomy.control.EOQualifier, net.wotonomy.access.EOEntity) + /* + * Creates and executes an UPDATE statement. + * + * @see + * net.wotonomy.access.EOAdaptorChannel#updateValuesInRowsDescribedByQualifier( + * net.wotonomy.foundation.NSDictionary, net.wotonomy.control.EOQualifier, + * net.wotonomy.access.EOEntity) */ - public int updateValuesInRowsDescribedByQualifier( - NSDictionary row, EOQualifier q, EOEntity entity) { + public int updateValuesInRowsDescribedByQualifier(NSDictionary row, EOQualifier q, EOEntity entity) { EOSQLExpression exp = adaptorContext().adaptor().expressionFactory().createExpression(entity); exp.prepareUpdateExpressionWithRow(row, q); evaluateExpression(exp); @@ -429,21 +470,18 @@ public class JDBCChannel extends EOAdaptorChannel { } /* -* $Log$ -* Revision 1.2 2006/02/18 22:59:22 cgruber -* make it compile with maven dependencies and add a cvsignore. -* -* Revision 1.1 2006/02/16 13:22:23 cgruber -* Check in all sources in eclipse-friendly maven-enabled packages. -* -* Revision 1.3 2003/08/14 02:15:11 chochos -* added lots of comments -* -* Revision 1.2 2003/08/13 20:45:20 chochos -* small fixes in evaluateExpression, which has been successfully tested with a SELECT statement. -* -* Revision 1.1 2003/08/13 20:12:48 chochos -* a subclass of EOAdaptorChannel to be used with JDBC. -* -*/ - + * $Log$ Revision 1.2 2006/02/18 22:59:22 cgruber make it compile with maven + * dependencies and add a cvsignore. + * + * Revision 1.1 2006/02/16 13:22:23 cgruber Check in all sources in + * eclipse-friendly maven-enabled packages. + * + * Revision 1.3 2003/08/14 02:15:11 chochos added lots of comments + * + * Revision 1.2 2003/08/13 20:45:20 chochos small fixes in evaluateExpression, + * which has been successfully tested with a SELECT statement. + * + * Revision 1.1 2003/08/13 20:12:48 chochos a subclass of EOAdaptorChannel to be + * used with JDBC. + * + */ -- cgit v1.2.3