KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > PyStatement


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: PyStatement.java,v 1.6 2005/06/20 17:12:13 fwierzbicki Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql;
10
11 import org.python.core.Py;
12 import org.python.core.PyClass;
13 import org.python.core.PyException;
14 import org.python.core.PyInteger;
15 import org.python.core.PyList;
16 import org.python.core.PyObject;
17 import org.python.core.PyString;
18
19 import java.sql.CallableStatement JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.Statement JavaDoc;
23
24 /**
25  * Class PyStatement
26  *
27  * @author brian zimmer
28  * @version $Revision: 1.6 $
29  */

30 public class PyStatement extends PyObject {
31
32     /**
33      * Field STATEMENT_STATIC
34      */

35     public static final int STATEMENT_STATIC = 2;
36
37     /**
38      * Field STATEMENT_PREPARED
39      */

40     public static final int STATEMENT_PREPARED = 4;
41
42     /**
43      * Field STATEMENT_CALLABLE
44      */

45     public static final int STATEMENT_CALLABLE = 8;
46
47     /**
48      * Field style
49      */

50     private int style;
51
52     /**
53      * Field sql
54      */

55     private Object JavaDoc sql;
56
57     /**
58      * Field closed
59      */

60     private boolean closed;
61
62     /**
63      * Field statement
64      */

65     Statement JavaDoc statement;
66
67     /**
68      * Constructor PyStatement
69      *
70      * @param statement
71      * @param sql
72      * @param style
73      */

74     public PyStatement(Statement JavaDoc statement, Object JavaDoc sql, int style) {
75
76         this.sql = sql;
77         this.style = style;
78         this.closed = false;
79         this.statement = statement;
80     }
81
82     /**
83      * Constructor PyStatement
84      *
85      * @param statement
86      * @param procedure
87      */

88     public PyStatement(Statement JavaDoc statement, Procedure procedure) {
89         this(statement, procedure, STATEMENT_CALLABLE);
90     }
91
92     /**
93      * Field __class__
94      */

95     public static PyClass __class__;
96
97     /**
98      * Method getPyClass
99      *
100      * @return PyClass
101      */

102     protected PyClass getPyClass() {
103         return __class__;
104     }
105
106     /**
107      * Field __methods__
108      */

109     protected static PyList __methods__;
110
111     /**
112      * Field __members__
113      */

114     protected static PyList __members__;
115
116     static {
117         PyObject[] m = new PyObject[1];
118
119         m[0] = new PyString("close");
120         __methods__ = new PyList(m);
121         m = new PyObject[3];
122         m[0] = new PyString("style");
123         m[1] = new PyString("closed");
124         m[2] = new PyString("__statement__");
125         __members__ = new PyList(m);
126     }
127
128     /**
129      * Method __str__
130      *
131      * @return PyString
132      */

133     public PyString __str__() {
134
135         if (sql instanceof String JavaDoc) {
136             return Py.newString((String JavaDoc) sql);
137         } else if (sql instanceof Procedure) {
138             try {
139                 return Py.newString(((Procedure) sql).toSql());
140             } catch (SQLException JavaDoc e) {
141                 throw zxJDBC.makeException(e);
142             }
143         }
144
145         return super.__str__();
146     }
147
148     /**
149      * Method __repr__
150      *
151      * @return PyString
152      */

153     public PyString __repr__() {
154
155         // care is taken not to display a rounded second value
156
StringBuffer JavaDoc buf = new StringBuffer JavaDoc("<PyStatement object for [");
157
158         buf.append(__str__().toString());
159         buf.append("] at ").append(Py.id(this)).append(">");
160
161         return Py.newString(buf.toString());
162     }
163
164     /**
165      * Method toString
166      *
167      * @return String
168      */

169     public String JavaDoc toString() {
170         return __repr__().toString();
171     }
172
173     /**
174      * Gets the value of the attribute name.
175      *
176      * @param name
177      * @return the attribute for the given name
178      */

179     public PyObject __findattr__(String JavaDoc name) {
180
181         if ("style".equals(name)) {
182             return Py.newInteger(style);
183         } else if ("closed".equals(name)) {
184             return Py.newBoolean(closed);
185         } else if ("__statement__".equals(name)) {
186             return Py.java2py(statement);
187         } else if ("__methods__".equals(name)) {
188             return __methods__;
189         } else if ("__members__".equals(name)) {
190             return __members__;
191         }
192
193         return super.__findattr__(name);
194     }
195
196     /**
197      * Initializes the object's namespace.
198      *
199      * @param dict
200      */

201     static public void classDictInit(PyObject dict) {
202
203         dict.__setitem__("__version__", Py.newString("$Revision: 1.6 $").__getslice__(Py.newInteger(11), Py.newInteger(-2), null));
204
205         // hide from python
206
dict.__setitem__("classDictInit", null);
207         dict.__setitem__("statement", null);
208         dict.__setitem__("execute", null);
209         dict.__setitem__("prepare", null);
210         dict.__setitem__("STATEMENT_STATIC", null);
211         dict.__setitem__("STATEMENT_PREPARED", null);
212         dict.__setitem__("STATEMENT_CALLABLE", null);
213     }
214
215     /**
216      * Delete the statement.
217      */

218     public void __del__() {
219         close();
220     }
221
222     /**
223      * Method execute
224      *
225      * @param cursor
226      * @param params
227      * @param bindings
228      * @throws SQLException
229      */

230     public void execute(PyCursor cursor, PyObject params, PyObject bindings) throws SQLException JavaDoc {
231
232         if (this.closed) {
233             throw zxJDBC.makeException(zxJDBC.ProgrammingError, "statement is closed");
234         }
235
236         this.prepare(cursor, params, bindings);
237
238         Fetch fetch = cursor.fetch;
239
240         switch (this.style) {
241
242             case STATEMENT_STATIC:
243                 if (this.statement.execute((String JavaDoc) this.sql)) {
244                     fetch.add(this.statement.getResultSet());
245                 }
246                 break;
247
248             case STATEMENT_PREPARED:
249                 final PreparedStatement JavaDoc preparedStatement = (PreparedStatement JavaDoc) this.statement;
250
251                 if (preparedStatement.execute()) {
252                     fetch.add(preparedStatement.getResultSet());
253                 }
254                 break;
255
256             case STATEMENT_CALLABLE:
257                 final CallableStatement JavaDoc callableStatement = (CallableStatement JavaDoc) this.statement;
258
259                 if (callableStatement.execute()) {
260                     fetch.add(callableStatement.getResultSet());
261                 }
262
263                 fetch.add(callableStatement, (Procedure) sql, params);
264                 break;
265
266             default :
267                 throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("invalidStyle"));
268         }
269     }
270
271     /**
272      * Method prepare
273      *
274      * @param cursor
275      * @param params
276      * @param bindings
277      * @throws SQLException
278      */

279     private void prepare(PyCursor cursor, PyObject params, PyObject bindings) throws SQLException JavaDoc {
280
281         if ((params == Py.None) || (this.style == STATEMENT_STATIC)) {
282             return;
283         }
284
285         // [3, 4] or (3, 4)
286
final DataHandler datahandler = cursor.datahandler;
287         int columns = 0, column = 0, index = params.__len__();
288         final PreparedStatement JavaDoc preparedStatement = (PreparedStatement JavaDoc) statement;
289         final Procedure procedure = (this.style == STATEMENT_CALLABLE) ? (Procedure) this.sql : null;
290
291         if (this.style != STATEMENT_CALLABLE) {
292             columns = params.__len__();
293
294             // clear the statement so all new bindings take affect only if not a callproc
295
// this is because Procedure already registered the OUT parameters and we
296
// don't want to lose those
297
preparedStatement.clearParameters();
298         } else {
299             columns = (procedure.columns == Py.None) ? 0 : procedure.columns.__len__();
300         }
301
302         // count backwards through all the columns
303
while (columns-- > 0) {
304             column = columns + 1;
305
306             if ((procedure != null) && (!procedure.isInput(column))) {
307                 continue;
308             }
309
310             // working from right to left
311
PyObject param = params.__getitem__(--index);
312
313             if (bindings != Py.None) {
314                 PyObject binding = bindings.__finditem__(Py.newInteger(index));
315
316                 if (binding != null) {
317                     try {
318                         int bindingValue = ((PyInteger)binding.__int__()).getValue();
319
320                         datahandler.setJDBCObject(preparedStatement, column, param, bindingValue);
321                     } catch (PyException e) {
322                         throw zxJDBC.makeException(zxJDBC.ProgrammingError, zxJDBC.getString("bindingValue"));
323                     }
324
325                     continue;
326                 }
327             }
328
329             datahandler.setJDBCObject(preparedStatement, column, param);
330         }
331
332         return;
333     }
334
335     /**
336      * Method close
337      */

338     public void close() {
339
340         try {
341             this.statement.close();
342         } catch (SQLException JavaDoc e) {
343             throw zxJDBC.makeException(e);
344         } finally {
345             this.closed = true;
346         }
347     }
348 }
349
Popular Tags