mysqlsyncr 1.0.0

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/src/parsers.js ADDED
@@ -0,0 +1,129 @@
1
+
2
+ export const parseCreateTableSQL = (createTableSQL) => {
3
+ if(!createTableSQL) return null;
4
+ const name = createTableSQL.match(/CREATE TABLE\s+`([^`]+)`/)[1];
5
+ const columns = [];
6
+ const indexes = [];
7
+ const constraints = [];
8
+ let engine = null;
9
+ let charset = null;
10
+ let collate = null;
11
+
12
+ // Split the SQL statement into lines and filter out empty lines
13
+ const lines = createTableSQL.split('\n').map(line => line.trim()).filter(line => line.length > 0);
14
+
15
+ // Regular expression to match column definitions
16
+ const columnRegex = /`([^`]+)`\s+([^\s,]+)(\s+NOT NULL|\s+NULL)?(\s+DEFAULT\s+([^,\s]+))?(\s+AUTO_INCREMENT)?/;
17
+
18
+ // Regular expression to match index definitions
19
+ const indexRegex = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(([^)]+)\)|PRIMARY KEY\s*\(([^)]+)\)/;
20
+
21
+ // Regular expression to match table options (ENGINE, CHARSET, COLLATE)
22
+ const optionsRegex = /ENGINE=(\w+)\s+DEFAULT\s+CHARSET=(\w+)(\s+COLLATE=(\w+))?/;
23
+
24
+
25
+ // Process each line
26
+ for (const line of lines) {
27
+ if (line.includes('CREATE TABLE')) {
28
+ continue;
29
+ }
30
+ // Check if the line contains a column definition
31
+ const columnMatch = columnRegex.exec(line);
32
+ const indexMatch = indexRegex.exec(line);
33
+ const constraintMatch = parseConstraint(line);
34
+ if (columnMatch && !indexMatch && !constraintMatch) {
35
+ const column = {
36
+ Field: columnMatch[1],
37
+ Type: columnMatch[2]
38
+ };
39
+
40
+ // Set Null property based on NOT NULL presence
41
+ if (columnMatch[3] && columnMatch[3].includes('NOT NULL')) {
42
+ column.NotNull = true; // Set to false if NOT NULL is present
43
+ }
44
+
45
+ // Set Default property if it exists
46
+ if (columnMatch[5]) {
47
+ column.Default = columnMatch[5];
48
+ }
49
+
50
+ // Set AutoIncrement property if it exists
51
+ if (columnMatch[6]) {
52
+ column.AutoIncrement = true; // Set to true if AUTO_INCREMENT is present
53
+ }
54
+
55
+ columns.push(column);
56
+ } else if (indexMatch && !constraintMatch) {
57
+
58
+ const isPrimary = line.includes("PRIMARY KEY"); // Check for PRIMARY in both patterns
59
+ const isUnique = line.includes("UNIQUE KEY"); // Only set Unique if it matches
60
+
61
+ const keyName = isPrimary ? null : indexMatch[2]; // Set KeyName to null if it's a primary key
62
+ const columnNames = indexMatch[3]
63
+ ? indexMatch[3].split(',').map(col => col.trim().replace(/`/g, '')) // Remove backticks
64
+ : indexMatch[4].split(',').map(col => col.trim().replace(/`/g, '')); // Remove backticks
65
+ const index = {
66
+ ColumnName: columnNames
67
+ };
68
+ if (isPrimary) index.Primary = true, index.Name = "PRIMARY"
69
+ if (isUnique) index.Unique = true;
70
+ if (keyName) index.Name = keyName;
71
+ indexes.push(index);
72
+
73
+ } else if (constraintMatch) {
74
+ constraints.push(constraintMatch);
75
+ }
76
+
77
+ }
78
+
79
+ // Handle table options
80
+ const optionsMatch = optionsRegex.exec(createTableSQL);
81
+ if (optionsMatch) {
82
+ engine = optionsMatch[1];
83
+ charset = optionsMatch[2];
84
+ collate = optionsMatch[4] || null; // Collate may not be present
85
+ }
86
+ return { name, columns, indexes, constraints, engine, charset, collate };
87
+ }
88
+
89
+ export const parseConstraint = (sql) => {
90
+ if(!sql) return null;
91
+ const regex = /FOREIGN KEY \((`[^`]+`)\) REFERENCES (`[^`]+`)\s?\((`[^`]+`)\)(?:\sON DELETE (NO|CASCADE|SET NULL|SET DEFAULT))?(?:\sON UPDATE (NO ACTION|CASCADE|SET NULL|SET DEFAULT))?/i;
92
+ const match = sql.match(regex);
93
+
94
+ if (!match) {
95
+ return null;
96
+ }
97
+ let onDelete = null;
98
+ let onUpdate = null;
99
+ if(sql.includes('ON DELETE CASCADE')){
100
+ onDelete = 'CASCADE';
101
+ }else if(sql.includes('ON DELETE SET NULL')){
102
+ onDelete = 'SET NULL';
103
+ }else if(sql.includes('ON DELETE SET DEFAULT')){
104
+ onDelete = 'SET DEFAULT';
105
+ }else{
106
+ onDelete = 'NO ACTION';
107
+ }
108
+
109
+ if(sql.includes('ON UPDATE CASCADE')){
110
+ onUpdate = 'CASCADE';
111
+ }else if(sql.includes('ON UPDATE SET NULL')){
112
+ onUpdate = 'SET NULL';
113
+ }else if(sql.includes('ON UPDATE SET DEFAULT')){
114
+ onUpdate = 'SET DEFAULT';
115
+ }else{
116
+ onUpdate = 'NO ACTION';
117
+ }
118
+ return {
119
+ Name: "AlertRule", // Replace with actual constraint name if needed
120
+ Type: "FOREIGN KEY",
121
+ ColumnName: [match[1]], // Column in the current table
122
+ References: {
123
+ Table: match[2], // Referenced table
124
+ Column: match[3], // Referenced column
125
+ },
126
+ OnDelete: onDelete,
127
+ OnUpdate: onUpdate,
128
+ };
129
+ };