@chihqiang/sql-quicktype 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cli.js CHANGED
@@ -6,13 +6,6 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
6
  var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
- var __esm = (fn, res) => function __init() {
10
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
11
- };
12
- var __export = (target, all) => {
13
- for (var name in all)
14
- __defProp(target, name, { get: all[name], enumerable: true });
15
- };
16
9
  var __copyProps = (to, from, except, desc) => {
17
10
  if (from && typeof from === "object" || typeof from === "function") {
18
11
  for (let key of __getOwnPropNames(from))
@@ -30,695 +23,18 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
30
23
  mod
31
24
  ));
32
25
 
33
- // src/generator/TypeScriptGenerator.ts
34
- var TypeScriptGenerator_exports = {};
35
- __export(TypeScriptGenerator_exports, {
36
- TypeScriptGenerator: () => TypeScriptGenerator
37
- });
38
- var TypeScriptGenerator;
39
- var init_TypeScriptGenerator = __esm({
40
- "src/generator/TypeScriptGenerator.ts"() {
41
- "use strict";
42
- init_generator();
43
- TypeScriptGenerator = class extends AGenerator {
44
- constructor(options = { language: "typescript" }) {
45
- super();
46
- this.options = options;
47
- }
48
- /**
49
- * 格式化字段名称(使用驼峰命名)
50
- */
51
- formatFieldName(name) {
52
- return name.split("_").map((part, index) => {
53
- if (index === 0) {
54
- return part.toLowerCase();
55
- }
56
- return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase();
57
- }).join("");
58
- }
59
- /**
60
- * 生成整个数据库模式的类型定义
61
- */
62
- generateDatabase(database) {
63
- let result = `// Database: ${database.name}
64
- `;
65
- result += `// Dialect: ${database.dialect}
66
-
67
- `;
68
- for (const table of database.tables) {
69
- result += this.generateTable(table);
70
- result += "\n";
71
- }
72
- return result;
73
- }
74
- /**
75
- * 生成单个表的类型定义
76
- */
77
- generateTable(table) {
78
- let result = `// ${table.name} \u8868\u7ED3\u6784
79
- `;
80
- result += `export interface ${this.formatTypeName(table.name)} {
81
- `;
82
- for (const column of table.columns) {
83
- result += this.generateColumn(column);
84
- }
85
- result += "}\n";
86
- return result;
87
- }
88
- /**
89
- * 生成列的类型定义
90
- */
91
- generateColumn(column) {
92
- const fieldName = this.formatFieldName(column.name);
93
- const typeName = this.mapSQLType(column.type, column.name);
94
- const optional = column.nullable ? "?" : "";
95
- let result = ` ${fieldName}${optional}: ${typeName}`;
96
- if (column.comment && this.options.generateComments !== false) {
97
- result += `; // ${column.comment}`;
98
- }
99
- result += "\n";
100
- return result;
101
- }
102
- /**
103
- * 映射 SQL 类型到 TypeScript 类型
104
- */
105
- mapSQLType(type, columnName) {
106
- switch (type.kind) {
107
- case "int":
108
- case "bigint":
109
- return "number";
110
- case "float":
111
- case "decimal":
112
- return "number";
113
- case "varchar":
114
- case "text":
115
- return "string";
116
- case "boolean":
117
- return "boolean";
118
- case "date":
119
- case "datetime":
120
- return "Date";
121
- case "json":
122
- return "unknown";
123
- case "enum":
124
- if (type.values && type.values.length > 0) {
125
- const enumValues = type.values.map((v) => `'${v}'`).join(" | ");
126
- if (columnName) {
127
- const fieldName = this.formatFieldName(columnName);
128
- return `${fieldName}: ${enumValues}`;
129
- }
130
- return enumValues;
131
- }
132
- return "string";
133
- default:
134
- return "string";
135
- }
136
- }
137
- };
138
- }
139
- });
140
-
141
- // src/generator/GolangGenerator.ts
142
- var GolangGenerator_exports = {};
143
- __export(GolangGenerator_exports, {
144
- GolangGenerator: () => GolangGenerator
145
- });
146
- var GolangGenerator;
147
- var init_GolangGenerator = __esm({
148
- "src/generator/GolangGenerator.ts"() {
149
- "use strict";
150
- init_generator();
151
- GolangGenerator = class extends AGenerator {
152
- constructor(options = { language: "go" }) {
153
- super();
154
- this.options = options;
155
- }
156
- /**
157
- * 生成整个数据库模式的类型定义
158
- */
159
- generateDatabase(database) {
160
- let result = `// Database: ${database.name}
161
- `;
162
- result += `// Dialect: ${database.dialect}
163
-
164
- `;
165
- if (this.options.namespace) {
166
- result += `package ${this.options.namespace}
167
-
168
- `;
169
- }
170
- if (this.needsTimeImport(database)) {
171
- result += `import "time"
172
-
173
- `;
174
- }
175
- for (const table of database.tables) {
176
- result += this.generateTable(table);
177
- result += "\n";
178
- }
179
- return result;
180
- }
181
- /**
182
- * 生成单个表的类型定义
183
- */
184
- generateTable(table) {
185
- let result = `// ${table.name} \u8868\u7ED3\u6784
186
- `;
187
- result += `type ${this.formatTypeName(table.name)} struct {
188
- `;
189
- for (const column of table.columns) {
190
- result += this.generateColumn(column);
191
- }
192
- result += "}\n";
193
- return result;
194
- }
195
- /**
196
- * 生成列的类型定义
197
- */
198
- generateColumn(column) {
199
- const fieldName = this.formatFieldName(column.name);
200
- const typeName = this.mapSQLType(column.type);
201
- const tag = this.generateGoTag(column);
202
- let result = ` ${fieldName} ${typeName} ${tag}`;
203
- if (column.comment && this.options.generateComments !== false) {
204
- result += ` // ${column.comment}`;
205
- }
206
- result += "\n";
207
- return result;
208
- }
209
- /**
210
- * 映射 SQL 类型到 Go 类型
211
- */
212
- mapSQLType(type) {
213
- switch (type.kind) {
214
- case "int":
215
- return "int";
216
- case "bigint":
217
- return "int64";
218
- case "float":
219
- return "float64";
220
- case "decimal":
221
- return "float64";
222
- case "varchar":
223
- case "text":
224
- return "string";
225
- case "boolean":
226
- return "bool";
227
- case "date":
228
- case "datetime":
229
- return "time.Time";
230
- case "json":
231
- return "interface{}";
232
- case "enum":
233
- return "string";
234
- default:
235
- return "string";
236
- }
237
- }
238
- /**
239
- * 生成 Go 结构体标签
240
- */
241
- generateGoTag(column) {
242
- const tags = [];
243
- tags.push(`json:"${column.name}"`);
244
- tags.push(`db:"${column.name}"`);
245
- return `\`${tags.join(" ")}\``;
246
- }
247
- };
248
- }
249
- });
250
-
251
- // src/generator/GormGenerator.ts
252
- var GormGenerator_exports = {};
253
- __export(GormGenerator_exports, {
254
- GormGenerator: () => GormGenerator
255
- });
256
- var GormGenerator;
257
- var init_GormGenerator = __esm({
258
- "src/generator/GormGenerator.ts"() {
259
- "use strict";
260
- init_generator();
261
- GormGenerator = class extends AGenerator {
262
- constructor(options = { language: "gorm" }) {
263
- super();
264
- this.options = options;
265
- }
266
- /**
267
- * 生成整个数据库模式的类型定义
268
- */
269
- generateDatabase(database) {
270
- let result = `// Database: ${database.name}
271
- `;
272
- result += `// Dialect: ${database.dialect}
273
-
274
- `;
275
- if (this.options.namespace) {
276
- result += `package ${this.options.namespace}
277
-
278
- `;
279
- }
280
- if (this.needsTimeImport(database)) {
281
- result += `import "time"
282
-
283
- `;
284
- }
285
- for (const table of database.tables) {
286
- result += this.generateTable(table);
287
- result += "\n";
288
- }
289
- return result;
290
- }
291
- /**
292
- * 生成单个表的类型定义
293
- */
294
- generateTable(table) {
295
- let result = `// ${table.name} \u8868\u7ED3\u6784
296
- `;
297
- result += `type ${this.formatTypeName(table.name)} struct {
298
- `;
299
- for (const column of table.columns) {
300
- result += this.generateColumn(column);
301
- }
302
- result += "}\n";
303
- return result;
304
- }
305
- /**
306
- * 生成列的类型定义
307
- */
308
- generateColumn(column) {
309
- const fieldName = this.formatFieldName(column.name);
310
- const typeName = this.mapSQLType(column.type);
311
- const tag = this.generateGormTag(column);
312
- let result = ` ${fieldName} ${typeName} ${tag}`;
313
- if (column.comment && this.options.generateComments !== false) {
314
- result += ` // ${column.comment}`;
315
- }
316
- result += "\n";
317
- return result;
318
- }
319
- /**
320
- * 映射 SQL 类型到 GORM 类型
321
- */
322
- mapSQLType(type) {
323
- switch (type.kind) {
324
- case "int":
325
- return "int";
326
- case "bigint":
327
- return "int64";
328
- case "float":
329
- return "float64";
330
- case "decimal":
331
- return "float64";
332
- case "varchar":
333
- case "text":
334
- return "string";
335
- case "boolean":
336
- return "bool";
337
- case "date":
338
- case "datetime":
339
- return "time.Time";
340
- case "json":
341
- return "interface{}";
342
- case "enum":
343
- return "string";
344
- default:
345
- return "string";
346
- }
347
- }
348
- /**
349
- * 生成 GORM 结构体标签
350
- */
351
- generateGormTag(column) {
352
- const tags = [];
353
- tags.push(`column:${column.name}`);
354
- let typeTag = "type:";
355
- switch (column.type.kind) {
356
- case "int":
357
- typeTag += "int";
358
- if (column.type.length) {
359
- typeTag += `(${column.type.length})`;
360
- }
361
- break;
362
- case "bigint":
363
- typeTag += "bigint";
364
- if (column.type.length) {
365
- typeTag += `(${column.type.length})`;
366
- }
367
- break;
368
- case "float":
369
- typeTag += "float";
370
- if (column.type.precision) {
371
- typeTag += `(${column.type.precision}`;
372
- if (column.type.scale) {
373
- typeTag += `,${column.type.scale}`;
374
- }
375
- typeTag += ")";
376
- }
377
- break;
378
- case "decimal":
379
- typeTag += "decimal";
380
- if (column.type.precision) {
381
- typeTag += `(${column.type.precision}`;
382
- if (column.type.scale) {
383
- typeTag += `,${column.type.scale}`;
384
- }
385
- typeTag += ")";
386
- }
387
- break;
388
- case "varchar":
389
- typeTag += "varchar";
390
- if (column.type.length) {
391
- typeTag += `(${column.type.length})`;
392
- }
393
- break;
394
- case "text":
395
- typeTag += "text";
396
- break;
397
- case "boolean":
398
- typeTag += "bool";
399
- break;
400
- case "date":
401
- typeTag += "date";
402
- break;
403
- case "datetime":
404
- typeTag += "datetime";
405
- break;
406
- case "json":
407
- typeTag += "json";
408
- break;
409
- case "enum":
410
- typeTag += "enum";
411
- break;
412
- default:
413
- typeTag += "string";
414
- }
415
- tags.push(typeTag);
416
- if (column.primaryKey) {
417
- tags.push("primaryKey");
418
- }
419
- if (column.unique) {
420
- tags.push("unique");
421
- }
422
- if (!column.nullable) {
423
- tags.push("not null");
424
- }
425
- if (column.default) {
426
- tags.push(`default:${column.default}`);
427
- }
428
- if (column.generated) {
429
- tags.push("autoIncrement");
430
- }
431
- if (column.comment) {
432
- tags.push(`comment:${column.comment}`);
433
- }
434
- return `\`gorm:"${tags.join(";")}" json:"${column.name}"\``;
435
- }
436
- };
437
- }
438
- });
439
-
440
- // src/generator/XormGenerator.ts
441
- var XormGenerator_exports = {};
442
- __export(XormGenerator_exports, {
443
- XormGenerator: () => XormGenerator
444
- });
445
- var XormGenerator;
446
- var init_XormGenerator = __esm({
447
- "src/generator/XormGenerator.ts"() {
448
- "use strict";
449
- init_generator();
450
- XormGenerator = class extends AGenerator {
451
- constructor(options = { language: "xorm" }) {
452
- super();
453
- this.options = options;
454
- }
455
- /**
456
- * 生成整个数据库模式的类型定义
457
- */
458
- generateDatabase(database) {
459
- let result = `// Database: ${database.name}
460
- `;
461
- result += `// Dialect: ${database.dialect}
462
-
463
- `;
464
- if (this.options.namespace) {
465
- result += `package ${this.options.namespace}
466
-
467
- `;
468
- }
469
- if (this.needsTimeImport(database)) {
470
- result += `import "time"
471
-
472
- `;
473
- }
474
- for (const table of database.tables) {
475
- result += this.generateTable(table);
476
- result += "\n";
477
- }
478
- return result;
479
- }
480
- /**
481
- * 生成单个表的类型定义
482
- */
483
- generateTable(table) {
484
- let result = `// ${table.name} \u8868\u7ED3\u6784
485
- `;
486
- result += `type ${this.formatTypeName(table.name)} struct {
487
- `;
488
- for (const column of table.columns) {
489
- result += this.generateColumn(column);
490
- }
491
- result += "}\n";
492
- return result;
493
- }
494
- /**
495
- * 生成列的类型定义
496
- */
497
- generateColumn(column) {
498
- const fieldName = this.formatFieldName(column.name);
499
- const typeName = this.mapSQLType(column.type);
500
- const tag = this.generateXormTag(column);
501
- let result = ` ${fieldName} ${typeName} ${tag}`;
502
- if (column.comment && this.options.generateComments !== false) {
503
- result += ` // ${column.comment}`;
504
- }
505
- result += "\n";
506
- return result;
507
- }
508
- /**
509
- * 映射 SQL 类型到 XORM 类型
510
- */
511
- mapSQLType(type) {
512
- switch (type.kind) {
513
- case "int":
514
- return "int";
515
- case "bigint":
516
- return "int64";
517
- case "float":
518
- return "float64";
519
- case "decimal":
520
- return "float64";
521
- case "varchar":
522
- case "text":
523
- return "string";
524
- case "boolean":
525
- return "bool";
526
- case "date":
527
- case "datetime":
528
- return "time.Time";
529
- case "json":
530
- return "interface{}";
531
- case "enum":
532
- return "string";
533
- default:
534
- return "string";
535
- }
536
- }
537
- /**
538
- * 生成 XORM 结构体标签
539
- */
540
- generateXormTag(column) {
541
- const tags = [];
542
- tags.push(`${column.name}`);
543
- let typeTag = "";
544
- switch (column.type.kind) {
545
- case "int":
546
- typeTag = "int";
547
- if (column.type.length) {
548
- typeTag += `(${column.type.length})`;
549
- }
550
- break;
551
- case "bigint":
552
- typeTag = "bigint";
553
- if (column.type.length) {
554
- typeTag += `(${column.type.length})`;
555
- }
556
- break;
557
- case "float":
558
- typeTag = "float";
559
- if (column.type.precision) {
560
- typeTag += `(${column.type.precision}`;
561
- if (column.type.scale) {
562
- typeTag += `,${column.type.scale}`;
563
- }
564
- typeTag += ")";
565
- }
566
- break;
567
- case "decimal":
568
- typeTag = "decimal";
569
- if (column.type.precision) {
570
- typeTag += `(${column.type.precision}`;
571
- if (column.type.scale) {
572
- typeTag += `,${column.type.scale}`;
573
- }
574
- typeTag += ")";
575
- }
576
- break;
577
- case "varchar":
578
- typeTag = "varchar";
579
- if (column.type.length) {
580
- typeTag += `(${column.type.length})`;
581
- }
582
- break;
583
- case "text":
584
- typeTag = "text";
585
- break;
586
- case "boolean":
587
- typeTag = "bool";
588
- break;
589
- case "date":
590
- typeTag = "date";
591
- break;
592
- case "datetime":
593
- typeTag = "datetime";
594
- break;
595
- case "json":
596
- typeTag = "json";
597
- break;
598
- case "enum":
599
- typeTag = "enum";
600
- break;
601
- default:
602
- typeTag = "string";
603
- }
604
- if (typeTag) {
605
- tags.push(typeTag);
606
- }
607
- if (column.primaryKey) {
608
- tags.push("pk");
609
- }
610
- if (column.unique) {
611
- tags.push("unique");
612
- }
613
- if (!column.nullable) {
614
- tags.push("notnull");
615
- }
616
- if (column.default) {
617
- tags.push(`default(${column.default})`);
618
- }
619
- if (column.generated) {
620
- tags.push("autoincr");
621
- }
622
- if (column.comment) {
623
- tags.push(`comment(${column.comment})`);
624
- }
625
- return `\`xorm:"${tags.join(" ")}" json:"${column.name}"\``;
626
- }
627
- };
628
- }
629
- });
630
-
631
- // src/generator/generator.ts
632
- var AGenerator, GeneratorFactory;
633
- var init_generator = __esm({
634
- "src/generator/generator.ts"() {
635
- "use strict";
636
- AGenerator = class {
637
- /**
638
- * 格式化类型名称(如驼峰命名、帕斯卡命名等)
639
- */
640
- formatTypeName(name) {
641
- return name.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
642
- }
643
- /**
644
- * 格式化字段名称
645
- */
646
- formatFieldName(name) {
647
- return name.split("_").map((part) => {
648
- return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase();
649
- }).join("");
650
- }
651
- /**
652
- * 生成默认值
653
- */
654
- generateDefaultValue(column) {
655
- if (!column.default) {
656
- return "";
657
- }
658
- return column.default;
659
- }
660
- /**
661
- * 检查是否需要导入 time 包
662
- */
663
- needsTimeImport(database) {
664
- for (const table of database.tables) {
665
- for (const column of table.columns) {
666
- if (column.type.kind === "date" || column.type.kind === "datetime") {
667
- return true;
668
- }
669
- }
670
- }
671
- return false;
672
- }
673
- };
674
- GeneratorFactory = class {
675
- /**
676
- * 创建语言生成器实例
677
- * @param language 目标语言
678
- * @param options 生成器配置选项
679
- * @returns 语言生成器实例
680
- */
681
- static async createGenerator(language, options = { language }) {
682
- switch (language) {
683
- case "typescript":
684
- const { TypeScriptGenerator: TypeScriptGenerator2 } = await Promise.resolve().then(() => (init_TypeScriptGenerator(), TypeScriptGenerator_exports));
685
- return new TypeScriptGenerator2(options);
686
- case "go":
687
- const { GolangGenerator: GolangGenerator2 } = await Promise.resolve().then(() => (init_GolangGenerator(), GolangGenerator_exports));
688
- return new GolangGenerator2(options);
689
- case "gorm":
690
- const { GormGenerator: GormGenerator2 } = await Promise.resolve().then(() => (init_GormGenerator(), GormGenerator_exports));
691
- return new GormGenerator2(options);
692
- case "xorm":
693
- const { XormGenerator: XormGenerator2 } = await Promise.resolve().then(() => (init_XormGenerator(), XormGenerator_exports));
694
- return new XormGenerator2(options);
695
- default:
696
- throw new Error(`Unsupported language: ${language}`);
697
- }
698
- }
699
- };
700
- }
701
- });
702
-
703
26
  // src/cli.ts
704
27
  var import_commander = require("commander");
705
28
 
706
29
  // src/sql-parser.ts
707
30
  var import_node_sql_parser = require("node-sql-parser");
708
- var parserCache = /* @__PURE__ */ new Map();
709
- function getParser(dialect) {
710
- if (!parserCache.has(dialect)) {
711
- parserCache.set(dialect, new import_node_sql_parser.Parser());
712
- }
713
- return parserCache.get(dialect);
714
- }
715
31
  function parseSQL(sql, options = { dialect: "mysql" }) {
716
32
  if (!sql || typeof sql !== "string") {
717
33
  throw new Error("SQL string is required and must be a string");
718
34
  }
719
35
  try {
720
- const parser = getParser(options.dialect);
721
- const processedSql = sql.trim().replace(/\s+/g, " ").replace(/;\s*;/g, ";");
36
+ const parser = new import_node_sql_parser.Parser();
37
+ const processedSql = sql.trim().replace(/\s+/g, " ").replace(/;[\s;]*;/g, ";");
722
38
  const ast = parser.astify(processedSql, { database: options.dialect });
723
39
  if (!ast) {
724
40
  throw new Error("Failed to parse SQL: AST is null or undefined");
@@ -827,7 +143,7 @@ var SQLParser = class {
827
143
  if (v.value) {
828
144
  value = v.value;
829
145
  } else if (v.raw) {
830
- value = v.raw;
146
+ value = String(v.raw);
831
147
  } else {
832
148
  value = String(v);
833
149
  }
@@ -882,19 +198,18 @@ var SQLParser = class {
882
198
  db.tables.push(this.parseTable(node));
883
199
  }
884
200
  }
885
- db.tablesMap = {};
886
- for (const table of db.tables) {
887
- if (table.name) {
888
- db.tablesMap[table.name] = table;
889
- }
890
- }
201
+ db.tablesMap = Object.fromEntries(
202
+ db.tables.filter((t) => t.name).map((t) => [t.name, t])
203
+ );
891
204
  return db;
892
205
  }
893
206
  /**
894
207
  * 类型守卫:判断是否为 CREATE TABLE 语句
895
208
  */
896
209
  isCreateTable(node) {
897
- return node.type === "create" && node.keyword === "table";
210
+ if (!node || typeof node !== "object") return false;
211
+ const n = node;
212
+ return n.type === "create" && n.keyword === "table";
898
213
  }
899
214
  /**
900
215
  * 解析单表 AST -> TableSchema
@@ -1065,37 +380,661 @@ var SQLParser = class {
1065
380
  * 使用 sqlify 确保函数/表达式被正确序列化
1066
381
  */
1067
382
  parseDefault(def) {
1068
- var _a, _b, _c, _d, _e, _f, _g;
1069
- if (((_a = def.value) == null ? void 0 : _a.type) === "null" || def.value === null) {
383
+ var _a, _b;
384
+ const val = def.value;
385
+ if ((val == null ? void 0 : val.type) === "null" || val === null) {
1070
386
  return null;
1071
387
  }
1072
388
  try {
1073
- return this.parser.sqlify(def.value);
1074
- } catch (error) {
1075
- if (((_b = def.value) == null ? void 0 : _b.type) === "function") {
1076
- if ((_e = (_d = (_c = def.value.name) == null ? void 0 : _c.name) == null ? void 0 : _d[0]) == null ? void 0 : _e.value) {
1077
- return def.value.name.name[0].value;
389
+ return this.parser.sqlify(val);
390
+ } catch (e) {
391
+ if ((val == null ? void 0 : val.type) === "function") {
392
+ const name = val.name;
393
+ const firstName = name == null ? void 0 : name.name;
394
+ if ((_a = firstName == null ? void 0 : firstName[0]) == null ? void 0 : _a.value) {
395
+ return String(firstName[0].value);
1078
396
  }
1079
397
  }
1080
- return String((_g = (_f = def.value) == null ? void 0 : _f.value) != null ? _g : def.value);
398
+ const inner = val;
399
+ return String((_b = inner == null ? void 0 : inner.value) != null ? _b : val);
1081
400
  }
1082
401
  }
1083
402
  };
1084
403
 
1085
- // src/cli/generate.ts
1086
- init_generator();
404
+ // src/generator/base.ts
405
+ var BaseGenerator = class {
406
+ constructor() {
407
+ this.needsTimeCache = null;
408
+ }
409
+ /**
410
+ * 格式化类型名称(如驼峰命名、帕斯卡命名等)
411
+ */
412
+ formatTypeName(name) {
413
+ return name.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
414
+ }
415
+ /**
416
+ * 格式化字段名称
417
+ */
418
+ formatPascalCase(name) {
419
+ return name.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
420
+ }
421
+ formatFieldName(name) {
422
+ return this.formatPascalCase(name);
423
+ }
424
+ /**
425
+ * 生成默认值
426
+ */
427
+ generateDefaultValue(column) {
428
+ if (!column.default) {
429
+ return "";
430
+ }
431
+ return column.default;
432
+ }
433
+ needsTimeImport(database) {
434
+ if (this.needsTimeCache !== null) return this.needsTimeCache;
435
+ for (const table of database.tables) {
436
+ for (const column of table.columns) {
437
+ if (column.type.kind === "date" || column.type.kind === "datetime") {
438
+ this.needsTimeCache = true;
439
+ return true;
440
+ }
441
+ }
442
+ }
443
+ this.needsTimeCache = false;
444
+ return false;
445
+ }
446
+ };
447
+ var GeneratorFactory = class {
448
+ static register(language, constructor) {
449
+ this.registry[language] = constructor;
450
+ }
451
+ static createGenerator(language, options = { language }) {
452
+ const Generator = this.registry[language];
453
+ if (!Generator) {
454
+ throw new Error(`Unsupported language: ${language}`);
455
+ }
456
+ return new Generator(options);
457
+ }
458
+ };
459
+ GeneratorFactory.registry = {};
460
+
461
+ // src/generator/typescript-generator.ts
462
+ var TypeScriptGenerator = class extends BaseGenerator {
463
+ constructor(options = { language: "typescript" }) {
464
+ super();
465
+ this.options = options;
466
+ }
467
+ /**
468
+ * 格式化字段名称(使用驼峰命名)
469
+ */
470
+ formatFieldName(name) {
471
+ return name.split("_").map((part, index) => {
472
+ if (index === 0) {
473
+ return part.toLowerCase();
474
+ }
475
+ return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase();
476
+ }).join("");
477
+ }
478
+ /**
479
+ * 生成整个数据库模式的类型定义
480
+ */
481
+ generateDatabase(database) {
482
+ let result = `// Database: ${database.name}
483
+ `;
484
+ result += `// Dialect: ${database.dialect}
485
+
486
+ `;
487
+ for (const table of database.tables) {
488
+ result += this.generateTable(table);
489
+ result += "\n";
490
+ }
491
+ return result;
492
+ }
493
+ /**
494
+ * 生成单个表的类型定义
495
+ */
496
+ generateTable(table) {
497
+ let result = `// ${table.name} \u8868\u7ED3\u6784
498
+ `;
499
+ result += `export interface ${this.formatTypeName(table.name)} {
500
+ `;
501
+ for (const column of table.columns) {
502
+ result += this.generateColumn(column);
503
+ }
504
+ result += "}\n";
505
+ return result;
506
+ }
507
+ /**
508
+ * 生成列的类型定义
509
+ */
510
+ generateColumn(column) {
511
+ const fieldName = this.formatFieldName(column.name);
512
+ const typeName = this.mapSQLType(column.type, column.name);
513
+ const optional = column.nullable ? "?" : "";
514
+ let result = ` ${fieldName}${optional}: ${typeName}`;
515
+ if (column.comment && this.options.generateComments !== false) {
516
+ result += `; // ${column.comment}`;
517
+ }
518
+ result += "\n";
519
+ return result;
520
+ }
521
+ /**
522
+ * 映射 SQL 类型到 TypeScript 类型
523
+ */
524
+ mapSQLType(type, columnName) {
525
+ switch (type.kind) {
526
+ case "int":
527
+ case "bigint":
528
+ return "number";
529
+ case "float":
530
+ case "decimal":
531
+ return "number";
532
+ case "varchar":
533
+ case "text":
534
+ return "string";
535
+ case "boolean":
536
+ return "boolean";
537
+ case "date":
538
+ case "datetime":
539
+ return "Date";
540
+ case "json":
541
+ return "unknown";
542
+ case "enum":
543
+ if (type.values && type.values.length > 0) {
544
+ const enumValues = type.values.map((v) => `'${v}'`).join(" | ");
545
+ if (columnName) {
546
+ const fieldName = this.formatFieldName(columnName);
547
+ return `${fieldName}: ${enumValues}`;
548
+ }
549
+ return enumValues;
550
+ }
551
+ return "string";
552
+ default:
553
+ return "string";
554
+ }
555
+ }
556
+ };
557
+
558
+ // src/generator/go-generator.ts
559
+ var GoGenerator = class extends BaseGenerator {
560
+ constructor(options = { language: "go" }) {
561
+ super();
562
+ this.options = options;
563
+ }
564
+ /**
565
+ * 生成整个数据库模式的类型定义
566
+ */
567
+ generateDatabase(database) {
568
+ let result = `// Database: ${database.name}
569
+ `;
570
+ result += `// Dialect: ${database.dialect}
571
+
572
+ `;
573
+ if (this.options.namespace) {
574
+ result += `package ${this.options.namespace}
575
+
576
+ `;
577
+ }
578
+ if (this.needsTimeImport(database)) {
579
+ result += `import "time"
580
+
581
+ `;
582
+ }
583
+ for (const table of database.tables) {
584
+ result += this.generateTable(table);
585
+ result += "\n";
586
+ }
587
+ return result;
588
+ }
589
+ /**
590
+ * 生成单个表的类型定义
591
+ */
592
+ generateTable(table) {
593
+ let result = `// ${table.name} \u8868\u7ED3\u6784
594
+ `;
595
+ result += `type ${this.formatTypeName(table.name)} struct {
596
+ `;
597
+ for (const column of table.columns) {
598
+ result += this.generateColumn(column);
599
+ }
600
+ result += "}\n";
601
+ return result;
602
+ }
603
+ /**
604
+ * 生成列的类型定义
605
+ */
606
+ generateColumn(column) {
607
+ const fieldName = this.formatFieldName(column.name);
608
+ const typeName = this.mapSQLType(column.type);
609
+ const tag = this.generateGoTag(column);
610
+ let result = ` ${fieldName} ${typeName} ${tag}`;
611
+ if (column.comment && this.options.generateComments !== false) {
612
+ result += ` // ${column.comment}`;
613
+ }
614
+ result += "\n";
615
+ return result;
616
+ }
617
+ /**
618
+ * 映射 SQL 类型到 Go 类型
619
+ */
620
+ mapSQLType(type) {
621
+ switch (type.kind) {
622
+ case "int":
623
+ return "int";
624
+ case "bigint":
625
+ return "int64";
626
+ case "float":
627
+ return "float64";
628
+ case "decimal":
629
+ return "float64";
630
+ case "varchar":
631
+ case "text":
632
+ return "string";
633
+ case "boolean":
634
+ return "bool";
635
+ case "date":
636
+ case "datetime":
637
+ return "time.Time";
638
+ case "json":
639
+ return "interface{}";
640
+ case "enum":
641
+ return "string";
642
+ default:
643
+ return "string";
644
+ }
645
+ }
646
+ /**
647
+ * 生成 Go 结构体标签
648
+ */
649
+ generateGoTag(column) {
650
+ const tags = [];
651
+ tags.push(`json:"${column.name}"`);
652
+ tags.push(`db:"${column.name}"`);
653
+ return `\`${tags.join(" ")}\``;
654
+ }
655
+ };
656
+
657
+ // src/generator/gorm-generator.ts
658
+ var GormGenerator = class extends BaseGenerator {
659
+ constructor(options = { language: "gorm" }) {
660
+ super();
661
+ this.options = options;
662
+ }
663
+ /**
664
+ * 生成整个数据库模式的类型定义
665
+ */
666
+ generateDatabase(database) {
667
+ let result = `// Database: ${database.name}
668
+ `;
669
+ result += `// Dialect: ${database.dialect}
670
+
671
+ `;
672
+ if (this.options.namespace) {
673
+ result += `package ${this.options.namespace}
674
+
675
+ `;
676
+ }
677
+ if (this.needsTimeImport(database)) {
678
+ result += `import "time"
679
+
680
+ `;
681
+ }
682
+ for (const table of database.tables) {
683
+ result += this.generateTable(table);
684
+ result += "\n";
685
+ }
686
+ return result;
687
+ }
688
+ /**
689
+ * 生成单个表的类型定义
690
+ */
691
+ generateTable(table) {
692
+ let result = `// ${table.name} \u8868\u7ED3\u6784
693
+ `;
694
+ result += `type ${this.formatTypeName(table.name)} struct {
695
+ `;
696
+ for (const column of table.columns) {
697
+ result += this.generateColumn(column);
698
+ }
699
+ result += "}\n";
700
+ return result;
701
+ }
702
+ /**
703
+ * 生成列的类型定义
704
+ */
705
+ generateColumn(column) {
706
+ const fieldName = this.formatFieldName(column.name);
707
+ const typeName = this.mapSQLType(column.type);
708
+ const tag = this.generateGormTag(column);
709
+ let result = ` ${fieldName} ${typeName} ${tag}`;
710
+ if (column.comment && this.options.generateComments !== false) {
711
+ result += ` // ${column.comment}`;
712
+ }
713
+ result += "\n";
714
+ return result;
715
+ }
716
+ /**
717
+ * 映射 SQL 类型到 GORM 类型
718
+ */
719
+ mapSQLType(type) {
720
+ switch (type.kind) {
721
+ case "int":
722
+ return "int";
723
+ case "bigint":
724
+ return "int64";
725
+ case "float":
726
+ return "float64";
727
+ case "decimal":
728
+ return "float64";
729
+ case "varchar":
730
+ case "text":
731
+ return "string";
732
+ case "boolean":
733
+ return "bool";
734
+ case "date":
735
+ case "datetime":
736
+ return "time.Time";
737
+ case "json":
738
+ return "interface{}";
739
+ case "enum":
740
+ return "string";
741
+ default:
742
+ return "string";
743
+ }
744
+ }
745
+ /**
746
+ * 生成 GORM 结构体标签
747
+ */
748
+ generateGormTag(column) {
749
+ const tags = [];
750
+ tags.push(`column:${column.name}`);
751
+ let typeTag = "type:";
752
+ switch (column.type.kind) {
753
+ case "int":
754
+ typeTag += "int";
755
+ if (column.type.length) {
756
+ typeTag += `(${column.type.length})`;
757
+ }
758
+ break;
759
+ case "bigint":
760
+ typeTag += "bigint";
761
+ if (column.type.length) {
762
+ typeTag += `(${column.type.length})`;
763
+ }
764
+ break;
765
+ case "float":
766
+ typeTag += "float";
767
+ if (column.type.precision) {
768
+ typeTag += `(${column.type.precision}`;
769
+ if (column.type.scale) {
770
+ typeTag += `,${column.type.scale}`;
771
+ }
772
+ typeTag += ")";
773
+ }
774
+ break;
775
+ case "decimal":
776
+ typeTag += "decimal";
777
+ if (column.type.precision) {
778
+ typeTag += `(${column.type.precision}`;
779
+ if (column.type.scale) {
780
+ typeTag += `,${column.type.scale}`;
781
+ }
782
+ typeTag += ")";
783
+ }
784
+ break;
785
+ case "varchar":
786
+ typeTag += "varchar";
787
+ if (column.type.length) {
788
+ typeTag += `(${column.type.length})`;
789
+ }
790
+ break;
791
+ case "text":
792
+ typeTag += "text";
793
+ break;
794
+ case "boolean":
795
+ typeTag += "bool";
796
+ break;
797
+ case "date":
798
+ typeTag += "date";
799
+ break;
800
+ case "datetime":
801
+ typeTag += "datetime";
802
+ break;
803
+ case "json":
804
+ typeTag += "json";
805
+ break;
806
+ case "enum":
807
+ typeTag += "enum";
808
+ break;
809
+ default:
810
+ typeTag += "string";
811
+ }
812
+ tags.push(typeTag);
813
+ if (column.primaryKey) {
814
+ tags.push("primaryKey");
815
+ }
816
+ if (column.unique) {
817
+ tags.push("unique");
818
+ }
819
+ if (!column.nullable) {
820
+ tags.push("not null");
821
+ }
822
+ if (column.default) {
823
+ tags.push(`default:${column.default}`);
824
+ }
825
+ if (column.generated) {
826
+ tags.push("autoIncrement");
827
+ }
828
+ if (column.comment) {
829
+ tags.push(`comment:${column.comment}`);
830
+ }
831
+ return `\`gorm:"${tags.join(";")}" json:"${column.name}"\``;
832
+ }
833
+ };
834
+
835
+ // src/generator/xorm-generator.ts
836
+ var XormGenerator = class extends BaseGenerator {
837
+ constructor(options = { language: "xorm" }) {
838
+ super();
839
+ this.options = options;
840
+ }
841
+ /**
842
+ * 生成整个数据库模式的类型定义
843
+ */
844
+ generateDatabase(database) {
845
+ let result = `// Database: ${database.name}
846
+ `;
847
+ result += `// Dialect: ${database.dialect}
848
+
849
+ `;
850
+ if (this.options.namespace) {
851
+ result += `package ${this.options.namespace}
852
+
853
+ `;
854
+ }
855
+ if (this.needsTimeImport(database)) {
856
+ result += `import "time"
857
+
858
+ `;
859
+ }
860
+ for (const table of database.tables) {
861
+ result += this.generateTable(table);
862
+ result += "\n";
863
+ }
864
+ return result;
865
+ }
866
+ /**
867
+ * 生成单个表的类型定义
868
+ */
869
+ generateTable(table) {
870
+ let result = `// ${table.name} \u8868\u7ED3\u6784
871
+ `;
872
+ result += `type ${this.formatTypeName(table.name)} struct {
873
+ `;
874
+ for (const column of table.columns) {
875
+ result += this.generateColumn(column);
876
+ }
877
+ result += "}\n";
878
+ return result;
879
+ }
880
+ /**
881
+ * 生成列的类型定义
882
+ */
883
+ generateColumn(column) {
884
+ const fieldName = this.formatFieldName(column.name);
885
+ const typeName = this.mapSQLType(column.type);
886
+ const tag = this.generateXormTag(column);
887
+ let result = ` ${fieldName} ${typeName} ${tag}`;
888
+ if (column.comment && this.options.generateComments !== false) {
889
+ result += ` // ${column.comment}`;
890
+ }
891
+ result += "\n";
892
+ return result;
893
+ }
894
+ /**
895
+ * 映射 SQL 类型到 XORM 类型
896
+ */
897
+ mapSQLType(type) {
898
+ switch (type.kind) {
899
+ case "int":
900
+ return "int";
901
+ case "bigint":
902
+ return "int64";
903
+ case "float":
904
+ return "float64";
905
+ case "decimal":
906
+ return "float64";
907
+ case "varchar":
908
+ case "text":
909
+ return "string";
910
+ case "boolean":
911
+ return "bool";
912
+ case "date":
913
+ case "datetime":
914
+ return "time.Time";
915
+ case "json":
916
+ return "interface{}";
917
+ case "enum":
918
+ return "string";
919
+ default:
920
+ return "string";
921
+ }
922
+ }
923
+ /**
924
+ * 生成 XORM 结构体标签
925
+ */
926
+ generateXormTag(column) {
927
+ const tags = [];
928
+ tags.push(`${column.name}`);
929
+ let typeTag = "";
930
+ switch (column.type.kind) {
931
+ case "int":
932
+ typeTag = "int";
933
+ if (column.type.length) {
934
+ typeTag += `(${column.type.length})`;
935
+ }
936
+ break;
937
+ case "bigint":
938
+ typeTag = "bigint";
939
+ if (column.type.length) {
940
+ typeTag += `(${column.type.length})`;
941
+ }
942
+ break;
943
+ case "float":
944
+ typeTag = "float";
945
+ if (column.type.precision) {
946
+ typeTag += `(${column.type.precision}`;
947
+ if (column.type.scale) {
948
+ typeTag += `,${column.type.scale}`;
949
+ }
950
+ typeTag += ")";
951
+ }
952
+ break;
953
+ case "decimal":
954
+ typeTag = "decimal";
955
+ if (column.type.precision) {
956
+ typeTag += `(${column.type.precision}`;
957
+ if (column.type.scale) {
958
+ typeTag += `,${column.type.scale}`;
959
+ }
960
+ typeTag += ")";
961
+ }
962
+ break;
963
+ case "varchar":
964
+ typeTag = "varchar";
965
+ if (column.type.length) {
966
+ typeTag += `(${column.type.length})`;
967
+ }
968
+ break;
969
+ case "text":
970
+ typeTag = "text";
971
+ break;
972
+ case "boolean":
973
+ typeTag = "bool";
974
+ break;
975
+ case "date":
976
+ typeTag = "date";
977
+ break;
978
+ case "datetime":
979
+ typeTag = "datetime";
980
+ break;
981
+ case "json":
982
+ typeTag = "json";
983
+ break;
984
+ case "enum":
985
+ typeTag = "enum";
986
+ break;
987
+ default:
988
+ typeTag = "string";
989
+ }
990
+ if (typeTag) {
991
+ tags.push(typeTag);
992
+ }
993
+ if (column.primaryKey) {
994
+ tags.push("pk");
995
+ }
996
+ if (column.unique) {
997
+ tags.push("unique");
998
+ }
999
+ if (!column.nullable) {
1000
+ tags.push("notnull");
1001
+ }
1002
+ if (column.default) {
1003
+ tags.push(`default(${column.default})`);
1004
+ }
1005
+ if (column.generated) {
1006
+ tags.push("autoincr");
1007
+ }
1008
+ if (column.comment) {
1009
+ tags.push(`comment(${column.comment})`);
1010
+ }
1011
+ return `\`xorm:"${tags.join(" ")}" json:"${column.name}"\``;
1012
+ }
1013
+ };
1014
+
1015
+ // src/generator/index.ts
1016
+ GeneratorFactory.register("typescript", TypeScriptGenerator);
1017
+ GeneratorFactory.register("go", GoGenerator);
1018
+ GeneratorFactory.register("gorm", GormGenerator);
1019
+ GeneratorFactory.register("xorm", XormGenerator);
1020
+
1021
+ // src/cli/output.ts
1087
1022
  var fs = __toESM(require("fs"));
1088
1023
  var path = __toESM(require("path"));
1024
+
1025
+ // src/constants.ts
1026
+ var LANGUAGES = ["go", "typescript", "gorm", "xorm"];
1027
+ var MODES = ["single", "multi"];
1089
1028
  var LANGUAGE_EXTENSIONS = {
1090
1029
  typescript: ".ts",
1091
1030
  go: ".go",
1092
1031
  gorm: ".go",
1093
1032
  xorm: ".go"
1094
1033
  };
1095
- var VALID_LANGUAGES = ["go", "typescript", "gorm", "xorm"];
1096
- var VALID_MODES = ["single", "multi"];
1097
- var DEFAULT_NAMESPACE = "models";
1098
1034
  var LANGUAGES_REQUIRING_NAMESPACE = ["go", "gorm", "xorm"];
1035
+ var DEFAULT_NAMESPACE = "models";
1036
+
1037
+ // src/cli/output.ts
1099
1038
  function addCommonGenerateOptions(command) {
1100
1039
  return command.option("-o, --output <dir>", "Output directory", "./output").option(
1101
1040
  "-l, --language <lang>",
@@ -1110,18 +1049,16 @@ function addCommonGenerateOptions(command) {
1110
1049
  "mysql"
1111
1050
  ).option("--db-name <name>", "Database name", "my_database");
1112
1051
  }
1113
- async function generateCode(sql, options) {
1114
- if (!VALID_LANGUAGES.includes(options.language)) {
1115
- console.error(
1116
- `Error: Invalid language: ${options.language}. Valid options: ${VALID_LANGUAGES.join(", ")}`
1052
+ async function generateCodeToFiles(sql, options) {
1053
+ if (!LANGUAGES.includes(options.language)) {
1054
+ throw new Error(
1055
+ `Invalid language: ${options.language}. Valid options: ${LANGUAGES.join(", ")}`
1117
1056
  );
1118
- process.exit(1);
1119
1057
  }
1120
- if (!VALID_MODES.includes(options.mode)) {
1121
- console.error(
1122
- `Error: Invalid mode: ${options.mode}. Valid options: ${VALID_MODES.join(", ")}`
1058
+ if (!MODES.includes(options.mode)) {
1059
+ throw new Error(
1060
+ `Invalid mode: ${options.mode}. Valid options: ${MODES.join(", ")}`
1123
1061
  );
1124
- process.exit(1);
1125
1062
  }
1126
1063
  const needsNamespace = LANGUAGES_REQUIRING_NAMESPACE.includes(
1127
1064
  options.language
@@ -1139,16 +1076,10 @@ async function generateCode(sql, options) {
1139
1076
  namespace: options.namespace,
1140
1077
  generateComments: true
1141
1078
  };
1142
- let generator;
1143
- try {
1144
- generator = await GeneratorFactory.createGenerator(
1145
- options.language,
1146
- generatorOptions
1147
- );
1148
- } catch (error) {
1149
- console.error(`Error: ${error.message}`);
1150
- process.exit(1);
1151
- }
1079
+ const generator = GeneratorFactory.createGenerator(
1080
+ options.language,
1081
+ generatorOptions
1082
+ );
1152
1083
  const outputDir = path.resolve(options.output);
1153
1084
  if (!fs.existsSync(outputDir)) {
1154
1085
  fs.mkdirSync(outputDir, { recursive: true });
@@ -1178,8 +1109,8 @@ async function generateCode(sql, options) {
1178
1109
  console.log("Done!");
1179
1110
  }
1180
1111
 
1181
- // src/cli/generate-sql.ts
1182
- async function commandGenerateSqlString(program2) {
1112
+ // src/cli/sql-command.ts
1113
+ function sqlCommand(program2) {
1183
1114
  const command = program2.command("sql").description("Generate code from SQL string").requiredOption("-s, --sql <sql>", "SQL string is required").option(
1184
1115
  "-m, --mode <mode>",
1185
1116
  "Output mode: single (one file) or multi (one file per table)",
@@ -1189,7 +1120,7 @@ async function commandGenerateSqlString(program2) {
1189
1120
  command.action(async (options) => {
1190
1121
  try {
1191
1122
  console.log("Using provided SQL string");
1192
- await generateCode(options.sql, {
1123
+ await generateCodeToFiles(options.sql, {
1193
1124
  output: options.output,
1194
1125
  language: options.language,
1195
1126
  mode: options.mode,
@@ -1204,9 +1135,9 @@ async function commandGenerateSqlString(program2) {
1204
1135
  });
1205
1136
  }
1206
1137
 
1207
- // src/cli/generate-db.ts
1138
+ // src/cli/db-command.ts
1208
1139
  var import_promise = __toESM(require("mysql2/promise"));
1209
- async function commandGenerateDb(program2) {
1140
+ function dbCommand(program2) {
1210
1141
  const command = program2.command("db").description("Generate code from database connection").option("-h, --host <host>", "MySQL database host", "127.0.0.1").option(
1211
1142
  "-P, --port <port>",
1212
1143
  "MySQL database port",
@@ -1215,6 +1146,7 @@ async function commandGenerateDb(program2) {
1215
1146
  ).option("-u, --user <user>", "MySQL database username", "root").option("-p, --password <password>", "MySQL database password", "").option("-d, --database <database>", "MySQL database name", "test");
1216
1147
  addCommonGenerateOptions(command);
1217
1148
  command.action(async (options) => {
1149
+ var _a;
1218
1150
  let connection = null;
1219
1151
  try {
1220
1152
  const connectionOptions = {
@@ -1230,9 +1162,8 @@ async function commandGenerateDb(program2) {
1230
1162
  connection = await import_promise.default.createConnection(connectionOptions);
1231
1163
  console.log("Connected to database successfully");
1232
1164
  const [tablesResult] = await connection.execute("SHOW TABLES");
1233
- const tables = tablesResult.map(
1234
- (row) => Object.values(row)[0]
1235
- );
1165
+ const rows = tablesResult;
1166
+ const tables = rows.map((row) => Object.values(row)[0]);
1236
1167
  console.log(`Found ${tables.length} tables: ${tables.join(", ")}`);
1237
1168
  const needsNamespace = LANGUAGES_REQUIRING_NAMESPACE.includes(
1238
1169
  options.language
@@ -1240,18 +1171,19 @@ async function commandGenerateDb(program2) {
1240
1171
  if (needsNamespace && !options.namespace) {
1241
1172
  options.namespace = DEFAULT_NAMESPACE;
1242
1173
  }
1243
- let successCount = 0;
1244
- let failCount = 0;
1245
- const failedTables = [];
1246
- for (const tableName of tables) {
1247
- console.log(`Processing table: ${tableName}`);
1248
- try {
1174
+ const results = await Promise.allSettled(
1175
+ tables.map(async (tableName) => {
1176
+ var _a2;
1177
+ console.log(`Processing table: ${tableName}`);
1249
1178
  const [result] = await connection.execute(
1250
1179
  `SHOW CREATE TABLE \`${tableName}\``
1251
1180
  );
1252
- const row = result[0];
1253
- const createTableSql = row["Create Table"];
1254
- await generateCode(createTableSql, {
1181
+ const resultRows = result;
1182
+ const createTableSql = (_a2 = resultRows[0]) == null ? void 0 : _a2["Create Table"];
1183
+ if (!createTableSql) {
1184
+ throw new Error(`Failed to get CREATE TABLE for '${tableName}'`);
1185
+ }
1186
+ await generateCodeToFiles(createTableSql, {
1255
1187
  output: options.output,
1256
1188
  language: options.language,
1257
1189
  mode: "multi",
@@ -1259,19 +1191,24 @@ async function commandGenerateDb(program2) {
1259
1191
  dialect: options.dialect,
1260
1192
  dbName: options.dbName || options.database
1261
1193
  });
1194
+ })
1195
+ );
1196
+ let successCount = 0;
1197
+ const failedTables = [];
1198
+ for (let i = 0; i < results.length; i++) {
1199
+ if (results[i].status === "fulfilled") {
1262
1200
  successCount++;
1263
- } catch (error) {
1201
+ } else {
1202
+ failedTables.push(tables[i]);
1264
1203
  console.error(
1265
- `Failed to process table '${tableName}':`,
1266
- error.message
1204
+ `Failed to process table '${tables[i]}':`,
1205
+ ((_a = results[i].reason) == null ? void 0 : _a.message) || "Unknown error"
1267
1206
  );
1268
- failCount++;
1269
- failedTables.push(tableName);
1270
1207
  }
1271
1208
  }
1272
1209
  console.log(
1273
1210
  `
1274
- Summary: ${successCount} tables succeeded, ${failCount} tables failed`
1211
+ Summary: ${successCount} tables succeeded, ${failedTables.length} tables failed`
1275
1212
  );
1276
1213
  if (failedTables.length > 0) {
1277
1214
  console.log(`Failed tables: ${failedTables.join(", ")}`);
@@ -1292,11 +1229,11 @@ Summary: ${successCount} tables succeeded, ${failCount} tables failed`
1292
1229
  }
1293
1230
 
1294
1231
  // package.json
1295
- var version = "0.0.1";
1232
+ var version = "0.0.2";
1296
1233
 
1297
1234
  // src/cli.ts
1298
1235
  var program = new import_commander.Command();
1299
1236
  program.name("sql-quicktype").description("Generate code from SQL schema definitions").version(version);
1300
- commandGenerateSqlString(program);
1301
- commandGenerateDb(program);
1237
+ sqlCommand(program);
1238
+ dbCommand(program);
1302
1239
  program.parse();