generate-dac 1.0.6 → 1.0.8

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/DAC_Utils.js CHANGED
@@ -60,6 +60,14 @@ function get_SqlDbType(type) {
60
60
  codeType: 'int'
61
61
  };
62
62
  }
63
+ if (type === 'ntext') {
64
+ return {
65
+ type: 'NText',
66
+ codeGetMethodType: 'GetString',
67
+ sqlType: 'ntext',
68
+ codeType: 'string'
69
+ };
70
+ }
63
71
  var nvarcharReg = /NVARCHAR\(([^\(\)]+)\)/i;
64
72
  if (nvarcharReg.test(type)) {
65
73
  var match = nvarcharReg.exec(type);
@@ -475,7 +483,6 @@ function DAC_Generate_From_XLSX(filePath, sheetIndex) {
475
483
  var outputFolder = (0, file_utils_1.get_filename_without_extension)(filePath);
476
484
  console.log("DAC_Generate_From_XLSX: ".concat(outputFolder).yellow);
477
485
  var sheets = (0, excel_utils_1.json_read_all_sheets_from_excel)(filePath, { sheetIndex: sheetIndex, transformColumnName: true });
478
- (0, excel_utils_1.json_to_file)(filePath.replace('xlsx', 'json'), sheets);
479
486
  var schemas = [];
480
487
  var stores = [];
481
488
  var dropTables = [];
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var colors_1 = __importDefault(require("colors"));
7
+ var file_utils_1 = require("./utils/file_utils");
8
+ var table_utils_1 = require("./tables/table-utils");
9
+ var excel_utils_1 = require("./utils/excel-utils");
10
+ colors_1.default.enable();
11
+ var SOURCE_FILE_NAME = 'BCSDev_Schema.sql';
12
+ var SOURCE_FILE_PATH = "./input/".concat(SOURCE_FILE_NAME);
13
+ var sourceSchemaData = (0, file_utils_1.text_from_file)(SOURCE_FILE_PATH);
14
+ var sourceTables = (0, table_utils_1.schema_to_table_models)(sourceSchemaData);
15
+ console.log("sourceTables=".concat(sourceTables.length).green);
16
+ (0, excel_utils_1.json_to_file)("./output/source-schema.json", sourceTables);
17
+ var TARGET_FILE_NAME = 'LCRESQA_Schema_11_09_24.sql';
18
+ var TARGET_FILE_PATH = "./input/".concat(TARGET_FILE_NAME);
19
+ var targetSchemaData = (0, file_utils_1.text_from_file)(TARGET_FILE_PATH);
20
+ var targetTables = (0, table_utils_1.schema_to_table_models)(targetSchemaData);
21
+ console.log("targetTables=".concat(targetTables.length).green);
22
+ (0, excel_utils_1.json_to_file)("./output/target-schema.json", targetTables);
23
+ var changes = (0, table_utils_1.table_compares)(sourceTables, targetTables);
24
+ var newTable = changes.filter(function (e) { return e.action === 'new'; });
25
+ console.log("changes ".concat(changes.length, ", newTable=").concat(newTable.length).yellow);
26
+ (0, excel_utils_1.json_to_file)("./output/changes-schema.json", changes);
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,117 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.table_change = exports.table_compares = exports.schema_to_table_models = void 0;
4
+ var utils_1 = require("../utils/utils");
5
+ function schema_to_table_models(content) {
6
+ var tableStrs = split_to_table_schema(content);
7
+ console.log("Table split ".concat(tableStrs.length).green);
8
+ var tables = tableStrs.map(function (e) { return parse_table(e); }).filter(function (e) { return e; });
9
+ return tables;
10
+ }
11
+ exports.schema_to_table_models = schema_to_table_models;
12
+ function split_to_table_schema(content) {
13
+ var result = content.split(/\/\*\*\*\*\*\*\s*Object\s*:\s*Table\s*\[dbo\]\.\[[^\[\]]+\]/i).filter(function (e) { return /CREATE\s+TABLE/i.test(e); }).map(function (e) { return e.trim(); });
14
+ return result;
15
+ }
16
+ function parse_table(content) {
17
+ var result = {
18
+ tableName: parse_table_name(content),
19
+ columns: [],
20
+ content: content,
21
+ };
22
+ if (/^_/.test(result.tableName)) {
23
+ console.log("Ignore table ".concat(result.tableName).yellow);
24
+ return null;
25
+ }
26
+ var columnMatchs = split_to_column_content(content);
27
+ var columns = columnMatchs.map(function (e) { return parse_table_column(e); }).filter(function (e) { return e; });
28
+ result.columns = columns;
29
+ return result;
30
+ }
31
+ function parse_table_name(content) {
32
+ var reg = /CREATE\s*TABLE\s*\[dbo\]\.\[([^\[\]]+)\]/i;
33
+ if (reg.test(content)) {
34
+ var match = reg.exec(content);
35
+ return match[1];
36
+ }
37
+ return '';
38
+ }
39
+ function split_to_column_content(content) {
40
+ var result = (0, utils_1.match_all)(/\[([^\[\]]+)\][ ]+\[([^\[\]]+)\][ ]*(.*)$/gm, content);
41
+ return result;
42
+ }
43
+ function parse_table_column(match) {
44
+ var content = match[0];
45
+ var type = match[2].toLowerCase();
46
+ var result = {
47
+ columnName: match[1],
48
+ type: type,
49
+ content: content,
50
+ isNull: true,
51
+ };
52
+ if (type === 'nvarchar' || type === 'decimal') {
53
+ var reg = /\[(nvarchar|decimal)\](\([^\(\)]+\))/i;
54
+ if (reg.test(content)) {
55
+ var match_1 = reg.exec(content);
56
+ result.typeArgs = match_1[2];
57
+ }
58
+ }
59
+ if (/NOT[ ]+NULL/i.test(content)) {
60
+ result.isNull = false;
61
+ }
62
+ if (/IDENTITY/i.test(content)) {
63
+ result.isPrimary = true;
64
+ }
65
+ return result;
66
+ }
67
+ function table_compares(tables, targetTables) {
68
+ var changes = [];
69
+ tables.forEach(function (source) {
70
+ var target = targetTables.find(function (e) { return e.tableName === source.tableName; });
71
+ if (!target) {
72
+ changes.push({
73
+ action: 'new',
74
+ source: source,
75
+ });
76
+ }
77
+ else {
78
+ var change = table_change(source, target);
79
+ if (change) {
80
+ changes.push(change);
81
+ }
82
+ }
83
+ });
84
+ return changes;
85
+ }
86
+ exports.table_compares = table_compares;
87
+ function table_change(source, target) {
88
+ var result = {
89
+ source: source,
90
+ target: target,
91
+ action: 'modify',
92
+ };
93
+ var changes = [];
94
+ source.columns.forEach(function (sourceCol) {
95
+ var targetCol = target.columns.find(function (e) { return e.columnName === sourceCol.columnName; });
96
+ if (!targetCol) {
97
+ changes.push({
98
+ action: 'new',
99
+ source: sourceCol,
100
+ });
101
+ }
102
+ else {
103
+ if (sourceCol.type !== targetCol.type
104
+ || sourceCol.typeArgs !== targetCol.typeArgs
105
+ || sourceCol.isNull !== targetCol.isNull) {
106
+ changes.push({
107
+ action: 'modify',
108
+ source: sourceCol,
109
+ target: targetCol,
110
+ });
111
+ }
112
+ }
113
+ });
114
+ result.columnChanges = changes;
115
+ return changes.length ? result : null;
116
+ }
117
+ exports.table_change = table_change;
@@ -1,8 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.roundTo2Decimals = void 0;
3
+ exports.match_all = exports.roundTo2Decimals = void 0;
4
4
  function roundTo2Decimals(str) {
5
5
  var num = parseFloat(str);
6
6
  return Math.round(num * 100) / 100;
7
7
  }
8
8
  exports.roundTo2Decimals = roundTo2Decimals;
9
+ function match_all(reg, content) {
10
+ var result = [];
11
+ var match;
12
+ while ((match = reg.exec(content)) !== null) {
13
+ result.push(match);
14
+ }
15
+ return result;
16
+ }
17
+ exports.match_all = match_all;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "generate-dac",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,6 +19,7 @@
19
19
  "@types/encoding-japanese": "^2.0.1",
20
20
  "@types/fs-extra": "^11.0.1",
21
21
  "@types/uuid": "^8.3.4",
22
+ "canvas": "^2.11.2",
22
23
  "colors": "^1.4.0",
23
24
  "command-line-args": "^5.2.1",
24
25
  "dayjs": "^1.11.10",
package/src/DAC_Utils.ts CHANGED
@@ -27,7 +27,7 @@ export function binding_template(content: string, data: any){
27
27
  return result;
28
28
  }
29
29
 
30
- export type SqlDbTypeType = '' | 'Int' | 'TinyInt' | 'NVarChar' | 'DateTime' | 'Bit' | 'Money' | 'Decimal' | 'Date';
30
+ export type SqlDbTypeType = '' | 'Int' | 'TinyInt' | 'NVarChar' | 'DateTime' | 'Bit' | 'Money' | 'Decimal' | 'Date' | 'NText';
31
31
  export type CSharpType = 'int' | 'string' | 'DateTime' | 'bool' | 'decimal';
32
32
  export type CShareGetMethodType = '' | 'GetDateTime' | 'GetInt32' | 'GetBoolean' | 'GetString' | 'GetByte' | 'GetDecimal';
33
33
 
@@ -78,6 +78,14 @@ export function get_SqlDbType(type: string): SqlDbType{
78
78
  codeType: 'int'
79
79
  };
80
80
  }
81
+ if(type === 'ntext'){
82
+ return {
83
+ type: 'NText',
84
+ codeGetMethodType: 'GetString',
85
+ sqlType: 'ntext',
86
+ codeType: 'string'
87
+ };
88
+ }
81
89
  const nvarcharReg = /NVARCHAR\(([^\(\)]+)\)/i;
82
90
  if(nvarcharReg.test(type)){
83
91
  const match = nvarcharReg.exec(type);
@@ -622,8 +630,6 @@ export function DAC_Generate_From_XLSX(filePath: string, sheetIndex: number = 0)
622
630
 
623
631
  const sheets: SheetData[] = json_read_all_sheets_from_excel(filePath, { sheetIndex: sheetIndex, transformColumnName: true });
624
632
 
625
- json_to_file(filePath.replace('xlsx', 'json'), sheets);
626
-
627
633
  const schemas: string[] = [];
628
634
  const stores: string[] = [];
629
635
  const dropTables: string[] = [];
@@ -0,0 +1,33 @@
1
+ import colors from 'colors';
2
+ import fs from 'fs';
3
+
4
+ import { text_from_file } from "./utils/file_utils";
5
+ import { schema_to_table_models, table_compares } from './tables/table-utils';
6
+ import { json_to_file } from './utils/excel-utils';
7
+ import { createCanvas } from 'canvas';
8
+ import { SQLTable } from './tables/sql-interface';
9
+
10
+ colors.enable();
11
+
12
+ const SOURCE_FILE_NAME = 'BCSDev_Schema.sql';
13
+ const SOURCE_FILE_PATH = `./input/${SOURCE_FILE_NAME}`;
14
+
15
+ const sourceSchemaData = text_from_file(SOURCE_FILE_PATH);
16
+ const sourceTables = schema_to_table_models(sourceSchemaData);
17
+ console.log(`sourceTables=${sourceTables.length}`.green);
18
+ json_to_file(`./output/source-schema.json`, sourceTables);
19
+
20
+ const TARGET_FILE_NAME = 'LCRESQA_Schema_11_09_24.sql';
21
+ const TARGET_FILE_PATH = `./input/${TARGET_FILE_NAME}`;
22
+
23
+ const targetSchemaData = text_from_file(TARGET_FILE_PATH);
24
+ const targetTables = schema_to_table_models(targetSchemaData);
25
+
26
+ console.log(`targetTables=${targetTables.length}`.green);
27
+ json_to_file(`./output/target-schema.json`, targetTables);
28
+
29
+ const changes = table_compares(sourceTables, targetTables);
30
+ const newTable = changes.filter(e => e.action === 'new');
31
+ console.log(`changes ${changes.length}, newTable=${newTable.length}`.yellow);
32
+
33
+ json_to_file(`./output/changes-schema.json`, changes);
package/src/index.ts CHANGED
@@ -13,6 +13,4 @@ colors.enable();
13
13
 
14
14
  const INPUT_DIR = './input/';
15
15
 
16
- start_generate_dac_for_new_table(INPUT_DIR);
17
-
18
- // startProcess({inputFolder: INPUT_DIR});
16
+ start_generate_dac_for_new_table(INPUT_DIR);
@@ -0,0 +1,31 @@
1
+ export interface SQLColumn{
2
+ content: string,
3
+ columnName: string,
4
+ type: SQLType,
5
+ typeArgs?: string,
6
+ isNull?: boolean,
7
+ isPrimary?: boolean,
8
+ }
9
+
10
+ export interface SQLTable{
11
+ tableName: string,
12
+ content: string,
13
+ columns: SQLColumn[],
14
+ }
15
+
16
+ export type SQLType = 'decimal' | 'tinyint' | 'datetime' | 'int' | 'nvarchar' | 'ntext';
17
+
18
+ export type SQLTableChangeAction = 'new' | 'modify' | 'deleted';
19
+
20
+ export interface SQLTableChange{
21
+ source: SQLTable,
22
+ target?: SQLTable,
23
+ action: SQLTableChangeAction,
24
+ columnChanges?: SQLColumnChange[],
25
+ }
26
+
27
+ export interface SQLColumnChange{
28
+ action: SQLTableChangeAction,
29
+ source?: SQLColumn,
30
+ target?: SQLColumn,
31
+ }
@@ -0,0 +1,141 @@
1
+ import { match_all } from "../utils/utils";
2
+ import { SQLColumn, SQLColumnChange, SQLTable, SQLTableChange, SQLType } from "./sql-interface";
3
+
4
+ export function schema_to_table_models(content: string): SQLTable[]{
5
+ const tableStrs: string[] = split_to_table_schema(content);
6
+ console.log(`Table split ${tableStrs.length}`.green);
7
+ const tables = tableStrs.map(e => parse_table(e)).filter(e => e);
8
+
9
+ return tables;
10
+ }
11
+
12
+ function split_to_table_schema(content: string): string[]{
13
+ // /****** Object: Table [dbo].[Profile_Application_Step]
14
+ let result: string[] = content.split(/\/\*\*\*\*\*\*\s*Object\s*:\s*Table\s*\[dbo\]\.\[[^\[\]]+\]/i).filter(e => /CREATE\s+TABLE/i.test(e)).map(e => e.trim());
15
+
16
+ return result;
17
+ }
18
+
19
+ function parse_table(content: string): SQLTable | null{
20
+ let result: SQLTable = {
21
+ tableName: parse_table_name(content),
22
+ columns: [],
23
+ content: content,
24
+ };
25
+
26
+ // Ignore table begin with "_"
27
+ if(/^_/.test(result.tableName)){
28
+ console.log(`Ignore table ${result.tableName}`.yellow);
29
+ return null;
30
+ }
31
+
32
+ const columnMatchs: any[] = split_to_column_content(content);
33
+ const columns = columnMatchs.map(e => parse_table_column(e)).filter(e => e);
34
+ result.columns = columns;
35
+
36
+ return result;
37
+ }
38
+
39
+ function parse_table_name(content: string): string{
40
+ // CREATE TABLE [dbo].[_imp_Insp_Template_Stage]
41
+ const reg = /CREATE\s*TABLE\s*\[dbo\]\.\[([^\[\]]+)\]/i;
42
+ if(reg.test(content)){
43
+ const match = reg.exec(content);
44
+ return match[1];
45
+ }
46
+ return '';
47
+ }
48
+
49
+ function split_to_column_content(content: string): any[]{
50
+ // [Create_DateTime] [datetime] NULL,
51
+ const result = match_all(/\[([^\[\]]+)\][ ]+\[([^\[\]]+)\][ ]*(.*)$/gm, content);
52
+ return result;
53
+ }
54
+
55
+ function parse_table_column(match: string[]): SQLColumn | null{
56
+ const content = match[0];
57
+ const type = match[2].toLowerCase() as SQLType;
58
+ let result: SQLColumn = {
59
+ columnName: match[1],
60
+ type: type,
61
+ content: content,
62
+ isNull: true,
63
+ };
64
+
65
+ // [nvarchar](255)
66
+ // [decimal](18, 2)
67
+ if(type === 'nvarchar' || type === 'decimal'){
68
+ const reg = /\[(nvarchar|decimal)\](\([^\(\)]+\))/i;
69
+ if(reg.test(content)){
70
+ const match = reg.exec(content);
71
+ result.typeArgs = match[2];
72
+ }
73
+ }
74
+
75
+ // NULL
76
+ if(/NOT[ ]+NULL/i.test(content)){
77
+ result.isNull = false;
78
+ }
79
+
80
+ // IDENTITY
81
+ if(/IDENTITY/i.test(content)){
82
+ result.isPrimary = true;
83
+ }
84
+
85
+ return result;
86
+ }
87
+
88
+ export function table_compares(tables: SQLTable[], targetTables: SQLTable[]): SQLTableChange[]{
89
+ const changes: SQLTableChange[] = [];
90
+ tables.forEach(source => {
91
+ const target = targetTables.find(e => e.tableName === source.tableName);
92
+ if(!target){
93
+ changes.push({
94
+ action: 'new',
95
+ source: source,
96
+ })
97
+ }
98
+ else{
99
+ const change = table_change(source, target);
100
+ if(change){
101
+ changes.push(change);
102
+ }
103
+ }
104
+ });
105
+
106
+ return changes;
107
+ }
108
+
109
+ export function table_change(source: SQLTable, target: SQLTable): SQLTableChange | null{
110
+ const result: SQLTableChange = {
111
+ source: source,
112
+ target: target,
113
+ action: 'modify',
114
+ };
115
+ const changes: SQLColumnChange[] = [];
116
+ source.columns.forEach((sourceCol) => {
117
+ const targetCol = target.columns.find(e => e.columnName === sourceCol.columnName);
118
+ if(!targetCol){
119
+ changes.push({
120
+ action: 'new',
121
+ source: sourceCol,
122
+ })
123
+ }
124
+ else{
125
+ if(sourceCol.type !== targetCol.type
126
+ || sourceCol.typeArgs !== targetCol.typeArgs
127
+ || sourceCol.isNull !== targetCol.isNull
128
+ ){
129
+ changes.push({
130
+ action: 'modify',
131
+ source: sourceCol,
132
+ target: targetCol,
133
+ })
134
+ }
135
+ }
136
+ })
137
+
138
+ result.columnChanges = changes;
139
+
140
+ return changes.length ? result : null;
141
+ }
@@ -3,3 +3,13 @@ export function roundTo2Decimals(str: any) {
3
3
 
4
4
  return Math.round(num * 100) / 100;
5
5
  }
6
+
7
+ export function match_all(reg: RegExp, content: string): any[]{
8
+ let result: any[] = [];
9
+ let match: any;
10
+ while ((match = reg.exec(content)) !== null) {
11
+ result.push(match);
12
+ }
13
+
14
+ return result;
15
+ }