Skip to content

Commit

Permalink
Refactor code to improve readability and maintainability (#1870)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiasrw authored Jan 29, 2024
1 parent a0e34dd commit fd84d77
Show file tree
Hide file tree
Showing 15 changed files with 496 additions and 985 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"blueimp-md5": "2.19.0",
"cmdmix": "2.1.1",
"dom-storage": "2.1.0",
"esbuild": "0.19.12",
"esbuild": "0.20.0",
"eslint": "8.56.0",
"eslint-config-standard": "17.1.0",
"eslint-plugin-import": "2.29.1",
Expand Down
6 changes: 3 additions & 3 deletions src/05copyright.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
//! AlaSQL vPACKAGE_VERSION build: BUILD_VERSION | © 2014-2023 Andrey Gershun & Mathias Wulff | License: MIT
//! AlaSQL vPACKAGE_VERSION build: BUILD_VERSION | © 2014-2024 Andrey Gershun & Mathias Wulff | License: MIT
/*
@module alasql
@version PACKAGE_VERSION
AlaSQL - JavaScript SQL database
© 2014-2023 Andrey Gershun & Mathias Wulff
© 2014-2024 Andrey Gershun & Mathias Wulff
@license
The MIT License (MIT)
Copyright 2014-2023 Andrey Gershun ([email protected]) & Mathias Wulff ([email protected])
Copyright 2014-2024 Andrey Gershun ([email protected]) & Mathias Wulff ([email protected])
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
186 changes: 93 additions & 93 deletions src/10start.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,109 +18,109 @@
*/

(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
/** alasql main function */
module.exports = factory();
} else {
root.alasql = factory();
}
if (typeof define === 'function' && define.amd) {
define([], factory);
} else if (typeof exports === 'object') {
/** alasql main function */
module.exports = factory();
} else {
root.alasql = factory();
}
}(this, function () {

/**
AlaSQL - Main Alasql class
@function
@param {string|function|object} sql - SQL-statement or data object for fluent interface
@param {object} params - SQL parameters
@param {function} cb - callback function
@param {object} scope - Scope for nested queries
@return {any} - Result data object
@example
Standard sync call:
alasql('CREATE TABLE one');
Query:
var res = alasql('SELECT * FROM one');
Call with parameters:
var res = alasql('SELECT * FROM ?',[data]);
Standard async call with callback function:
alasql('SELECT * FROM ?',[data],function(res){
console.log(data);
});
Call with scope for subquery (to pass common values):
var scope = {one:{a:2,b;20}}
alasql('SELECT * FROM ? two WHERE two.a = one.a',[data],null,scope);
Call for fluent interface with data object:
alasql(data).Where(function(x){return x.a == 10}).exec();
Call for fluent interface without data object:
alasql().From(data).Where(function(x){return x.a == 10}).exec();
*/

var alasql = function(sql, params, cb, scope) {

params = params||[];

if(typeof importScripts !== 'function' && alasql.webworker) {
var id = alasql.lastid++;
alasql.buffer[id] = cb;
alasql.webworker.postMessage({id:id,sql:sql,params:params});
return;
}

if(arguments.length === 0) {
// Without arguments - Fluent interface
return new yy.Select({
columns:[new yy.Column({columnid:'*'})],
from: [new yy.ParamValue({param:0})]
});
} else if(arguments.length === 1){
// Access promise notation without using `.promise(...)`
if(sql.constructor === Array){
return alasql.promise(sql);
/**
AlaSQL - Main Alasql class
@function
@param {string|function|object} sql - SQL-statement or data object for fluent interface
@param {object} params - SQL parameters
@param {function} cb - callback function
@param {object} scope - Scope for nested queries
@return {any} - Result data object
@example
Standard sync call:
alasql('CREATE TABLE one');
Query:
var res = alasql('SELECT * FROM one');
Call with parameters:
var res = alasql('SELECT * FROM ?',[data]);
Standard async call with callback function:
alasql('SELECT * FROM ?',[data],function(res){
console.log(data);
});
Call with scope for subquery (to pass common values):
var scope = {one:{a:2,b;20}}
alasql('SELECT * FROM ? two WHERE two.a = one.a',[data],null,scope);
Call for fluent interface with data object:
alasql(data).Where(function(x){return x.a == 10}).exec();
Call for fluent interface without data object:
alasql().From(data).Where(function(x){return x.a == 10}).exec();
*/

let alasql = function (sql, params, cb, scope) {

params = params || [];

if (typeof importScripts !== 'function' && alasql.webworker) {
var id = alasql.lastid++;
alasql.buffer[id] = cb;
alasql.webworker.postMessage({ id: id, sql: sql, params: params });
return;
}
}
// Avoid setting params if not needed even with callback
if(typeof params === 'function'){
scope = cb;
cb = params;
params = [];
}

if(typeof params !== 'object'){
if (arguments.length === 0) {
// Without arguments - Fluent interface
return new yy.Select({
columns: [new yy.Column({ columnid: '*' })],
from: [new yy.ParamValue({ param: 0 })]
});
} else if (arguments.length === 1) {
// Access promise notation without using `.promise(...)`
if (sql.constructor === Array) {
return alasql.promise(sql);
}
}
// Avoid setting params if not needed even with callback
if (typeof params === 'function') {
scope = cb;
cb = params;
params = [];
}

if (typeof params !== 'object') {
params = [params];
}
}

// Standard interface
// alasql('#sql');
if(typeof sql === 'string' && sql[0]==='#' && typeof document === "object") {
sql = document.querySelector(sql).textContent;
} else if(typeof sql === 'object' && sql instanceof HTMLElement) {
sql = sql.textContent;
} else if(typeof sql === 'function') {
// to run multiline functions
sql = sql.toString();
sql = (/\/\*([\S\s]+)\*\//m.exec(sql) || ['','Function given as SQL. Plese Provide SQL string or have a /* ... */ syle comment with SQL in the function.'])[1];
}
// Run SQL
return alasql.exec(sql, params, cb, scope);
};
// Standard interface
// alasql('#sql');
if (typeof sql === 'string' && sql[0] === '#' && typeof document === "object") {
sql = document.querySelector(sql).textContent;
} else if (typeof sql === 'object' && sql instanceof HTMLElement) {
sql = sql.textContent;
} else if (typeof sql === 'function') {
// to run multiline functions
sql = sql.toString();
sql = (/\/\*([\S\s]+)\*\//m.exec(sql) || ['', 'Function given as SQL. Plese Provide SQL string or have a /* ... */ syle comment with SQL in the function.'])[1];
}
// Run SQL
return alasql.exec(sql, params, cb, scope);
};

/**
Current version of alasql
@constant {string}
*/
alasql.version = 'PACKAGE_VERSION';
alasql.build = 'BUILD_VERSION';
/**
Current version of alasql
@constant {string}
*/
alasql.version = 'PACKAGE_VERSION';
alasql.build = 'BUILD_VERSION';

/**
Debug flag
@type {boolean}
*/
alasql.debug = undefined; // Initial debug variable
/**
Debug flag
@type {boolean}
*/
alasql.debug = undefined; // Initial debug variable


/*only-for-browser/*
var require = function(){return null}; // as alasqlparser.js is generated, we can not "remove" references to
var require = function(){return null}; // as alasqlparser.js is generated, we can not "remove" references to
var __dirname = '';
//*/
103 changes: 2 additions & 101 deletions src/12pretty.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/**
Pretty flag - nice HTML output or standard text without any tags
Pretty flag - nice HTML output or standard text without any tags
@type {boolean}
*/

Expand All @@ -15,7 +15,7 @@ alasql.prettyflag = false;
@function
@param {string} sql SQL statement
@param {boolean} flag value
@return {string} HTML or text string with pretty output
@return {string} HTML or text string with pretty output
*/

alasql.pretty = function (sql, flag) {
Expand All @@ -25,102 +25,3 @@ alasql.pretty = function (sql, flag) {
alasql.prettyflag = pf;
return s;
};

/*/*
Pretty keyword
@param {string} s Keyword
@return {string} pretty keyword
* /
function K(s){
console.log('K')
if(alasql.prettyflag) {
return '<b style="color:blue">'+s.toUpperCase()+'</b>';
} else {
return s;
}
}
/**
Pretty
@param {string}
@return {string} pretty keyword
* /
function P(s){
console.log('P')
if(alasql.prettyflag) {
return '<span style="color:green">'+s+'</span>';
} else {
return s;
}
}
/**
Pretty
@param {string}
@return {string} pretty keyword
* /
function L(s){
console.log('L')
if(alasql.prettyflag) {
return '<span style="color:red">'+s+'</span>';
} else {
return s;
}
}
/**
Pretty number
@param {string | number} s number
@return {string} pretty number
* /
function N(s){
console.log('N')
if(alasql.prettyflag) {
return '<span style="color:green">'+s+'</span>';
} else {
return s;
}
}
/**
Pretty string
@param {string} s string
@return {string} pretty string
* /
function S(s){
console.log('S')
if(alasql.prettyflag) {
return '<span style="color:green">'+s+'</span>';
} else {
return s;
}
}
/**
Pretty new line
@return {string} HTML new line character
* /
function NL(){
console.log('NL')
if(alasql.prettyflag) {
return '<br/>';
} else {
return ' '; // '\n'
}
}
/**
Pretty ident
@return {string} HTML ident
* /
function ID(){
console.log('ID')
if(alasql.prettyflag) {
return '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
} else {
return ''; //' ';
}
}
*/
Loading

0 comments on commit fd84d77

Please sign in to comment.