transactd 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) hide show
  1. checksums.yaml +7 -0
  2. data/BUILD_UNIX-JA +174 -0
  3. data/BUILD_WIN-JA +256 -0
  4. data/CMakeLists.txt +96 -0
  5. data/COPYING +339 -0
  6. data/README +406 -0
  7. data/README-JA +424 -0
  8. data/bin/common/tdclc_32_1_0.dll +0 -0
  9. data/bin/common/tdclc_64_1_0.dll +0 -0
  10. data/build/common/check_for_link_iconv.cmake +73 -0
  11. data/build/common/copyifgreater.cmd +30 -0
  12. data/build/common/copyifgreater.js +290 -0
  13. data/build/common/get_boost_libs.cmake +106 -0
  14. data/build/common/get_ruby_path.cmake +115 -0
  15. data/build/common/options.cmake +127 -0
  16. data/build/common/smart_install.cmake +263 -0
  17. data/build/common/system.cmake +122 -0
  18. data/build/common/transactd.rc.in +52 -0
  19. data/build/common/transactd_cl_common.cmake +101 -0
  20. data/build/common/transactd_cl_output.cmake +93 -0
  21. data/build/common/transactd_common.cmake +237 -0
  22. data/build/common/transactd_required.cmake +28 -0
  23. data/build/swig/ruby/generate.cmake.in +35 -0
  24. data/build/swig/ruby/generate.cmd.in +19 -0
  25. data/build/swig/ruby/ruby.swg +101 -0
  26. data/build/swig/tdcl.i +188 -0
  27. data/build/tdclc/BUILDNUMBER.txt +1 -0
  28. data/build/tdclc/CMakeLists.txt +170 -0
  29. data/build/tdclc/tdclc_32.cbproj +181 -0
  30. data/build/tdclc/tdclc_64.cbproj +205 -0
  31. data/build/tdclcpp/BUILDNUMBER.txt +1 -0
  32. data/build/tdclcpp/CMakeLists.txt +142 -0
  33. data/build/tdclcpp/tdclcpp_bcb_32.cbproj +239 -0
  34. data/build/tdclcpp/tdclcpp_bcb_64.cbproj +304 -0
  35. data/build/tdclrb/BUILDNUMBER.txt +1 -0
  36. data/build/tdclrb/CMakeLists.txt +258 -0
  37. data/build/tdclrb/GEM_VERSION +3 -0
  38. data/build/tdclrb/bldgem/extconf.rb +123 -0
  39. data/build/tdclrb/gem/INSTALLLOG.win32 +9 -0
  40. data/build/tdclrb/gem/Makefile.win32-VS +65 -0
  41. data/build/tdclrb/gem/Makefile.win32-prebuilt +48 -0
  42. data/build/tdclrb/gem/detect.rb +31 -0
  43. data/build/tdclrb/gem/helper.rb +113 -0
  44. data/build/tdclrb/gem/transactd.rb +22 -0
  45. data/build/tdclrb/gem_output.cmake +44 -0
  46. data/source/bzs/db/IBlobBuffer.h +51 -0
  47. data/source/bzs/db/blobBuffer.h +177 -0
  48. data/source/bzs/db/blobStructs.h +85 -0
  49. data/source/bzs/db/engine/mysql/IReadRecords.h +52 -0
  50. data/source/bzs/db/engine/mysql/bookmark.h +195 -0
  51. data/source/bzs/db/engine/mysql/database.cpp +1882 -0
  52. data/source/bzs/db/engine/mysql/database.h +465 -0
  53. data/source/bzs/db/engine/mysql/dbManager.cpp +303 -0
  54. data/source/bzs/db/engine/mysql/dbManager.h +143 -0
  55. data/source/bzs/db/engine/mysql/errorMessage.cpp +75 -0
  56. data/source/bzs/db/engine/mysql/errorMessage.h +43 -0
  57. data/source/bzs/db/engine/mysql/fieldAccess.h +158 -0
  58. data/source/bzs/db/engine/mysql/mydebuglog.cpp +349 -0
  59. data/source/bzs/db/engine/mysql/mydebuglog.h +89 -0
  60. data/source/bzs/db/engine/mysql/mysqlInternal.h +171 -0
  61. data/source/bzs/db/engine/mysql/mysqlThd.cpp +169 -0
  62. data/source/bzs/db/engine/mysql/mysqlThd.h +35 -0
  63. data/source/bzs/db/engine/mysql/percentageKey.h +260 -0
  64. data/source/bzs/db/protocol/ICommandExecuter.h +49 -0
  65. data/source/bzs/db/protocol/hs/hsCommandExecuter.cpp +689 -0
  66. data/source/bzs/db/protocol/hs/hsCommandExecuter.h +228 -0
  67. data/source/bzs/db/protocol/tdap/btrDate.cpp +437 -0
  68. data/source/bzs/db/protocol/tdap/btrDate.h +227 -0
  69. data/source/bzs/db/protocol/tdap/client/bulkInsert.h +127 -0
  70. data/source/bzs/db/protocol/tdap/client/client.cpp +106 -0
  71. data/source/bzs/db/protocol/tdap/client/client.h +292 -0
  72. data/source/bzs/db/protocol/tdap/client/connMgr.cpp +144 -0
  73. data/source/bzs/db/protocol/tdap/client/connMgr.h +82 -0
  74. data/source/bzs/db/protocol/tdap/client/database.cpp +863 -0
  75. data/source/bzs/db/protocol/tdap/client/database.h +118 -0
  76. data/source/bzs/db/protocol/tdap/client/databaseFactory.cpp +100 -0
  77. data/source/bzs/db/protocol/tdap/client/dbDef.cpp +1640 -0
  78. data/source/bzs/db/protocol/tdap/client/dbDef.h +135 -0
  79. data/source/bzs/db/protocol/tdap/client/dllmain.cpp +434 -0
  80. data/source/bzs/db/protocol/tdap/client/errorMessage.cpp +92 -0
  81. data/source/bzs/db/protocol/tdap/client/errorMessage_ja.cpp +98 -0
  82. data/source/bzs/db/protocol/tdap/client/fieldDDF.cpp +174 -0
  83. data/source/bzs/db/protocol/tdap/client/fieldDDF.h +91 -0
  84. data/source/bzs/db/protocol/tdap/client/fileDDF.cpp +140 -0
  85. data/source/bzs/db/protocol/tdap/client/fileDDF.h +86 -0
  86. data/source/bzs/db/protocol/tdap/client/filter.cpp +527 -0
  87. data/source/bzs/db/protocol/tdap/client/filter.h +154 -0
  88. data/source/bzs/db/protocol/tdap/client/indexDDF.cpp +137 -0
  89. data/source/bzs/db/protocol/tdap/client/indexDDF.h +84 -0
  90. data/source/bzs/db/protocol/tdap/client/nsDatabase.cpp +724 -0
  91. data/source/bzs/db/protocol/tdap/client/nsDatabase.h +123 -0
  92. data/source/bzs/db/protocol/tdap/client/nsTable.cpp +899 -0
  93. data/source/bzs/db/protocol/tdap/client/nsTable.h +199 -0
  94. data/source/bzs/db/protocol/tdap/client/request.h +198 -0
  95. data/source/bzs/db/protocol/tdap/client/sharedData.cpp +58 -0
  96. data/source/bzs/db/protocol/tdap/client/sharedData.h +56 -0
  97. data/source/bzs/db/protocol/tdap/client/sqlBuilder.cpp +574 -0
  98. data/source/bzs/db/protocol/tdap/client/sqlBuilder.h +53 -0
  99. data/source/bzs/db/protocol/tdap/client/stringConverter.h +627 -0
  100. data/source/bzs/db/protocol/tdap/client/table.cpp +2613 -0
  101. data/source/bzs/db/protocol/tdap/client/table.h +221 -0
  102. data/source/bzs/db/protocol/tdap/client/trdboostapi.h +1096 -0
  103. data/source/bzs/db/protocol/tdap/client/trdboostapiInternal.h +179 -0
  104. data/source/bzs/db/protocol/tdap/client/trdclcppautolink.h +40 -0
  105. data/source/bzs/db/protocol/tdap/client/trnsctcl.def +11 -0
  106. data/source/bzs/db/protocol/tdap/myDateTime.cpp +500 -0
  107. data/source/bzs/db/protocol/tdap/mysql/characterset.cpp +184 -0
  108. data/source/bzs/db/protocol/tdap/mysql/characterset.h +60 -0
  109. data/source/bzs/db/protocol/tdap/mysql/databaseSchema.cpp +284 -0
  110. data/source/bzs/db/protocol/tdap/mysql/databaseSchema.h +53 -0
  111. data/source/bzs/db/protocol/tdap/mysql/debuglog.cpp +383 -0
  112. data/source/bzs/db/protocol/tdap/mysql/debuglog.h +106 -0
  113. data/source/bzs/db/protocol/tdap/mysql/recordsetReader.h +680 -0
  114. data/source/bzs/db/protocol/tdap/mysql/request.h +202 -0
  115. data/source/bzs/db/protocol/tdap/mysql/tdapCommandExecuter.cpp +1020 -0
  116. data/source/bzs/db/protocol/tdap/mysql/tdapCommandExecuter.h +141 -0
  117. data/source/bzs/db/protocol/tdap/tdapRequest.h +190 -0
  118. data/source/bzs/db/protocol/tdap/tdapSchema.cpp +295 -0
  119. data/source/bzs/db/protocol/tdap/tdapSchema.h +558 -0
  120. data/source/bzs/db/protocol/tdap/tdapcapi.h +423 -0
  121. data/source/bzs/db/transactd/appBuilderImple.h +55 -0
  122. data/source/bzs/db/transactd/appModule.cpp +183 -0
  123. data/source/bzs/db/transactd/appModule.h +80 -0
  124. data/source/bzs/db/transactd/connManager.cpp +201 -0
  125. data/source/bzs/db/transactd/connManager.h +60 -0
  126. data/source/bzs/db/transactd/connectionRecord.h +69 -0
  127. data/source/bzs/db/transactd/transactd.cpp +325 -0
  128. data/source/bzs/env/compiler.h +135 -0
  129. data/source/bzs/env/crosscompile.cpp +130 -0
  130. data/source/bzs/env/crosscompile.h +150 -0
  131. data/source/bzs/env/fileopen.h +36 -0
  132. data/source/bzs/env/mbcswchrLinux.cpp +40 -0
  133. data/source/bzs/env/mbcswchrLinux.h +88 -0
  134. data/source/bzs/env/tstring.h +183 -0
  135. data/source/bzs/example/changeSchema.cpp +117 -0
  136. data/source/bzs/example/changeSchema_c.cpp +78 -0
  137. data/source/bzs/example/connection_pool_c.cpp +171 -0
  138. data/source/bzs/example/createDatabase.cpp +305 -0
  139. data/source/bzs/example/createDatabase_c.cpp +202 -0
  140. data/source/bzs/example/deleteRecords.cpp +87 -0
  141. data/source/bzs/example/deleteRecords_c.cpp +57 -0
  142. data/source/bzs/example/dropDatabase.cpp +59 -0
  143. data/source/bzs/example/dropDatabase_c.cpp +34 -0
  144. data/source/bzs/example/insertRecords.cpp +212 -0
  145. data/source/bzs/example/insertRecords_c.cpp +153 -0
  146. data/source/bzs/example/readRecords.cpp +141 -0
  147. data/source/bzs/example/readRecords_c.cpp +107 -0
  148. data/source/bzs/example/updateRecords.cpp +99 -0
  149. data/source/bzs/example/updateRecords_c.cpp +71 -0
  150. data/source/bzs/example/update_with_transaction.cpp +104 -0
  151. data/source/bzs/example/update_with_transaction_c.cpp +80 -0
  152. data/source/bzs/netsvc/client/tcpClient.cpp +226 -0
  153. data/source/bzs/netsvc/client/tcpClient.h +489 -0
  154. data/source/bzs/netsvc/server/IAppModule.h +94 -0
  155. data/source/bzs/netsvc/server/iserver.h +65 -0
  156. data/source/bzs/netsvc/server/serverCpt.cpp +522 -0
  157. data/source/bzs/netsvc/server/serverCpt.h +88 -0
  158. data/source/bzs/netsvc/server/serverPipe.cpp +705 -0
  159. data/source/bzs/netsvc/server/serverPipe.h +96 -0
  160. data/source/bzs/netsvc/server/serverTpool.cpp +416 -0
  161. data/source/bzs/netsvc/server/serverTpool.h +84 -0
  162. data/source/bzs/rtl/benchmark.cpp +96 -0
  163. data/source/bzs/rtl/benchmark.h +65 -0
  164. data/source/bzs/rtl/datetime.cpp +375 -0
  165. data/source/bzs/rtl/datetime.h +53 -0
  166. data/source/bzs/rtl/debuglog.cpp +106 -0
  167. data/source/bzs/rtl/debuglog.h +97 -0
  168. data/source/bzs/rtl/exception.h +116 -0
  169. data/source/bzs/rtl/stl_uty.cpp +35 -0
  170. data/source/bzs/rtl/stl_uty.h +29 -0
  171. data/source/bzs/rtl/stringBuffers.cpp +101 -0
  172. data/source/bzs/rtl/stringBuffers.h +58 -0
  173. data/source/bzs/rtl/strtrim.cpp +135 -0
  174. data/source/bzs/rtl/strtrim.h +46 -0
  175. data/source/bzs/test/tdclatl/bench_tdclatl.js +445 -0
  176. data/source/bzs/test/tdclrb/bench_tdclcpp.rb +375 -0
  177. data/source/bzs/test/tdclrb/prepare.rb +226 -0
  178. data/source/bzs/test/tdclrb/transactd_datetime_spec.rb +172 -0
  179. data/source/bzs/test/tdclrb/transactd_kanjischema_spec.rb +208 -0
  180. data/source/bzs/test/tdclrb/transactd_spec.rb +1536 -0
  181. data/source/bzs/test/transactdBench/transactdBench.cpp +430 -0
  182. data/source/bzs/test/transactdBench/transactdBench2.cpp +342 -0
  183. data/source/bzs/test/trdclengn/test_trdclengn.cpp +2030 -0
  184. data/source/global/tdclatl/Database.cpp +503 -0
  185. data/source/global/tdclatl/Database.h +139 -0
  186. data/source/global/tdclatl/DbDef.cpp +242 -0
  187. data/source/global/tdclatl/DbDef.h +79 -0
  188. data/source/global/tdclatl/Field.cpp +92 -0
  189. data/source/global/tdclatl/Field.h +59 -0
  190. data/source/global/tdclatl/FieldDef.cpp +238 -0
  191. data/source/global/tdclatl/FieldDef.h +87 -0
  192. data/source/global/tdclatl/Flags.cpp +111 -0
  193. data/source/global/tdclatl/Flags.h +55 -0
  194. data/source/global/tdclatl/KeyDef.cpp +51 -0
  195. data/source/global/tdclatl/KeyDef.h +55 -0
  196. data/source/global/tdclatl/KeySegment.cpp +55 -0
  197. data/source/global/tdclatl/Table.cpp +600 -0
  198. data/source/global/tdclatl/Table.h +144 -0
  199. data/source/global/tdclatl/TableDef.cpp +291 -0
  200. data/source/global/tdclatl/TableDef.h +86 -0
  201. data/source/global/tdclatl/TdVersion.cpp +74 -0
  202. data/source/global/tdclatl/TdVersion.h +54 -0
  203. data/source/global/tdclatl/_IDatabaseEvents_CP.h +113 -0
  204. data/source/global/tdclatl/dllmain.cpp +30 -0
  205. data/source/global/tdclatl/dllmain.h +27 -0
  206. data/source/global/tdclatl/keySegment.h +57 -0
  207. data/source/global/tdclatl/resource.h +0 -0
  208. data/source/global/tdclatl/stdafx.cpp +2 -0
  209. data/source/global/tdclatl/stdafx.h +25 -0
  210. data/source/global/tdclatl/targetver.h +4 -0
  211. data/source/global/tdclatl/tdclatl.cpp +68 -0
  212. data/source/global/tdclatl/tdclatl.def +10 -0
  213. data/source/global/tdclatl/tdclatl.idl +1035 -0
  214. data/source/linux/charsetConvert.h +112 -0
  215. data/source/linux/linuxTypes.h +33 -0
  216. data/source/linux/tchar.h +428 -0
  217. data/transactd.gemspec +97 -0
  218. metadata +267 -0
@@ -0,0 +1,574 @@
1
+ /*=================================================================
2
+ Copyright (C) 2012 2013 BizStation Corp All rights reserved.
3
+
4
+ This program is free software; you can redistribute it and/or
5
+ modify it under the terms of the GNU General Public License
6
+ as published by the Free Software Foundation; either version 2
7
+ of the License, or (at your option) any later version.
8
+
9
+ This program is distributed in the hope that it will be useful,
10
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
+ GNU General Public License for more details.
13
+
14
+ You should have received a copy of the GNU General Public License
15
+ along with this program; if not, write to the Free Software
16
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17
+ 02111-1307, USA.
18
+ =================================================================*/
19
+
20
+ #include "sqlBuilder.h"
21
+ #include <bzs/rtl/stl_uty.h>
22
+ #include <algorithm>
23
+ #include <stdio.h>
24
+ #include <bzs/db/protocol/tdap/mysql/characterset.h>
25
+ #include <stdlib.h>
26
+ #include <bzs/env/crosscompile.h>
27
+ #include "stringConverter.h"
28
+ #ifdef _WIN32
29
+ #include <windows.h>
30
+ #else
31
+ #include <bzs/env/mbcswchrLinux.h>
32
+ #endif
33
+
34
+ namespace bzs
35
+ {
36
+ namespace db
37
+ {
38
+ namespace protocol
39
+ {
40
+ namespace tdap
41
+ {
42
+ namespace client
43
+ {
44
+
45
+
46
+ /** Remove extention
47
+ * To lower_case
48
+ */
49
+ std::string getFileName(const char* name)
50
+ {
51
+ const char* st = name;
52
+ const char* en = st + strlen(name);
53
+ const char* p = strrchr(name, '.');
54
+ if (p)
55
+ en = p;
56
+ p = strrchr(name, PSEPARATOR_C);
57
+ if (p)
58
+ st = p+1;
59
+ p = strrchr(name, '=');
60
+ if (p && p+1 > st)
61
+ st = p+1;
62
+
63
+ char tableName[MYSQL_TBNAME_SIZE]={0x00};
64
+ strncpy_s(tableName, MYSQL_TBNAME_SIZE, st, en-st);
65
+ _strlwr_s(tableName, MYSQL_TBNAME_SIZE);
66
+ return tableName;
67
+ }
68
+
69
+ #define TMP_BUFSIZE 100
70
+ char g_buf[TMP_BUFSIZE];
71
+
72
+ #define NOTE_TYPE 12
73
+ #define VAR_TYPE 13
74
+
75
+ const char* getFieldTypeName(uchar_td fieldType, int size, bool nobinary,
76
+ const char* charsetName)
77
+ {
78
+ const char* bin_ptr = nobinary ? "" : "binary";
79
+ char charsetString[128] = {" CHARACTER SET "};
80
+ if (charsetName[0])
81
+ strcat_s(charsetString, 128, charsetName);
82
+ else
83
+ charsetString[0] = 0x00;
84
+
85
+ switch(fieldType)
86
+ {
87
+
88
+ case ft_integer:
89
+ case ft_autoinc:
90
+ case ft_currency:
91
+ case ft_date:
92
+ case ft_time:
93
+ case ft_timestamp:
94
+ if (size==1)return "TINYINT";
95
+ if (size==2)return "SMALLINT";
96
+ if (size==4)return "INT";
97
+ if (size==8)return "BIGINT";
98
+ case ft_uinteger:
99
+ if (size==1)return "TINYINT UNSIGNED";
100
+ if (size==2)return "SMALLINT UNSIGNED";
101
+ if (size==4)return "INT UNSIGNED";
102
+ if (size==8)return "BIGINT UNSIGNED";
103
+ case ft_logical:return "TINYINT UNSIGNED";
104
+ case ft_mydate:return "DATE";
105
+ case ft_mytime:
106
+ sprintf_s(g_buf, TMP_BUFSIZE, "TIME(%d)", (size-3)*2);
107
+ return g_buf;
108
+ case ft_mydatetime:
109
+ sprintf_s(g_buf, TMP_BUFSIZE, "DATETIME(%d)", (size-5)*2);
110
+ return g_buf;
111
+ case ft_mytimestamp:
112
+
113
+ sprintf_s(g_buf, TMP_BUFSIZE, "TIMESTAMP(%d)", (size-4)*2);
114
+ return g_buf;
115
+ case ft_mytext:
116
+ if (size-8 == 4)
117
+ return "LONGTEXT";
118
+ else if (size-8 == 3)
119
+ return "MEDIUMTEXT";
120
+ else if (size-8 == 2)
121
+ return "TEXT";
122
+ return "TINYTEXT";
123
+ case ft_myblob:
124
+ if (size-8 == 4)
125
+ return "LONGBLOB";
126
+ else if (size-8 == 3)
127
+ return "MEDIUMBLOB";
128
+ else if (size-8 == 2)
129
+ return "BLOB";
130
+ return "TINYBLOB";
131
+ case ft_float:
132
+ if (size==4)return "FLOAT";
133
+ if (size==8)return "DOUBLE";
134
+ case ft_string:
135
+ sprintf_s(g_buf, TMP_BUFSIZE, "BINARY(%d)", size);
136
+ return g_buf;
137
+ case ft_zstring:
138
+ case ft_wzstring:
139
+ sprintf_s(g_buf, TMP_BUFSIZE, "BINARY(%d)", size);
140
+ return g_buf;
141
+ case ft_note: //note can not use key
142
+ case ft_lvar: //var
143
+ sprintf_s(g_buf, TMP_BUFSIZE, "VARBINARY(%d)", size);
144
+ return g_buf;
145
+ case ft_myvarbinary:
146
+ sprintf_s(g_buf, TMP_BUFSIZE, "VARBINARY(%d)", size);
147
+ return g_buf;
148
+ case ft_mywvarbinary:
149
+ sprintf_s(g_buf, TMP_BUFSIZE, "VARBINARY(%d)", size);
150
+ return g_buf;
151
+
152
+ case ft_mywvarchar:
153
+ sprintf_s(g_buf, TMP_BUFSIZE, "VARCHAR(%d) %s CHARACTER SET utf16le", size, bin_ptr);
154
+ return g_buf;
155
+ case ft_myvarchar:
156
+ sprintf_s(g_buf, TMP_BUFSIZE, "VARCHAR(%d) %s%s", size, bin_ptr, charsetString);
157
+ return g_buf;
158
+ case ft_mychar:
159
+ sprintf_s(g_buf, TMP_BUFSIZE, "CHAR(%d) %s%s", size, bin_ptr, charsetString);
160
+ return g_buf;
161
+ case ft_mywchar:
162
+ sprintf_s(g_buf, TMP_BUFSIZE, "CHAR(%d) %s CHARACTER SET utf16le", size, bin_ptr);
163
+ return g_buf;
164
+ case ft_decimal:
165
+ case ft_money:
166
+ case ft_numeric:
167
+ case ft_bfloat:
168
+ case ft_numericsts:
169
+ case ft_numericsa:
170
+ case ft_guid:
171
+ sprintf_s(g_buf, TMP_BUFSIZE, "BINARY(%d)", size);
172
+ return g_buf;
173
+ }
174
+ return "";
175
+ }
176
+
177
+ FLAGS getKeyFlags(tabledef* table, short fieldNum)
178
+ {
179
+ for (int i=0;i<table->keyCount;i++)
180
+ {
181
+ keydef& key = table->keyDefs[i];
182
+ for (int j=0;j<key.segmentCount;j++)
183
+ {
184
+ if (key.segments[j].fieldNum == fieldNum)
185
+ return key.segments[j].flags;
186
+ }
187
+ }
188
+ FLAGS a;
189
+ a.all = 0;
190
+ return a;
191
+ }
192
+
193
+ bool isNumericFieldName(const char* name)
194
+ {
195
+ if (name )
196
+ {
197
+ if (name[0] >= '0' && name[0] <= '9')
198
+ return true;
199
+ }
200
+ return false;
201
+ }
202
+
203
+ bool isNULLKeySegment(tabledef* table, short fieldIndex)
204
+ {
205
+ bool ret = 0;
206
+ for (int i=0;i<table->keyCount;i++)
207
+ {
208
+ keydef& key = table->keyDefs[i];
209
+ for (int j=0;j<key.segmentCount;j++)
210
+ {
211
+ if (key.segments[j].fieldNum == fieldIndex)
212
+ {
213
+ // bit3 all segment NULL key
214
+ // bit9 part segment NULL key
215
+ // if fd.nullValue != 0x00 then this field is type of not null .
216
+ const fielddef& fd = table->fieldDefs[fieldIndex];
217
+ if (((key.segments[j].flags.bit3 || key.segments[j].flags.bit9))
218
+ && (fd.nullValue == 0x00))
219
+ ret = true;
220
+ else
221
+ return false;
222
+ }
223
+ }
224
+ }
225
+ return ret;
226
+ }
227
+
228
+ std::string getFiledList(tabledef* table, std::vector<std::string>& fdl)
229
+ {
230
+ std::string s;
231
+ int len;
232
+ for (int i=0;i<table->fieldCount;i++)
233
+ {
234
+ fielddef& fd = table->fieldDefs[i];
235
+ s += "`";
236
+ s += fdl[i];
237
+ s += "` ";
238
+ len = fd.len - fd.varLenBytes();
239
+
240
+ /*
241
+ Although len is a number of bytes in BDF, it becomes the number of characters in MySQL.
242
+ Moreover, unicode cannot be specified by charset of the field. */
243
+ if (fd.charsetIndex() == 0)
244
+ fd.setCharsetIndex(table->charsetIndex);
245
+ if ((fd.type== ft_myvarchar)||(fd.type== ft_mychar))
246
+ len /= mysql::charsize(fd.charsetIndex());
247
+ else if ((fd.type== ft_mywvarchar)|| (fd.type== ft_mywchar))
248
+ len /= mysql::charsize(CHARSET_UTF16LE);
249
+
250
+ FLAGS f = getKeyFlags(table, i);
251
+ const char* charsetName = "";
252
+ if (fd.charsetIndex() != table->charsetIndex)
253
+ charsetName = mysql::charsetName(fd.charsetIndex());
254
+
255
+ s += getFieldTypeName(fd.type, len, f.bitA, charsetName);
256
+ if (isNULLKeySegment(table, i))
257
+ s += " NULL";
258
+ else
259
+ s += " NOT NULL";
260
+ if (fd.type == ft_autoinc)
261
+ s += " AUTO_INCREMENT";
262
+ s += ",";
263
+ }
264
+ return s;
265
+ }
266
+
267
+ void insertNisFields(tabledef* table, std::vector<std::string>& fdl, std::string& s)
268
+ {
269
+ char buf[20];
270
+ for (int i=0;i<table->keyCount;i++)
271
+ {
272
+ _ltoa_s(i, buf, 20, 10);
273
+ std::string fddef="";
274
+ keydef& key = table->keyDefs[i];
275
+ if (key.segmentCount > 1)
276
+ {
277
+ if (key.segments[0].flags.bit9)
278
+ fddef = std::string("`") + "$nfn" + buf + "` TINYINT UNSIGNED NULL," ;
279
+ else if (key.segments[0].flags.bit3)
280
+ fddef = std::string("`") + "$nfa" + buf + "` TINYINT UNSIGNED NULL," ;
281
+
282
+ }
283
+ if (fddef!="")
284
+ s += fddef;
285
+ }
286
+ }
287
+
288
+ std::string getKeyList(tabledef* table, std::vector<std::string>& fdl)
289
+ {
290
+ char buf[20];
291
+ std::string s;
292
+
293
+ for (int i=0;i<table->keyCount;i++)
294
+ {
295
+ keydef& key = table->keyDefs[i];
296
+ _ltoa_s(i, buf, 20, 10);
297
+ if ((table->primaryKeyNum==i)
298
+ && (fdl[key.segments[0].fieldNum]=="auto_id_field"))
299
+ s += " PRIMARY KEY ";
300
+ else
301
+ {
302
+ if(key.segments[0].flags.bit0==false)
303
+ s += " UNIQUE ";
304
+ else
305
+ s += " INDEX ";
306
+ s += "key";
307
+
308
+ s += buf;
309
+ }
310
+ s += "(";
311
+
312
+
313
+ //"nf" segment is added to a head.
314
+ if (key.segmentCount > 1)
315
+ {
316
+ //If a first segment is not 1 byte of Logical
317
+ fielddef& fd = table->fieldDefs[key.segments[0].fieldNum];
318
+ if (!((fd.len == 1) && (fd.type == ft_logical)))
319
+ {
320
+ if (key.segments[0].flags.bit9)
321
+ s += std::string("`") + "$nfn" + buf + "`," ;
322
+ else if (key.segments[0].flags.bit3)
323
+ s += std::string("`") + "$nfa" + buf + "`," ;
324
+ }
325
+ }
326
+ for (int j=0;j<key.segmentCount;j++)
327
+ {
328
+ s += "`" ;
329
+ s += fdl[key.segments[j].fieldNum];
330
+ s += "`" ;
331
+
332
+ //part key
333
+ fielddef& fd = table->fieldDefs[key.segments[j].fieldNum];
334
+ if (fd.keylen && ((fd.keylen != fd.len)||fd.blobLenBytes()))
335
+ {
336
+ sprintf_s(buf, 20, "(%d)", fd.keylen);
337
+ s += buf;
338
+ }
339
+
340
+ if (key.segments[j].flags.bit6)
341
+ s += " DESC";
342
+ s += ",";
343
+ }
344
+ s.erase(s.end()-1);
345
+ s += "),";
346
+ }
347
+ return s;
348
+ }
349
+
350
+ std::string convertString(unsigned int toPage, unsigned int fromPage, const char* p)
351
+ {
352
+ std::string s;
353
+ stringConverter cv(toPage, fromPage);
354
+ int osize = (int)strlen(p)*3;
355
+ char* srvchar = new char[osize];
356
+ size_t len = cv.convert(srvchar, osize, p, strlen(p));
357
+ srvchar[len] = 0x00;
358
+ s = srvchar;
359
+ delete [] srvchar;
360
+ return s;
361
+ }
362
+
363
+ std::string sqlCreateTable(const char* name/*utf8*/, tabledef* table, uchar_td charsetIndexServer)
364
+ {
365
+ //Duplication of a name is inspected and, in duplication, _1 is added.
366
+ //It does not correspond to two or more duplications.
367
+ std::vector<std::string> fdl;
368
+ std::vector<std::string> fds;
369
+ char tmp[1024];
370
+ char num[10];
371
+ for (int i=0;i<table->fieldCount;i++)
372
+ {
373
+ fielddef& fd = table->fieldDefs[i];
374
+ strcpy_s(tmp, 256, fd.nameA());
375
+ _strlwr_s(tmp, 256);
376
+ if (std::find(fdl.begin(), fdl.end(), tmp) != fdl.end())
377
+ {
378
+ _ltoa_s(i, num, 10, 10);
379
+ strcat_s(tmp, 256, num);
380
+ fds.push_back(std::string(fd.nameA())+ num);
381
+ }else
382
+ fds.push_back(fd.nameA());
383
+ fdl.push_back(tmp);
384
+
385
+ }
386
+ uint_td schemaCodePage = table->schemaCodePage ? table->schemaCodePage : GetACP();
387
+ std::string s = "CREATE TABLE `";
388
+ if ((name && name[0]))
389
+ {
390
+ std::string name2 = name;
391
+ if (schemaCodePage != CP_UTF8)
392
+ name2 = convertString(schemaCodePage, CP_UTF8, name);
393
+ s += getFileName(name2.c_str()) + "` (";
394
+ }
395
+ else
396
+ s += getFileName(table->fileNameA()) + "` (";
397
+ s += getFiledList(table, fds);
398
+ insertNisFields(table, fds, s);
399
+ s += getKeyList(table, fds);
400
+ if (s[s.size()-1] == ',')
401
+ s.erase(s.end()-1);
402
+ s += ") ENGINE=InnoDB default charset="+ std::string(mysql::charsetName(table->charsetIndex));
403
+
404
+ // create statement charset must be server default charset.
405
+ // server default charset writen in my.cnf.
406
+ if (schemaCodePage != mysql::codePage(charsetIndexServer))
407
+ s = convertString(mysql::codePage(charsetIndexServer), schemaCodePage, s.c_str());
408
+ return s;
409
+ }
410
+
411
+
412
+ int findFieldNum(std::vector<fielddef>& fds, int pos)
413
+ {
414
+ for (int i=0;i<(int)fds.size();i++)
415
+ {
416
+ if (fds[i].pos==pos)
417
+ return i;
418
+ }
419
+ return -1;
420
+ }
421
+
422
+ void makeField(std::vector<fielddef>& fds, keySpec* ks, int keynum, int seg)
423
+ {
424
+ char buf[100];
425
+ fielddef fd ;
426
+ memset(&fd, 0x00, sizeof(fielddef));
427
+ sprintf_s(buf, 100, "key%d_%d", keynum, seg);
428
+ fd.setNameA(buf);
429
+ fd.len = ks->keyLen;
430
+ fd.type = ks->keyType;
431
+ fd.pos = ks->keyPos;
432
+ fds.push_back(fd);
433
+ }
434
+
435
+ bool field_sort(const fielddef& l, const fielddef& r)
436
+ {
437
+ if (l.pos < r.pos)
438
+ return 1;
439
+ return 0;
440
+ }
441
+
442
+ void completeFields(std::vector<fielddef>& fds, int reclen, bool valiableLen)
443
+ {
444
+ char buf[100];
445
+ int pos = 1;
446
+ for (int i=0;i<(int)fds.size();i++)
447
+ {
448
+ if (fds[i].pos > pos)
449
+ {
450
+ fielddef fd;
451
+ memset(&fd, 0x00, sizeof(fielddef));
452
+ sprintf_s(buf, 100, "field%d",i);
453
+ fd.setNameA(buf);
454
+ fd.pos = pos;
455
+ fd.len = fds[i].pos - pos;
456
+ fd.type = (fd.len > 255)? ft_note:ft_zstring;
457
+ fds.insert(fds.begin() + i, fd);
458
+ i++;
459
+ }
460
+ pos = fds[i].pos + fds[i].len;
461
+ }
462
+ if (pos <= reclen)
463
+ {
464
+ fielddef fd;
465
+ memset(&fd, 0x00, sizeof(fielddef));
466
+
467
+ sprintf_s(buf, 100, "field%d",(int)fds.size());
468
+ fd.setNameA(buf);
469
+ fd.pos = pos;
470
+ fd.len = reclen - pos + 1;
471
+ fd.type = (fd.len > 255)? ft_note:ft_zstring;
472
+ if (fd.type == NOTE_TYPE)
473
+ fd.len = 57000 - pos + 1;
474
+ fds.push_back(fd);
475
+ pos = fd.pos + fd.len;
476
+ }
477
+ //In the case of a variable-length record, the last must have a variable length field.
478
+ if (valiableLen)
479
+ {
480
+ if (fds[fds.size()-1].type != ft_note)
481
+ {
482
+ fielddef fd;
483
+ memset(&fd, 0x00, sizeof(fielddef));
484
+ sprintf_s(buf, 100, "field%d",(int)fds.size());
485
+ fd.setNameA(buf);
486
+ fd.pos = pos;
487
+ fd.len = 57000 - pos + 1;
488
+ fd.type = NOTE_TYPE;
489
+ fds.push_back(fd);
490
+ }
491
+ }
492
+
493
+
494
+ }
495
+
496
+ void makeTableDef(tabledef* TableDef, fileSpec* fs, std::vector<fielddef>& fds)
497
+ {
498
+ TableDef->keyCount = (uchar_td)fs->indexCount;
499
+ TableDef->flags.all = fs->fileFlag.all;
500
+ TableDef->fixedRecordLen = fs->recLen;
501
+
502
+ int k=0;
503
+ int seg = 0;
504
+ int keynum = 0;
505
+ while(keynum<TableDef->keyCount)
506
+ {
507
+ keySpec* ks = &(fs->keySpecs[k++]);
508
+ //key flag
509
+ TableDef->keyDefs[keynum].segments[seg].flags.all = ks->keyFlag.all;
510
+ TableDef->keyDefs[keynum].segmentCount = seg+1;
511
+
512
+ //field info
513
+ if (findFieldNum(fds, ks->keyPos)==-1)
514
+ makeField(fds, ks, keynum, seg);
515
+ seg++;
516
+ if (ks->keyFlag.bit4==false)
517
+ {
518
+ keynum++;
519
+ seg = 0;
520
+ }
521
+ }
522
+ //Sort by position.
523
+ std::sort(fds.begin(), fds.end(), field_sort);
524
+
525
+ //Fields are added to non key spaces
526
+ completeFields(fds, fs->recLen, (fs->fileFlag.all & 1));
527
+ //Assignment of a field number
528
+ k=0;
529
+ seg = 0;
530
+ keynum = 0;
531
+ while(keynum<TableDef->keyCount)
532
+ {
533
+ keySpec* ks = &(fs->keySpecs[k++]);
534
+ TableDef->keyDefs[keynum].segments[seg].fieldNum = findFieldNum(fds, ks->keyPos);
535
+ seg++;
536
+ if (ks->keyFlag.bit4==false)
537
+ {
538
+ keynum++;
539
+ seg = 0;
540
+ }
541
+ }
542
+
543
+ //Copy of field info.
544
+ TableDef->fieldDefs = &fds[0];
545
+ TableDef->fieldCount = (ushort_td)fds.size();
546
+
547
+ }
548
+
549
+ std::string sqlCreateTable(const char* fileName, fileSpec* fs, uchar_td charsetIndexServer)
550
+ {
551
+ tabledef table;
552
+ memset(&table, 0, sizeof(tabledef));
553
+ table.setFileNameA(fileName);
554
+ table.charsetIndex = mysql::charsetIndex(GetACP());
555
+ table.schemaCodePage = CP_UTF8;
556
+ std::vector<fielddef> fds;
557
+ std::vector<keydef> kds;
558
+ for (int i=0;i<fs->indexCount;i++)
559
+ kds.push_back(keydef());
560
+ if (fs->indexCount)
561
+ table.keyDefs = &kds[0];
562
+ makeTableDef(&table, fs, fds);
563
+
564
+ return sqlCreateTable(fileName, &table, charsetIndexServer);
565
+
566
+ }
567
+
568
+ }//namespace client
569
+ }//namespace tdap
570
+ }//namespace protocol
571
+ }//namespace db
572
+ }//namespace bzs
573
+
574
+
@@ -0,0 +1,53 @@
1
+ #ifndef BZS_DB_PROTOCOL_TDAP_CLIENT_SQLBUILDER_H
2
+ #define BZS_DB_PROTOCOL_TDAP_CLIENT_SQLBUILDER_H
3
+ /*=================================================================
4
+ Copyright (C) 2012 2013 BizStation Corp All rights reserved.
5
+
6
+ This program is free software; you can redistribute it and/or
7
+ modify it under the terms of the GNU General Public License
8
+ as published by the Free Software Foundation; either version 2
9
+ of the License, or (at your option) any later version.
10
+
11
+ This program is distributed in the hope that it will be useful,
12
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
13
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
+ GNU General Public License for more details.
15
+
16
+ You should have received a copy of the GNU General Public License
17
+ along with this program; if not, write to the Free Software
18
+ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19
+ 02111-1307, USA.
20
+ =================================================================*/
21
+ #include <bzs/db/protocol/tdap/tdapSchema.h>
22
+ #include <string>
23
+
24
+
25
+ namespace bzs
26
+ {
27
+ namespace db
28
+ {
29
+ namespace protocol
30
+ {
31
+ namespace tdap
32
+ {
33
+ namespace client
34
+ {
35
+
36
+ /** create statement charset must be server default charset.
37
+ * server default charset writen in my.cnf.
38
+ *
39
+ * @param charsetIndexServer server default charset
40
+ */
41
+ std::string sqlCreateTable(const char* name, tabledef* table, uchar_td charsetIndexServer);
42
+ std::string sqlCreateTable(const char* fileName, fileSpec* fs, uchar_td charsetIndexServer);
43
+
44
+
45
+
46
+ }//namespace client
47
+ }//namespace tdap
48
+ }//namespace protocol
49
+ }//namespace db
50
+ }//namespace bzs
51
+
52
+
53
+ #endif //BZS_DB_PROTOCOL_TDAP_CLIENT_SQLBUILDER_H