Add default code of my better module, fucking slave, suck my dick
This commit is contained in:
parent
17f12db3e2
commit
b21860c73f
109
database.js
Normal file
109
database.js
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
class DataBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
static sqlite3 = require('sqlite3').verbose();
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
static database = new this.sqlite3.Database('./database/database.db');
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {String[]} keys
|
||||||
|
* @param {String} table
|
||||||
|
* @param {String} condition
|
||||||
|
* @param {Boolean} some
|
||||||
|
* @param {Function()} callback
|
||||||
|
*/
|
||||||
|
static getData(keys, table, condition = '', some = true, callback = () => {}) {
|
||||||
|
let sql = 'SELECT ';
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
sql += keys[i] === '*' ? keys[i] : '`' + keys[i] + '`';
|
||||||
|
if (keys.length > i + 1)
|
||||||
|
sql += ', ';
|
||||||
|
}
|
||||||
|
sql += ' FROM `' + table + '` ' + condition;
|
||||||
|
|
||||||
|
if (some)
|
||||||
|
this.database.all(sql, (err, rows) => {
|
||||||
|
callback(err, rows);
|
||||||
|
});
|
||||||
|
else
|
||||||
|
this.database.get(sql, (err, row) => {
|
||||||
|
callback(err, row);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {String[]} keys
|
||||||
|
* @param {Values[]} values
|
||||||
|
* @param {String} table
|
||||||
|
* @param {String} condition
|
||||||
|
* @param {Function()} callback
|
||||||
|
*/
|
||||||
|
static updateData(keys, values, table, condition, callback = () => {}) {
|
||||||
|
let sql = 'UPDATE `' + table + '` SET ';
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
sql += '`' + keys[i] + '` = ' + this.ToString(values[i]);
|
||||||
|
if (keys.length > i + 1)
|
||||||
|
sql += ', ';
|
||||||
|
}
|
||||||
|
sql += ' ' + condition;
|
||||||
|
|
||||||
|
this.database.run(sql, (err) => {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {String[]} keys
|
||||||
|
* @param {String[]} values
|
||||||
|
* @param {String} table
|
||||||
|
* @param {Function()} callback
|
||||||
|
*/
|
||||||
|
static insertData(keys, values, table, callback = () => {}) {
|
||||||
|
let sql = 'INSERT INTO `' + table + '` (';
|
||||||
|
for (let i = 0; i < keys.length; i++) {
|
||||||
|
sql += '`' + keys[i] + '`';
|
||||||
|
if (keys.length > i + 1)
|
||||||
|
sql += ', ';
|
||||||
|
}
|
||||||
|
sql += ') VALUES (';
|
||||||
|
for (let i = 0; i < values.length; i++) {
|
||||||
|
sql += this.ToString(values[i]);
|
||||||
|
if (values.length > i + 1)
|
||||||
|
sql += ', ';
|
||||||
|
}
|
||||||
|
sql += ')';
|
||||||
|
|
||||||
|
this.database.run(sql, (err) => {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param {String} table
|
||||||
|
* @param {String} condition
|
||||||
|
* @param {Function()} callback
|
||||||
|
*/
|
||||||
|
static deleteData(table, condition = '', callback = () => {}) {
|
||||||
|
this.database.run('DELETE FROM `' + table + '` ' + condition, (err) => {
|
||||||
|
callback(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static ToString(value) {
|
||||||
|
return typeof(value) === 'string' ? '\'' + value + '\'' : value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
database: DataBase
|
||||||
|
};
|
148
index.js
Normal file
148
index.js
Normal file
@ -0,0 +1,148 @@
|
|||||||
|
// const sqlite3 = require('sqlite3').verbose(); // Пошли в пизду со своим ассинхроном, ублюдки!
|
||||||
|
const sqlite3 = require('better-sqlite3');
|
||||||
|
|
||||||
|
|
||||||
|
class global_data_Parser{
|
||||||
|
getTable(err, rows){
|
||||||
|
this.rows = rows;
|
||||||
|
this.err = err;
|
||||||
|
// console.log('globalParser:', this.rows);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class SQLEasy_error extends Error {
|
||||||
|
constructor(code='GENERIC', status=500, ...params) {
|
||||||
|
super(...params);
|
||||||
|
if(Error.captureStackTrace) {
|
||||||
|
Error.captureStackTrace(this, 'SQLEasy_error');
|
||||||
|
}
|
||||||
|
this.code = code;
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class database {
|
||||||
|
constructor(path){
|
||||||
|
this.PATH = path;
|
||||||
|
// this.db = new sqlite3.Database(this.PATH); // async - heresy!
|
||||||
|
this.db = new sqlite3(this.PATH);
|
||||||
|
// Создаём "функции-двойники"
|
||||||
|
this.getTable = this.getBase;
|
||||||
|
this.get = this.getTable;
|
||||||
|
this.select = this.get;
|
||||||
|
this.set = this.setItem;
|
||||||
|
this.add = this.insert;
|
||||||
|
this.update = this.set;
|
||||||
|
this.remove = this.del;
|
||||||
|
this.pop = this.del;
|
||||||
|
this.exec = this.execute;
|
||||||
|
}
|
||||||
|
ToString(value) {
|
||||||
|
return typeof(value) === 'string' ? '\'' + value + '\'' : value;
|
||||||
|
}
|
||||||
|
execute(SQLRequest) {
|
||||||
|
try {
|
||||||
|
return this.db.prepare(SQLRequest).all();
|
||||||
|
} catch(err) {
|
||||||
|
if(err.message.indexOf('run()') !== -1) {
|
||||||
|
try {
|
||||||
|
this.db.prepare(SQLRequest).run();
|
||||||
|
return null;
|
||||||
|
} catch(err) {
|
||||||
|
throw new Error(`SQLEasy error: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
throw new Error(`SQLEasy error: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
getBase(table, condition=null, keys='*') {
|
||||||
|
let SQLRequest = `SELECT ${keys} FROM ${table}`;
|
||||||
|
if(condition !== null){
|
||||||
|
let orBlock = new Array();
|
||||||
|
for(let i = 0; i < condition.length; i++){
|
||||||
|
let andBlock = new Array();
|
||||||
|
for(let key in condition[i]){
|
||||||
|
andBlock.push(`${key}=${this.ToString(condition[i][key])}`);
|
||||||
|
}
|
||||||
|
orBlock.push(`(${andBlock.join(' AND ')})`);
|
||||||
|
}
|
||||||
|
SQLRequest = `${SQLRequest} WHERE ${orBlock.join(' OR ')}`;
|
||||||
|
}
|
||||||
|
// console.log(SQLRequest); // Убрать после тестов!!
|
||||||
|
try {
|
||||||
|
let rows = this.db.prepare(SQLRequest).all();
|
||||||
|
if(rows !== null & rows !== undefined) return rows;
|
||||||
|
else throw new Error('SQLEasy error: Rows given null.');
|
||||||
|
} catch(err) {
|
||||||
|
if(err.message.indexOf('no such table') !== -1){
|
||||||
|
throw new Error('SQLEasy error: this table not founded.');
|
||||||
|
}
|
||||||
|
else throw new Error(`SQLEasy error: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
add(table, addvArray, ignore=false){
|
||||||
|
this.getBase(table);
|
||||||
|
let SQLRequest = new Array();
|
||||||
|
for(let i = 0; i < addvArray.length; i++) {
|
||||||
|
let addObject = addvArray[i];
|
||||||
|
let keys = new Array();
|
||||||
|
let values = new Array();
|
||||||
|
for(let key in addObject){
|
||||||
|
keys.push(key);
|
||||||
|
values.push(this.ToString(addObject[key]));
|
||||||
|
}
|
||||||
|
let op = 'INSERT';
|
||||||
|
if(ignore) op = 'INSERT OR IGNORE';
|
||||||
|
SQLRequest.push(`${op} INTO ${table} (${keys.join(', ')}) VALUES (${values.join(', ')});`);
|
||||||
|
}
|
||||||
|
SQLRequest = SQLRequest.join('\n');
|
||||||
|
try{
|
||||||
|
this.db.prepare(SQLRequest).run();
|
||||||
|
} catch(err){
|
||||||
|
if(ignore) throw new Error(`SQLEasy error: ${err.message}`);
|
||||||
|
else this.add(table, addvArray, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
del(table, index){
|
||||||
|
this.get(table);
|
||||||
|
let equal_req = '';
|
||||||
|
for(let key in index) {
|
||||||
|
equal_req = `${key} = ${this.ToString(index[key])}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let SQLRequest = `DELETE FROM ${table} WHERE ${equal_req}`;
|
||||||
|
try {
|
||||||
|
this.db.prepare(SQLRequest).run();
|
||||||
|
} catch(err) {
|
||||||
|
throw new Error(`SQLEasy error: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
setItem(table, index, values){
|
||||||
|
this.getBase(table);
|
||||||
|
let equal_index = '';
|
||||||
|
let equal_values = '';
|
||||||
|
for(let key in index){
|
||||||
|
equal_index = `${key} = ${this.ToString(index[key])}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
for(let key in values){
|
||||||
|
equal_values = `${key} = ${this.ToString(values[key])}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
let SQLRequest = `UPDATE ${table} SET ${equal_values} WHERE ${equal_index}`;
|
||||||
|
try {
|
||||||
|
this.db.prepare(SQLRequest).run();
|
||||||
|
} catch(err) {
|
||||||
|
throw new Error(`SQLEasy error: ${err.message}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
database: database
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user