KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > pipe > db > DBSource


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: DBSource.java,v 1.3 2005/02/23 04:26:20 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.pipe.db;
10
11 import com.ziclix.python.sql.PyConnection;
12 import com.ziclix.python.sql.pipe.Source;
13 import org.python.core.Py;
14 import org.python.core.PyObject;
15 import org.python.core.PyTuple;
16
17 /**
18  * A database source. Given a PyConnection and information about the query, produce the data.
19  *
20  * @author brian zimmer
21  * @version $Revision: 1.3 $
22  */

23 public class DBSource extends BaseDB implements Source {
24
25     /**
26      * Field sql
27      */

28     protected String JavaDoc sql;
29
30     /**
31      * Field sentHeader
32      */

33     protected boolean sentHeader;
34
35     /**
36      * Field params, include
37      */

38     protected PyObject params, include;
39
40     /**
41      * Constructor for handling the generation of data.
42      *
43      * @param connection the database connection
44      * @param dataHandler a custom DataHandler for the cursor, can be None
45      * @param tableName the table in question on the source database
46      * @param where an optional where clause, defaults to '(1=1)' if null
47      * @param include the columns to be queried from the source, '*' if None
48      * @param params optional params to substituted in the where clause
49      */

50     public DBSource(PyConnection connection, Class JavaDoc dataHandler, String JavaDoc tableName, String JavaDoc where, PyObject include, PyObject params) {
51
52         super(connection, dataHandler, tableName);
53
54         this.params = params;
55         this.include = include;
56         this.sentHeader = false;
57         this.sql = this.createSql(where);
58     }
59
60     /**
61      * Create the sql string given the where clause.
62      */

63     protected String JavaDoc createSql(String JavaDoc where) {
64
65         // create the sql statement, using the columns if available
66
StringBuffer JavaDoc sb = new StringBuffer JavaDoc("select ");
67
68         if ((this.include == Py.None) || (this.include.__len__() == 0)) {
69             sb.append("*");
70         } else {
71             for (int i = 1; i < this.include.__len__(); i++) {
72                 sb.append(this.include.__getitem__(i)).append(",");
73             }
74
75             sb.append(this.include.__getitem__(this.include.__len__() - 1));
76         }
77
78         sb.append(" from ").append(this.tableName);
79         sb.append(" where ").append((where == null) ? "(1=1)" : where);
80
81         String JavaDoc sql = sb.toString();
82
83         return sql;
84     }
85
86     /**
87      * Return the next row in the result set. The first row returned will be column information.
88      */

89     public PyObject next() {
90
91         if (this.sentHeader) {
92
93             // Py.None will be sent when all done, so this will close down the queue
94
return this.cursor.fetchone();
95         } else {
96             this.cursor.execute(Py.newString(this.sql), this.params, Py.None, Py.None);
97
98             PyObject description = this.cursor.__findattr__("description");
99
100             // we can't insert if we don't know column names
101
if ((description == Py.None) || (description.__len__() == 0)) {
102
103                 // let the destination worry about handling the empty set
104
return Py.None;
105             }
106
107             int len = description.__len__();
108             PyObject[] columns = new PyObject[len];
109
110             for (int i = 0; i < len; i++) {
111                 PyObject[] colInfo = new PyObject[2];
112
113                 // col name
114
colInfo[0] = description.__getitem__(i).__getitem__(0);
115
116                 // col type
117
colInfo[1] = description.__getitem__(i).__getitem__(1);
118                 columns[i] = new PyTuple(colInfo);
119             }
120
121             PyObject row = new PyTuple(columns);
122
123             Py.writeDebug("db-source", row.toString());
124
125             this.sentHeader = true;
126
127             return row;
128         }
129     }
130
131     /**
132      * Method start
133      */

134     public void start() {
135     }
136
137     /**
138      * Close the cursor.
139      */

140     public void end() {
141
142         if (this.cursor != null) {
143             this.cursor.close();
144         }
145     }
146 }
147
Popular Tags