-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathrender-uttils.ts
More file actions
154 lines (128 loc) · 4.33 KB
/
render-uttils.ts
File metadata and controls
154 lines (128 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Fixes charset/collation placement in SQL CREATE TABLE statements
* @param sql The SQL string to process
* @returns SQL with properly placed charset/collation clauses
*/
export function fixCharsetPlacement(sql: string): string {
// Split into lines to handle multi-line cases better
const lines = sql.split('\n');
let currentColumn = '';
const result: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
// Handle table start/end and other non-column lines
if (isTableStructureLine(trimmed)) {
if (currentColumn) {
result.push(processColumn(currentColumn));
currentColumn = '';
}
result.push(line);
continue;
}
// Handle column definitions
if (trimmed.endsWith(',')) {
currentColumn += ' ' + trimmed.slice(0, -1);
result.push(processColumn(currentColumn) + ',');
currentColumn = '';
} else {
currentColumn += ' ' + trimmed;
}
}
// Process any remaining column
if (currentColumn) {
result.push(processColumn(currentColumn));
}
return result.join('\n');
}
/**
* Checks if a line is part of table structure (not a column definition)
*/
function isTableStructureLine(line: string): boolean {
return line.startsWith('CREATE TABLE') ||
line === '(' ||
line === ')' ||
line.endsWith('(') ||
line.endsWith(')');
}
/**
* Processes individual column definitions to fix charset/collation placement
*/
function processColumn(columnDef: string): string {
// Extract column name and data type
const typeMatch = columnDef.match(/^\s*(\w+)\s+(\w+(?:\([^)]*\))?)/i);
if (!typeMatch) return columnDef;
const [_, colName, dataType] = typeMatch;
const rest = columnDef.slice(typeMatch[0].length);
// Extract charset and collate
const charsetMatch = rest.match(/CHARACTER\s+SET\s+\S+/i);
const collateMatch = rest.match(/COLLATE\s+\S+/i);
// Clean the remaining attributes
const cleanRest = rest
.replace(/CHARACTER\s+SET\s+\S+/gi, '')
.replace(/COLLATE\s+\S+/gi, '')
.replace(/\s+/g, ' ')
.trim();
// Reconstruct in correct order
let reconstructed = `${colName} ${dataType}`;
if (charsetMatch) reconstructed += ` ${charsetMatch[0]}`;
if (collateMatch) reconstructed += ` ${collateMatch[0]}`;
if (cleanRest) reconstructed += ` ${cleanRest}`;
return reconstructed;
}
export function fixSQLiteColumnOrder(sql: string): string {
const lines = sql.split('\n');
let currentColumn = '';
const result: string[] = [];
for (const line of lines) {
const trimmed = line.trim();
if (isTableStructureLine(trimmed)) {
if (currentColumn) {
result.push(processSQLiteIntegerColumn(currentColumn));
currentColumn = '';
}
result.push(line);
continue;
}
if (trimmed.endsWith(',')) {
currentColumn += ' ' + trimmed.slice(0, -1);
result.push(processSQLiteIntegerColumn(currentColumn) + ',');
currentColumn = '';
} else {
currentColumn += ' ' + trimmed;
}
}
if (currentColumn) result.push(processSQLiteIntegerColumn(currentColumn));
return result.join('\n');
}
function processSQLiteIntegerColumn(columnDef: string): string {
// Only process INTEGER columns with PRIMARY KEY and/or AUTOINCREMENT
const integerPkMatch = columnDef.match(/^\s*(\w+)\s+INTEGER\s+(.*)/i);
if (!integerPkMatch) return columnDef;
const [_, colName, rest] = integerPkMatch;
// Check if this is a PRIMARY KEY column
const isPrimaryKey = rest.match(/\bPRIMARY\s+KEY\b/i);
const isAutoIncrement = rest.match(/\bAUTOINCREMENT\b/i);
const isNotNull = rest.match(/\bNOT\s+NULL\b/i);
if (!isPrimaryKey && !isAutoIncrement) {
return columnDef; // Leave non-PK INTEGER columns unchanged
}
// Clean the remaining attributes
let cleanRest = rest
.replace(/\bPRIMARY\s+KEY\b/gi, '')
.replace(/\bAUTOINCREMENT\b/gi, '')
.replace(/\bNOT\s+NULL\b/gi, '')
.replace(/\s+/g, ' ')
.trim();
// Reconstruct with SQLite's required order
let reconstructed = `${colName} INTEGER`;
if (isPrimaryKey) reconstructed += ' PRIMARY KEY';
if (isAutoIncrement) reconstructed += ' AUTOINCREMENT';
if (isNotNull) reconstructed += ' NOT NULL';
if (cleanRest) reconstructed += ` ${cleanRest}`;
return reconstructed.trim();
}
export interface CircularDependencyError {
cycle: string[];
success: boolean;
message: string;
}