Got SQLite -> IndexedDB migration working.

master
Sander Vocke 6 years ago
parent 5134ed0043
commit 25a648d2c8
  1. 60
      src/database.js
  2. 2
      src/index.js

@ -1,44 +1,71 @@
import React from 'react';
var DBTypeEnum = {
SQLITE: 1,
// INDEXEDDB: 2 // TODO: implement with migration
ALASQL_SQLITE: 1,
ALASQL_INDEXEDDB: 2 // TODO: implement with migration
};
var DBLibraryEnum = {
ALASQL: 1
}
export class DB {
state = {
db_type: false,
db_object: false,
db_name: false,
db_library: false,
}
// TODO random database names
constructor(obj, name, type, library) {
this.state.db_object = obj;
this.state.db_name = name;
this.state.db_type = type;
this.state.db_library = library;
}
query_async(s) {
if(this.state.db_library === DBLibraryEnum.ALASQL) {
if(this.state.db_type === DBTypeEnum.ALASQL_SQLITE ||
this.state.db_type === DBTypeEnum.ALASQL_INDEXEDDB) {
return this.state.db_object.promise(s);
}
throw new Error("Unsupported database library: " + this.state.db_library);
throw new Error("Unsupported database type: " + this.state.db_type);
}
query_sync(s) {
if(this.state.db_library === DBLibraryEnum.ALASQL) {
if(this.state.db_type === DBTypeEnum.ALASQL_SQLITE ||
this.state.db_type === DBTypeEnum.ALASQL_INDEXEDDB) {
return this.state.db_object(s);
}
throw new Error("Unsupported database library: " + this.state.db_library);
throw new Error("Unsupported database type: " + this.state.db_type);
}
migrate_async(db_type, db_name) {
var self = this;
if(this.state.db_type === DBTypeEnum.ALASQL_SQLITE && db_type === DBTypeEnum.ALASQL_INDEXEDDB) {
return new Promise(function(resolve, reject) {
console.log(self.state);
var tables = self.query_sync("SHOW TABLES;");
console.log(tables);
console.log(self.state);
var query = "";
query += "DROP INDEXEDDB DATABASE " + db_name + ";";
query += "ATTACH INDEXEDDB DATABASE " + db_name + ";";
query += "CREATE INDEXEDDB DATABASE IF NOT EXISTS " + db_name + ";";
tables.forEach(elem => {
query += "CREATE TABLE " + db_name + "." + elem.tableid + ";";
query += "SELECT * INTO " + db_name + "." + elem.tableid +
" FROM " + self.state.db_name + "." + elem.tableid + ";";
});
query += "DETACH DATABASE " + self.state.db_name + ";";
query += "USE " + db_name + ";";
self.query_async(query).then(() => {
self.state.db_type = db_type;
self.state.db_name = db_name;
resolve();
});
});
}
throw new Error("Unsupported copy creation from db_type " + this.state.db_type + " to " + db_type);
}
}
// TODO random database names
function fetch_db_from_sqlite(filename) {
return new Promise(function(resolve, reject) {
var initSqlJs = require('sql.js');
@ -52,7 +79,7 @@ function fetch_db_from_sqlite(filename) {
query += "ATTACH SQLITE DATABASE db(\"" + filename + "\"); USE db;";
asql.promise(query)
.then(res => {
var imported_db = new DB(asql, "db", DBTypeEnum.SQLITE, DBLibraryEnum.ALASQL);
var imported_db = new DB(asql, "db", DBTypeEnum.ALASQL_SQLITE);
resolve(imported_db);
});
});
@ -70,7 +97,12 @@ export class FetchSQLiteDB extends React.Component {
componentDidMount() {
fetch_db_from_sqlite(this.props.sqlite_file)
.then(db => { this.setState({ loading: false, done: true, db: db }); })
.then(db => {
if(this.props.migrate_to_indexeddb) {
db.migrate_async(DBTypeEnum.ALASQL_INDEXEDDB, "indexed_db")
.then(() => { this.setState({ loading: false, done: true, db: db }) });
} else { this.setState({ loading: false, done: true, db: db }) };
})
.catch(error => this.setState({ loading: false, done: false, error }));
}

@ -26,7 +26,7 @@ const TestFetch = ({url}) => (
)
const TestDBFetch = ({sqlite_file}) => (
<FetchSQLiteDB sqlite_file={sqlite_file}>
<FetchSQLiteDB sqlite_file={sqlite_file} migrate_to_indexeddb={true}>
{({ loading, error, done, db }) => (
<>
{ loading && <DBLoading sqlite_file={sqlite_file}/> }

Loading…
Cancel
Save