DCL 4.0
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 404 of file EntityCopy.cpp.

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

491{
492 for (size_t i = 0; i < __entities.size(); i++) {
494 }
495}
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 507 of file EntityCopy.cpp.

508{
509 StringBuilder sb = L"INSERT INTO ";
510 sb.append(!_e.toName.isEmpty() ? _e.toName : _e.name)
511 .append(L"(");
512 if (!_e.toColumns.isEmpty()) {
513 sb.append(_e.toColumns);
514 }
515 else if (!_e.columns.isEmpty()) {
516 sb.append(_e.columns);
517 }
518 else {
519 sb.append(_fields[0].name());
520 for (size_t i = 1; i < _fields.count(); i++) {
521 sb.append(L",").append(_fields[i].name());
522 }
523 }
524 sb.append(L") VALUES (?");
525
526 for (size_t i = 1; i < _fields.count(); i++) {
527 sb.append(L",?");
528 }
529 sb.append(L")");
530
531 return sb;
532}
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 497 of file EntityCopy.cpp.

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

◆ getTruncateSQL()

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

Definition at line 534 of file EntityCopy.cpp.

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

◆ 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 SQL::Time value;
363 _field.getValue(value);
364 _param.setData(&value, sizeof(value),
366 break;
367 }
370 SQL::TimeStamp value;
371 _field.getValue(value);
372 _param.setData(&value, sizeof(value),
373 _field.dataType(), SQL::typeTimeStamp);
374 break;
375 }
378 case SQL::typeIntervalDs: {
379 SQL::Interval value;
380 _field.getValue(value);
381 _param.setData(&value, sizeof(value),
382 _field.dataType(), SQL::typeInterval);
383 break;
384 }
385 case SQL::typeText:
387 case SQL::typeClob: {
388 ByteString value;
389 _field.getValue(value, SQL::typeText);
390 _param.setValue(value, SQL::typeText);
391 break;
392 }
393 case SQL::typeBinary:
395 case SQL::typeBlob: {
396 ByteString value;
397 _field.getValue(value, SQL::typeBinary);
398 _param.setValue(value, SQL::typeBinary);
399 break;
400 }
401 }
402}
@ typeBinary
Definition SQLCore.h:76
@ typeClob
Definition SQLCore.h:79
@ typeNumeric
Definition SQLCore.h:67
@ typeTime
Definition SQLCore.h:69
@ typeLongBinary
Definition SQLCore.h:78
@ typeUInteger
Definition SQLCore.h:65
@ typeTimeStamp
Definition SQLCore.h:70
@ typeBlob
Definition SQLCore.h:80
@ typeTimeStampTz
Definition SQLCore.h:71
@ typeInterval
Definition SQLCore.h:72
@ typeIntervalDs
Definition SQLCore.h:74
@ typeDate
Definition SQLCore.h:68
@ typeText
Definition SQLCore.h:75
@ typeFloat
Definition SQLCore.h:66
@ typeInteger
Definition SQLCore.h:64
@ typeIntervalYm
Definition SQLCore.h:73
@ typeLongText
Definition SQLCore.h:77

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: