Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ cat \
src/76usedatabase.js \
src/77declare.js \
src/78show.js \
src/79dump.js \
src/79set.js \
src/80console.js \
src/81commit.js \
Expand Down
104 changes: 104 additions & 0 deletions src/79dump.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// DUMP DATABASE databaseid
// or DUMP (for current database)

yy.DumpDatabase = function (params) {
return Object.assign(this, params);
};

yy.DumpDatabase.prototype.toString = function () {
var s = 'DUMP';
if (this.databaseid) {
s += ' DATABASE ' + this.databaseid;
}
return s;
};

yy.DumpDatabase.prototype.execute = function (databaseid, params, cb) {
var dbid = this.databaseid || databaseid;
var db = alasql.databases[dbid];

if (!db) {
throw new Error("Database '" + dbid + "' does not exist");
}

var sql = '';

// Iterate through all tables in the database
for (var tableid in db.tables) {
var table = db.tables[tableid];

// Generate CREATE TABLE statement
sql += 'CREATE TABLE ' + tableid + ' (';
var ss = [];

if (table.columns && table.columns.length > 0) {
table.columns.forEach(function (col) {
var a = col.columnid + ' ' + (col.dbtypeid || col.typeid || 'STRING');
if (col.dbsize) a += '(' + col.dbsize + ')';
if (col.primarykey) a += ' PRIMARY KEY';
// TODO: Add more column properties like NOT NULL, DEFAULT, etc.
ss.push(a);
});
sql += ss.join(', ');
}

sql += ');\n';

// Generate INSERT statements for table data
if (table.data && table.data.length > 0) {
var columns = table.columns || [];

// If no columns defined, infer from first data row
if (columns.length === 0 && table.data.length > 0) {
columns = Object.keys(table.data[0]).map(function (columnid) {
return {columnid: columnid};
});
}

// Generate INSERT statements
for (var i = 0; i < table.data.length; i++) {
sql += 'INSERT INTO ' + tableid + '(';
sql += columns
.map(function (col) {
return col.columnid;
})
.join(',');
sql += ') VALUES (';
sql += columns
.map(function (col) {
var val = table.data[i][col.columnid];
// Handle null, undefined, and NaN values
if (
val === null ||
val === undefined ||
(typeof val === 'number' && Number.isNaN(val))
) {
return 'NULL';
}
// Check if value should be escaped as a string
var shouldEscape =
(col.typeid &&
(col.typeid === 'STRING' ||
col.typeid === 'VARCHAR' ||
col.typeid === 'NVARCHAR' ||
col.typeid === 'CHAR' ||
col.typeid === 'NCHAR')) ||
typeof val == 'string';
if (shouldEscape) {
val = "'" + escapeqq(val) + "'";
}
return val;
})
.join(',');
sql += ');\n';
}
}

sql += '\n';
}

if (cb) {
return cb(sql);
}
return sql;
};
9 changes: 9 additions & 0 deletions src/alasqlparser.jison
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ DATABASE(S)? return 'DATABASE'
'DISTINCT' return 'DISTINCT'
/* DOUBLE\s+PRECISION return 'LITERAL' */
'DROP' return 'DROP'
'DUMP' return 'DUMP'
'ECHO' return 'ECHO'
'EDGE' return 'EDGE'
'END' return 'END'
Expand Down Expand Up @@ -420,6 +421,7 @@ Statement
| DropIndex
| DropTable
| DropView
| DumpDatabase
| If
| Insert
| Merge
Expand Down Expand Up @@ -2279,6 +2281,13 @@ UseDatabase
{ $$ = new yy.UseDatabase({databaseid: $2 });}
;

DumpDatabase
: DUMP DATABASE Literal
{ $$ = new yy.DumpDatabase({databaseid: $3 });}
| DUMP
{ $$ = new yy.DumpDatabase();}
;

DropDatabase
: DROP DATABASE IfExists Literal
{ $$ = new yy.DropDatabase({databaseid: $4 }); yy.extend($$,$3); }
Expand Down
Loading