diff --git a/public/image1.jpg b/public/image1.jpg new file mode 100644 index 0000000..2a31d39 Binary files /dev/null and b/public/image1.jpg differ diff --git a/public/image2.jpg b/public/image2.jpg new file mode 100644 index 0000000..818427c Binary files /dev/null and b/public/image2.jpg differ diff --git a/public/test.sqlite b/public/test.sqlite index cec45fd..a530ad4 100644 Binary files a/public/test.sqlite and b/public/test.sqlite differ diff --git a/src/database.js b/src/database.js index 324bfed..3b70c80 100644 --- a/src/database.js +++ b/src/database.js @@ -19,42 +19,51 @@ export class DB { this.state.db_type = type; } - query_async(s) { + // Perform a chain of asynchronous queries, each one waiting for the last before + // starting. Not supported on all database types. + queries_async(s) { 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 type: " + this.state.db_type); + this.state.db_type === DBTypeEnum.ALASQL_INDEXEDDB) { + var self = this; + var p = Promise.resolve(null); + for(let i = 0; i < s.length; i++) { + p = p.then(() => { return self.state.db_object.promise(s[i]); }); + } + + return p; + } + throw new Error("Unsupported database type for async operations: " + this.state.db_type); } - query_sync(s) { - if(this.state.db_type === DBTypeEnum.ALASQL_SQLITE || - this.state.db_type === DBTypeEnum.ALASQL_INDEXEDDB) { - return this.state.db_object(s); + // Perform a series of queries sequentially, then return the last result. + // Note that synchronous operation is not supported for all database types. + queries_sync(s) { + if(this.state.db_type === DBTypeEnum.ALASQL_SQLITE) { + for (var i = 0; i < (s.length-1); i++) { + this.state.db_object(s[i]); + } + return this.state.db_object(s[s.length-1]); } - throw new Error("Unsupported database type: " + this.state.db_type); + throw new Error("Unsupported database type for sync operations: " + 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 + ";"; + var tables = self.queries_sync(["SHOW TABLES;"]); + var queries = []; + queries.push("DROP INDEXEDDB DATABASE " + db_name + ";"); + queries.push("CREATE INDEXEDDB DATABASE IF NOT EXISTS " + db_name + ";"); + queries.push("ATTACH INDEXEDDB DATABASE " + 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 + ";"; + queries.push("CREATE TABLE " + db_name + "." + elem.tableid + ";"); + queries.push("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(() => { + queries.push("DETACH DATABASE " + self.state.db_name + ";"); + queries.push("USE " + db_name + ";"); + self.queries_async(queries).then(() => { self.state.db_type = db_type; self.state.db_name = db_name; resolve(); @@ -134,9 +143,8 @@ export class DBQueryConsole extends React.Component { onQueryChangeHandler = query => { this.setState( { query: query } ); } onQuerySubmitHandler = () => { - console.log("Submitting query: " + this.state.query); this.setState( { processing: true, result: false } ); - this.props.database.query_async(this.state.query) + this.props.database.queries_async([this.state.query]) .then(result => { this.setState( { processing: false, result: JSON.stringify(result) } ); }); @@ -145,10 +153,12 @@ export class DBQueryConsole extends React.Component { render() { return ( <> +
Result:
{ this.state.result &&{this.state.result}
} +{url} was loaded: {data.byteLength} byte const DBLoading = ({sqlite_file}) =>
Loading: {sqlite_file}
; const DBError = ({error}) =>Failed to load database: {error.message}
; -const DBFinished = ({sqlite_file, db}) =>{sqlite_file} was loaded. Name: { db.state.db_name }. Tables: { JSON.stringify(db.query_sync("SHOW TABLES;")) }
; +const DBFinished = ({sqlite_file, db}) =>{sqlite_file} was loaded. Name: { db.state.db_name }.
; const TestFetch = ({url}) => (