1 9 package com.ziclix.python.sql.connect; 10 11 import java.sql.*; 12 import java.util.*; 13 14 import org.python.core.*; 15 import com.ziclix.python.sql.*; 16 import com.ziclix.python.sql.util.*; 17 18 25 public class Connect extends PyObject { 26 27 private static final PyString _doc = new PyString("establish a connection through java.sql.DriverManager"); 28 29 32 public Connect() { 33 } 34 35 41 public PyObject __findattr__(String name) { 42 43 if ("__doc__".equals(name)) { 44 return _doc; 45 } 46 47 return super.__findattr__(name); 48 } 49 50 53 public PyObject __call__(PyObject[] args, String [] keywords) { 54 55 Connection c = null; 56 PyArgParser parser = new PyArgParser(args, keywords); 57 Object arg = parser.arg(0).__tojava__(Connection.class); 58 59 if (arg == Py.NoConversion) { 60 Properties props = new Properties(); 61 String url = (String ) parser.arg(0).__tojava__(String .class); 62 String user = (String ) parser.arg(1).__tojava__(String .class); 63 String password = (String ) parser.arg(2).__tojava__(String .class); 64 String driver = (String ) parser.arg(3).__tojava__(String .class); 65 66 if (url == null) { 67 throw zxJDBC.makeException(zxJDBC.DatabaseError, "no url specified"); 68 } 69 70 if (driver == null) { 71 throw zxJDBC.makeException(zxJDBC.DatabaseError, "no driver specified"); 72 } 73 74 props.put("user", (user == null) ? "" : user); 76 props.put("password", (password == null) ? "" : password); 77 78 String [] kws = parser.kws(); 79 80 for (int i = 0; i < kws.length; i++) { 81 Object value = parser.kw(kws[i]).__tojava__(Object .class); 82 83 props.put(kws[i], value); 84 } 85 86 try { 87 Class.forName(driver); 88 } catch (Throwable e) { 89 throw zxJDBC.makeException(zxJDBC.DatabaseError, "driver [" + driver + "] not found"); 90 } 91 92 try { 93 c = DriverManager.getConnection(url, props); 94 } catch (SQLException e) { 95 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 96 } 97 } else { 98 c = (Connection) arg; 99 } 100 101 try { 102 if ((c == null) || c.isClosed()) { 103 throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); 104 } 105 106 return new PyConnection(c); 107 } catch (SQLException e) { 108 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 109 } 110 } 111 112 117 public String toString() { 118 return "<connect object instance at " + Py.id(this) + ">"; 119 } 120 121 123 126 public static PyClass __class__; 127 128 133 protected PyClass getPyClass() { 134 return __class__; 135 } 136 } 137 | Popular Tags |