-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_defs.pl
executable file
·367 lines (294 loc) · 10.1 KB
/
create_defs.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#!/usr/bin/env perl
use strict;
use DBI;
use Data::Dumper;
use POSIX;
use Getopt::Long;
#----------------------------------------------------------------
# Parse command line
# Defaults
my $db_name = "test";
my $db_host = "127.0.0.1";
my $db_port = 3306;
my $db_user = "root";
my $db_pass = "";
my $db_table = "";
my $help = 0;
# Parse options
my $res = GetOptions ("db=s" => \$db_name,
"host=s" => \$db_host,
"port=i" => \$db_port,
"user=s" => \$db_user,
"password=s" => \$db_pass,
"table=s" => \$db_table,
"help|?" => \$help
);
Usage() if !$res || $help;
#----------------------------------------------------------------
# Connect to mysql
my $dsn = "DBI:mysql:database=$db_name;host=$db_host;port=$db_port";
my $dbh = DBI->connect($dsn, $db_user, $db_pass);
Usage("Can't connect to mysql!") unless $dbh;
#----------------------------------------------------------------
# Dump tables definitions
my $sth = $dbh->prepare("SHOW TABLES");
$sth->execute;
print("#ifndef table_defs_h\n#define table_defs_h\n\n");
print("// Table definitions\ntable_def_t table_definitions[] = {\n");
while (my $row = $sth->fetchrow_arrayref) {
my $table = $row->[0];
# Skip if not a requested specific table
next if ($db_table ne '' && $table ne $db_table);
# Skip if it is not an innodb table
next unless(GetTableStorageEngine($table) =~ /innodb/i);
# Get fields list for table
my @fields = ();
my $tbl_sth = $dbh->prepare("SHOW FIELDS FROM $table");
$tbl_sth->execute;
while (my $field = $tbl_sth->fetchrow_hashref) { push @fields, $field }
# Get primary key fields
my @pk_fields = ();
$tbl_sth = $dbh->prepare("show indexes from $table");
$tbl_sth->execute;
while (my $field = $tbl_sth->fetchrow_hashref) {
push @pk_fields, $field if ($field->{Key_name} eq 'PRIMARY');
}
# If no primary keys defined, check unique keys and use first one as primary
if (scalar(@pk_fields) == 0) {
$tbl_sth = $dbh->prepare("show indexes from $table");
$tbl_sth->execute;
my $pk_name = undef;
while (my $field = $tbl_sth->fetchrow_hashref) {
next unless ($field->{Non_unique} == 0);
$pk_name = $field->{Key_name} unless ($pk_name);
last if ($field->{Key_name} ne $pk_name);
push @pk_fields, $field;
}
}
DumpTableDef($table, \@fields, \@pk_fields);
}
print("};\n\n#endif\n");
exit(0);
#------------------------------------------------------------------
sub DumpTableDef($$$) {
my $table = shift;
my $fields = shift;
my $pk_fields = shift;
print("\t{\n\t\tname: \"$table\",\n\t\t{\n");
# Dump all PK fields
foreach my $pk_field (@$pk_fields) {
DumpField($table, FindFieldByName($fields, $pk_field->{Column_name}));
}
# Dump system PK if no PK fields found
if (scalar(@$pk_fields) == 0) {
DumpFieldLow(Name => 'DB_ROW_ID', ParsedType => 'FT_INTERNAL', FixedLen => 6, Null => 0);
}
# Dump 2 more sys fields
DumpFieldLow(Name => 'DB_TRX_ID', ParsedType => 'FT_INTERNAL', FixedLen => 6, Null => 0);
DumpFieldLow(Name => 'DB_ROLL_PTR', ParsedType => 'FT_INTERNAL', FixedLen => 7, Null => 0);
# Dump the rest of the fields
foreach my $field (@$fields) {
DumpField($table, $field) unless $field->{Key} eq 'PRI';
}
print("\t\t\t{ type: FT_NONE }\n");
print("\t\t}\n\t},\n");
}
#------------------------------------------------------------------
sub FindFieldByName($$) {
my $fields = shift;
my $name = shift;
foreach my $field (@$fields) {
return $field if $field->{Field} eq $name;
}
return undef;
}
#------------------------------------------------------------------
sub GetUIntLimits($) {
my $len = shift;
return (0, 255) if ($len == 1);
return (0, 65535) if ($len == 2);
return (0, "16777215UL") if ($len == 3);
return (0, "4294967295ULL") if ($len == 4);
return (0, "18446744073709551615ULL") if ($len == 8);
return (0, 30000);
}
#------------------------------------------------------------------
sub GetIntLimits($) {
my $len = shift;
return (-128, 127) if ($len == 1);
return (-32768, 32767) if ($len == 2);
return ("-8388608L", "8388607L") if ($len == 3);
return ("-2147483648LL", "2147483647LL") if ($len == 4);
return ("-9223372036854775808LL", "9223372036854775807LL") if ($len == 8);
return (0, 30000);
}
#------------------------------------------------------------------
sub DumpFieldLow {
my %info = @_;
printf("\t\t\t{ /* %s */\n", $info{Type});
printf("\t\t\t\tname: \"%s\",\n", $info{Name});
printf("\t\t\t\ttype: %s,\n", $info{ParsedType});
if ($info{FixedLen}) {
printf("\t\t\t\tfixed_length: %d,\n", $info{FixedLen});
} else {
printf("\t\t\t\tmin_length: %d,\n", $info{MinLen});
printf("\t\t\t\tmax_length: %d,\n", $info{MaxLen});
}
printf("\n");
if ($info{ParsedType} eq 'FT_TEXT' || $info{ParsedType} eq 'FT_CHAR') {
printf("\t\t\t\thas_limits: TRUE,\n");
printf("\t\t\t\tlimits: {\n");
printf("\t\t\t\t\tcan_be_null: %s,\n", $info{Null} ? 'TRUE' : 'FALSE');
printf("\t\t\t\t\tchar_min_len: 0,\n");
printf("\t\t\t\t\tchar_max_len: %d,\n", $info{MaxLen} + $info{FixedLen});
printf("\t\t\t\t\tchar_ascii_only: TRUE\n");
printf("\t\t\t\t},\n\n");
}
if ($info{ParsedType} eq 'FT_INT' && $info{Name} =~ /id$/i) {
my ($min, $max) = GetIntLimits($info{FixedLen});
printf("\t\t\t\thas_limits: TRUE,\n");
printf("\t\t\t\tlimits: {\n");
printf("\t\t\t\t\tcan_be_null: %s,\n", $info{Null} ? 'TRUE' : 'FALSE');
printf("\t\t\t\t\tint_min_val: %s,\n", $min);
printf("\t\t\t\t\tint_max_val: %s\n", $max);
printf("\t\t\t\t},\n\n");
}
if ($info{ParsedType} eq 'FT_UINT' && $info{Name} =~ /id$/i) {
my ($min, $max) = GetUIntLimits($info{FixedLen});
printf("\t\t\t\thas_limits: TRUE,\n");
printf("\t\t\t\tlimits: {\n");
printf("\t\t\t\t\tcan_be_null: %s,\n", $info{Null} ? 'TRUE' : 'FALSE');
printf("\t\t\t\t\tuint_min_val: %s,\n", $min);
printf("\t\t\t\t\tuint_max_val: %s\n", $max);
printf("\t\t\t\t},\n\n");
}
if ($info{ParsedType} eq 'FT_ENUM') {
printf("\t\t\t\thas_limits: TRUE,\n");
printf("\t\t\t\tlimits: {\n");
printf("\t\t\t\t\tcan_be_null: %s,\n", $info{Null} ? 'TRUE' : 'FALSE');
printf("\t\t\t\t\tenum_values_count: %d,\n", scalar(@{$info{Values}}));
printf("\t\t\t\t\tenum_values: { \"%s\" }\n", join('", "', @{$info{Values}}));
printf("\t\t\t\t},\n\n");
}
printf("\t\t\t\tcan_be_null: %s\n", $info{Null} ? 'TRUE' : 'FALSE');
printf("\t\t\t},\n");
}
#------------------------------------------------------------------
sub DumpField($$) {
my $table = shift;
my $field = shift;
my %info;
$info{Null} = ($field->{Null} eq 'YES');
$info{Name} = $field->{Field};
my $type_info = ParseFieldType($field->{Type});
if ($type_info->{type} eq 'FT_INT' && IsFieldUnsigned($table, $field->{Field})) {
$type_info->{type} = 'FT_UINT';
}
$info{Type} = $field->{Type};
$info{Values} = $type_info->{values};
$info{ParsedType} = $type_info->{type};
$info{MinLen} = $type_info->{min_len};
$info{MaxLen} = $type_info->{max_len};
$info{FixedLen} = $type_info->{fixed_len};
DumpFieldLow(%info);
}
#------------------------------------------------------------------
sub IsFieldUnsigned($$) {
my ($table, $field) = @_;
my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
$sth->execute;
my $row = $sth->fetchrow_arrayref;
return ($row->[1] =~ /$field[^,]*unsigned/i);
}
#------------------------------------------------------------------
sub GetTableStorageEngine($) {
my $table = shift;
my $sth = $dbh->prepare("SHOW TABLE STATUS LIKE '$table'");
$sth->execute;
my $row = $sth->fetchrow_hashref;
return $row->{Engine};
}
#------------------------------------------------------------------
sub Usage {
my $msg = shift;
if ($msg) {
print "Error: $msg\n";
}
print "Usage: $0 [options]\n" .
"Where options are:\n" .
"\t--host - mysql host\n" .
"\t--port - mysql port\n" .
"\t--user - mysql username\n" .
"\t--password - mysql password\n" .
"\t--db - mysql database\n" .
"\t--table - specific table only\n" .
"\t--help - show this help\n\n";
exit(1);
}
#------------------------------------------------------------------
sub ParseFieldType($) {
my $type = shift;
if ($type =~ /DATETIME/i) {
return { type => 'FT_DATETIME', fixed_len => 8 };
}
if ($type =~ /TIMESTAMP/i) {
return { type => 'FT_UINT', fixed_len => 4 };
}
if ($type =~ /DATE/i) {
return { type => 'FT_DATE', fixed_len => 3 };
}
if ($type =~ /TIME/i) {
return { type => 'FT_DATE', fixed_len => 3 };
}
if ($type =~ /YEAR/i) {
return { type => 'FT_UINT', fixed_len => 1 };
}
if ($type =~ /^TINYINT/i) {
return { type => 'FT_INT', fixed_len => 1 };
}
if ($type =~ /^SMALLINT/i) {
return { type => 'FT_INT', fixed_len => 2 };
}
if ($type =~ /^MEDIUMINT/i) {
return { type => 'FT_INT', fixed_len => 3 };
}
if ($type =~ /^INT/i) {
return { type => 'FT_INT', fixed_len => 4 };
}
if ($type =~ /^BIGINT/i) {
return { type => 'FT_INT', fixed_len => 8 };
}
if ($type =~ /^CHAR\((\d+)\)/i) {
return { type => 'FT_CHAR', fixed_len => $1 };
}
if ($type =~ /VARCHAR\((\d+)\)/i) {
return { type => 'FT_CHAR', min_len => 0, max_len => $1 };
}
if ($type =~ /^TINYTEXT$/i) {
return { type => 'FT_TEXT', min_len => 0, max_len => 255 };
}
if ($type =~ /^TEXT$/i) {
return { type => 'FT_TEXT', min_len => 0, max_len => 65535 };
}
if ($type =~ /^MEDIUMTEXT$/i) {
return { type => 'FT_TEXT', min_len => 0, max_len => 16777215 };
}
if ($type =~ /^LONGTEXT$/i) {
return { type => 'FT_TEXT', min_len => 0, max_len => 4294967295 };
}
if ($type =~ /^FLOAT/i) {
return { type => 'FT_FLOAT', fixed_len => 4 };
}
if ($type =~ /^DOUBLE/i) {
return { type => 'FT_DOUBLE', fixed_len => 8 };
}
if ($type =~ /^ENUM\(\'(.*)\'\)/i) {
my @enum_values = split(/[\'\"]\,[\'\"]/, $1);
return { type => 'FT_ENUM', fixed_len => 1, values => \@enum_values };
}
if ($type =~ /^DECIMAL\((\d+),\s*(\d+)\)/i) {
my $len_bytes = ceil(($1-$2) * 4 / 9) + ceil($2*4/9);
return { type => 'FT_CHAR', fixed_len => $len_bytes };
}
die "Unsupported type: $type!\n";
}