@chihqiang/sql-quicktype 0.0.1 → 0.0.3

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/index.js CHANGED
@@ -1,13 +1,8 @@
1
1
  "use strict";
2
- var __create = Object.create;
3
2
  var __defProp = Object.defineProperty;
4
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
5
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __esm = (fn, res) => function __init() {
9
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
10
- };
11
6
  var __export = (target, all) => {
12
7
  for (var name in all)
13
8
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -20,697 +15,16 @@ var __copyProps = (to, from, except, desc) => {
20
15
  }
21
16
  return to;
22
17
  };
23
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
24
- // If the importer is in node compatibility mode or this is not an ESM
25
- // file that has been converted to a CommonJS file using a Babel-
26
- // compatible transform (i.e. "__esModule" has not been set), then set
27
- // "default" to the CommonJS "module.exports" for node compatibility.
28
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
29
- mod
30
- ));
31
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
32
19
 
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
20
  // src/index.ts
704
21
  var index_exports = {};
705
22
  __export(index_exports, {
706
- AGenerator: () => AGenerator,
707
- FileReader: () => FileReader,
23
+ BaseGenerator: () => BaseGenerator,
708
24
  GeneratorFactory: () => GeneratorFactory,
709
- GolangGenerator: () => GolangGenerator,
25
+ GoGenerator: () => GoGenerator,
710
26
  GormGenerator: () => GormGenerator,
711
- ReaderFactory: () => ReaderFactory,
712
27
  SQLParser: () => SQLParser,
713
- StringReader: () => StringReader,
714
28
  TypeScriptGenerator: () => TypeScriptGenerator,
715
29
  XormGenerator: () => XormGenerator,
716
30
  generateCode: () => generateCode,
@@ -720,20 +34,13 @@ module.exports = __toCommonJS(index_exports);
720
34
 
721
35
  // src/sql-parser.ts
722
36
  var import_node_sql_parser = require("node-sql-parser");
723
- var parserCache = /* @__PURE__ */ new Map();
724
- function getParser(dialect) {
725
- if (!parserCache.has(dialect)) {
726
- parserCache.set(dialect, new import_node_sql_parser.Parser());
727
- }
728
- return parserCache.get(dialect);
729
- }
730
37
  function parseSQL(sql, options = { dialect: "mysql" }) {
731
38
  if (!sql || typeof sql !== "string") {
732
39
  throw new Error("SQL string is required and must be a string");
733
40
  }
734
41
  try {
735
- const parser = getParser(options.dialect);
736
- const processedSql = sql.trim().replace(/\s+/g, " ").replace(/;\s*;/g, ";");
42
+ const parser = new import_node_sql_parser.Parser();
43
+ const processedSql = sql.trim().replace(/\s+/g, " ").replace(/;[\s;]*;/g, ";");
737
44
  const ast = parser.astify(processedSql, { database: options.dialect });
738
45
  if (!ast) {
739
46
  throw new Error("Failed to parse SQL: AST is null or undefined");
@@ -760,8 +67,8 @@ SQL length: ${sql.length} characters`
760
67
  var SQLParser = class {
761
68
  constructor(options = { dialect: "mysql" }) {
762
69
  /**
763
- * 默认类型解析器
764
- * 提供基本的 SQL 类型到 SQLType 的映射逻辑
70
+ * Default type resolver
71
+ * Provides basic SQL type to SQLType mapping logic
765
72
  */
766
73
  this.defaultTypeResolver = {
767
74
  resolve: (def) => {
@@ -842,7 +149,7 @@ var SQLParser = class {
842
149
  if (v.value) {
843
150
  value = v.value;
844
151
  } else if (v.raw) {
845
- value = v.raw;
152
+ value = String(v.raw);
846
153
  } else {
847
154
  value = String(v);
848
155
  }
@@ -861,8 +168,8 @@ var SQLParser = class {
861
168
  };
862
169
  return emptyEnumType;
863
170
  /**
864
- * 未识别类型默认降级为 text,避免解析失败
865
- * 在严格模式下,遇到未识别的类型会抛出错误
171
+ * Unrecognized types fall back to text by default to avoid parse failure
172
+ * In strict mode, unrecognized types throw an error
866
173
  */
867
174
  default:
868
175
  if (this.options.strictMode) {
@@ -884,7 +191,7 @@ var SQLParser = class {
884
191
  this.parser = new import_node_sql_parser.Parser();
885
192
  }
886
193
  /**
887
- * 遍历 AST,提取所有 CREATE TABLE
194
+ * Traverse AST, extract all CREATE TABLE statements
888
195
  */
889
196
  parseDatabase(ast, dbName = this.options.dbName || "db") {
890
197
  const db = {
@@ -897,22 +204,21 @@ var SQLParser = class {
897
204
  db.tables.push(this.parseTable(node));
898
205
  }
899
206
  }
900
- db.tablesMap = {};
901
- for (const table of db.tables) {
902
- if (table.name) {
903
- db.tablesMap[table.name] = table;
904
- }
905
- }
207
+ db.tablesMap = Object.fromEntries(
208
+ db.tables.filter((t) => t.name).map((t) => [t.name, t])
209
+ );
906
210
  return db;
907
211
  }
908
212
  /**
909
- * 类型守卫:判断是否为 CREATE TABLE 语句
213
+ * Type guard: check if node is a CREATE TABLE statement
910
214
  */
911
215
  isCreateTable(node) {
912
- return node.type === "create" && node.keyword === "table";
216
+ if (!node || typeof node !== "object") return false;
217
+ const n = node;
218
+ return n.type === "create" && n.keyword === "table";
913
219
  }
914
220
  /**
915
- * 解析单表 AST -> TableSchema
221
+ * Parse single table AST -> TableSchema
916
222
  */
917
223
  parseTable(node) {
918
224
  var _a, _b, _c, _d;
@@ -956,9 +262,9 @@ var SQLParser = class {
956
262
  return table;
957
263
  }
958
264
  /**
959
- * 解析列定义 AST -> ColumnSchema
265
+ * Parse column definition AST -> ColumnSchema
960
266
  *
961
- * 注意:字段级 primary/unique 与表级定义会叠加
267
+ * Note: Field-level primary/unique and table-level definitions will overlap
962
268
  */
963
269
  parseColumn(def) {
964
270
  var _a, _b, _c;
@@ -972,18 +278,18 @@ var SQLParser = class {
972
278
  return {
973
279
  name: columnName,
974
280
  /**
975
- * 抽象 SQL 类型映射
281
+ * Abstract SQL type mapping
976
282
  */
977
283
  type: this.mapSQLType(def.definition),
978
284
  /**
979
- * node-sql-parser 中:
980
- * nullable 是一个对象,当它存在且 type "not null" 时,表示 NOT NULL
285
+ * In node-sql-parser:
286
+ * nullable is an object; when it exists and type is "not null", it means NOT NULL
981
287
  */
982
288
  nullable: !(def.nullable && def.nullable.type === "not null"),
983
289
  primaryKey: !!def.primary_key,
984
290
  unique: !!def.unique,
985
291
  /**
986
- * 默认值需要序列化为 SQL 字符串
292
+ * Default value needs to be serialized as SQL string
987
293
  */
988
294
  default: def.default_val ? this.parseDefault(def.default_val) : void 0,
989
295
  comment: (_c = (_b = def.comment) == null ? void 0 : _b.value) == null ? void 0 : _c.value,
@@ -992,7 +298,7 @@ var SQLParser = class {
992
298
  };
993
299
  }
994
300
  /**
995
- * 解析表级 PRIMARY KEY / UNIQUE / FOREIGN KEY
301
+ * Parse table-level PRIMARY KEY / UNIQUE / FOREIGN KEY
996
302
  */
997
303
  parseTableConstraint(def, table) {
998
304
  var _a, _b, _c, _d, _e, _f, _g, _h;
@@ -1044,7 +350,7 @@ var SQLParser = class {
1044
350
  }
1045
351
  }
1046
352
  /**
1047
- * 解析普通索引
353
+ * Parse regular index
1048
354
  */
1049
355
  parseIndex(def, table) {
1050
356
  var _a;
@@ -1058,7 +364,7 @@ var SQLParser = class {
1058
364
  }
1059
365
  }
1060
366
  /**
1061
- * SQL AST 类型 -> SQLType(跨方言抽象)
367
+ * SQL AST type -> SQLType (cross-dialect abstraction)
1062
368
  */
1063
369
  mapSQLType(def) {
1064
370
  const allResolvers = [
@@ -1075,140 +381,673 @@ var SQLParser = class {
1075
381
  return textType;
1076
382
  }
1077
383
  /**
1078
- * 默认值 AST -> SQL 字符串
384
+ * Default value AST -> SQL string
1079
385
  *
1080
- * 使用 sqlify 确保函数/表达式被正确序列化
386
+ * Uses sqlify to ensure functions/expressions are serialized correctly
1081
387
  */
1082
388
  parseDefault(def) {
1083
- var _a, _b, _c, _d, _e, _f, _g;
1084
- if (((_a = def.value) == null ? void 0 : _a.type) === "null" || def.value === null) {
389
+ var _a, _b;
390
+ const val = def.value;
391
+ if ((val == null ? void 0 : val.type) === "null" || val === null) {
1085
392
  return null;
1086
393
  }
1087
394
  try {
1088
- return this.parser.sqlify(def.value);
1089
- } catch (error) {
1090
- if (((_b = def.value) == null ? void 0 : _b.type) === "function") {
1091
- if ((_e = (_d = (_c = def.value.name) == null ? void 0 : _c.name) == null ? void 0 : _d[0]) == null ? void 0 : _e.value) {
1092
- return def.value.name.name[0].value;
395
+ return this.parser.sqlify(val);
396
+ } catch (e) {
397
+ if ((val == null ? void 0 : val.type) === "function") {
398
+ const name = val.name;
399
+ const firstName = name == null ? void 0 : name.name;
400
+ if ((_a = firstName == null ? void 0 : firstName[0]) == null ? void 0 : _a.value) {
401
+ return String(firstName[0].value);
1093
402
  }
1094
403
  }
1095
- return String((_g = (_f = def.value) == null ? void 0 : _f.value) != null ? _g : def.value);
404
+ const inner = val;
405
+ return String((_b = inner == null ? void 0 : inner.value) != null ? _b : val);
1096
406
  }
1097
407
  }
1098
408
  };
1099
409
 
1100
- // src/generator/index.ts
1101
- init_generator();
1102
- init_TypeScriptGenerator();
1103
- init_GolangGenerator();
1104
- init_GormGenerator();
1105
- init_XormGenerator();
410
+ // src/generator/base.ts
411
+ var BaseGenerator = class {
412
+ constructor() {
413
+ this.needsTimeCache = null;
414
+ }
415
+ /**
416
+ * Format type name (e.g., PascalCase)
417
+ */
418
+ formatTypeName(name) {
419
+ return name.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
420
+ }
421
+ /**
422
+ * Format field name
423
+ */
424
+ formatPascalCase(name) {
425
+ return name.split("_").map((part) => part.charAt(0).toUpperCase() + part.slice(1).toLowerCase()).join("");
426
+ }
427
+ formatFieldName(name) {
428
+ return this.formatPascalCase(name);
429
+ }
430
+ /**
431
+ * Generate default value
432
+ */
433
+ generateDefaultValue(column) {
434
+ if (!column.default) {
435
+ return "";
436
+ }
437
+ return column.default;
438
+ }
439
+ needsTimeImport(database) {
440
+ if (this.needsTimeCache !== null) return this.needsTimeCache;
441
+ for (const table of database.tables) {
442
+ for (const column of table.columns) {
443
+ if (column.type.kind === "date" || column.type.kind === "datetime") {
444
+ this.needsTimeCache = true;
445
+ return true;
446
+ }
447
+ }
448
+ }
449
+ this.needsTimeCache = false;
450
+ return false;
451
+ }
452
+ };
453
+ var GeneratorFactory = class {
454
+ static register(language, constructor) {
455
+ this.registry[language] = constructor;
456
+ }
457
+ static createGenerator(language, options = { language }) {
458
+ const Generator = this.registry[language];
459
+ if (!Generator) {
460
+ throw new Error(`Unsupported language: ${language}`);
461
+ }
462
+ return new Generator(options);
463
+ }
464
+ };
465
+ GeneratorFactory.registry = {};
1106
466
 
1107
- // src/utils.ts
1108
- init_generator();
1109
- async function generateCode(sql, options) {
1110
- const dbSchema = parseSQL(sql, {
1111
- dialect: options.dialect || "mysql",
1112
- dbName: options.dbName || "my_database"
1113
- });
1114
- const generatorOptions = {
1115
- language: options.language,
1116
- namespace: options.namespace,
1117
- generateComments: true
1118
- };
1119
- const generator = await GeneratorFactory.createGenerator(
1120
- options.language,
1121
- generatorOptions
1122
- );
1123
- return generator.generateDatabase(dbSchema);
1124
- }
467
+ // src/generator/typescript-generator.ts
468
+ var TypeScriptGenerator = class extends BaseGenerator {
469
+ constructor(options = { language: "typescript" }) {
470
+ super();
471
+ this.options = options;
472
+ }
473
+ /**
474
+ * Format field name (camelCase)
475
+ */
476
+ formatFieldName(name) {
477
+ return name.split("_").map((part, index) => {
478
+ if (index === 0) {
479
+ return part.toLowerCase();
480
+ }
481
+ return part.charAt(0).toUpperCase() + part.slice(1).toLowerCase();
482
+ }).join("");
483
+ }
484
+ /**
485
+ * Generate type definitions for the entire database schema
486
+ */
487
+ generateDatabase(database) {
488
+ let result = `// Database: ${database.name}
489
+ `;
490
+ result += `// Dialect: ${database.dialect}
1125
491
 
1126
- // src/reader/StringReader.ts
1127
- var StringReader = class {
492
+ `;
493
+ for (const table of database.tables) {
494
+ result += this.generateTable(table);
495
+ result += "\n";
496
+ }
497
+ return result;
498
+ }
1128
499
  /**
1129
- * 构造函数
1130
- * @param sql SQL 字符串
500
+ * Generate type definition for a single table
1131
501
  */
1132
- constructor(sql) {
1133
- this.sql = sql;
502
+ generateTable(table) {
503
+ let result = `// ${table.name} table structure
504
+ `;
505
+ result += `export interface ${this.formatTypeName(table.name)} {
506
+ `;
507
+ for (const column of table.columns) {
508
+ result += this.generateColumn(column);
509
+ }
510
+ result += "}\n";
511
+ return result;
1134
512
  }
1135
513
  /**
1136
- * 读取 SQL 内容
1137
- * @returns Promise<string> SQL 内容
514
+ * Generate column type definition
1138
515
  */
1139
- async read() {
1140
- return this.sql;
516
+ generateColumn(column) {
517
+ const fieldName = this.formatFieldName(column.name);
518
+ const typeName = this.mapSQLType(column.type, column.name);
519
+ const optional = column.nullable ? "?" : "";
520
+ let result = ` ${fieldName}${optional}: ${typeName}`;
521
+ if (column.comment && this.options.generateComments !== false) {
522
+ result += `; // ${column.comment}`;
523
+ }
524
+ result += "\n";
525
+ return result;
526
+ }
527
+ /**
528
+ * Map SQL type to TypeScript type
529
+ */
530
+ mapSQLType(type, columnName) {
531
+ switch (type.kind) {
532
+ case "int":
533
+ case "bigint":
534
+ return "number";
535
+ case "float":
536
+ case "decimal":
537
+ return "number";
538
+ case "varchar":
539
+ case "text":
540
+ return "string";
541
+ case "boolean":
542
+ return "boolean";
543
+ case "date":
544
+ case "datetime":
545
+ return "Date";
546
+ case "json":
547
+ return "unknown";
548
+ case "enum":
549
+ if (type.values && type.values.length > 0) {
550
+ const enumValues = type.values.map((v) => `'${v}'`).join(" | ");
551
+ if (columnName) {
552
+ const fieldName = this.formatFieldName(columnName);
553
+ return `${fieldName}: ${enumValues}`;
554
+ }
555
+ return enumValues;
556
+ }
557
+ return "string";
558
+ default:
559
+ return "string";
560
+ }
1141
561
  }
1142
562
  };
1143
563
 
1144
- // src/reader/FileReader.ts
1145
- var fs = __toESM(require("fs/promises"));
1146
- var FileReader = class {
564
+ // src/generator/go-generator.ts
565
+ var GoGenerator = class extends BaseGenerator {
566
+ constructor(options = { language: "go" }) {
567
+ super();
568
+ this.options = options;
569
+ }
1147
570
  /**
1148
- * 构造函数
1149
- * @param filePath SQL 文件路径
571
+ * Generate type definitions for the entire database schema
1150
572
  */
1151
- constructor(filePath) {
1152
- this.filePath = filePath;
573
+ generateDatabase(database) {
574
+ let result = `// Database: ${database.name}
575
+ `;
576
+ result += `// Dialect: ${database.dialect}
577
+
578
+ `;
579
+ if (this.options.namespace) {
580
+ result += `package ${this.options.namespace}
581
+
582
+ `;
583
+ }
584
+ if (this.needsTimeImport(database)) {
585
+ result += `import "time"
586
+
587
+ `;
588
+ }
589
+ for (const table of database.tables) {
590
+ result += this.generateTable(table);
591
+ result += "\n";
592
+ }
593
+ return result;
1153
594
  }
1154
595
  /**
1155
- * 读取 SQL 内容
1156
- * @returns Promise<string> SQL 内容
1157
- * @throws Error 文件不存在或读取失败
596
+ * Generate type definition for a single table
1158
597
  */
1159
- async read() {
1160
- try {
1161
- await fs.access(this.filePath);
1162
- const content = await fs.readFile(this.filePath, "utf-8");
1163
- console.log(`Reading SQL from file: ${this.filePath}`);
1164
- return content;
1165
- } catch (error) {
1166
- if (error instanceof Error) {
1167
- if (error.code === "ENOENT") {
1168
- throw new Error(`File not found: ${this.filePath}`);
598
+ generateTable(table) {
599
+ let result = `// ${table.name} table structure
600
+ `;
601
+ result += `type ${this.formatTypeName(table.name)} struct {
602
+ `;
603
+ for (const column of table.columns) {
604
+ result += this.generateColumn(column);
605
+ }
606
+ result += "}\n";
607
+ return result;
608
+ }
609
+ /**
610
+ * Generate column type definition
611
+ */
612
+ generateColumn(column) {
613
+ const fieldName = this.formatFieldName(column.name);
614
+ const typeName = this.mapSQLType(column.type);
615
+ const tag = this.generateGoTag(column);
616
+ let result = ` ${fieldName} ${typeName} ${tag}`;
617
+ if (column.comment && this.options.generateComments !== false) {
618
+ result += ` // ${column.comment}`;
619
+ }
620
+ result += "\n";
621
+ return result;
622
+ }
623
+ /**
624
+ * Map SQL type to Go type
625
+ */
626
+ mapSQLType(type) {
627
+ switch (type.kind) {
628
+ case "int":
629
+ return "int";
630
+ case "bigint":
631
+ return "int64";
632
+ case "float":
633
+ return "float64";
634
+ case "decimal":
635
+ return "float64";
636
+ case "varchar":
637
+ case "text":
638
+ return "string";
639
+ case "boolean":
640
+ return "bool";
641
+ case "date":
642
+ case "datetime":
643
+ return "time.Time";
644
+ case "json":
645
+ return "interface{}";
646
+ case "enum":
647
+ return "string";
648
+ default:
649
+ return "string";
650
+ }
651
+ }
652
+ /**
653
+ * Generate Go struct tags
654
+ */
655
+ generateGoTag(column) {
656
+ const tags = [];
657
+ tags.push(`json:"${column.name}"`);
658
+ tags.push(`db:"${column.name}"`);
659
+ return `\`${tags.join(" ")}\``;
660
+ }
661
+ };
662
+
663
+ // src/generator/gorm-generator.ts
664
+ var GormGenerator = class extends BaseGenerator {
665
+ constructor(options = { language: "gorm" }) {
666
+ super();
667
+ this.options = options;
668
+ }
669
+ /**
670
+ * Generate type definitions for the entire database schema
671
+ */
672
+ generateDatabase(database) {
673
+ let result = `// Database: ${database.name}
674
+ `;
675
+ result += `// Dialect: ${database.dialect}
676
+
677
+ `;
678
+ if (this.options.namespace) {
679
+ result += `package ${this.options.namespace}
680
+
681
+ `;
682
+ }
683
+ if (this.needsTimeImport(database)) {
684
+ result += `import "time"
685
+
686
+ `;
687
+ }
688
+ for (const table of database.tables) {
689
+ result += this.generateTable(table);
690
+ result += "\n";
691
+ }
692
+ return result;
693
+ }
694
+ /**
695
+ * Generate type definition for a single table
696
+ */
697
+ generateTable(table) {
698
+ let result = `// ${table.name} table structure
699
+ `;
700
+ result += `type ${this.formatTypeName(table.name)} struct {
701
+ `;
702
+ for (const column of table.columns) {
703
+ result += this.generateColumn(column);
704
+ }
705
+ result += "}\n";
706
+ return result;
707
+ }
708
+ /**
709
+ * Generate column type definition
710
+ */
711
+ generateColumn(column) {
712
+ const fieldName = this.formatFieldName(column.name);
713
+ const typeName = this.mapSQLType(column.type);
714
+ const tag = this.generateGormTag(column);
715
+ let result = ` ${fieldName} ${typeName} ${tag}`;
716
+ if (column.comment && this.options.generateComments !== false) {
717
+ result += ` // ${column.comment}`;
718
+ }
719
+ result += "\n";
720
+ return result;
721
+ }
722
+ /**
723
+ * Map SQL type to GORM type
724
+ */
725
+ mapSQLType(type) {
726
+ switch (type.kind) {
727
+ case "int":
728
+ return "int";
729
+ case "bigint":
730
+ return "int64";
731
+ case "float":
732
+ return "float64";
733
+ case "decimal":
734
+ return "float64";
735
+ case "varchar":
736
+ case "text":
737
+ return "string";
738
+ case "boolean":
739
+ return "bool";
740
+ case "date":
741
+ case "datetime":
742
+ return "time.Time";
743
+ case "json":
744
+ return "interface{}";
745
+ case "enum":
746
+ return "string";
747
+ default:
748
+ return "string";
749
+ }
750
+ }
751
+ /**
752
+ * Generate GORM struct tags
753
+ */
754
+ generateGormTag(column) {
755
+ const tags = [];
756
+ tags.push(`column:${column.name}`);
757
+ let typeTag = "type:";
758
+ switch (column.type.kind) {
759
+ case "int":
760
+ typeTag += "int";
761
+ if (column.type.length) {
762
+ typeTag += `(${column.type.length})`;
1169
763
  }
1170
- throw new Error(`Failed to read file: ${error.message}`);
1171
- }
1172
- throw new Error("Unknown error reading file");
764
+ break;
765
+ case "bigint":
766
+ typeTag += "bigint";
767
+ if (column.type.length) {
768
+ typeTag += `(${column.type.length})`;
769
+ }
770
+ break;
771
+ case "float":
772
+ typeTag += "float";
773
+ if (column.type.precision) {
774
+ typeTag += `(${column.type.precision}`;
775
+ if (column.type.scale) {
776
+ typeTag += `,${column.type.scale}`;
777
+ }
778
+ typeTag += ")";
779
+ }
780
+ break;
781
+ case "decimal":
782
+ typeTag += "decimal";
783
+ if (column.type.precision) {
784
+ typeTag += `(${column.type.precision}`;
785
+ if (column.type.scale) {
786
+ typeTag += `,${column.type.scale}`;
787
+ }
788
+ typeTag += ")";
789
+ }
790
+ break;
791
+ case "varchar":
792
+ typeTag += "varchar";
793
+ if (column.type.length) {
794
+ typeTag += `(${column.type.length})`;
795
+ }
796
+ break;
797
+ case "text":
798
+ typeTag += "text";
799
+ break;
800
+ case "boolean":
801
+ typeTag += "bool";
802
+ break;
803
+ case "date":
804
+ typeTag += "date";
805
+ break;
806
+ case "datetime":
807
+ typeTag += "datetime";
808
+ break;
809
+ case "json":
810
+ typeTag += "json";
811
+ break;
812
+ case "enum":
813
+ typeTag += "enum";
814
+ break;
815
+ default:
816
+ typeTag += "string";
817
+ }
818
+ tags.push(typeTag);
819
+ if (column.primaryKey) {
820
+ tags.push("primaryKey");
821
+ }
822
+ if (column.unique) {
823
+ tags.push("unique");
824
+ }
825
+ if (!column.nullable) {
826
+ tags.push("not null");
1173
827
  }
828
+ if (column.default) {
829
+ tags.push(`default:${column.default}`);
830
+ }
831
+ if (column.generated) {
832
+ tags.push("autoIncrement");
833
+ }
834
+ if (column.comment) {
835
+ tags.push(`comment:${column.comment}`);
836
+ }
837
+ return `\`gorm:"${tags.join(";")}" json:"${column.name}"\``;
1174
838
  }
1175
839
  };
1176
840
 
1177
- // src/reader/ReaderFactory.ts
1178
- var ReaderFactory = class {
841
+ // src/generator/xorm-generator.ts
842
+ var XormGenerator = class extends BaseGenerator {
843
+ constructor(options = { language: "xorm" }) {
844
+ super();
845
+ this.options = options;
846
+ }
1179
847
  /**
1180
- * 创建 Reader 实例
1181
- * @param options 读取器选项
1182
- * @returns Reader 实例
1183
- * @throws Error 不支持的读取器类型
848
+ * Generate type definitions for the entire database schema
1184
849
  */
1185
- static createReader(options) {
1186
- switch (options.type) {
1187
- case "string":
1188
- if (!options.source) {
1189
- throw new Error("Source is required for string reader");
850
+ generateDatabase(database) {
851
+ let result = `// Database: ${database.name}
852
+ `;
853
+ result += `// Dialect: ${database.dialect}
854
+
855
+ `;
856
+ if (this.options.namespace) {
857
+ result += `package ${this.options.namespace}
858
+
859
+ `;
860
+ }
861
+ if (this.needsTimeImport(database)) {
862
+ result += `import "time"
863
+
864
+ `;
865
+ }
866
+ for (const table of database.tables) {
867
+ result += this.generateTable(table);
868
+ result += "\n";
869
+ }
870
+ return result;
871
+ }
872
+ /**
873
+ * Generate type definition for a single table
874
+ */
875
+ generateTable(table) {
876
+ let result = `// ${table.name} table structure
877
+ `;
878
+ result += `type ${this.formatTypeName(table.name)} struct {
879
+ `;
880
+ for (const column of table.columns) {
881
+ result += this.generateColumn(column);
882
+ }
883
+ result += "}\n";
884
+ return result;
885
+ }
886
+ /**
887
+ * Generate column type definition
888
+ */
889
+ generateColumn(column) {
890
+ const fieldName = this.formatFieldName(column.name);
891
+ const typeName = this.mapSQLType(column.type);
892
+ const tag = this.generateXormTag(column);
893
+ let result = ` ${fieldName} ${typeName} ${tag}`;
894
+ if (column.comment && this.options.generateComments !== false) {
895
+ result += ` // ${column.comment}`;
896
+ }
897
+ result += "\n";
898
+ return result;
899
+ }
900
+ /**
901
+ * Map SQL type to XORM type
902
+ */
903
+ mapSQLType(type) {
904
+ switch (type.kind) {
905
+ case "int":
906
+ return "int";
907
+ case "bigint":
908
+ return "int64";
909
+ case "float":
910
+ return "float64";
911
+ case "decimal":
912
+ return "float64";
913
+ case "varchar":
914
+ case "text":
915
+ return "string";
916
+ case "boolean":
917
+ return "bool";
918
+ case "date":
919
+ case "datetime":
920
+ return "time.Time";
921
+ case "json":
922
+ return "interface{}";
923
+ case "enum":
924
+ return "string";
925
+ default:
926
+ return "string";
927
+ }
928
+ }
929
+ /**
930
+ * Generate XORM struct tags
931
+ */
932
+ generateXormTag(column) {
933
+ const tags = [];
934
+ tags.push(`${column.name}`);
935
+ let typeTag = "";
936
+ switch (column.type.kind) {
937
+ case "int":
938
+ typeTag = "int";
939
+ if (column.type.length) {
940
+ typeTag += `(${column.type.length})`;
941
+ }
942
+ break;
943
+ case "bigint":
944
+ typeTag = "bigint";
945
+ if (column.type.length) {
946
+ typeTag += `(${column.type.length})`;
947
+ }
948
+ break;
949
+ case "float":
950
+ typeTag = "float";
951
+ if (column.type.precision) {
952
+ typeTag += `(${column.type.precision}`;
953
+ if (column.type.scale) {
954
+ typeTag += `,${column.type.scale}`;
955
+ }
956
+ typeTag += ")";
1190
957
  }
1191
- return new StringReader(options.source);
1192
- case "file":
1193
- if (!options.source) {
1194
- throw new Error("Source is required for file reader");
958
+ break;
959
+ case "decimal":
960
+ typeTag = "decimal";
961
+ if (column.type.precision) {
962
+ typeTag += `(${column.type.precision}`;
963
+ if (column.type.scale) {
964
+ typeTag += `,${column.type.scale}`;
965
+ }
966
+ typeTag += ")";
967
+ }
968
+ break;
969
+ case "varchar":
970
+ typeTag = "varchar";
971
+ if (column.type.length) {
972
+ typeTag += `(${column.type.length})`;
1195
973
  }
1196
- return new FileReader(options.source);
974
+ break;
975
+ case "text":
976
+ typeTag = "text";
977
+ break;
978
+ case "boolean":
979
+ typeTag = "bool";
980
+ break;
981
+ case "date":
982
+ typeTag = "date";
983
+ break;
984
+ case "datetime":
985
+ typeTag = "datetime";
986
+ break;
987
+ case "json":
988
+ typeTag = "json";
989
+ break;
990
+ case "enum":
991
+ typeTag = "enum";
992
+ break;
1197
993
  default:
1198
- throw new Error(`Unsupported reader type: ${options.type}`);
994
+ typeTag = "string";
995
+ }
996
+ if (typeTag) {
997
+ tags.push(typeTag);
998
+ }
999
+ if (column.primaryKey) {
1000
+ tags.push("pk");
1199
1001
  }
1002
+ if (column.unique) {
1003
+ tags.push("unique");
1004
+ }
1005
+ if (!column.nullable) {
1006
+ tags.push("notnull");
1007
+ }
1008
+ if (column.default) {
1009
+ tags.push(`default(${column.default})`);
1010
+ }
1011
+ if (column.generated) {
1012
+ tags.push("autoincr");
1013
+ }
1014
+ if (column.comment) {
1015
+ tags.push(`comment(${column.comment})`);
1016
+ }
1017
+ return `\`xorm:"${tags.join(" ")}" json:"${column.name}"\``;
1200
1018
  }
1201
1019
  };
1020
+
1021
+ // src/generator/index.ts
1022
+ GeneratorFactory.register("typescript", TypeScriptGenerator);
1023
+ GeneratorFactory.register("go", GoGenerator);
1024
+ GeneratorFactory.register("gorm", GormGenerator);
1025
+ GeneratorFactory.register("xorm", XormGenerator);
1026
+
1027
+ // src/generate.ts
1028
+ function generateCode(sql, options) {
1029
+ const dbSchema = parseSQL(sql, {
1030
+ dialect: options.dialect || "mysql",
1031
+ dbName: options.dbName || "my_database"
1032
+ });
1033
+ const generatorOptions = {
1034
+ language: options.language,
1035
+ namespace: options.namespace,
1036
+ generateComments: true
1037
+ };
1038
+ const generator = GeneratorFactory.createGenerator(
1039
+ options.language,
1040
+ generatorOptions
1041
+ );
1042
+ return generator.generateDatabase(dbSchema);
1043
+ }
1202
1044
  // Annotate the CommonJS export names for ESM import in node:
1203
1045
  0 && (module.exports = {
1204
- AGenerator,
1205
- FileReader,
1046
+ BaseGenerator,
1206
1047
  GeneratorFactory,
1207
- GolangGenerator,
1048
+ GoGenerator,
1208
1049
  GormGenerator,
1209
- ReaderFactory,
1210
1050
  SQLParser,
1211
- StringReader,
1212
1051
  TypeScriptGenerator,
1213
1052
  XormGenerator,
1214
1053
  generateCode,