1 9 package com.ziclix.python.sql.connect; 10 11 import java.sql.*; 12 import java.util.*; 13 import javax.sql.*; 14 import java.lang.reflect.*; 15 16 import org.python.core.*; 17 import com.ziclix.python.sql.*; 18 import com.ziclix.python.sql.util.*; 19 20 27 public class Connectx extends PyObject { 28 29 private final String SET = "set"; 30 private final PyString doc = new PyString("establish a connection through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource"); 31 32 35 public Connectx() { 36 } 37 38 44 public PyObject __findattr__(String name) { 45 46 if ("__doc__".equals(name)) { 47 return doc; 48 } 49 50 return super.__findattr__(name); 51 } 52 53 56 public PyObject __call__(PyObject[] args, String [] keywords) { 57 58 Connection c = null; 59 PyConnection pc = null; 60 Object datasource = null; 61 PyArgParser parser = new PyArgParser(args, keywords); 62 63 try { 64 String _class = (String ) parser.arg(0).__tojava__(String .class); 65 66 datasource = Class.forName(_class).newInstance(); 67 } catch (Exception e) { 68 throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to instantiate datasource"); 69 } 70 71 String [] kws = parser.kws(); 72 Class clazz = datasource.getClass(); 73 74 for (int i = 0; i < kws.length; i++) { 75 String methodName = kws[i]; 76 77 if (methodName == null) { 78 continue; 79 } 80 81 Object value = parser.kw(kws[i]).__tojava__(Object .class); 82 83 if (methodName.length() > SET.length()) { 84 if (!SET.equals(methodName.substring(0, SET.length()))) { 85 86 invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value); 88 } else { 89 90 invoke(datasource, methodName, value); 92 } 93 } else { 94 95 invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value); 97 } 98 } 99 100 try { 101 if (datasource instanceof ConnectionPoolDataSource) { 102 c = ((ConnectionPoolDataSource) datasource).getPooledConnection().getConnection(); 103 } else if (datasource instanceof DataSource) { 104 c = ((DataSource) datasource).getConnection(); 105 } 106 } catch (SQLException e) { 107 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 108 } 109 110 try { 111 if ((c == null) || c.isClosed()) { 112 throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); 113 } 114 115 pc = new PyConnection(c); 116 } catch (SQLException e) { 117 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 118 } 119 120 return pc; 121 } 122 123 128 public String toString() { 129 return "<connectx object instance at " + Py.id(this) + ">"; 130 } 131 132 139 protected void invoke(Object src, String methodName, Object value) { 140 141 Method method = null; 142 StringBuffer exceptionMsg = new StringBuffer ("method [").append(methodName).append("] using arg type ["); 143 144 exceptionMsg.append(value.getClass()).append("], value [").append(value.toString()).append("]"); 145 146 try { 147 method = getMethod(src.getClass(), methodName, value.getClass()); 148 149 if (method == null) { 150 throw zxJDBC.makeException("no such " + exceptionMsg); 151 } 152 153 method.invoke(src, new Object []{value}); 154 } catch (IllegalAccessException e) { 155 throw zxJDBC.makeException("illegal access for " + exceptionMsg); 156 } catch (InvocationTargetException e) { 157 throw zxJDBC.makeException("invocation target exception for " + exceptionMsg); 158 } 159 160 return; 161 } 162 163 168 protected Method getMethod(Class srcClass, String methodName, Class valueClass) { 169 170 Method method = null; 171 172 try { 173 method = srcClass.getMethod(methodName, new Class []{valueClass}); 174 } catch (NoSuchMethodException e) { 175 Class primitive = null; 176 177 try { 178 Field f = valueClass.getField("TYPE"); 179 180 primitive = (Class ) f.get(valueClass); 181 } catch (NoSuchFieldException ex) { 182 } catch (IllegalAccessException ex) { 183 } catch (ClassCastException ex) { 184 } 185 186 if ((primitive != null) && primitive.isPrimitive()) { 187 return getMethod(srcClass, methodName, primitive); 188 } 189 } 190 191 return method; 192 } 193 194 196 199 public static PyClass __class__; 200 201 206 protected PyClass getPyClass() { 207 return __class__; 208 } 209 } 210 | Popular Tags |