Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Dec 7, 2025

Description

Implements DUMP and DUMP DATABASE commands to export database schema and data as executable SQL script, equivalent to SQLite's .dump or MySQL's mysqldump.

Changes

  • Parser: Added DUMP token and grammar rules to alasqlparser.jison
  • Implementation: Created src/79dump.js with DumpDatabase class
    • Generates CREATE TABLE statements from table schemas
    • Generates INSERT statements from table data
    • Handles NULL values (including NaN for numeric columns), undefined values, and string escaping
    • Supports DUMP (current database) and DUMP DATABASE dbname (specific database)
  • Build: Added source file to build.sh concatenation list
  • Tests: Added test/test110-B.js with 8 test cases with complete output assertions covering edge cases including NULL handling and quote escaping

Usage

// Export current database
var sql = alasql('DUMP');

// Export specific database  
var sql = alasql('DUMP DATABASE mybase');

// Restore from dump
alasql('CREATE DATABASE restored');
alasql('USE restored');
alasql(sql);

Output example:

CREATE TABLE users (id INT, name STRING, email STRING);
INSERT INTO users(id,name,email) VALUES (1,'Alice','[email protected]');
INSERT INTO users(id,name,email) VALUES (2,'Bob','[email protected]');
INSERT INTO users(id,name,email) VALUES (NULL,'Charlie',NULL);

Testing

  • ✅ All 8 tests pass with complete output assertions
  • ✅ NULL values (including NaN) correctly output as NULL in SQL
  • ✅ String escaping works correctly (e.g., Val's Diner'Val''s Diner')
  • ✅ Database can be successfully restored from dump output
  • ✅ Build process completes successfully
Original prompt

This section details on the original issue you should resolve

<issue_title>Export database as SQL script</issue_title>
<issue_description>SQLite have .dump command, MySQL have special tool mysqldump. Do you have something similar?
</issue_description>

<agent_instructions>Name the test file test/test110-B.js</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@agershun Sorry, not yet.

There are some command to dump data to SQL:

    SELECT * INTO SQL("mydata.sql") FROM one;

and

    SHOW CREATE TABLE one;

But not whole database.

@agershun What do you thing, what approach llisted below is better?

a) to have dump command for specific database, like:

    var sql = alasql('DUMP DATABASE mybase');

b) dump all databases in current alasql object:

    var sql = alasql('DUMP');
@mathiasrw Another solution (not involving making new "SQL" things) would be to extend the alasql object with a dump method
var sql = alasql().dump();

Should work on both alasql and objects created from new alasql

@agershun I think it is a good idea to minimize alasql() interface, because in case of `alasql.dump()` we need to maintain this additional function in WebWorker version (alasql-worker.js), but in case of `res = alasql('DUMP');` or `res = alasql('DUMP DATABASE mybase');`` Alasql will use the same version.

Of course, alasql.dump() can be accessible as a function outside, but only for non-worker version.

@mathiasrw > What do you thing, what approach llisted below is better? > > a) to have dump command for specific database, like: > > ``` > var sql = alasql('DUMP DATABASE mybase'); > ``` > > b) dump all databases in current alasql object: > > ``` > var sql = alasql('DUMP'); > ```

I like it. I have added it to the feature requests

http://feathub.com/agershun/alasql/+25


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Add export database as SQL script functionality Add DUMP DATABASE command to export database as SQL script Dec 7, 2025
Copilot AI requested a review from mathiasrw December 7, 2025 06:36
src/79dump.js Outdated
Comment on lines 1 to 7
/*
//
// DUMP for Alasql.js
// Date: 07.12.2024
// (c) 2024, Andrey Gershun
//
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Removed copyright header in commit 17e4c67.

Comment on lines 27 to 29
assert(sql.includes('CREATE TABLE one'));
assert(sql.includes('a INT'));
assert(sql.includes('b STRING'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all the tests, please assert the specific complete output we expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Updated all tests to assert complete expected output instead of partial includes. Also fixed NaN handling for NULL numeric values. Commit 17e4c67.

Copilot AI requested a review from mathiasrw December 7, 2025 15:06
@mathiasrw mathiasrw added this to the The Back Burner milestone Dec 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export database as SQL script

2 participants