Update index.js

This commit is contained in:
Nikiroy78 2022-08-06 14:01:39 +03:00 committed by GitHub
parent 45d0825029
commit 87f82457ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

405
index.js
View File

@ -1,4 +1,3 @@
<<<<<<< HEAD
const mysql = require('./mysql'); const mysql = require('./mysql');
const sqlite = require('./sqlite3'); const sqlite = require('./sqlite3');
const sqleasy_tools = require('./SQLEasyTools'); const sqleasy_tools = require('./SQLEasyTools');
@ -22,205 +21,205 @@ module.exports = {
tools: { tools: {
get_from_key: sqleasy_tools.get_from_key get_from_key: sqleasy_tools.get_from_key
} }
} }
======= =======
const sqlite3 = require('better-sqlite3'); const sqlite3 = require('better-sqlite3');
class global_data_Parser{ class global_data_Parser{
getTable(err, rows){ getTable(err, rows){
this.rows = rows; this.rows = rows;
this.err = err; this.err = err;
// console.log('globalParser:', this.rows); // console.log('globalParser:', this.rows);
} }
} }
class SQLEasy_error extends Error { class SQLEasy_error extends Error {
constructor(code='GENERIC', status=500, ...params) { constructor(code='GENERIC', status=500, ...params) {
super(...params); super(...params);
if(Error.captureStackTrace) { if(Error.captureStackTrace) {
Error.captureStackTrace(this, 'SQLEasy_error'); Error.captureStackTrace(this, 'SQLEasy_error');
} }
this.code = code; this.code = code;
this.status = status; this.status = status;
} }
} }
function get_from_key (db_data, conditions) { function get_from_key (db_data, conditions) {
conditions = conditions.filter(i => i != {}); conditions = conditions.filter(i => i != {});
if (conditions == [] || !conditions) return db_data; if (conditions == [] || !conditions) return db_data;
let item = new Object(); let item = new Object();
let bool_conditions = new Array(); let bool_conditions = new Array();
let condition_item = new Object(); let condition_item = new Object();
let int_condition = 1; let int_condition = 1;
let out_objs = new Array(); let out_objs = new Array();
for (let i in db_data) { for (let i in db_data) {
bool_conditions = new Array(); bool_conditions = new Array();
item = db_data[i]; item = db_data[i];
for (let index in conditions) { for (let index in conditions) {
int_condition = 1; int_condition = 1;
condition_item = conditions[index]; condition_item = conditions[index];
for (key in condition_item) { for (key in condition_item) {
int_condition *= Number(item[key] == condition_item[key]); int_condition *= Number(item[key] == condition_item[key]);
} }
bool_conditions.push(int_condition); bool_conditions.push(int_condition);
} }
if (bool_conditions.reduce((a, b) => Boolean(a + b))) out_objs.push(item); if (bool_conditions.reduce((a, b) => Boolean(a + b))) out_objs.push(item);
} }
return out_objs; return out_objs;
} }
class database { class database {
constructor(path, warning=true){ constructor(path, warning=true){
if (warning) console.log("You use LEGACY version of SQLEasy library (v. 0.9.1). In new version structure was been edited. Show more in: https://www.npmjs.com/package/sql-easy-lib"); if (warning) console.log("You use LEGACY version of SQLEasy library (v. 0.9.1). In new version structure was been edited. Show more in: https://www.npmjs.com/package/sql-easy-lib");
this.PATH = path; this.PATH = path;
// this.db = new sqlite3.Database(this.PATH); // async - heresy! // this.db = new sqlite3.Database(this.PATH); // async - heresy!
this.db = new sqlite3(this.PATH); this.db = new sqlite3(this.PATH);
// Создаём "функции-двойники" // Создаём "функции-двойники"
this.getTable = this.getBase; this.getTable = this.getBase;
this.get = this.getTable; this.get = this.getTable;
this.select = this.get; this.select = this.get;
this.set = this.setItem; this.set = this.setItem;
this.insert = this.add; this.insert = this.add;
this.update = this.set; this.update = this.set;
this.remove = this.del; this.remove = this.del;
this.pop = this.del; this.pop = this.del;
this.exec = this.execute; this.exec = this.execute;
} }
ToString(value) { ToString(value) {
return typeof(value) === 'string' ? '\'' + value + '\'' : value; return typeof(value) === 'string' ? '\'' + value + '\'' : value;
} }
getIndex (table, indexColumn, index_starts = 0) { getIndex (table, indexColumn, index_starts = 0) {
// return this.get(table).length; // return this.get(table).length;
let tableData = this.get(table).sort((a, b) => a[indexColumn] - b[indexColumn]); let tableData = this.get(table).sort((a, b) => a[indexColumn] - b[indexColumn]);
let index = index_starts - 1; let index = index_starts - 1;
for (let i in tableData) { for (let i in tableData) {
if (index_starts <= i) { if (index_starts <= i) {
if (i != tableData[i][indexColumn]) return i; if (i != tableData[i][indexColumn]) return i;
index = i; index = i;
} }
} }
return Number(index) + 1; return Number(index) + 1;
} }
execute(SQLRequest) { execute(SQLRequest) {
try { try {
return this.db.prepare(SQLRequest).all(); return this.db.prepare(SQLRequest).all();
} catch(err) { } catch(err) {
if(err.message.indexOf('run()') !== -1) { if(err.message.indexOf('run()') !== -1) {
try { try {
this.db.prepare(SQLRequest).run(); this.db.prepare(SQLRequest).run();
return null; return null;
} catch(err) { } catch(err) {
throw new Error(`SQLEasy error: ${err.message}`); throw new Error(`SQLEasy error: ${err.message}`);
} }
} }
else{ else{
throw new Error(`SQLEasy error: ${err.message}`); throw new Error(`SQLEasy error: ${err.message}`);
} }
} }
} }
getBase(table, condition=null, keys='*') { getBase(table, condition=null, keys='*') {
let SQLRequest = `SELECT ${keys} FROM ${table}`; let SQLRequest = `SELECT ${keys} FROM ${table}`;
if(condition !== null){ if(condition !== null){
let orBlock = new Array(); let orBlock = new Array();
for(let i = 0; i < condition.length; i++){ for(let i = 0; i < condition.length; i++){
let andBlock = new Array(); let andBlock = new Array();
for(let key in condition[i]){ for(let key in condition[i]){
andBlock.push(`${key}=${this.ToString(condition[i][key])}`); andBlock.push(`${key}=${this.ToString(condition[i][key])}`);
} }
orBlock.push(`(${andBlock.join(' AND ')})`); orBlock.push(`(${andBlock.join(' AND ')})`);
} }
SQLRequest = `${SQLRequest} WHERE ${orBlock.join(' OR ')}`; SQLRequest = `${SQLRequest} WHERE ${orBlock.join(' OR ')}`;
} }
// console.log(SQLRequest); // Убрать после тестов!! // console.log(SQLRequest); // Убрать после тестов!!
try { try {
let rows = this.db.prepare(SQLRequest).all(); let rows = this.db.prepare(SQLRequest).all();
if(rows !== null & rows !== undefined) return rows; if(rows !== null & rows !== undefined) return rows;
else throw new Error('SQLEasy error: Rows given null.'); else throw new Error('SQLEasy error: Rows given null.');
} catch(err) { } catch(err) {
if(err.message.indexOf('no such table') !== -1){ if(err.message.indexOf('no such table') !== -1){
throw new Error('SQLEasy error: this table not founded.'); throw new Error('SQLEasy error: this table not founded.');
} }
else throw new Error(`SQLEasy error: ${err.message}`); else throw new Error(`SQLEasy error: ${err.message}`);
} }
} }
add(table, addvArray, ignore=false){ add(table, addvArray, ignore=false){
this.getBase(table); this.getBase(table);
let SQLRequest = new Array(); let SQLRequest = new Array();
let setting_values = new Array(); let setting_values = new Array();
for(let i = 0; i < addvArray.length; i++) { for(let i = 0; i < addvArray.length; i++) {
let addObject = addvArray[i]; let addObject = addvArray[i];
let keys = new Array(); let keys = new Array();
let values = new Array(); let values = new Array();
setting_values = new Array(); setting_values = new Array();
for(let key in addObject){ for(let key in addObject){
keys.push(key); keys.push(key);
setting_values.push(addObject[key]); setting_values.push(addObject[key]);
values.push('?'); values.push('?');
} }
let op = 'INSERT'; let op = 'INSERT';
if(ignore) op = 'INSERT OR IGNORE'; if(ignore) op = 'INSERT OR IGNORE';
SQLRequest.push(`${op} INTO ${table} (${keys.join(', ')}) VALUES (${values.join(', ')});`); SQLRequest.push(`${op} INTO ${table} (${keys.join(', ')}) VALUES (${values.join(', ')});`);
} }
SQLRequest = SQLRequest.join('\n'); SQLRequest = SQLRequest.join('\n');
try{ try{
this.db.prepare(SQLRequest).run(setting_values); this.db.prepare(SQLRequest).run(setting_values);
} catch(err){ } catch(err){
if (ignore) throw new Error(`SQLEasy error: ${err.message}`); if (ignore) throw new Error(`SQLEasy error: ${err.message}`);
else this.add(table, addvArray, true); else this.add(table, addvArray, true);
} }
} }
del(table, index){ del(table, index){
this.get(table); this.get(table);
let equal_req = ''; let equal_req = '';
for(let key in index) { for(let key in index) {
equal_req = `${key} = ${this.ToString(index[key])}`; equal_req = `${key} = ${this.ToString(index[key])}`;
break; break;
} }
let SQLRequest = `DELETE FROM ${table} WHERE ${equal_req}`; let SQLRequest = `DELETE FROM ${table} WHERE ${equal_req}`;
try { try {
this.db.prepare(SQLRequest).run(); this.db.prepare(SQLRequest).run();
} catch(err) { } catch(err) {
throw new Error(`SQLEasy error: ${err.message}`); throw new Error(`SQLEasy error: ${err.message}`);
} }
} }
setItem(table, index, values){ setItem(table, index, values){
this.getBase(table); this.getBase(table);
let equal_index = ''; let equal_index = '';
let equal_values = ''; let equal_values = '';
let value_array = new Array(); let value_array = new Array();
for(let key in index){ for(let key in index){
equal_index = `${key} = ${this.ToString(index[key])}`; equal_index = `${key} = ${this.ToString(index[key])}`;
// equal_index = `${key} = ?`; // equal_index = `${key} = ?`;
break; break;
} }
for(let key in values){ for(let key in values){
// equal_values = `${key} = ${this.ToString(values[key])}`; // equal_values = `${key} = ${this.ToString(values[key])}`;
equal_values = `${key} = ?`; equal_values = `${key} = ?`;
value_array.push(values[key]); value_array.push(values[key]);
break; break;
} }
let SQLRequest = `UPDATE ${table} SET ${equal_values} WHERE ${equal_index}`; let SQLRequest = `UPDATE ${table} SET ${equal_values} WHERE ${equal_index}`;
try { try {
// this.db.prepare(SQLRequest).get(value_array).run(); // this.db.prepare(SQLRequest).get(value_array).run();
this.db.prepare(SQLRequest).run(value_array); this.db.prepare(SQLRequest).run(value_array);
} catch(err) { } catch(err) {
throw new Error(`SQLEasy error: ${err.message}`); throw new Error(`SQLEasy error: ${err.message}`);
} }
} }
} }
module.exports = { module.exports = {
database: database, database: database,
get_from_key: get_from_key get_from_key: get_from_key
}; };
>>>>>>> 631660a213570390163bbcd41828a472dbf95841 >>>>>>> 631660a213570390163bbcd41828a472dbf95841