001: /*
002: * Jython Database Specification API 2.0
003: *
004: * $Id: Connect.java 2414 2005-02-23 04:26:23Z bzimmer $
005: *
006: * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
007: *
008: */
009: package com.ziclix.python.sql.connect;
010:
011: import java.sql.*;
012: import java.util.*;
013:
014: import org.python.core.*;
015: import com.ziclix.python.sql.*;
016: import com.ziclix.python.sql.util.*;
017:
018: /**
019: * Connect using DriverManager.
020: *
021: * @author brian zimmer
022: * @author last revised by $Author: bzimmer $
023: * @version $Revision: 2414 $
024: */
025: public class Connect extends PyObject {
026:
027: private static final PyString _doc = new PyString(
028: "establish a connection through java.sql.DriverManager");
029:
030: /**
031: * Default empty constructor.
032: */
033: public Connect() {
034: }
035:
036: /**
037: * Method __findattr__
038: *
039: * @param String name
040: * @return PyObject
041: */
042: public PyObject __findattr__(String name) {
043:
044: if ("__doc__".equals(name)) {
045: return _doc;
046: }
047:
048: return super .__findattr__(name);
049: }
050:
051: /**
052: * Establish a connection through DriverManager.
053: */
054: public PyObject __call__(PyObject[] args, String[] keywords) {
055:
056: Connection c = null;
057: PyArgParser parser = new PyArgParser(args, keywords);
058: Object arg = parser.arg(0).__tojava__(Connection.class);
059:
060: if (arg == Py.NoConversion) {
061: Properties props = new Properties();
062: String url = (String) parser.arg(0)
063: .__tojava__(String.class);
064: String user = (String) parser.arg(1).__tojava__(
065: String.class);
066: String password = (String) parser.arg(2).__tojava__(
067: String.class);
068: String driver = (String) parser.arg(3).__tojava__(
069: String.class);
070:
071: if (url == null) {
072: throw zxJDBC.makeException(zxJDBC.DatabaseError,
073: "no url specified");
074: }
075:
076: if (driver == null) {
077: throw zxJDBC.makeException(zxJDBC.DatabaseError,
078: "no driver specified");
079: }
080:
081: // the value can't be null
082: props.put("user", (user == null) ? "" : user);
083: props.put("password", (password == null) ? "" : password);
084:
085: String[] kws = parser.kws();
086:
087: for (int i = 0; i < kws.length; i++) {
088: Object value = parser.kw(kws[i]).__tojava__(
089: Object.class);
090:
091: props.put(kws[i], value);
092: }
093:
094: try {
095: Class.forName(driver);
096: } catch (Throwable e) {
097: throw zxJDBC.makeException(zxJDBC.DatabaseError,
098: "driver [" + driver + "] not found");
099: }
100:
101: try {
102: c = DriverManager.getConnection(url, props);
103: } catch (SQLException e) {
104: throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
105: }
106: } else {
107: c = (Connection) arg;
108: }
109:
110: try {
111: if ((c == null) || c.isClosed()) {
112: throw zxJDBC.makeException(zxJDBC.DatabaseError,
113: "unable to establish connection");
114: }
115:
116: return new PyConnection(c);
117: } catch (SQLException e) {
118: throw zxJDBC.makeException(zxJDBC.DatabaseError, e);
119: }
120: }
121:
122: /**
123: * Method toString
124: *
125: * @return String
126: */
127: public String toString() {
128: return "<connect object instance at " + Py.id(this ) + ">";
129: }
130:
131: // __class__ boilerplate -- see PyObject for details
132:
133: /**
134: * Field __class__
135: */
136: public static PyClass __class__;
137:
138: /**
139: * Method getPyClass
140: *
141: * @return PyClass
142: */
143: protected PyClass getPyClass() {
144: return __class__;
145: }
146: }
|