KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > util > BCP


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: BCP.java,v 1.2 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.util;
10
11 import com.ziclix.python.sql.PyConnection;
12 import com.ziclix.python.sql.pipe.Pipe;
13 import com.ziclix.python.sql.pipe.db.DBSink;
14 import com.ziclix.python.sql.pipe.db.DBSource;
15 import com.ziclix.python.sql.zxJDBC;
16 import org.python.core.ClassDictInit;
17 import org.python.core.Py;
18 import org.python.core.PyBuiltinFunctionSet;
19 import org.python.core.PyClass;
20 import org.python.core.PyList;
21 import org.python.core.PyObject;
22 import org.python.core.PyString;
23
24 /**
25  * A class to perform efficient Bulk CoPy of database tables.
26  */

27 public class BCP extends PyObject implements ClassDictInit {
28
29     /**
30      * Field sourceDH, destDH
31      */

32     protected Class JavaDoc sourceDH, destDH;
33
34     /**
35      * Field batchsize, queuesize
36      */

37     protected int batchsize, queuesize;
38
39     /**
40      * Field source, destination
41      */

42     protected PyConnection source, destination;
43
44     /**
45      * The source connection will produce the rows while the destination
46      * connection will consume the rows and coerce as necessary for the
47      * destination database.
48      */

49     public BCP(PyConnection source, PyConnection destination) {
50         this(source, destination, -1);
51     }
52
53     /**
54      * The source connection will produce the rows while the destination
55      * connection will consume the rows and coerce as necessary for the
56      * destination database.
57      *
58      * @param batchsize used to batch the inserts on the destination
59      */

60     public BCP(PyConnection source, PyConnection destination, int batchsize) {
61
62         this.source = source;
63         this.destination = destination;
64         this.destDH = null;
65         this.sourceDH = null;
66         this.batchsize = batchsize;
67         this.queuesize = 0;
68     }
69
70     // __class__ boilerplate -- see PyObject for details
71

72     /**
73      * Field __class__
74      */

75     public static PyClass __class__;
76
77     /**
78      * Method getPyClass
79      *
80      * @return PyClass
81      */

82     protected PyClass getPyClass() {
83         return __class__;
84     }
85
86     /**
87      * Field __methods__
88      */

89     protected static PyList __methods__;
90
91     /**
92      * Field __members__
93      */

94     protected static PyList __members__;
95
96     static {
97         PyObject[] m = new PyObject[1];
98
99         m[0] = new PyString("bcp");
100         __methods__ = new PyList(m);
101         m = new PyObject[6];
102         m[0] = new PyString("source");
103         m[1] = new PyString("destination");
104         m[2] = new PyString("batchsize");
105         m[3] = new PyString("queuesize");
106         m[4] = new PyString("sourceDataHandler");
107         m[5] = new PyString("destinationDataHandler");
108         __members__ = new PyList(m);
109     }
110
111     /**
112      * String representation of the object.
113      *
114      * @return a string representation of the object.
115      */

116     public String JavaDoc toString() {
117         return "<BCP object instance at " + hashCode() + ">";
118     }
119
120     /**
121      * Sets the attribute name to value.
122      *
123      * @param name
124      * @param value
125      */

126     public void __setattr__(String JavaDoc name, PyObject value) {
127
128         if ("destinationDataHandler".equals(name)) {
129             this.destDH = (Class JavaDoc) value.__tojava__(Class JavaDoc.class);
130         } else if ("sourceDataHandler".equals(name)) {
131             this.sourceDH = (Class JavaDoc) value.__tojava__(Class JavaDoc.class);
132         } else if ("batchsize".equals(name)) {
133             this.batchsize = ((Number JavaDoc) value.__tojava__(Number JavaDoc.class)).intValue();
134         } else if ("queuesize".equals(name)) {
135             this.queuesize = ((Number JavaDoc) value.__tojava__(Number JavaDoc.class)).intValue();
136         } else {
137             super.__setattr__(name, value);
138         }
139     }
140
141     /**
142      * Gets the value of the attribute name.
143      *
144      * @param name
145      * @return the attribute for the given name
146      */

147     public PyObject __findattr__(String JavaDoc name) {
148
149         if ("destinationDataHandler".equals(name)) {
150             return Py.java2py(this.destDH);
151         } else if ("sourceDataHandler".equals(name)) {
152             return Py.java2py(this.sourceDH);
153         } else if ("batchsize".equals(name)) {
154             return Py.newInteger(this.batchsize);
155         } else if ("queuesize".equals(name)) {
156             return Py.newInteger(this.queuesize);
157         }
158
159         return super.__findattr__(name);
160     }
161
162     /**
163      * Initializes the object's namespace.
164      *
165      * @param dict
166      */

167     static public void classDictInit(PyObject dict) {
168
169         dict.__setitem__("__version__", Py.newString("$Revision: 1.2 $").__getslice__(Py.newInteger(11), Py.newInteger(-2), null));
170         dict.__setitem__("bcp", new BCPFunc("bcp", 0, 1, 2, zxJDBC.getString("bcp")));
171         dict.__setitem__("batchsize", Py.newString(zxJDBC.getString("batchsize")));
172         dict.__setitem__("queuesize", Py.newString(zxJDBC.getString("queuesize")));
173
174         // hide from python
175
dict.__setitem__("classDictInit", null);
176         dict.__setitem__("toString", null);
177         dict.__setitem__("PyClass", null);
178         dict.__setitem__("getPyClass", null);
179         dict.__setitem__("sourceDH", null);
180         dict.__setitem__("destDH", null);
181     }
182
183     /**
184      * Bulkcopy data from one database to another.
185      *
186      * @param fromTable the table in question on the source database
187      * @param where an optional where clause, defaults to '(1=1)' if null
188      * @param params optional params to substituted in the where clause
189      * @param include the columns to be queried from the source, '*' if None
190      * @param exclude the columns to be excluded from insertion on the destination, all if None
191      * @param toTable if non-null, the table in the destination db, otherwise the same table name as the source
192      * @param bindings the optional bindings for the destination, this allows morphing of types during the copy
193      * @return the count of the total number of rows bulk copied, -1 if the query returned no rows
194      */

195     protected PyObject bcp(String JavaDoc fromTable, String JavaDoc where, PyObject params, PyObject include, PyObject exclude, String JavaDoc toTable, PyObject bindings) {
196
197         Pipe pipe = new Pipe();
198         String JavaDoc _toTable = (toTable == null) ? fromTable : toTable;
199         DBSource source = new DBSource(this.source, sourceDH, fromTable, where, include, params);
200         DBSink sink = new DBSink(this.destination, destDH, _toTable, exclude, bindings, this.batchsize);
201
202         return pipe.pipe(source, sink).__sub__(Py.newInteger(1));
203     }
204 }
205
206 /**
207  * Class BCPFunc
208  *
209  * @author
210  * @author last modified by $Author: bzimmer $
211  * @version $Revision: 1.2 $
212  * @date $today.date$
213  * @date last modified on $Date: 2005/02/23 04:26:20 $
214  * @copyright 2001 brian zimmer
215  */

216 class BCPFunc extends PyBuiltinFunctionSet {
217
218     /**
219      * Constructor BCPFunc
220      *
221      * @param name
222      * @param index
223      * @param argcount
224      * @param doc
225      */

226     BCPFunc(String JavaDoc name, int index, int argcount, String JavaDoc doc) {
227         super(name, index, argcount, argcount, true, doc);
228     }
229
230     /**
231      * Constructor BCPFunc
232      *
233      * @param name
234      * @param index
235      * @param minargs
236      * @param maxargs
237      * @param doc
238      */

239     BCPFunc(String JavaDoc name, int index, int minargs, int maxargs, String JavaDoc doc) {
240         super(name, index, minargs, maxargs, true, doc);
241     }
242
243     /**
244      * Method __call__
245      *
246      * @param arg
247      * @return PyObject
248      */

249     public PyObject __call__(PyObject arg) {
250
251         BCP bcp = (BCP) __self__;
252
253         switch (index) {
254
255             case 0:
256                 String JavaDoc table = (String JavaDoc) arg.__tojava__(String JavaDoc.class);
257
258                 if (table == null) {
259                     throw Py.ValueError(zxJDBC.getString("invalidTableName"));
260                 }
261
262                 PyObject count = bcp.bcp(table, null, Py.None, Py.None, Py.None, null, Py.None);
263
264                 return count;
265
266             default :
267                 throw argCountError(1);
268         }
269     }
270
271     public PyObject __call__(PyObject arga, PyObject argb) {
272
273         BCP bcp = (BCP) __self__;
274
275         switch (index) {
276
277             case 0:
278                 String JavaDoc table = (String JavaDoc) arga.__tojava__(String JavaDoc.class);
279
280                 if (table == null) {
281                     throw Py.ValueError(zxJDBC.getString("invalidTableName"));
282                 }
283
284                 String JavaDoc where = (String JavaDoc) argb.__tojava__(String JavaDoc.class);
285                 PyObject count = bcp.bcp(table, where, Py.None, Py.None, Py.None, null, Py.None);
286
287                 return count;
288
289             default :
290                 throw argCountError(2);
291         }
292     }
293
294     public PyObject __call__(PyObject arga, PyObject argb, PyObject argc) {
295
296         BCP bcp = (BCP) __self__;
297
298         switch (index) {
299
300             case 0:
301                 String JavaDoc table = (String JavaDoc) arga.__tojava__(String JavaDoc.class);
302
303                 if (table == null) {
304                     throw Py.ValueError(zxJDBC.getString("invalidTableName"));
305                 }
306
307                 String JavaDoc where = (String JavaDoc) argb.__tojava__(String JavaDoc.class);
308                 PyObject count = bcp.bcp(table, where, argc, Py.None, Py.None, null, Py.None);
309
310                 return count;
311
312             default :
313                 throw argCountError(3);
314         }
315     }
316
317     public PyObject __call__(PyObject[] args, String JavaDoc[] keywords) {
318
319         BCP bcp = (BCP) __self__;
320
321         switch (index) {
322
323             case 0:
324
325                 /*
326                  * B.bcp(table, [where=None, params=None, include=None, exclude=None, toTable=None, bindings=None])
327                  */

328                 String JavaDoc where = null;
329                 PyObject params = Py.None;
330                 PyArgParser parser = new PyArgParser(args, keywords);
331                 String JavaDoc table = (String JavaDoc) parser.arg(0, Py.None).__tojava__(String JavaDoc.class);
332
333                 if (table == null) {
334                     throw Py.ValueError(zxJDBC.getString("invalidTableName"));
335                 }
336
337                 // 'where' can be the second argument or a keyword
338
if (parser.numArg() >= 2) {
339                     where = (String JavaDoc) parser.arg(1, Py.None).__tojava__(String JavaDoc.class);
340                 }
341
342                 if (where == null) {
343                     where = (String JavaDoc) parser.kw("where", Py.None).__tojava__(String JavaDoc.class);
344                 }
345
346                 // 'params' can be the third argument or a keyword
347
if (parser.numArg() >= 3) {
348                     params = parser.arg(2, Py.None);
349                 }
350
351                 if (params == Py.None) {
352                     params = parser.kw("params", Py.None);
353                 }
354
355                 String JavaDoc toTable = (String JavaDoc) parser.kw("toTable", Py.None).__tojava__(String JavaDoc.class);
356                 PyObject include = parser.kw("include", Py.None);
357                 PyObject exclude = parser.kw("exclude", Py.None);
358                 PyObject bindings = parser.kw("bindings", Py.None);
359                 PyObject count = bcp.bcp(table, where, params, include, exclude, toTable, bindings);
360
361                 return count;
362
363             default :
364                 throw argCountError(3);
365         }
366     }
367 }
368
Popular Tags