DCL 3.7.4
Loading...
Searching...
No Matches
work/entitycopy/main.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#ifdef __WINNT__
4#include <windows.h>
5#endif
6
7#include <locale.h>
8
9#include <dcl/FileWriter.h>
10#include <dcl/Arguments.h>
11#include <dcl/DateTime.h>
12
13#include "main.h"
14#include "EntityCopy.h"
15#include "Version.h"
16
17#if __DCL_HAVE_THIS_FILE__
18#undef __THIS_FILE__
19static const char_t __UNUSED__ __THIS_FILE__[] = __T("entitycopy/main.cpp");
20#endif
21
22__DCL_BEGIN_NAMESPACE
23
24static Arguments::Option __options__[] =
25{
26 { L"verbose", L'v', NULL, 0, L"Produce verbose output" },
27 { NULL, 0, NULL, 0, L"The following options should be grouped together:" },
28 { L"source", L's', L"STRING", 0, L"Source database connection."
29 "\n\t\t\t DRIVER=DCLFirebird; USER=MT100;PASSWORD=pass;SERVER=g02u24kn2/32055;DATABASE=MT100"
30 "\n\t\t\t DRIVER=DCLOracle; USER=MT100;PASSWORD=pass;DATABASE=g01r08od21:1521/AL32UTF8" },
31 { L"destination", L'd', L"STRING", 0, L"Destination database connection."
32 "\n\t\t\t DRIVER=DCLInterBase;USER=MT100;PASSWORD=pass;SERVER=n0317ud70p/3050;DATABASE=MT100"
33 "\n\t\t\t DRIVER=DCLInformix; USER=MT100;PASSWORD=pass;SERVER=informix02;DATABASE=MT100"
34 "\n\t\t\t DRIVER=DCLMariaDB; USER=MT100;PASSWORD=pass;SERVER=g02u24kn2;PORT=32011;DATABASE=MT100"
35 "\n\t\t\t DRIVER=DCLPostgreSQL;USER=MT100;PASSWORD=pass;SERVER=g02u24kn2;PORT=32017;DATABASE=MT100"
36 "\n\t\t\t DRIVER=DCLSQLCipher;DATABASE=MT100.sqlcipher;KEY=key"
37 "\n\t\t\t DRIVER=DCLSQLite3; DATABASE=MT100.sqlite3"
38 "\n\t\t\t DRIVER={ODBC Driver 18 for SQL Server};Server=g02u24kn2,31433;Database=MT100;UID=MT100;PWD=pass" },
39 { L"entities", L'e', L"STRING", 0, L"Entity configuration list."
40 "\n\t\t\t Blank(all) or S1;S2;S3(c1,cn);S4(s1,sn)=D4(d1,dn); ..." },
41 { L"truncate", L'c', NULL, 0, L"Truncate table and insert" },
42 { L"rows", L'r', L"NUMBER", 0, L"Rows per transaction." },
43 { L"i4-precision", L'4', L"NUMBER", 0, L"32bits integer precision for decimal. default is 10" },
44 { L"i8-precision", L'8', L"NUMBER", 0, L"64bits integer precision for decimal, default is 19" },
45 { L"dry-run", L'n', NULL, 0, L"perform a trial run with no changes made" },
46 { NULL }
47};
48
50 : Arguments(
51 _output, _errout,
52 L"entitycopy " DCL_ENTITY_COPY_VERSION_STRING,
53 L"daejung@gowoonsoft.com, gowoonsoft.com@gmail.com",
54 NULL,
55 L"Copy Database Tables",
56 __options__
57 )
58{
59 __dryrun = false;
60 __verbose = false;
61 __truncate = false;
62 __rows = -1;
63 __i4precision = 10;
64 __i8precision = 19;
65}
66
68{
69 StringBuilder sb;
70 sb.append(value0())
71 .append(L" --verbose=").append(String::valueOf(__verbose))
72 .append(L" --source=").append(__source)
73 .append(L" --destination=").append(__destination)
74 .append(L" --entities=").append(__entities)
75 .append(L" --truncate=").append(String::valueOf(__truncate))
76 .append(L" --rows=").append(String::valueOf(__rows))
77 .append(L" --i4-precision=").append(String::valueOf(__i4precision))
78 .append(L" --i8-precision=").append(String::valueOf(__i8precision))
79 .append(L" --dry-run=").append(String::valueOf(__dryrun))
80 .append(L" values[").append(values().toString())
81 .append(L"]");
82
83 return sb;
84}
85
86void MainArguments::onOption(int _key, const String& _arg)
88{
89 switch (_key) {
90 case L'v':
91 __verbose = true;
92 break;
93 case L's':
94 __source = _arg;
95 break;
96 case L'd':
97 __destination = _arg;
98 break;
99 case L'e':
100 __entities = _arg;
101 break;
102 case L'c':
103 __truncate = true;
104 break;
105 case L'r':
106 __rows = Int32::parse(_arg.data());
107 break;
108 case L'4':
109 __i4precision = Int32::parse(_arg.data(), 10, __i4precision);
110 break;
111 case L'8':
112 __i8precision = Int32::parse(_arg.data(), 10, __i8precision);
113 break;
114 case L'n':
115 __dryrun = true;
116 break;
117 }
118}
119
121{
122 if (__source.isEmpty() || __destination.isEmpty()) {
123 return L"option '--source' and '--destination' must both be provided";
124 }
125 return Arguments::onValidate();
126}
127
128void __main(const MainArguments& _args)
129{
130 String srcDriver;
131 String dstDriver;
132
133 size_t first = _args.source().search(L"DRIVER *=", true);
134 if (first != (size_t)-1) {
135 srcDriver = _args.source().substring(L"DRIVER *= *[^;]+", true);
136 if (srcDriver.isEmpty()) {
137 srcDriver = _args.source().substring(first);
138 }
139 if (!srcDriver.isEmpty()) {
140 if (srcDriver.indexOf(L'{') != (size_t)-1) {
141 srcDriver = L"DCLODBC";
142 }
143 else {
144 srcDriver = srcDriver.substring(srcDriver.indexOf(L'=') + 1).trim();
145 }
146 }
147 }
148
149 first = _args.destination().search(L"DRIVER *=", true);
150 if (first != (size_t)-1) {
151 dstDriver = _args.destination().substring(L"DRIVER *= *[^;]+", true);
152 if (dstDriver.isEmpty()) {
153 dstDriver = _args.destination().substring(first);
154 }
155 if (!dstDriver.isEmpty()) {
156 if (dstDriver.indexOf(L'{') != (size_t)-1) {
157 dstDriver = L"DCLODBC";
158 }
159 else {
160 dstDriver = dstDriver.substring(dstDriver.indexOf(L'=') + 1).trim();
161 }
162 }
163 }
164
165 if (srcDriver.isEmpty() || dstDriver.isEmpty()) {
166 _args.errout() << _args.value0() << L": option '--source' or '--destination' is invalid"
167 << endl << _args.tryUsage() << endl;
168 return;
169 }
170
171 EntityCopy copy(_args, srcDriver, dstDriver);
172 if (copy.initialize()) {
173 copy.doIt();
174 }
175}
176
177__DCL_END_NAMESPACE
178
179__DCL_USING_NAMESPACE
180
181int main(int _argc, char* _argv[])
182{
183 // std::locale::global(std::locale(""));
184 setlocale(LC_ALL, "");
185
187 {
190#ifdef __DCL_DEBUG
191 DCLDebugSetGlobalReport(&errout);
192#endif
193 MainArguments args(output, errout);
194 if (args.parse(_argc, _argv, false)) {
195 String sep(L'=', 80);
197 output << L"Start [" << start.toString()
198 << L"]" << endl;
199 if (args.verbose()) {
200 output << L" [" << args.toString() << L"]" << endl;
201 }
202 output << sep << endl;
203
204 try {
205 __main(args);
206 }
207 catch (Exception* e) {
208 errout << e->toStringAll() << endl;
209 e->destroy();
210 }
211
213 output << sep << endl
214 << L"Finish [" << finish.toString()
215 << L" " << (finish - start).toString()
216 << L"]" << endl;
217 }
218#ifdef __DCL_DEBUG
219#if __DCL_HAVE_ALLOC_DEBUG
220 DCLDebugDumpGlobalMemoryLeak(DCL_ALLOC_DUMP_ALL, &errout);
221#endif
222 DCLDebugSetGlobalReport(NULL);
223#endif // __DCL_DEBUG
224 }
226
227 return 0;
228}
229
230#ifdef __WINNT_NEW_DELETE_OVERRIDE
231#undef new
232__WINNT_NEW_DELETE_OVERRIDE
233#endif
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:312
#define __UNUSED__
Definition Config.h:341
wchar_t char_t
Definition Config.h:247
#define __DCL_THROWS1(e)
Definition Config.h:152
#define STDOUT_HANDLE
Definition File.h:59
#define STDERR_HANDLE
Definition File.h:60
#define __DCL_CLEANUP
Definition Object.h:286
#define __T(str)
Definition Object.h:60
#define __DCL_INITIALIZE
Definition Object.h:285
DCLCVAR const struct __endl endl
virtual String onValidate()
Definition Arguments.cpp:48
String tryUsage() const
String toString() const
Definition DateTime.cpp:843
static DateTime getCurrentLocalTime()
Definition DateTime.cpp:954
bool initialize() __DCL_THROWS1(SQLException *)
virtual void destroy()
Definition Exception.cpp:74
String toStringAll() const
Definition Exception.cpp:45
static int32_t parse(const wchar_t *_number, unsigned _base=10) __DCL_THROWS1(NumericConvertException *)
Definition Numeric.cpp:264
MainArguments(Writer &_output, Writer &_errout)
bool verbose() const
virtual String onValidate()
const String & destination() const
const String & source() const
virtual void onOption(int _key, const String &_arg) __DCL_THROWS1(Exception *)
virtual String toString() const
#define DCL_ENTITY_COPY_VERSION_STRING
void __main(const MainArguments &_args)
__DCL_END_NAMESPACE __DCL_USING_NAMESPACE int main(int _argc, char *_argv[])