From: <pj...@us...> - 2009-08-12 03:12:32
|
Revision: 6663 http://jython.svn.sourceforge.net/jython/?rev=6663&view=rev Author: pjenvey Date: 2009-08-12 03:12:07 +0000 (Wed, 12 Aug 2009) Log Message: ----------- cleanup/quiet warnings Modified Paths: -------------- trunk/jython/src/com/ziclix/python/sql/PyConnection.java trunk/jython/src/com/ziclix/python/sql/connect/Connectx.java trunk/jython/src/com/ziclix/python/sql/connect/Lookup.java Modified: trunk/jython/src/com/ziclix/python/sql/PyConnection.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/PyConnection.java 2009-08-11 06:45:00 UTC (rev 6662) +++ trunk/jython/src/com/ziclix/python/sql/PyConnection.java 2009-08-12 03:12:07 UTC (rev 6663) @@ -12,13 +12,11 @@ import java.sql.SQLException; import java.util.Collections; import java.util.HashSet; -import java.util.Iterator; import java.util.Set; import org.python.core.ClassDictInit; import org.python.core.Py; import org.python.core.PyBuiltinMethodSet; -import org.python.core.PyClass; import org.python.core.PyInteger; import org.python.core.PyList; import org.python.core.PyObject; @@ -36,39 +34,25 @@ */ public class PyConnection extends PyObject implements ClassDictInit { - /** - * Field closed - */ + /** True if closed. */ protected boolean closed; - /** - * Field connection - */ - protected Connection connection; - - /** - * Field supportsTransactions - */ + /** Whether transactions are supported. */ protected boolean supportsTransactions; - /** - * Field cursors - */ - private Set cursors; + /** The underlying java.sql.Connection. */ + protected Connection connection; - /** - * Field statements - */ - private Set statements; + /** Underlying cursors. */ + private Set<PyCursor> cursors; - /** - * Field __members__ - */ + /** Underlying statements. */ + private Set<PyStatement> statements; + + /** Field __members__ */ protected static PyList __members__; - /** - * Field __methods__ - */ + /** Field __methods__ */ protected static PyList __methods__; static { @@ -101,11 +85,10 @@ * @throws SQLException */ public PyConnection(Connection connection) throws SQLException { - this.closed = false; - this.cursors = new HashSet(); + this.cursors = new HashSet<PyCursor>(); this.connection = connection; - this.statements = new HashSet(); + this.statements = new HashSet<PyStatement>(); this.supportsTransactions = this.connection.getMetaData().supportsTransactions(); if (this.supportsTransactions) { @@ -120,7 +103,6 @@ */ @Override public String toString() { - try { return String.format("<PyConnection object at %s user='%s', url='%s'>", Py.idstr(this), connection.getMetaData().getUserName(), @@ -136,14 +118,20 @@ * @param dict */ static public void classDictInit(PyObject dict) { - + PyObject version = + Py.newString("$Revision$").__getslice__(Py.newInteger(11), + Py.newInteger(-2)); + dict.__setitem__("__version__", version); dict.__setitem__("autocommit", new PyInteger(0)); - dict.__setitem__("__version__", Py.newString("$Revision$").__getslice__(Py.newInteger(11), Py.newInteger(-2), null)); dict.__setitem__("close", new ConnectionFunc("close", 0, 0, 0, zxJDBC.getString("close"))); - dict.__setitem__("commit", new ConnectionFunc("commit", 1, 0, 0, zxJDBC.getString("commit"))); - dict.__setitem__("cursor", new ConnectionFunc("cursor", 2, 0, 4, zxJDBC.getString("cursor"))); - dict.__setitem__("rollback", new ConnectionFunc("rollback", 3, 0, 0, zxJDBC.getString("rollback"))); - dict.__setitem__("nativesql", new ConnectionFunc("nativesql", 4, 1, 1, zxJDBC.getString("nativesql"))); + dict.__setitem__("commit", new ConnectionFunc("commit", 1, 0, 0, + zxJDBC.getString("commit"))); + dict.__setitem__("cursor", new ConnectionFunc("cursor", 2, 0, 4, + zxJDBC.getString("cursor"))); + dict.__setitem__("rollback", new ConnectionFunc("rollback", 3, 0, 0, + zxJDBC.getString("rollback"))); + dict.__setitem__("nativesql", new ConnectionFunc("nativesql", 4, 1, 1, + zxJDBC.getString("nativesql"))); // hide from python dict.__setitem__("initModule", null); @@ -161,8 +149,8 @@ * @param name * @param value */ + @Override public void __setattr__(String name, PyObject value) { - if ("autocommit".equals(name)) { try { if (this.supportsTransactions) { @@ -171,7 +159,6 @@ } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e); } - return; } @@ -184,8 +171,8 @@ * @param name the name of the attribute of interest * @return the value for the attribute of the specified name */ + @Override public PyObject __findattr_ex__(String name) { - if ("autocommit".equals(name)) { try { return connection.getAutoCommit() ? Py.One : Py.Zero; @@ -240,40 +227,31 @@ } /** - * Close the connection now (rather than whenever __del__ is called). - * The connection will be unusable from this point forward; an Error - * (or subclass) exception will be raised if any operation is attempted - * with the connection. The same applies to all cursor objects trying - * to use the connection. + * Close the connection now (rather than whenever __del__ is called). The connection + * will be unusable from this point forward; an Error (or subclass) exception will be + * raised if any operation is attempted with the connection. The same applies to all + * cursor objects trying to use the connection. */ public void close() { - if (closed) { throw zxJDBC.makeException(zxJDBC.ProgrammingError, "connection is closed"); } - // mark ourselves closed now so that any callbacks we - // get from closing down cursors and statements to not - // try and modify our internal sets + // mark ourselves closed now so that any callbacks we get from closing down + // cursors and statements to not try and modify our internal sets this.closed = true; synchronized (this.cursors) { - - // close the cursors - for (Iterator i = this.cursors.iterator(); i.hasNext();) { - ((PyCursor) i.next()).close(); + for (PyCursor cursor: cursors) { + cursor.close(); } - this.cursors.clear(); } synchronized (this.statements) { - - // close the cursors - for (Iterator i = this.statements.iterator(); i.hasNext();) { - ((PyStatement) i.next()).close(); + for (PyStatement statement : statements) { + statement.close(); } - this.statements.clear(); } @@ -285,15 +263,14 @@ } /** - * Commit any pending transaction to the database. Note that if the - * database supports an auto-commit feature, this must be initially - * off. An interface method may be provided to turn it back on. + * Commit any pending transaction to the database. Note that if the database supports + * an auto-commit feature, this must be initially off. An interface method may be + * provided to turn it back on. * <p/> - * Database modules that do not support transactions should implement - * this method with void functionality. + * Database modules that do not support transactions should implement this method with + * void functionality. */ public void commit() { - if (closed) { throw zxJDBC.makeException(zxJDBC.ProgrammingError, "connection is closed"); } @@ -310,16 +287,13 @@ } /** - * <i>This method is optional since not all databases provide transaction - * support.</i> + * <i>This method is optional since not all databases provide transaction support.</i> * <p/> - * In case a database does provide transactions this method causes the database - * to roll back to the start of any pending transaction. Closing a connection - * without committing the changes first will cause an implicit rollback to be - * performed. + * In case a database does provide transactions this method causes the database to + * roll back to the start of any pending transaction. Closing a connection without + * committing the changes first will cause an implicit rollback to be performed. */ public void rollback() { - if (closed) { throw zxJDBC.makeException(zxJDBC.ProgrammingError, "connection is closed"); } @@ -336,10 +310,10 @@ } /** - * Converts the given SQL statement into the system's native SQL grammar. A - * driver may convert the JDBC sql grammar into its system's native SQL grammar - * prior to sending it; this method returns the native form of the statement - * that the driver would have sent. + * Converts the given SQL statement into the system's native SQL grammar. A driver may + * convert the JDBC sql grammar into its system's native SQL grammar prior to sending + * it; this method returns the native form of the statement that the driver would have + * sent. * * @param nativeSQL * @return the native form of this statement @@ -364,9 +338,9 @@ } /** - * Return a new Cursor Object using the connection. If the database does not - * provide a direct cursor concept, the module will have to emulate cursors - * using other means to the extent needed by this specification. + * Return a new Cursor Object using the connection. If the database does not provide a + * direct cursor concept, the module will have to emulate cursors using other means to + * the extent needed by this specification. * * @return a new cursor using this connection */ @@ -375,9 +349,9 @@ } /** - * Return a new Cursor Object using the connection. If the database does not - * provide a direct cursor concept, the module will have to emulate cursors - * using other means to the extent needed by this specification. + * Return a new Cursor Object using the connection. If the database does not provide a + * direct cursor concept, the module will have to emulate cursors using other means to + * the extent needed by this specification. * * @param dynamicFetch if true, dynamically iterate the result * @return a new cursor using this connection @@ -387,9 +361,9 @@ } /** - * Return a new Cursor Object using the connection. If the database does not - * provide a direct cursor concept, the module will have to emulate cursors - * using other means to the extent needed by this specification. + * Return a new Cursor Object using the connection. If the database does not provide a + * direct cursor concept, the module will have to emulate cursors using other means to + * the extent needed by this specification. * * @param dynamicFetch if true, dynamically iterate the result * @param rsType the type of the underlying ResultSet @@ -397,15 +371,12 @@ * @return a new cursor using this connection */ public PyCursor cursor(boolean dynamicFetch, PyObject rsType, PyObject rsConcur) { - if (closed) { throw zxJDBC.makeException(zxJDBC.ProgrammingError, "connection is closed"); } PyCursor cursor = new PyExtendedCursor(this, dynamicFetch, rsType, rsConcur); - this.cursors.add(cursor); - return cursor; } @@ -415,11 +386,9 @@ * @param cursor */ void remove(PyCursor cursor) { - if (closed) { return; } - this.cursors.remove(cursor); } @@ -429,11 +398,9 @@ * @param statement statement */ void add(PyStatement statement) { - if (closed) { return; } - this.statements.add(statement); } @@ -444,20 +411,20 @@ * @return boolean */ boolean contains(PyStatement statement) { - if (closed) { return false; } - return this.statements.contains(statement); } } class ConnectionFunc extends PyBuiltinMethodSet { + ConnectionFunc(String name, int index, int minargs, int maxargs, String doc) { super(name, index, minargs, maxargs, doc, PyConnection.class); } + @Override public PyObject __call__() { PyConnection c = (PyConnection) __self__; switch (index) { @@ -472,11 +439,12 @@ case 3: c.rollback(); return Py.None; - default : + default: throw info.unexpectedCall(0, false); } } + @Override public PyObject __call__(PyObject arg) { PyConnection c = (PyConnection) __self__; switch (index) { @@ -484,21 +452,23 @@ return c.cursor(arg.__nonzero__()); case 4: return c.nativesql(arg); - default : + default: throw info.unexpectedCall(1, false); } } + @Override public PyObject __call__(PyObject arg1, PyObject arg2, PyObject arg3) { PyConnection c = (PyConnection) __self__; switch (index) { case 2: return c.cursor(arg1.__nonzero__(), arg2, arg3); - default : + default: throw info.unexpectedCall(3, false); } } + @Override public PyObject __call__(PyObject[] args, String[] keywords) { PyConnection c = (PyConnection) __self__; PyArgParser parser = new PyArgParser(args, keywords); @@ -514,7 +484,7 @@ return c.cursor(dynamic.__nonzero__(), rstype, rsconcur); - default : + default: throw info.unexpectedCall(args.length, true); } } Modified: trunk/jython/src/com/ziclix/python/sql/connect/Connectx.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/connect/Connectx.java 2009-08-11 06:45:00 UTC (rev 6662) +++ trunk/jython/src/com/ziclix/python/sql/connect/Connectx.java 2009-08-12 03:12:07 UTC (rev 6663) @@ -35,14 +35,11 @@ public class Connectx extends PyObject { private final String SET = "set"; - private final PyString doc = new PyString("establish a connection through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource"); + private final PyString doc = + new PyString("establish a connection through a javax.sql.DataSource or " + + "javax.sql.ConnectionPooledDataSource"); - /** - * Constructor Connectx - */ - public Connectx() { - } - + @Override public PyObject __findattr_ex__(String name) { if ("__doc__".equals(name)) { return doc; @@ -53,23 +50,21 @@ /** * Construct a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource */ + @Override public PyObject __call__(PyObject[] args, String[] keywords) { - Connection c = null; PyConnection pc = null; Object datasource = null; PyArgParser parser = new PyArgParser(args, keywords); try { - String _class = (String) parser.arg(0).__tojava__(String.class); - - datasource = Class.forName(_class).newInstance(); + String klass = (String) parser.arg(0).__tojava__(String.class); + datasource = Class.forName(klass).newInstance(); } catch (Exception e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to instantiate datasource"); } String[] kws = parser.kws(); - for (int i = 0; i < kws.length; i++) { String methodName = kws[i]; @@ -78,21 +73,21 @@ } Object value = parser.kw(kws[i]).__tojava__(Object.class); - if (methodName.length() > SET.length()) { if (!SET.equals(methodName.substring(0, SET.length()))) { - // prepend "set" - invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value); + invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + + methodName.substring(1), value); } else { - // starts with "set" so just pass it on invoke(datasource, methodName, value); } } else { // shorter than "set" so it can't be a full method name - invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value); + invoke(datasource, + SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), + value); } } @@ -107,7 +102,7 @@ } try { - if ((c == null) || c.isClosed()) { + if (c == null || c.isClosed()) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); } @@ -124,8 +119,9 @@ * * @return String */ + @Override public String toString() { - return "<connectx object instance at " + Py.id(this) + ">"; + return String.format("<connectx object at %s>", Py.id(this)); } /** @@ -136,19 +132,17 @@ * @param Object value */ protected void invoke(Object src, String methodName, Object value) { - Method method = null; - StringBuffer exceptionMsg = new StringBuffer("method [").append(methodName).append("] using arg type ["); + StringBuffer exceptionMsg = new StringBuffer("method ["); + exceptionMsg.append(methodName).append("] using arg type ["); + exceptionMsg.append(value.getClass()).append("], value ["); + exceptionMsg.append(value.toString()).append("]"); - exceptionMsg.append(value.getClass()).append("], value [").append(value.toString()).append("]"); - try { method = getMethod(src.getClass(), methodName, value.getClass()); - if (method == null) { throw zxJDBC.makeException("no such " + exceptionMsg); } - method.invoke(src, new Object[]{value}); } catch (IllegalAccessException e) { throw zxJDBC.makeException("illegal access for " + exceptionMsg); @@ -164,25 +158,23 @@ * is perhaps a primitive and attempt to recurse using the primitive type. Failing * that return null. */ - protected Method getMethod(Class srcClass, String methodName, Class valueClass) { - + protected Method getMethod(Class<?> srcClass, String methodName, Class<?> valueClass) { Method method = null; try { method = srcClass.getMethod(methodName, new Class[]{valueClass}); } catch (NoSuchMethodException e) { - Class primitive = null; + Class<?> primitive = null; try { Field f = valueClass.getField("TYPE"); - - primitive = (Class) f.get(valueClass); + primitive = (Class<?>) f.get(valueClass); } catch (NoSuchFieldException ex) { } catch (IllegalAccessException ex) { } catch (ClassCastException ex) { } - if ((primitive != null) && primitive.isPrimitive()) { + if (primitive != null && primitive.isPrimitive()) { return getMethod(srcClass, methodName, primitive); } } Modified: trunk/jython/src/com/ziclix/python/sql/connect/Lookup.java =================================================================== --- trunk/jython/src/com/ziclix/python/sql/connect/Lookup.java 2009-08-11 06:45:00 UTC (rev 6662) +++ trunk/jython/src/com/ziclix/python/sql/connect/Lookup.java 2009-08-12 03:12:07 UTC (rev 6663) @@ -8,20 +8,31 @@ */ package com.ziclix.python.sql.connect; -import java.sql.*; -import java.util.*; import java.lang.reflect.Field; -import javax.sql.*; -import javax.naming.*; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; +import java.util.Hashtable; -import org.python.core.*; -import com.ziclix.python.sql.*; -import com.ziclix.python.sql.util.*; +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.ConnectionPoolDataSource; +import javax.sql.DataSource; +import org.python.core.Py; +import org.python.core.PyObject; +import org.python.core.PyString; + +import com.ziclix.python.sql.PyConnection; +import com.ziclix.python.sql.zxJDBC; +import com.ziclix.python.sql.util.PyArgParser; + /** - * Establish a connection through a JNDI lookup. The bound object can be either a <code>DataSource</code>, - * <code>ConnectionPooledDataSource</code>, <code>Connection</code> or a <code>String</code>. If it's a - * <code>String</code> the value is passed to the DriverManager to obtain a connection, otherwise the + * Establish a connection through a JNDI lookup. The bound object can be either a + * <code>DataSource</code>, <code>ConnectionPooledDataSource</code>, + * <code>Connection</code> or a <code>String</code>. If it's a <code>String</code> the + * value is passed to the DriverManager to obtain a connection, otherwise the * <code>Connection</code> is established using the object. * * @author brian zimmer @@ -30,22 +41,17 @@ */ public class Lookup extends PyObject { - private static final PyString _doc = new PyString("establish a connection through a JNDI lookup"); + private static final PyString _doc = + new PyString("establish a connection through a JNDI lookup"); /** - * Constructor Lookup - */ - public Lookup() { - } - - /** * Method __findattr__ * * @param name * @return PyObject */ + @Override public PyObject __findattr_ex__(String name) { - if ("__doc__".equals(name)) { return _doc; } @@ -54,17 +60,18 @@ } /** - * Expects a single PyString argument which is the JNDI name of the bound Connection or DataSource. - * If any keywords are passed, an attempt is made to match the keyword to a static final Field on - * javax.naming.Context. If the Field is found, the value of the Field is substituted as the key - * and the value of the keyword is the value put in the Hashtable environment. If the Field is not - * found, the key is the keyword with no substitutions. + * Expects a single PyString argument which is the JNDI name of the bound Connection + * or DataSource. If any keywords are passed, an attempt is made to match the keyword + * to a static final Field on javax.naming.Context. If the Field is found, the value + * of the Field is substituted as the key and the value of the keyword is the value + * put in the Hashtable environment. If the Field is not found, the key is the + * keyword with no substitutions. */ + @Override public PyObject __call__(PyObject[] args, String[] keywords) { - Object ref = null; Connection connection = null; - Hashtable env = new Hashtable(); + Hashtable<String, Object> env = new Hashtable<String, Object>(); // figure out the correct params PyArgParser parser = new PyArgParser(args, keywords); @@ -95,7 +102,6 @@ } InitialContext context = null; - try { context = new InitialContext(env); ref = context.lookup((String) jndiName); @@ -106,12 +112,14 @@ try { context.close(); } catch (NamingException e) { + // ok } } } if (ref == null) { - throw zxJDBC.makeException(zxJDBC.ProgrammingError, "object [" + jndiName + "] not found in JNDI"); + throw zxJDBC.makeException(zxJDBC.ProgrammingError, + "object [" + jndiName + "] not found in JNDI"); } try { @@ -122,14 +130,15 @@ } else if (ref instanceof DataSource) { connection = ((DataSource) ref).getConnection(); } else if (ref instanceof ConnectionPoolDataSource) { - connection = ((ConnectionPoolDataSource) ref).getPooledConnection().getConnection(); + connection = + ((ConnectionPoolDataSource) ref).getPooledConnection().getConnection(); } } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e); } try { - if ((connection == null) || connection.isClosed()) { + if (connection == null || connection.isClosed()) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); } @@ -144,7 +153,8 @@ * * @return String */ + @Override public String toString() { - return "<lookup object instance at " + Py.id(this) + ">"; + return String.format("<lookup object at %s>", Py.idstr(this)); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |