1 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 27 public class BCP extends PyObject implements ClassDictInit { 28 29 32 protected Class sourceDH, destDH; 33 34 37 protected int batchsize, queuesize; 38 39 42 protected PyConnection source, destination; 43 44 49 public BCP(PyConnection source, PyConnection destination) { 50 this(source, destination, -1); 51 } 52 53 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 72 75 public static PyClass __class__; 76 77 82 protected PyClass getPyClass() { 83 return __class__; 84 } 85 86 89 protected static PyList __methods__; 90 91 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 116 public String toString() { 117 return "<BCP object instance at " + hashCode() + ">"; 118 } 119 120 126 public void __setattr__(String name, PyObject value) { 127 128 if ("destinationDataHandler".equals(name)) { 129 this.destDH = (Class ) value.__tojava__(Class .class); 130 } else if ("sourceDataHandler".equals(name)) { 131 this.sourceDH = (Class ) value.__tojava__(Class .class); 132 } else if ("batchsize".equals(name)) { 133 this.batchsize = ((Number ) value.__tojava__(Number .class)).intValue(); 134 } else if ("queuesize".equals(name)) { 135 this.queuesize = ((Number ) value.__tojava__(Number .class)).intValue(); 136 } else { 137 super.__setattr__(name, value); 138 } 139 } 140 141 147 public PyObject __findattr__(String 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 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 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 195 protected PyObject bcp(String fromTable, String where, PyObject params, PyObject include, PyObject exclude, String toTable, PyObject bindings) { 196 197 Pipe pipe = new Pipe(); 198 String _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 216 class BCPFunc extends PyBuiltinFunctionSet { 217 218 226 BCPFunc(String name, int index, int argcount, String doc) { 227 super(name, index, argcount, argcount, true, doc); 228 } 229 230 239 BCPFunc(String name, int index, int minargs, int maxargs, String doc) { 240 super(name, index, minargs, maxargs, true, doc); 241 } 242 243 249 public PyObject __call__(PyObject arg) { 250 251 BCP bcp = (BCP) __self__; 252 253 switch (index) { 254 255 case 0: 256 String table = (String ) arg.__tojava__(String .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 table = (String ) arga.__tojava__(String .class); 279 280 if (table == null) { 281 throw Py.ValueError(zxJDBC.getString("invalidTableName")); 282 } 283 284 String where = (String ) argb.__tojava__(String .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 table = (String ) arga.__tojava__(String .class); 302 303 if (table == null) { 304 throw Py.ValueError(zxJDBC.getString("invalidTableName")); 305 } 306 307 String where = (String ) argb.__tojava__(String .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 [] keywords) { 318 319 BCP bcp = (BCP) __self__; 320 321 switch (index) { 322 323 case 0: 324 325 328 String where = null; 329 PyObject params = Py.None; 330 PyArgParser parser = new PyArgParser(args, keywords); 331 String table = (String ) parser.arg(0, Py.None).__tojava__(String .class); 332 333 if (table == null) { 334 throw Py.ValueError(zxJDBC.getString("invalidTableName")); 335 } 336 337 if (parser.numArg() >= 2) { 339 where = (String ) parser.arg(1, Py.None).__tojava__(String .class); 340 } 341 342 if (where == null) { 343 where = (String ) parser.kw("where", Py.None).__tojava__(String .class); 344 } 345 346 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 toTable = (String ) parser.kw("toTable", Py.None).__tojava__(String .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 |