DCL 4.0
Loading...
Searching...
No Matches
entitycopy/main.cpp
Go to the documentation of this file.
1#include <dcl/Config.h>
2
3#if __DCL_WINDOWS
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
16#if __DCL_DEBUG
17#undef __THIS_FILE__
18static const char_t __THIS_FILE__[] = __T("entitycopy/main.cpp");
19#endif
20
21__DCL_BEGIN_NAMESPACE
22
23static Arguments::Option __options__[] =
24{
25 { L"verbose", L'v', NULL, 0, L"Produce verbose output" },
26 { NULL, 0, NULL, 0, L"The following options should be grouped together:" },
27 { L"source", L's', L"STRING", 0, L"Source database connection."
28 "\n\t\t\t DRIVER=DCLFirebird; USER=MT100;PASSWORD=pass;SERVER=g02u24kn2/32055;DATABASE=MT100" },
29 { L"destination", L'd', L"STRING", 0, L"Destination database connection."
30 "\n\t\t\t DRIVER=DCLInterBase;USER=MT100;PASSWORD=pass;SERVER=n0317ud70p/3050;DATABASE=MT100" },
31 { L"entities", L'e', L"STRING", 0, L"Entity configuration list."
32 "\n\t\t\t Blank(all) or S1;S2;S3(c1,cn);S4(s1,sn)=D4(d1,dn); ..." },
33 { L"truncate", L'c', NULL, 0, L"Truncate table and insert" },
34 { L"rows", L'r', L"NUMBER", 0, L"Rows per transaction." },
35 { L"i4-precision", L'4', L"NUMBER", 0, L"32bits integer precision for decimal. default is 10" },
36 { L"i8-precision", L'8', L"NUMBER", 0, L"64bits integer precision for decimal, default is 19" },
37 { L"dry-run", L'n', NULL, 0, L"perform a trial run with no changes made" },
38 { NULL }
39};
40
42 : Arguments(
43 _output, _errout,
44 L"entitycopy 1.0",
45 L"daejung@gowoonsoft.com",
46 NULL,
47 L"Copy Database Tables",
48 __options__
49 )
50{
51 __dryrun = false;
52 __verbose = false;
53 __truncate = false;
54 __rows = -1;
55 __i4precision = 10;
56 __i8precision = 19;
57}
58
60{
61 StringBuilder sb;
62 sb.append(value0())
63 .append(L" --verbose=").append(String::valueOf(__verbose))
64 .append(L" --source=").append(__source)
65 .append(L" --destination=").append(__destination)
66 .append(L" --entities=").append(__entities)
67 .append(L" --truncate=").append(String::valueOf(__truncate))
68 .append(L" --rows=").append(String::valueOf(__rows))
69 .append(L" --i4-precision=").append(String::valueOf(__i4precision))
70 .append(L" --i8-precision=").append(String::valueOf(__i8precision))
71 .append(L" --dry-run=").append(String::valueOf(__dryrun))
72 .append(L" values[").append(values().toString())
73 .append(L"]");
74
75 return sb;
76}
77
78void MainArguments::onOption(int _key, const String& _arg)
80{
81 switch (_key) {
82 case L'v':
83 __verbose = true;
84 break;
85 case L's':
86 __source = _arg;
87 break;
88 case L'd':
89 __destination = _arg;
90 break;
91 case L'e':
92 __entities = _arg;
93 break;
94 case L'c':
95 __truncate = true;
96 break;
97 case L'r':
98 __rows = Int32::parse(_arg.data());
99 break;
100 case L'4':
101 __i4precision = Int32::parse(_arg.data(), 10, __i4precision);
102 break;
103 case L'8':
104 __i8precision = Int32::parse(_arg.data(), 10, __i8precision);
105 break;
106 case L'n':
107 __dryrun = true;
108 break;
109 }
110}
111
113{
114 if (__source.isEmpty() || __destination.isEmpty()) {
115 return L"option '--source' and '--destination' must both be provided";
116 }
117 return Arguments::onValidate();
118}
119
120void __main(const MainArguments& _args)
121{
122 String srcDriver;
123 String dstDriver;
124 {
125 String s = _args.source().substring(L"DRIVER *= *[^ ;]+", true);
126 if (!s.isEmpty()) {
127 srcDriver = s.substring(s.indexOf(L'=') + 1).trim();
128 }
129 s = _args.destination().substring(L"DRIVER *= *[^ ;]+", true);
130 if (!s.isEmpty()) {
131 dstDriver = s.substring(s.indexOf(L'=') + 1).trim();
132 }
133 }
134
135 if (srcDriver.isEmpty() || dstDriver.isEmpty()) {
136 _args.errout() << _args.value0() << L": option '--source' or '--destination' is invalid"
137 << endl << _args.tryUsage() << endl;
138 return;
139 }
140
141 EntityCopy copy(_args, srcDriver, dstDriver);
142 if (copy.initialize()) {
143 copy.doIt();
144 }
145}
146
147__DCL_END_NAMESPACE
148
149__DCL_USING_NAMESPACE
150
151int main(int _argc, char* _argv[])
152{
153 // std::locale::global(std::locale(""));
154 setlocale(LC_ALL, "");
155
157 {
160#if __DCL_DEBUG
161 DCLDebugSetGlobalReport(&errout);
162#endif
163 MainArguments args(output, errout);
164 if (args.parse(_argc, _argv, false)) {
165 String sep(L'=', 80);
167 output << L"Start [" << start.toString()
168 << L"]" << endl;
169 if (args.verbose()) {
170 output << L" [" << args.toString() << L"]" << endl;
171 }
172 output << sep << endl;
173
174 try {
175 __main(args);
176 }
177 catch (Exception* e) {
178 errout << e->toStringAll() << endl;
179 e->destroy();
180 }
181
183 output << sep << endl
184 << L"Finish [" << finish.toString()
185 << L" " << (finish - start).toString()
186 << L"]" << endl;
187 }
188#if __DCL_DEBUG
189#if __DCL_HAVE_ALLOC_DEBUG
190 DCLDebugDumpGlobalMemoryLeak(DCL_ALLOC_DUMP_ALL, &errout);
191#endif
192 DCLDebugSetGlobalReport(NULL);
193#endif // __DCL_DEBUG
194 }
196
197 return 0;
198}
#define __THIS_FILE__
Definition _trace.h:14
#define NULL
Definition Config.h:340
wchar_t char_t
Definition Config.h:275
#define __DCL_THROWS1(e)
Definition Config.h:167
#define STDOUT_HANDLE
Definition File.h:55
#define STDERR_HANDLE
Definition File.h:56
#define __DCL_CLEANUP
Definition Object.h:269
#define __T(str)
Definition Object.h:44
#define __DCL_INITIALIZE
Definition Object.h:268
#define endl
virtual String onValidate()
Definition Arguments.cpp:49
String tryUsage() const
String toString() const
Definition DateTime.cpp:826
static DateTime getCurrentLocalTime()
Definition DateTime.cpp:937
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:284
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
void __main(const MainArguments &_args)
__DCL_END_NAMESPACE __DCL_USING_NAMESPACE int main(int _argc, char *_argv[])