-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathexport_import_demo.html
More file actions
652 lines (577 loc) · 25.9 KB
/
export_import_demo.html
File metadata and controls
652 lines (577 loc) · 25.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AbsurderSQL - Export/Import Demo</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.css" rel="stylesheet">
<style>
body {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.demo-container {
max-width: 1200px;
margin: 0 auto;
}
.card {
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.3);
margin-bottom: 20px;
}
.card-header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
border-radius: 15px 15px 0 0 !important;
padding: 20px;
}
.btn-primary {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
border: none;
}
.btn-primary:hover {
background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}
.status-badge {
display: inline-block;
padding: 5px 15px;
border-radius: 20px;
font-size: 0.9em;
font-weight: 600;
}
.status-success {
background-color: #10b981;
color: white;
}
.status-error {
background-color: #ef4444;
color: white;
}
.status-info {
background-color: #3b82f6;
color: white;
}
.log-output {
background-color: #1e293b;
color: #10b981;
border-radius: 10px;
padding: 15px;
font-family: 'Courier New', monospace;
font-size: 14px;
max-height: 300px;
overflow-y: auto;
margin-top: 15px;
}
.log-line {
margin: 5px 0;
padding: 5px;
border-left: 3px solid #10b981;
padding-left: 10px;
}
.log-error {
color: #ef4444;
border-left-color: #ef4444;
}
.log-info {
color: #3b82f6;
border-left-color: #3b82f6;
}
.log-success {
color: #10b981;
border-left-color: #10b981;
}
.progress {
height: 30px;
border-radius: 15px;
background-color: #e5e7eb;
}
.progress-bar {
background: linear-gradient(135deg, #10b981 0%, #059669 100%);
border-radius: 15px;
font-weight: 600;
}
.file-info {
background-color: #f3f4f6;
border-radius: 10px;
padding: 15px;
margin-top: 15px;
}
.file-info-item {
display: flex;
justify-content: space-between;
padding: 8px 0;
border-bottom: 1px solid #e5e7eb;
}
.file-info-item:last-child {
border-bottom: none;
}
.action-button {
min-width: 150px;
padding: 12px 24px;
border-radius: 10px;
font-weight: 600;
transition: all 0.3s ease;
}
.feature-badge {
background-color: #8b5cf6;
color: white;
padding: 3px 10px;
border-radius: 12px;
font-size: 0.85em;
margin-left: 10px;
}
.data-table {
max-height: 400px;
overflow-y: auto;
}
</style>
</head>
<body>
<div class="demo-container">
<!-- Header -->
<div class="card mb-4">
<div class="card-header">
<h1 class="mb-0">
<i data-feather="database"></i>
AbsurderSQL Export/Import Demo
<span class="feature-badge">Production Ready</span>
</h1>
<p class="mb-0 mt-2">Full-featured SQLite database export and import with IndexedDB persistence</p>
</div>
</div>
<!-- Step 1: Create & Populate Database -->
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i data-feather="edit"></i>
Step 1: Create & Populate Database
</h3>
</div>
<div class="card-body">
<p class="text-muted">Create a sample database with tables, data, indexes, and triggers.</p>
<button id="createDbBtn" class="btn btn-primary action-button">
<i data-feather="plus-circle"></i>
Create Sample Database
</button>
<div id="createStatus" class="mt-3"></div>
<div id="dataPreview" class="data-table mt-3" style="display: none;">
<h5>Sample Data:</h5>
<table class="table table-striped table-sm">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
<tbody id="dataTableBody"></tbody>
</table>
</div>
</div>
</div>
<!-- Step 2: Export Database -->
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i data-feather="download"></i>
Step 2: Export Database
</h3>
</div>
<div class="card-body">
<p class="text-muted">Export the database to a file. The file will be downloaded to your Downloads folder.</p>
<button id="exportBtn" class="btn btn-primary action-button" disabled>
<i data-feather="download"></i>
Export Database
</button>
<div id="exportProgress" class="mt-3" style="display: none;">
<div class="progress">
<div id="exportProgressBar" class="progress-bar" role="progressbar" style="width: 0%">0%</div>
</div>
</div>
<div id="exportStatus" class="mt-3"></div>
<div id="exportFileInfo" class="file-info" style="display: none;">
<h5 class="mb-3">Export Information:</h5>
<div class="file-info-item">
<span><strong>Database Name:</strong></span>
<span id="exportDbName"></span>
</div>
<div class="file-info-item">
<span><strong>File Size:</strong></span>
<span id="exportFileSize"></span>
</div>
<div class="file-info-item">
<span><strong>Export Time:</strong></span>
<span id="exportTime"></span>
</div>
<div class="file-info-item">
<span><strong>Status:</strong></span>
<span id="exportStatusBadge"></span>
</div>
</div>
</div>
</div>
<!-- Step 3: Import Database -->
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i data-feather="upload"></i>
Step 3: Import Database
</h3>
</div>
<div class="card-body">
<p class="text-muted">Import a database from a file. This will replace the current database.</p>
<div class="mb-3">
<label for="importFile" class="form-label">Select Database File:</label>
<input type="file" class="form-control" id="importFile" accept=".db,.sqlite,.sqlite3">
</div>
<button id="importBtn" class="btn btn-primary action-button" disabled>
<i data-feather="upload"></i>
Import Database
</button>
<div id="importProgress" class="mt-3" style="display: none;">
<div class="progress">
<div id="importProgressBar" class="progress-bar" role="progressbar" style="width: 0%">0%</div>
</div>
</div>
<div id="importStatus" class="mt-3"></div>
<div id="importFileInfo" class="file-info" style="display: none;">
<h5 class="mb-3">Import Information:</h5>
<div class="file-info-item">
<span><strong>File Name:</strong></span>
<span id="importFileName"></span>
</div>
<div class="file-info-item">
<span><strong>File Size:</strong></span>
<span id="importFileSize"></span>
</div>
<div class="file-info-item">
<span><strong>Import Time:</strong></span>
<span id="importTime"></span>
</div>
<div class="file-info-item">
<span><strong>Status:</strong></span>
<span id="importStatusBadge"></span>
</div>
</div>
</div>
</div>
<!-- Step 4: Verify Imported Data -->
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i data-feather="check-circle"></i>
Step 4: Verify Imported Data
</h3>
</div>
<div class="card-body">
<p class="text-muted">Query the imported database to verify data integrity.</p>
<button id="verifyBtn" class="btn btn-primary action-button" disabled>
<i data-feather="search"></i>
Verify Data
</button>
<div id="verifyStatus" class="mt-3"></div>
<div id="verifyDataPreview" class="data-table mt-3" style="display: none;">
<h5>Verified Data:</h5>
<table class="table table-striped table-sm">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Department</th>
<th>Salary</th>
</tr>
</thead>
<tbody id="verifyTableBody"></tbody>
</table>
</div>
</div>
</div>
<!-- Activity Log -->
<div class="card">
<div class="card-header">
<h3 class="mb-0">
<i data-feather="activity"></i>
Activity Log
</h3>
</div>
<div class="card-body">
<div id="logOutput" class="log-output"></div>
<button id="clearLogBtn" class="btn btn-sm btn-outline-secondary mt-2">
<i data-feather="trash-2"></i>
Clear Log
</button>
</div>
</div>
</div>
<!-- Scripts -->
<script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
<script type="module">
import init, { Database } from '../pkg/absurder_sql.js';
// Initialize Feather icons
feather.replace();
// Global state
let db = null;
let exportedData = null;
const DB_NAME = 'export_import_demo.db';
// Logging utility
function log(message, type = 'info') {
const logOutput = document.getElementById('logOutput');
const timestamp = new Date().toLocaleTimeString();
const logLine = document.createElement('div');
logLine.className = `log-line log-${type}`;
logLine.textContent = `[${timestamp}] ${message}`;
logOutput.appendChild(logLine);
logOutput.scrollTop = logOutput.scrollHeight;
console.log(`[${type.toUpperCase()}] ${message}`);
}
// Format bytes to human-readable format
function formatBytes(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}
// Show status message
function showStatus(elementId, message, type = 'info') {
const element = document.getElementById(elementId);
element.innerHTML = `<div class="alert alert-${type === 'error' ? 'danger' : type === 'success' ? 'success' : 'info'}">${message}</div>`;
}
// Step 1: Create and populate database
document.getElementById('createDbBtn').addEventListener('click', async () => {
try {
log('Creating sample database...', 'info');
showStatus('createStatus', 'Creating database...', 'info');
if (!db) {
await init();
db = await Database.newDatabase(DB_NAME);
log(`Database "${DB_NAME}" created`, 'success');
}
// Create table
await db.execute(`
CREATE TABLE IF NOT EXISTS employees (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
department TEXT,
salary REAL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
log('Table "employees" created', 'success');
// Create index
await db.execute('CREATE INDEX IF NOT EXISTS idx_department ON employees(department)');
log('Index created on department column', 'success');
// Create trigger
await db.execute(`
CREATE TRIGGER IF NOT EXISTS salary_audit
AFTER UPDATE OF salary ON employees
BEGIN
SELECT RAISE(FAIL, 'Salary cannot be negative') WHERE NEW.salary < 0;
END
`);
log('Trigger "salary_audit" created', 'success');
// Insert sample data
const sampleData = [
['Alice Johnson', 'alice@example.com', 'Engineering', 95000],
['Bob Smith', 'bob@example.com', 'Marketing', 75000],
['Carol Williams', 'carol@example.com', 'Engineering', 105000],
['David Brown', 'david@example.com', 'Sales', 85000],
['Eve Davis', 'eve@example.com', 'HR', 70000]
];
for (const [name, email, dept, salary] of sampleData) {
await db.execute(
`INSERT INTO employees (name, email, department, salary) VALUES ('${name}', '${email}', '${dept}', ${salary})`
);
}
log(`Inserted ${sampleData.length} employee records`, 'success');
// Display data
const result = await db.execute('SELECT * FROM employees ORDER BY id');
const rows = result.rows;
const tableBody = document.getElementById('dataTableBody');
tableBody.innerHTML = '';
rows.forEach(row => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${row.values[0].value}</td>
<td>${row.values[1].value}</td>
<td>${row.values[2].value}</td>
<td>${row.values[3].value}</td>
<td>$${parseFloat(row.values[4].value).toLocaleString()}</td>
`;
tableBody.appendChild(tr);
});
document.getElementById('dataPreview').style.display = 'block';
document.getElementById('exportBtn').disabled = false;
showStatus('createStatus', `✓ Database created with ${sampleData.length} records`, 'success');
log('Database setup complete', 'success');
} catch (error) {
log(`Error creating database: ${error.message}`, 'error');
showStatus('createStatus', `Error: ${error.message}`, 'error');
}
});
// Step 2: Export database
document.getElementById('exportBtn').addEventListener('click', async () => {
try {
log('Starting database export...', 'info');
showStatus('exportStatus', 'Exporting database...', 'info');
document.getElementById('exportProgress').style.display = 'block';
const startTime = Date.now();
// Simulate progress (actual export is very fast)
let progress = 0;
const progressInterval = setInterval(() => {
progress += 10;
if (progress <= 90) {
document.getElementById('exportProgressBar').style.width = progress + '%';
document.getElementById('exportProgressBar').textContent = progress + '%';
}
}, 50);
// Perform export
exportedData = await db.exportToFile();
clearInterval(progressInterval);
document.getElementById('exportProgressBar').style.width = '100%';
document.getElementById('exportProgressBar').textContent = '100%';
const exportTime = Date.now() - startTime;
const fileSize = exportedData.length;
log(`Export completed in ${exportTime}ms`, 'success');
log(`Exported ${formatBytes(fileSize)}`, 'success');
// Show export info
document.getElementById('exportDbName').textContent = DB_NAME;
document.getElementById('exportFileSize').textContent = formatBytes(fileSize);
document.getElementById('exportTime').textContent = `${exportTime}ms`;
document.getElementById('exportStatusBadge').innerHTML = '<span class="status-badge status-success">Success</span>';
document.getElementById('exportFileInfo').style.display = 'block';
// Download file
const blob = new Blob([exportedData], { type: 'application/octet-stream' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = DB_NAME;
a.click();
URL.revokeObjectURL(url);
log(`File "${DB_NAME}" downloaded`, 'success');
showStatus('exportStatus', `✓ Database exported successfully (${formatBytes(fileSize)})`, 'success');
document.getElementById('importBtn').disabled = false;
setTimeout(() => {
document.getElementById('exportProgress').style.display = 'none';
}, 1000);
} catch (error) {
log(`Export error: ${error.message}`, 'error');
showStatus('exportStatus', `Error: ${error.message}`, 'error');
document.getElementById('exportProgress').style.display = 'none';
}
});
// Enable import button when file is selected
document.getElementById('importFile').addEventListener('change', (e) => {
const file = e.target.files[0];
if (file) {
document.getElementById('importBtn').disabled = false;
log(`File selected: ${file.name} (${formatBytes(file.size)})`, 'info');
}
});
// Step 3: Import database
document.getElementById('importBtn').addEventListener('click', async () => {
try {
const fileInput = document.getElementById('importFile');
const file = fileInput.files[0];
if (!file) {
showStatus('importStatus', 'Please select a file to import', 'error');
return;
}
log(`Starting import of "${file.name}"...`, 'info');
showStatus('importStatus', 'Importing database...', 'info');
document.getElementById('importProgress').style.display = 'block';
const startTime = Date.now();
// Simulate progress
let progress = 0;
const progressInterval = setInterval(() => {
progress += 10;
if (progress <= 90) {
document.getElementById('importProgressBar').style.width = progress + '%';
document.getElementById('importProgressBar').textContent = progress + '%';
}
}, 50);
// Read file
const arrayBuffer = await file.arrayBuffer();
const uint8Array = new Uint8Array(arrayBuffer);
// Close existing database
if (db) {
await db.close();
log('Existing database closed', 'info');
}
// Create new database instance for import
await init();
db = await Database.newDatabase(DB_NAME);
// Perform import
await db.importFromFile(uint8Array);
clearInterval(progressInterval);
document.getElementById('importProgressBar').style.width = '100%';
document.getElementById('importProgressBar').textContent = '100%';
const importTime = Date.now() - startTime;
log(`Import completed in ${importTime}ms`, 'success');
// Reopen database after import
db = await Database.newDatabase(DB_NAME);
log('Database reopened after import', 'success');
// Show import info
document.getElementById('importFileName').textContent = file.name;
document.getElementById('importFileSize').textContent = formatBytes(file.size);
document.getElementById('importTime').textContent = `${importTime}ms`;
document.getElementById('importStatusBadge').innerHTML = '<span class="status-badge status-success">Success</span>';
document.getElementById('importFileInfo').style.display = 'block';
showStatus('importStatus', `✓ Database imported successfully from "${file.name}"`, 'success');
document.getElementById('verifyBtn').disabled = false;
setTimeout(() => {
document.getElementById('importProgress').style.display = 'none';
}, 1000);
} catch (error) {
log(`Import error: ${error.message}`, 'error');
showStatus('importStatus', `Error: ${error.message}`, 'error');
document.getElementById('importProgress').style.display = 'none';
}
});
// Step 4: Verify imported data
document.getElementById('verifyBtn').addEventListener('click', async () => {
try {
log('Verifying imported data...', 'info');
showStatus('verifyStatus', 'Querying database...', 'info');
const result = await db.execute('SELECT * FROM employees ORDER BY id');
const rows = result.rows;
log(`Found ${rows.length} records in imported database`, 'success');
const tableBody = document.getElementById('verifyTableBody');
tableBody.innerHTML = '';
rows.forEach(row => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${row.values[0].value}</td>
<td>${row.values[1].value}</td>
<td>${row.values[2].value}</td>
<td>${row.values[3].value}</td>
<td>$${parseFloat(row.values[4].value).toLocaleString()}</td>
`;
tableBody.appendChild(tr);
});
document.getElementById('verifyDataPreview').style.display = 'block';
showStatus('verifyStatus', `✓ Verification complete: ${rows.length} records found`, 'success');
log('Data integrity verified', 'success');
} catch (error) {
log(`Verification error: ${error.message}`, 'error');
showStatus('verifyStatus', `Error: ${error.message}`, 'error');
}
});
// Clear log
document.getElementById('clearLogBtn').addEventListener('click', () => {
document.getElementById('logOutput').innerHTML = '';
log('Log cleared', 'info');
});
// Initial log
log('Export/Import Demo loaded - Ready to start', 'success');
</script>
</body>
</html>