-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
352 lines (297 loc) · 11.9 KB
/
Copy pathscript.js
File metadata and controls
352 lines (297 loc) · 11.9 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Wait for DOM to load
document.addEventListener('DOMContentLoaded', function() {
// State variables
let dbName = '';
let tableName = '';
let columns = [];
let rows = [];
let currentStep = 1;
// Get DOM elements
const dbNameInput = document.getElementById('dbName');
const tableNameInput = document.getElementById('tableName');
const numColumnsInput = document.getElementById('numColumns');
const createColumnsBtn = document.getElementById('createColumnsBtn');
const columnNamesSection = document.getElementById('columnNamesSection');
const columnInputsContainer = document.getElementById('columnInputsContainer');
const confirmColumnsBtn = document.getElementById('confirmColumnsBtn');
const step3 = document.getElementById('step3');
const existingRowsSection = document.getElementById('existingRowsSection');
const existingRowsList = document.getElementById('existingRowsList');
const rowCount = document.getElementById('rowCount');
const currentRowInputs = document.getElementById('currentRowInputs');
const addRowBtn = document.getElementById('addRowBtn');
const generateSQLBtn = document.getElementById('generateSQLBtn');
const step4 = document.getElementById('step4');
const sqlOutput = document.getElementById('sqlOutput');
const copyBtn = document.getElementById('copyBtn');
const resetBtn = document.getElementById('resetBtn');
// Validation function for column names
function validateColumnName(name) {
const regex = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
return regex.test(name);
}
// Check if current row has any values
function hasCurrentRowValues() {
const inputs = currentRowInputs.querySelectorAll('input');
for (let input of inputs) {
if (input.value.trim() !== '') {
return true;
}
}
return false;
}
// Update Generate SQL button state
function updateGenerateSQLButton() {
// Enable if there are saved rows OR if current row has values
generateSQLBtn.disabled = !(rows.length > 0 || hasCurrentRowValues());
}
// Enable/disable Create button
function updateCreateButton() {
createColumnsBtn.disabled = !(dbNameInput.value && tableNameInput.value && numColumnsInput.value);
}
// Event listeners for Step 1
dbNameInput.addEventListener('input', function() {
dbName = this.value;
updateCreateButton();
});
tableNameInput.addEventListener('input', function() {
tableName = this.value;
updateCreateButton();
});
numColumnsInput.addEventListener('input', function() {
updateCreateButton();
});
// Create Columns button
createColumnsBtn.addEventListener('click', function() {
const numColumns = parseInt(numColumnsInput.value);
if (numColumns < 1) {
document.getElementById('numColumnsError').textContent = 'Must be at least 1';
return;
}
document.getElementById('numColumnsError').textContent = '';
// Disable inputs
dbNameInput.disabled = true;
tableNameInput.disabled = true;
numColumnsInput.disabled = true;
createColumnsBtn.disabled = true;
// Create column input fields
columnInputsContainer.innerHTML = '';
for (let i = 0; i < numColumns; i++) {
const div = document.createElement('div');
div.innerHTML = `
<input type="text"
class="column-input"
data-index="${i}"
placeholder="Column ${i + 1}">
<div class="error-message" id="colError${i}"></div>
`;
columnInputsContainer.appendChild(div);
}
columnNamesSection.style.display = 'block';
currentStep = 2;
});
// Confirm Columns button
confirmColumnsBtn.addEventListener('click', function() {
const columnInputs = document.querySelectorAll('.column-input');
const newColumns = [];
let hasErrors = false;
columnInputs.forEach((input, index) => {
const value = input.value.trim();
const errorDiv = document.getElementById(`colError${index}`);
if (!value) {
errorDiv.textContent = 'Required';
input.classList.add('input-error');
hasErrors = true;
} else if (!validateColumnName(value)) {
errorDiv.textContent = 'Invalid format';
input.classList.add('input-error');
hasErrors = true;
} else {
errorDiv.textContent = '';
input.classList.remove('input-error');
newColumns.push(value);
}
});
if (hasErrors) {
return;
}
// Save columns and reset rows
columns = newColumns;
rows = [];
// Hide column names section
columnNamesSection.style.display = 'none';
// Show Step 3
createCurrentRowInputs();
step3.style.display = 'block';
currentStep = 3;
// Update generate button state
updateGenerateSQLButton();
});
// Create input fields for current row
function createCurrentRowInputs() {
currentRowInputs.innerHTML = '';
columns.forEach(col => {
const div = document.createElement('div');
div.innerHTML = `
<label>${col}</label>
<input type="text"
class="current-row-input"
data-column="${col}"
placeholder="Value for ${col}">
`;
currentRowInputs.appendChild(div);
});
// Add event listeners to update Generate SQL button
const inputs = currentRowInputs.querySelectorAll('.current-row-input');
inputs.forEach(input => {
input.addEventListener('input', updateGenerateSQLButton);
});
}
// Add Row button
addRowBtn.addEventListener('click', function() {
const inputs = currentRowInputs.querySelectorAll('.current-row-input');
const newRow = {};
let hasValue = false;
inputs.forEach(input => {
const col = input.getAttribute('data-column');
const value = input.value.trim();
newRow[col] = value;
if (value) {
hasValue = true;
}
});
if (!hasValue) {
document.getElementById('rowError').textContent = 'At least one value required';
return;
}
document.getElementById('rowError').textContent = '';
// Add row to rows array
rows.push(newRow);
// Clear inputs
inputs.forEach(input => {
input.value = '';
});
// Update UI
updateRowsList();
updateGenerateSQLButton();
});
// Update rows list display
function updateRowsList() {
if (rows.length > 0) {
existingRowsSection.style.display = 'block';
rowCount.textContent = rows.length;
existingRowsList.innerHTML = '';
rows.forEach((row, index) => {
const rowDiv = document.createElement('div');
rowDiv.className = 'row-item';
const values = columns.map(col => row[col] || '(empty)').join(' | ');
rowDiv.innerHTML = `
<span class="row-content">${values}</span>
<button class="btn-remove" data-index="${index}">
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<line x1="18" y1="6" x2="6" y2="18"></line>
<line x1="6" y1="6" x2="18" y2="18"></line>
</svg>
</button>
`;
existingRowsList.appendChild(rowDiv);
});
// Add event listeners to remove buttons
const removeButtons = existingRowsList.querySelectorAll('.btn-remove');
removeButtons.forEach(btn => {
btn.addEventListener('click', function() {
const index = parseInt(this.getAttribute('data-index'));
rows.splice(index, 1);
updateRowsList();
updateGenerateSQLButton();
});
});
} else {
existingRowsSection.style.display = 'none';
}
}
// Generate SQL button
generateSQLBtn.addEventListener('click', function() {
// Get current row values if any
const inputs = currentRowInputs.querySelectorAll('.current-row-input');
const currentRow = {};
let hasCurrentValue = false;
inputs.forEach(input => {
const col = input.getAttribute('data-column');
const value = input.value.trim();
currentRow[col] = value;
if (value) {
hasCurrentValue = true;
}
});
// If current row has values and hasn't been added yet, add it
if (hasCurrentValue) {
rows.push(currentRow);
}
// Generate SQL
let sql = '';
// CREATE DATABASE
sql += `CREATE DATABASE IF NOT EXISTS ${dbName};\n\n`;
// USE DATABASE
sql += `USE ${dbName};\n\n`;
// CREATE TABLE
sql += `CREATE TABLE ${tableName} (\n`;
columns.forEach((col, index) => {
sql += ` ${col} VARCHAR(255)`;
if (index < columns.length - 1) {
sql += ',';
}
sql += '\n';
});
sql += ');\n\n';
// INSERT ROWS
rows.forEach(row => {
sql += `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (`;
const values = columns.map(col => `'${row[col] || ''}'`).join(', ');
sql += `${values});\n`;
});
// Display SQL
sqlOutput.textContent = sql;
step3.style.display = 'none';
step4.style.display = 'block';
currentStep = 4;
});
// Copy button
copyBtn.addEventListener('click', function() {
const sql = sqlOutput.textContent;
navigator.clipboard.writeText(sql).then(() => {
const btnText = document.getElementById('copyBtnText');
const originalText = btnText.textContent;
btnText.textContent = 'Copied!';
setTimeout(() => {
btnText.textContent = originalText;
}, 2000);
});
});
// Reset button
resetBtn.addEventListener('click', function() {
// Reset all state
dbName = '';
tableName = '';
columns = [];
rows = [];
currentStep = 1;
// Reset inputs
dbNameInput.value = '';
tableNameInput.value = '';
numColumnsInput.value = '';
dbNameInput.disabled = false;
tableNameInput.disabled = false;
numColumnsInput.disabled = false;
// Hide sections
columnNamesSection.style.display = 'none';
step3.style.display = 'none';
step4.style.display = 'none';
existingRowsSection.style.display = 'none';
// Clear errors
document.getElementById('numColumnsError').textContent = '';
document.getElementById('rowError').textContent = '';
// Update buttons
updateCreateButton();
});
});