From 11df616319c989364f9e973f2f46a9aef0b8d3a6 Mon Sep 17 00:00:00 2001 From: lxsang Date: Mon, 30 Jan 2023 09:37:44 +0100 Subject: [PATCH] add lib for sqlite3 database --- SQLiteDB/LibSQLite.ts | 130 +++++++++++++++++++++++++++ SQLiteDB/README.md | 15 ++++ SQLiteDB/api/api.lua | 76 ++++++++++++++++ SQLiteDB/build.json | 92 +++++++++++++++++++ SQLiteDB/build/debug/README.md | 15 ++++ SQLiteDB/build/debug/api/api.lua | 76 ++++++++++++++++ SQLiteDB/build/debug/libsqlite.js | 108 +++++++++++++++++++++++ SQLiteDB/build/debug/main.js | 141 ++++++++++++++++++++++++++++++ SQLiteDB/build/debug/package.json | 16 ++++ SQLiteDB/build/debug/scheme.html | 3 + SQLiteDB/main.ts | 34 +++++++ SQLiteDB/package.json | 16 ++++ SQLiteDB/scheme.html | 3 + 13 files changed, 725 insertions(+) create mode 100644 SQLiteDB/LibSQLite.ts create mode 100644 SQLiteDB/README.md create mode 100644 SQLiteDB/api/api.lua create mode 100644 SQLiteDB/build.json create mode 100644 SQLiteDB/build/debug/README.md create mode 100644 SQLiteDB/build/debug/api/api.lua create mode 100644 SQLiteDB/build/debug/libsqlite.js create mode 100644 SQLiteDB/build/debug/main.js create mode 100644 SQLiteDB/build/debug/package.json create mode 100644 SQLiteDB/build/debug/scheme.html create mode 100644 SQLiteDB/main.ts create mode 100644 SQLiteDB/package.json create mode 100644 SQLiteDB/scheme.html diff --git a/SQLiteDB/LibSQLite.ts b/SQLiteDB/LibSQLite.ts new file mode 100644 index 0000000..506394d --- /dev/null +++ b/SQLiteDB/LibSQLite.ts @@ -0,0 +1,130 @@ +namespace OS { + export namespace API + { + export class SQLiteDBBase { + private db_file: VFS.BaseFileHandle; + + constructor(path: VFS.BaseFileHandle | string) + { + this.db_file = path.asFileHandle(); + } + + private pwd(): VFS.BaseFileHandle + { + return "pkg://SQLiteDB/".asFileHandle(); + } + /** + * init and create the db file if it does not exist + */ + private init(): Promise + { + return new Promise(async (ok, reject) => { + try{ + let request = { + action: 'init', + args: { + db_source: this.db_file.path, + } + } + let _result = await this.call(request); + _result = await this.db_file.onready(); + if(!this.db_file || !this.db_file.ready || this.db_file.info.type !== "file") + { + throw __("DB file meta-data is invalid: {0}", this.db_file.path).__(); + } + ok(true); + } + catch(e) + { + reject(__e(e)); + } + }); + } + + private call(request: GenericObject): Promise { + return new Promise(async (ok, reject) => { + request.args.db_source = this.db_file.path; + let cmd = { + path: this.pwd().path + "/api/api.lua", + parameters: request + } + let data = await API.apigateway(cmd, false); + if(!data.error) + { + ok(data.result); + } + else + { + reject(API.throwe(__("SQLiteDB server call error: {0}", data.error))); + } + }); + } + + private request(rq: GenericObject): Promise + { + return new Promise(async (ok, reject) => { + try{ + if(!this.db_file.ready) + { + let _ = await this.init(); + } + let result = await this.call(rq); + ok(result); + } + catch(e) + { + reject(__e(e)); + } + }); + } + query(sql: string): Promise + { + let rq = { + action: 'query', + args: { + query: sql + } + } + return this.request(rq); + } + + select(table: string, fields: string[], condition: string): Promise[]> + { + let rq = { + action: 'select', + args: { + table: table, + fields: fields.join(","), + cond: condition + } + } + return this.request(rq); + } + + list_tables(): Promise + { + return new Promise(async (ok, reject) => { + try { + let result = await this.select( + "sqlite_master", ["name"], "type ='table'"); + return ok(result.map((e) => e.name)) + } + catch(e) + { + reject(__e(e)) + } + }); + } + + last_insert_id(): Promise + { + let rq = { + action: 'last_insert_id', + args: { + } + } + return this.request(rq); + } + } + } +} \ No newline at end of file diff --git a/SQLiteDB/README.md b/SQLiteDB/README.md new file mode 100644 index 0000000..76e9d49 --- /dev/null +++ b/SQLiteDB/README.md @@ -0,0 +1,15 @@ +# SQLiteDB +This is an example project, generated by AntOS Development Kit + +## Howto +Use the Antedit command palette to access to the SDK functionalities: + +1. Create new project +2. Init the project from the current folder located in side bar +3. Build and run the project +4. Release the project in zip package + +## Set up build target + +Open the `build.json` file from the current project tree and add/remove +build target entries and jobs. Save the file \ No newline at end of file diff --git a/SQLiteDB/api/api.lua b/SQLiteDB/api/api.lua new file mode 100644 index 0000000..3b581ff --- /dev/null +++ b/SQLiteDB/api/api.lua @@ -0,0 +1,76 @@ +-- collecting arguments +local args=... + +-- require libs +local vfs = require("vfs") +local sqlite = modules.sqlite() + +-- helper functions +local result = function(data) + return { error = false, result = data } +end + +local error = function(msg) + return {error = msg, result = false} +end + +-- handler object +local handle = {} + +-- Handle functions defined here + +handle.init = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + sqlite.dbclose(db) + return result(true) +end + +handle.query = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + local ret = sqlite.query(db, data.query) + sqlite.dbclose(db) + if ret ~= 1 then + return error("error executing query") + end + return result(true) +end + +handle.select = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + local ret = sqlite.select(db, data.table, data.fields, data.cond); + sqlite.dbclose(db) + if not ret then + return error("error executing select statement") + end + return result(ret) +end + +handle.last_insert_id = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + local ret = sqlite.lastInsertID(db) + sqlite.dbclose(db) + return result(ret) +end + +-- dispatching action +if args.action and handle[args.action] then + return handle[args.action](args.args) +else + return error("Invalid action parameter") +end \ No newline at end of file diff --git a/SQLiteDB/build.json b/SQLiteDB/build.json new file mode 100644 index 0000000..480851f --- /dev/null +++ b/SQLiteDB/build.json @@ -0,0 +1,92 @@ +{ + "name": "SQLiteDB", + "targets":{ + "clean": { + "jobs": [ + { + "name": "vfs-rm_no_error", + "data": ["build/debug","build/release"] + } + ] + }, + "build": { + "require": ["ts"], + "jobs":[ + { + "name": "vfs-mkdir", + "data": ["build","build/debug","build/release"] + }, + { + "name": "ts-import", + "data": ["sdk://core/ts/core.d.ts", "sdk://core/ts/jquery.d.ts","sdk://core/ts/antos.d.ts"] + }, + { + "name": "ts-compile", + "data": { + "src": ["main.ts"], + "dest": "build/debug/main.js" + } + }, + { + "name": "ts-compile", + "data": { + "src": ["LibSQLite.ts"], + "dest": "build/debug/libsqlite.js" + } + } + ] + }, + "uglify": { + "require": ["terser"], + "jobs": [ + { + "name":"terser-uglify", + "data": ["build/debug/main.js", "build/debug/libsqlite.js"] + } + ] + }, + "copy": { + "jobs": [ + { + "name": "vfs-cp", + "data": { + "src": [ + "scheme.html", + "package.json", + "README.md", + "api" + ], + "dest":"build/debug" + } + } + ] + }, + "release": { + "depend": ["clean","build","uglify", "copy"], + "require": ["zip"], + "jobs": [ + { + "name": "zip-mk", + "data": { + "src":"build/debug", + "dest":"build/release/SQLiteDB.zip" + } + } + ] + }, + "debug": { + "depend": ["clean","build", "copy"], + "jobs": [ + { + "name": "vfs-cat", + "data": { + "src": [ + "build/debug/main.js", "build/debug/libsqlite.js" + ], + "dest":"build/debug/main.js" + } + } + ] + } + } +} \ No newline at end of file diff --git a/SQLiteDB/build/debug/README.md b/SQLiteDB/build/debug/README.md new file mode 100644 index 0000000..76e9d49 --- /dev/null +++ b/SQLiteDB/build/debug/README.md @@ -0,0 +1,15 @@ +# SQLiteDB +This is an example project, generated by AntOS Development Kit + +## Howto +Use the Antedit command palette to access to the SDK functionalities: + +1. Create new project +2. Init the project from the current folder located in side bar +3. Build and run the project +4. Release the project in zip package + +## Set up build target + +Open the `build.json` file from the current project tree and add/remove +build target entries and jobs. Save the file \ No newline at end of file diff --git a/SQLiteDB/build/debug/api/api.lua b/SQLiteDB/build/debug/api/api.lua new file mode 100644 index 0000000..3b581ff --- /dev/null +++ b/SQLiteDB/build/debug/api/api.lua @@ -0,0 +1,76 @@ +-- collecting arguments +local args=... + +-- require libs +local vfs = require("vfs") +local sqlite = modules.sqlite() + +-- helper functions +local result = function(data) + return { error = false, result = data } +end + +local error = function(msg) + return {error = msg, result = false} +end + +-- handler object +local handle = {} + +-- Handle functions defined here + +handle.init = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + sqlite.dbclose(db) + return result(true) +end + +handle.query = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + local ret = sqlite.query(db, data.query) + sqlite.dbclose(db) + if ret ~= 1 then + return error("error executing query") + end + return result(true) +end + +handle.select = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + local ret = sqlite.select(db, data.table, data.fields, data.cond); + sqlite.dbclose(db) + if not ret then + return error("error executing select statement") + end + return result(ret) +end + +handle.last_insert_id = function(data) + local os_path = vfs.ospath(data.db_source) + local db = sqlite._getdb(os_path) + if not db then + return error("Unable to open sqlite db file") + end + local ret = sqlite.lastInsertID(db) + sqlite.dbclose(db) + return result(ret) +end + +-- dispatching action +if args.action and handle[args.action] then + return handle[args.action](args.args) +else + return error("Invalid action parameter") +end \ No newline at end of file diff --git a/SQLiteDB/build/debug/libsqlite.js b/SQLiteDB/build/debug/libsqlite.js new file mode 100644 index 0000000..57bded6 --- /dev/null +++ b/SQLiteDB/build/debug/libsqlite.js @@ -0,0 +1,108 @@ + +var OS; +(function (OS) { + let API; + (function (API) { + class SQLiteDBBase { + constructor(path) { + this.db_file = path.asFileHandle(); + } + pwd() { + return "pkg://SQLiteDB/".asFileHandle(); + } + /** + * init and create the db file if it doesnot exist + */ + init() { + return new Promise(async (ok, reject) => { + try { + let request = { + action: 'init', + args: { + db_source: this.db_file.path, + } + }; + let _result = await this.call(request); + _result = await this.db_file.onready(); + if (!this.db_file || !this.db_file.ready || this.db_file.info.type !== "file") { + throw __("DB file meta-data is invalid: {0}", this.db_file.path).__(); + } + ok(true); + } + catch (e) { + reject(__e(e)); + } + }); + } + call(request) { + return new Promise(async (ok, reject) => { + request.args.db_source = this.db_file.path; + let cmd = { + path: this.pwd().path + "/api/api.lua", + parameters: request + }; + let data = await API.apigateway(cmd, false); + if (!data.error) { + ok(data.result); + } + else { + reject(API.throwe(__("SQLiteDB server call error: {0}", data.error))); + } + }); + } + request(rq) { + return new Promise(async (ok, reject) => { + try { + if (!this.db_file.ready) { + let _ = await this.init(); + } + let result = await this.call(rq); + ok(result); + } + catch (e) { + reject(__e(e)); + } + }); + } + query(sql) { + let rq = { + action: 'query', + args: { + query: sql + } + }; + return this.request(rq); + } + select(table, fields, condition) { + let rq = { + action: 'select', + args: { + table: table, + fields: fields.join(","), + cond: condition + } + }; + return this.request(rq); + } + list_tables() { + return new Promise(async (ok, reject) => { + try { + let result = await this.select("sqlite_master", ["name"], "type ='table'"); + return ok(result.map((e) => e.name)); + } + catch (e) { + reject(__e(e)); + } + }); + } + last_insert_id() { + let rq = { + action: 'last_insert_id', + args: {} + }; + return this.request(rq); + } + } + API.SQLiteDBBase = SQLiteDBBase; + })(API = OS.API || (OS.API = {})); +})(OS || (OS = {})); diff --git a/SQLiteDB/build/debug/main.js b/SQLiteDB/build/debug/main.js new file mode 100644 index 0000000..49d58d6 --- /dev/null +++ b/SQLiteDB/build/debug/main.js @@ -0,0 +1,141 @@ + + +var OS; +(function (OS) { + let application; + (function (application) { + ; + ; + /** + * + * @class SQLiteDB + * @extends {BaseApplication} + */ + class SQLiteDB extends application.BaseApplication { + constructor(args) { + super("SQLiteDB", args); + } + main() { + // YOUR CODE HERE + let handle = new OS.API.SQLiteDBBase("home://tmp/test.db"); + handle.list_tables().then((list) => { + console.log(list); + if (list.indexOf("contacts") < 0) { + handle.query("CREATE TABLE contacts (id INTEGER PRIMARY KEY,first_name TEXT NOT NULL,last_name TEXT NOT NULL,email TEXT NOT NULL UNIQUE,phone TEXT NOT NULL UNIQUE)"); + } + }); + handle.last_insert_id().then(o => console.log(o)); + } + } + application.SQLiteDB = SQLiteDB; + })(application = OS.application || (OS.application = {})); +})(OS || (OS = {})); + + +var OS; +(function (OS) { + let API; + (function (API) { + class SQLiteDBBase { + constructor(path) { + this.db_file = path.asFileHandle(); + } + pwd() { + return "pkg://SQLiteDB/".asFileHandle(); + } + /** + * init and create the db file if it doesnot exist + */ + init() { + return new Promise(async (ok, reject) => { + try { + let request = { + action: 'init', + args: { + db_source: this.db_file.path, + } + }; + let _result = await this.call(request); + _result = await this.db_file.onready(); + if (!this.db_file || !this.db_file.ready || this.db_file.info.type !== "file") { + throw __("DB file meta-data is invalid: {0}", this.db_file.path).__(); + } + ok(true); + } + catch (e) { + reject(__e(e)); + } + }); + } + call(request) { + return new Promise(async (ok, reject) => { + request.args.db_source = this.db_file.path; + let cmd = { + path: this.pwd().path + "/api/api.lua", + parameters: request + }; + let data = await API.apigateway(cmd, false); + if (!data.error) { + ok(data.result); + } + else { + reject(API.throwe(__("SQLiteDB server call error: {0}", data.error))); + } + }); + } + request(rq) { + return new Promise(async (ok, reject) => { + try { + if (!this.db_file.ready) { + let _ = await this.init(); + } + let result = await this.call(rq); + ok(result); + } + catch (e) { + reject(__e(e)); + } + }); + } + query(sql) { + let rq = { + action: 'query', + args: { + query: sql + } + }; + return this.request(rq); + } + select(table, fields, condition) { + let rq = { + action: 'select', + args: { + table: table, + fields: fields.join(","), + cond: condition + } + }; + return this.request(rq); + } + list_tables() { + return new Promise(async (ok, reject) => { + try { + let result = await this.select("sqlite_master", ["name"], "type ='table'"); + return ok(result.map((e) => e.name)); + } + catch (e) { + reject(__e(e)); + } + }); + } + last_insert_id() { + let rq = { + action: 'last_insert_id', + args: {} + }; + return this.request(rq); + } + } + API.SQLiteDBBase = SQLiteDBBase; + })(API = OS.API || (OS.API = {})); +})(OS || (OS = {})); diff --git a/SQLiteDB/build/debug/package.json b/SQLiteDB/build/debug/package.json new file mode 100644 index 0000000..063d3ce --- /dev/null +++ b/SQLiteDB/build/debug/package.json @@ -0,0 +1,16 @@ +{ + "pkgname": "SQLiteDB", + "app":"SQLiteDB", + "name":"SQLiteDB", + "description":"SQLiteDB", + "info":{ + "author": "", + "email": "" + }, + "version":"0.0.1-a", + "category":"Other", + "iconclass":"fa fa-adn", + "mimes":["none"], + "dependencies":[], + "locale": {} +} \ No newline at end of file diff --git a/SQLiteDB/build/debug/scheme.html b/SQLiteDB/build/debug/scheme.html new file mode 100644 index 0000000..6f6c454 --- /dev/null +++ b/SQLiteDB/build/debug/scheme.html @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/SQLiteDB/main.ts b/SQLiteDB/main.ts new file mode 100644 index 0000000..ac31084 --- /dev/null +++ b/SQLiteDB/main.ts @@ -0,0 +1,34 @@ +namespace OS { + export namespace application { + interface SQLiteDBBaseConstructor{ + new(pqth: API.VFS.BaseFileHandle| string): SQLiteDBBase; + }; + interface SQLiteDBBase{ + list_tables(): Promise>, + last_insert_id(): Promise, + query(sql): Promise + }; + /** + * + * @class SQLiteDB + * @extends {BaseApplication} + */ + export class SQLiteDB extends BaseApplication { + constructor(args: AppArgumentsType[]) { + super("SQLiteDB", args); + } + main(): void { + // YOUR CODE HERE + let handle = new ((OS.API as any).SQLiteDBBase as SQLiteDBBaseConstructor)("home://tmp/test.db"); + handle.list_tables().then((list) => { + console.log(list); + if(list.indexOf("contacts") < 0) + { + handle.query("CREATE TABLE contacts (id INTEGER PRIMARY KEY,first_name TEXT NOT NULL,last_name TEXT NOT NULL,email TEXT NOT NULL UNIQUE,phone TEXT NOT NULL UNIQUE)"); + } + }); + handle.last_insert_id().then(o => console.log(o)); + } + } + } +} \ No newline at end of file diff --git a/SQLiteDB/package.json b/SQLiteDB/package.json new file mode 100644 index 0000000..063d3ce --- /dev/null +++ b/SQLiteDB/package.json @@ -0,0 +1,16 @@ +{ + "pkgname": "SQLiteDB", + "app":"SQLiteDB", + "name":"SQLiteDB", + "description":"SQLiteDB", + "info":{ + "author": "", + "email": "" + }, + "version":"0.0.1-a", + "category":"Other", + "iconclass":"fa fa-adn", + "mimes":["none"], + "dependencies":[], + "locale": {} +} \ No newline at end of file diff --git a/SQLiteDB/scheme.html b/SQLiteDB/scheme.html new file mode 100644 index 0000000..6f6c454 --- /dev/null +++ b/SQLiteDB/scheme.html @@ -0,0 +1,3 @@ + + + \ No newline at end of file