|
|
@ -1,44 +1,71 @@ |
|
|
|
import React from 'react'; |
|
|
|
import React from 'react'; |
|
|
|
|
|
|
|
|
|
|
|
var DBTypeEnum = { |
|
|
|
var DBTypeEnum = { |
|
|
|
SQLITE: 1, |
|
|
|
ALASQL_SQLITE: 1, |
|
|
|
// INDEXEDDB: 2 // TODO: implement with migration
|
|
|
|
ALASQL_INDEXEDDB: 2 // TODO: implement with migration
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
var DBLibraryEnum = { |
|
|
|
|
|
|
|
ALASQL: 1 |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class DB { |
|
|
|
export class DB { |
|
|
|
state = { |
|
|
|
state = { |
|
|
|
db_type: false, |
|
|
|
db_type: false, |
|
|
|
db_object: false, |
|
|
|
db_object: false, |
|
|
|
db_name: false, |
|
|
|
db_name: false, |
|
|
|
db_library: false, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO random database names
|
|
|
|
constructor(obj, name, type, library) { |
|
|
|
constructor(obj, name, type, library) { |
|
|
|
this.state.db_object = obj; |
|
|
|
this.state.db_object = obj; |
|
|
|
this.state.db_name = name; |
|
|
|
this.state.db_name = name; |
|
|
|
this.state.db_type = type; |
|
|
|
this.state.db_type = type; |
|
|
|
this.state.db_library = library; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
query_async(s) { |
|
|
|
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); |
|
|
|
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) { |
|
|
|
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); |
|
|
|
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) { |
|
|
|
function fetch_db_from_sqlite(filename) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
return new Promise(function(resolve, reject) { |
|
|
|
var initSqlJs = require('sql.js'); |
|
|
|
var initSqlJs = require('sql.js'); |
|
|
@ -52,7 +79,7 @@ function fetch_db_from_sqlite(filename) { |
|
|
|
query += "ATTACH SQLITE DATABASE db(\"" + filename + "\"); USE db;"; |
|
|
|
query += "ATTACH SQLITE DATABASE db(\"" + filename + "\"); USE db;"; |
|
|
|
asql.promise(query) |
|
|
|
asql.promise(query) |
|
|
|
.then(res => { |
|
|
|
.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); |
|
|
|
resolve(imported_db); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -70,7 +97,12 @@ export class FetchSQLiteDB extends React.Component { |
|
|
|
|
|
|
|
|
|
|
|
componentDidMount() { |
|
|
|
componentDidMount() { |
|
|
|
fetch_db_from_sqlite(this.props.sqlite_file) |
|
|
|
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 })); |
|
|
|
.catch(error => this.setState({ loading: false, done: false, error })); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|