DCL 4.1
Loading...
Searching...
No Matches
EntityCopy Class Reference

#include <EntityCopy.h>

Public Member Functions

 EntityCopy (const MainArguments &_args, const String &_srcDriver, const String &_dstDriver) __DCL_THROWS1(SQLDriverException *)
bool initialize () __DCL_THROWS1(SQLException *)
void doIt ()

Protected Member Functions

String getSelectSQL (const Entity &_e)
String getInsertSQL (const Entity &_e, const SQLFields &_fields)
String getTruncateSQL (const Entity &_e)
void translateFieldValue (SQLField &_field, SQLParam &_param) __DCL_THROWS1(SQLException *)
void copyEntity (const Entity &_e) __DCL_THROWS1(SQLException *)

Protected Attributes

const MainArguments__args
SQLConnection __srcConn
SQLConnection __dstConn
const DatabaseHelper__srcHelper
const DatabaseHelper__dstHelper
Array< Entity__entities
String __lineRuler
String __lineIndent

Detailed Description

Definition at line 38 of file EntityCopy.h.

Constructor & Destructor Documentation

◆ EntityCopy()

EntityCopy::EntityCopy ( const MainArguments & _args,
const String & _srcDriver,
const String & _dstDriver )

Definition at line 117 of file EntityCopy.cpp.

119 : __args(_args), __srcConn(_srcDriver), __dstConn(_dstDriver)
120 __DCL_THROWS2(SQLDriverException*, SQLException*)
121{
122 __srcHelper = __get_helper(_srcDriver);
123 __dstHelper = __get_helper(_dstDriver);
124
126 __DCL_TRACE2_N(L"helper[%ls][%ls]\n", __srcHelper->driverName, __dstHelper->driverName);
127
128 __lineRuler.assign(L'-', 80);
129 __lineIndent.assign(L' ', 4);
130}
#define NULL
Definition Config.h:340
#define __DCL_THROWS2(e1, e2)
Definition Config.h:168
#define __DCL_TRACE2_N(fmt, arg1, arg2)
#define __DCL_ASSERT(expr)
Definition Object.h:371
const MainArguments & __args
Definition EntityCopy.h:51
SQLConnection __dstConn
Definition EntityCopy.h:53
const DatabaseHelper * __srcHelper
Definition EntityCopy.h:55
String __lineIndent
Definition EntityCopy.h:71
SQLConnection __srcConn
Definition EntityCopy.h:52
const DatabaseHelper * __dstHelper
Definition EntityCopy.h:56
String __lineRuler
Definition EntityCopy.h:70

Member Function Documentation

◆ copyEntity()

void EntityCopy::copyEntity ( const Entity & _e)
protected

Definition at line 405 of file EntityCopy.cpp.

407{
408 DateTime begin = DateTime::getCurrentLocalTime();
409 __args.output() << __lineRuler << endl
410 << _e.name << L" [" << begin.toString()
411 << L"]" << endl;
412
413 __srcConn.startTrans();
414 String selectSQL = getSelectSQL(_e);
415 SQLQuery select(__srcConn);
416 select.execute(selectSQL);
417
418 String insertSQL = getInsertSQL(_e, select.fields());
419 String truncateSQL = getTruncateSQL(_e);
420
421 if (__args.truncate()) {
422 if (__args.verbose()) {
423 __args.output() << __lineIndent << truncateSQL << endl;
424 }
425 if (!__args.dryrun()) {
426 __dstConn.startTrans();
427 __dstConn.execute(truncateSQL);
428 __dstConn.commitTrans();
429 }
430 }
431
432 if (__args.verbose()) {
433 __verbose_out(__args.output(), selectSQL, __lineIndent);
434 __verbose_out(__args.output(), insertSQL, __lineIndent);
435 }
436
437 __dstConn.startTrans();
438
439 SQLQuery insert(__dstConn);
440 insert.prepare(insertSQL);
441
442 __DCL_ASSERT(select.fields().count() == insert.params().count());
443
444 select.fetch();
445 int rows = 0;
446 while (!select.eof()) {
447 rows++;
448
449 for (size_t i = 0; i < select.fields().count(); i++) {
450 SQLField& field = select.fields()[i];
451 SQLParam& param = insert.params()[i];
452 if (field.isNull()) {
453 param.setNull();
454 }
455 else {
456 try {
457 translateFieldValue(field, param);
458 }
459 catch (Exception* e) {
460 __DCL_TRACE4(L"%ls[%d][%ls] [%ls]\n", _e.name.data(), rows,
461 field.name().data(), e->toStringAll().data());
462 throw e;
463 }
464 }
465 }
466
467 if (!__args.dryrun()) {
468 insert.execute();
469 //__DCL_TRACE1_N(L"affectedRows[%zd]\n", insert.affectedRows());
470 if (__args.rows() > 0 && (rows % __args.rows()) == 0) {
471 __dstConn.commitTrans();
472 __dstConn.startTrans();
473 insert.prepare(insertSQL);
474 if (__args.verbose()) {
475 __args.output() << __lineIndent << L"COMMIT " << rows << endl;
476 }
477 }
478 }
479 select.fetch();
480 }
481
482 __dstConn.commitTrans();
483 __srcConn.commitTrans();
484
485 DateTime end = DateTime::getCurrentLocalTime();
486 __args.output() << _e.name
487 << L" [" << rows << L" rows transfered. " << (end - begin).toString()
488 << L"]" << endl;
489}
#define __DCL_TRACE4(fmt, arg1, arg2, arg3, arg4)
Definition Object.h:379
#define endl
String toString() const
Definition DateTime.cpp:826
static DateTime getCurrentLocalTime()
Definition DateTime.cpp:937
String getInsertSQL(const Entity &_e, const SQLFields &_fields)
String getTruncateSQL(const Entity &_e)
void translateFieldValue(SQLField &_field, SQLParam &_param) __DCL_THROWS1(SQLException *)
String getSelectSQL(const Entity &_e)
String toStringAll() const
Definition Exception.cpp:45
String name
Definition EntityCopy.h:27

◆ doIt()

void EntityCopy::doIt ( )

Definition at line 491 of file EntityCopy.cpp.

492{
493 for (size_t i = 0; i < __entities.size(); i++) {
495 }
496}
void copyEntity(const Entity &_e) __DCL_THROWS1(SQLException *)
Array< Entity > __entities
Definition EntityCopy.h:58

◆ getInsertSQL()

String EntityCopy::getInsertSQL ( const Entity & _e,
const SQLFields & _fields )
protected

Definition at line 508 of file EntityCopy.cpp.

509{
510 StringBuilder sb = L"INSERT INTO ";
511 sb.append(!_e.toName.isEmpty() ? _e.toName : _e.name)
512 .append(L"(");
513 if (!_e.toColumns.isEmpty()) {
514 sb.append(_e.toColumns);
515 }
516 else if (!_e.columns.isEmpty()) {
517 sb.append(_e.columns);
518 }
519 else {
520 sb.append(_fields[0].name());
521 for (size_t i = 1; i < _fields.count(); i++) {
522 sb.append(L",").append(_fields[i].name());
523 }
524 }
525 sb.append(L") VALUES (?");
526
527 for (size_t i = 1; i < _fields.count(); i++) {
528 sb.append(L",?");
529 }
530 sb.append(L")");
531
532 return sb;
533}
size_t count() const
Definition SQL.inl:93
String toName
Definition EntityCopy.h:29
String toColumns
Definition EntityCopy.h:30
String columns
Definition EntityCopy.h:28

◆ getSelectSQL()

String EntityCopy::getSelectSQL ( const Entity & _e)
protected

Definition at line 498 of file EntityCopy.cpp.

499{
500 StringBuilder sb = L"SELECT ";
501 sb.append(!_e.columns.isEmpty() ? _e.columns : L"*")
502 .append(L" FROM ")
503 .append(_e.name);
504
505 return sb;
506}

◆ getTruncateSQL()

String EntityCopy::getTruncateSQL ( const Entity & _e)
protected

Definition at line 535 of file EntityCopy.cpp.

536{
537 StringBuilder sb = __dstHelper->truncateTable;
538 sb.append(!_e.toName.isEmpty() ? _e.toName : _e.name);
539 return sb;
540}

◆ initialize()

bool EntityCopy::initialize ( )

Definition at line 182 of file EntityCopy.cpp.

184{
185 try {
186 __srcConn.open(__args.source());
187 __args.output() << L"Source database connected. ["
188 << __args.source() << L"]" << endl;
189
190 __dstConn.open(__args.destination());
191 __args.output() << L"Destination database connected. ["
192 << __args.destination() << L"]" << endl;
193 }
194 catch (Exception* _e) {
195 __args.output() << L"Database connection failed. ["
196 << _e->toStringAll() << L"]" << endl;
197 _e->destroy();
198 return false;
199 }
200
201 if (!__args.entities().isEmpty()) {
202 StringArray a;
203 __args.entities().split(L';', a);
204 for (size_t i = 0; i < a.size(); i++) {
205 const String& s = a[i];
206 Entity e;
207 if (s.contains(L'=')) {
208 StringArray pair;
209 s.split(L'=', pair);
210 __get_name_columns(pair[0], e.name, e.columns);
211 __get_name_columns(pair[1], e.toName, e.toColumns);
212 }
213 else {
214 __get_name_columns(s, e.name, e.columns);
215 }
216 __entities.add(e);
217 __DCL_TRACE1_N(L"entity[%ls]\n", e.toString().data());
218 }
219 }
220 else {
221 Array<Entity> srcEntities;
222 Array<Entity> dstEntities;
223
224 String srcTableSQL = __srcHelper->selectTableList;
225 String dstTableSQL = __dstHelper->selectTableList;
226
227 if (__args.verbose()) {
228 __args.output() << L"Source entities SQL" << endl;
229 __verbose_out(__args.output(), srcTableSQL, __lineIndent);
230 __args.output() << L"Destination entities SQL" << endl;
231 __verbose_out(__args.output(), dstTableSQL, __lineIndent);
232 }
233
234 {
235 __srcConn.startTrans();
236 SQLQuery q(__srcConn);
237 q.execute(srcTableSQL);
238 q.fetch();
239 while (!q.eof()) {
240 Entity e;
241 e.name = q.fields()[0].asString().trim();
242 srcEntities.add(e);
243 q.fetch();
244 }
245 __srcConn.commitTrans();
246 }
247
248 {
249 __dstConn.startTrans();
250 SQLQuery q(__dstConn);
251 q.execute(dstTableSQL);
252 q.fetch();
253 while (!q.eof()) {
254 Entity e;
255 e.name = q.fields()[0].asString().trim();
256 dstEntities.add(e);
257 q.fetch();
258 }
259 __dstConn.commitTrans();
260 }
261
262 if (srcEntities.size() < dstEntities.size()) {
263 __entities = srcEntities;
264 }
265 else {
266 __entities = dstEntities;
267 }
268#if __TRACE_THIS
269 StringBuilder sb = L"{";
270 for (size_t i = 0; i < __entities.size(); i++) {
271 if (i > 0) {
272 sb.append(L", ");
273 }
274 sb.append(__entities[i].name);
275 }
276 sb.append(L"}");
277 __DCL_TRACE2_N(L"__entities[%ls][%ls]\n",
278 srcEntities.size() < dstEntities.size() ? L"src" : L"dst",
279 sb.toString().data());
280#endif
281 }
282 return __entities.size() > 0;
283}
#define __DCL_TRACE1_N(fmt, arg)
size_t size() const
Definition ArrayT.h:197
Array< ELEMENT > & add(const ELEMENT &_element)
Definition ArrayT.h:144
virtual void destroy()
Definition Exception.cpp:74
String toString()

◆ translateFieldValue()

void EntityCopy::translateFieldValue ( SQLField & _field,
SQLParam & _param )
protected

Definition at line 285 of file EntityCopy.cpp.

287{
288 switch (_field.dataType()) {
289 case SQL::typeInteger: {
290 if (_field.dataSizeMax() <= sizeof(int32_t)) {
291 int32_t value = 0;
292 _field.getValue(value);
293 _param.setValue(value);
294 }
295 else {
296 int64_t value = 0;
297 _field.getValue(value);
298 _param.setValue(value);
299 }
300 break;
301 }
302 case SQL::typeUInteger: {
303 if (_field.dataSizeMax() <= sizeof(uint32_t)) {
304 uint32_t value = 0;
305 _field.getValue(value);
306 _param.setValue(value);
307 }
308 else {
309 uint64_t value = 0;
310 _field.getValue(value);
311 _param.setValue(value);
312 }
313 break;
314 }
315 case SQL::typeFloat: {
316 if (_field.dataSizeMax() == sizeof(float)) {
317 float value = 0.;
318 _field.getValue(value);
319 _param.setValue(value);
320 }
321 else {
322 double value = 0.;
323 _field.getValue(value);
324 _param.setValue(value);
325 }
326 break;
327 }
328 case SQL::typeNumeric: {
329 if (_field.precision() > 0 && _field.scale() == 0) {
330 // 12345678901234567890
331 // 2147483647
332 // 9223372036854775807
333 if (_field.precision() <= __args.i4precision()) {
334 int32_t value = 0;
335 _field.getValue(value);
336 _param.setValue(value);
337 break;
338 }
339 else if (_field.precision() <= __args.i8precision()) {
340 int64_t value = 0;
341 _field.getValue(value);
342 _param.setValue(value);
343 break;
344 }
345 else {
346 // BigInteger
347 }
348 }
349 ByteString value;
350 _field.getValue(value, SQL::typeText);
351 _param.setValue(value, SQL::typeText);
352 break;
353 }
354 case SQL::typeDate: {
355 SQL::Date value;
356 _field.getValue(value);
357 _param.setData(&value, sizeof(value),
359 break;
360 }
361 case SQL::typeTime:
362 case SQL::typeTimeTz: {
363 SQL::Time value;
364 _field.getValue(value);
365 _param.setData(&value, sizeof(value),
367 break;
368 }
371 SQL::TimeStamp value;
372 _field.getValue(value);
373 _param.setData(&value, sizeof(value),
374 _field.dataType(), SQL::typeTimeStamp);
375 break;
376 }
379 case SQL::typeIntervalDs: {
380 SQL::Interval value;
381 _field.getValue(value);
382 _param.setData(&value, sizeof(value),
383 _field.dataType(), SQL::typeInterval);
384 break;
385 }
386 case SQL::typeText:
388 case SQL::typeClob: {
389 ByteString value;
390 _field.getValue(value, SQL::typeText);
391 _param.setValue(value, SQL::typeText);
392 break;
393 }
394 case SQL::typeBinary:
396 case SQL::typeBlob: {
397 ByteString value;
398 _field.getValue(value, SQL::typeBinary);
399 _param.setValue(value, SQL::typeBinary);
400 break;
401 }
402 }
403}
@ typeBinary
Definition SQLCore.h:77
@ typeClob
Definition SQLCore.h:80
@ typeNumeric
Definition SQLCore.h:67
@ typeTime
Definition SQLCore.h:69
@ typeLongBinary
Definition SQLCore.h:79
@ typeUInteger
Definition SQLCore.h:65
@ typeTimeStamp
Definition SQLCore.h:71
@ typeBlob
Definition SQLCore.h:81
@ typeTimeStampTz
Definition SQLCore.h:72
@ typeInterval
Definition SQLCore.h:73
@ typeIntervalDs
Definition SQLCore.h:75
@ typeDate
Definition SQLCore.h:68
@ typeText
Definition SQLCore.h:76
@ typeTimeTz
Definition SQLCore.h:70
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeIntervalYm
Definition SQLCore.h:74
@ typeLongText
Definition SQLCore.h:78

Member Data Documentation

◆ __args

const MainArguments& EntityCopy::__args
protected

Definition at line 51 of file EntityCopy.h.

◆ __dstConn

SQLConnection EntityCopy::__dstConn
protected

Definition at line 53 of file EntityCopy.h.

◆ __dstHelper

const DatabaseHelper* EntityCopy::__dstHelper
protected

Definition at line 56 of file EntityCopy.h.

◆ __entities

Array<Entity> EntityCopy::__entities
protected

Definition at line 58 of file EntityCopy.h.

◆ __lineIndent

String EntityCopy::__lineIndent
protected

Definition at line 71 of file EntityCopy.h.

◆ __lineRuler

String EntityCopy::__lineRuler
protected

Definition at line 70 of file EntityCopy.h.

◆ __srcConn

SQLConnection EntityCopy::__srcConn
protected

Definition at line 52 of file EntityCopy.h.

◆ __srcHelper

const DatabaseHelper* EntityCopy::__srcHelper
protected

Definition at line 55 of file EntityCopy.h.


The documentation for this class was generated from the following files: