git.schokokeks.org
Repositories
Help
Report an Issue
fs-words.git
Code
Commits
Branches
Tags
Suche
Strukturansicht:
faf7e12
Branches
Tags
develop-client_server
master
typescript
fs-words.git
server
source
helpers
database.ts
[mov] server
Christian Fraß
commited
faf7e12
at 2021-03-08 23:05:42
database.ts
Blame
History
Raw
// import {int} from './types'; namespace helpers.database { /** */ type type_query = { sql_template : string; arguments : {[name : string] : any}; }; /** */ function resolve_path ( path_raw : string ) : string { return ("sql/" + path_raw.replace(new RegExp("\\.", "g"), "/") + ".sql"); } /** */ function fix_query ( query : type_query, ) : type_query { return { "sql_template": query.sql_template.replace ( new RegExp(":([0-9a-zA-Z_-]+)", "g"), "$$$1" ), "arguments": ( () => { let arguments_ : {[name : string] : any} = {}; Object.keys(query.arguments).forEach ( (name) => {arguments_["$" + name] = query.arguments[name];} ); return arguments_; } ) (), }; } /** */ function query_do_free_ ( query : type_query ) : Promise<void> { const nm_sqlite3 = require("sqlite3"); const query_fixed : type_query = fix_query(query); // console.info("---\n" + query_fixed.sql_template + "\n" + JSON.stringify(query_fixed.arguments) + "\n---\n"); return ( new Promise<void> ( (resolve, reject) => { const database/* : nm_sqlite3.Database*/ = new nm_sqlite3.Database("data.sqlite"); database.run ( query_fixed.sql_template, query_fixed.arguments, function (error) { if (error) { reject(error); } else { resolve(undefined); } } ); database.close(); } ) ); } /** */ function query_get_free_ ( query : type_query ) : Promise<Array<type_row>> { const nm_sqlite3 = require("sqlite3"); const query_fixed : type_query = fix_query(query); // console.info("---\n" + query_fixed.sql_template + "\n" + JSON.stringify(query_fixed.arguments) + "\n---\n"); return ( new Promise<Array<type_row>> ( (resolve, reject) => { const database/* : nm_sqlite3.Database*/ = new nm_sqlite3.Database("data.sqlite"); database.all ( query_fixed.sql_template, query_fixed.arguments, function (error, rows) { // console.info({error,rows}); if (error) { reject(error); } else { resolve(rows); } } ); database.close(); } ) ); } /** */ function query_put_free_ ( query : type_query ) : Promise<int> { const nm_sqlite3 = require("sqlite3"); const query_fixed : type_query = fix_query(query); // console.info("---\n" + query_fixed.sql_template + "\n" + JSON.stringify(query_fixed.arguments) + "\n---\n"); return ( new Promise<int> ( (resolve, reject) => { const database/* : nm_sqlite3.Database*/ = new nm_sqlite3.Database("data.sqlite"); database.run ( query_fixed.sql_template, query_fixed.arguments, function (error) { if (error) { reject(error); } else { resolve(this.lastID); } } ); database.close(); } ) ); } /** */ export function query_do_free ( sql_template : string, arguments_ : {[name : string] : any} = {} ) : Promise<void> { return query_do_free_({"sql_template": sql_template, "arguments": arguments_}); } /** */ export function query_get_free ( sql_template : string, arguments_ : {[name : string] : any} = {} ) : Promise<Array<type_row>> { return query_get_free_({"sql_template": sql_template, "arguments": arguments_}); } /** */ export function query_put_free ( sql_template : string, arguments_ : {[name : string] : any} = {} ) : Promise<int> { return query_put_free_({"sql_template": sql_template, "arguments": arguments_}); } /** */ export function query_do_named ( name : string, arguments_ : {[name : string] : any} = {} ) : Promise<void> { return query_do_free ( helpers.file.read(resolve_path(name)), arguments_ ); } /** */ export function query_get_named ( name : string, arguments_ : {[name : string] : any} = {} ) : Promise<Array<type_row>> { return query_get_free ( helpers.file.read(resolve_path(name)), arguments_ ); } /** */ export function query_put_named ( name : string, arguments_ : {[name : string] : any} = {} ) : Promise<int> { return query_put_free ( helpers.file.read(resolve_path(name)), arguments_ ); } }