UNPKG

node-qsb2

Version:

Node.js Query String Builder (QSB) for MySQL/MariaDB, Forked from node-qsb

447 lines (399 loc) 12.6 kB
"use strict"; /** * MySQL Query string builder Ver.2 * forked from node-qsb */ class qsb { #comm = ""; #get = ""; #cols = ""; #values = ""; #set = ""; #from = ""; #join = ""; #where = ""; #limit = ""; #order = ""; #group = ""; #duplicate = ""; #qs = "SELECT 'QUERY NOT BUILD'"; constructor() {} #esc(str) { // added by basagee@gmail.com. 2019.08.27 // support set NULL value if (str === null || typeof str === "undefined") { return "NULL"; } if (typeof str === "number" || typeof str === "boolean") { return str; } else { if (typeof str !== "string") { str = "" + str; } str = str.replace(/[\0\n\r\b\t\\\'\"\x1a]/g, function (res) { switch (res) { case "\0": return "\\0"; case "\n": return "\\n"; case "\r": return "\\r"; case "\b": return "\\b"; case "\t": return "\\t"; case "\x1a": return "\\Z"; default: return "\\" + res; } }); } return "'" + str + "'"; } //apply Esapce Character #bq(str) { if (str.indexOf(".") > 0) { str = str.split("."); return "`" + str[0] + "`.`" + str[1] + "`"; } return "`" + str + "`"; } //add Back Quote #joinFunc(table, short, joinType = null, onClause = null, isQuery = false) { this.#join += "\n"; if (joinType) { this.#join += joinType + " JOIN "; } else { this.#join += "JOIN "; } this.#join += !isQuery ? this.#bq(table) : "(" + table + ")"; if ( short !== null && typeof short !== "undefined" && short.trim().length > 0 ) { this.#join += " AS " + this.#bq(short); } // basagee@gmail.com if (onClause !== null && typeof onClause !== "undefined") { if (typeof onClause === "object" && Array.isArray(onClause)) { this.#join += " ON "; for (var i = 0; i < onClause.length; i++) { if (i > 0) { this.#join += " AND "; } this.#join += onClause[i]; } } else { this.#join += " ON " + onClause; } } return this; } #makeWhereClause(column, op, value) { var str = ""; if (op.toLowerCase() === "in" || op.toLowerCase() === "not in") { if (typeof value === "string") { return this.#bq(column) + " " + op + " (" + value + ")"; } else if (typeof value === "object" && Array.isArray(value)) { if (value.length === 0) { throw new Error("Value is empty array"); return; } var value2 = "("; for (var i = 0; i < value.length; i++) { value2 += this.#esc(value[i]); if (i < value.length - 1) { value2 += ", "; } } value2 += ")"; return this.#bq(column) + " " + op + " " + value2; } } else if (op.toLowerCase() === "between") { return this.#bq(column) + " " + op + " " + value; } else if ( op.toLowerCase() === "is null" || op.toLowerCase() === "is not null" ) { return this.#bq(column) + " " + op; } else { return this.#bq(column) + " " + op + " " + this.#esc(value); } } forceQuery(query) { this.#qs = query; return this; } select(from, short = null, isQuery = false) { if (this.#from.length !== 0) { this.#from += ", "; } if (this.#from.length === 0) { this.#from = "FROM "; } this.#comm = "SELECT "; if (isQuery) { this.#from += "(" + from + ")"; } else { this.#from += this.#bq(from); } if (short !== null && typeof short !== "undefined") { this.#from += " AS " + this.#bq(short); } return this; } update(table, short = null) { this.#comm = "UPDATE "; this.#from = this.#bq(table); // 예약어 들과 중복되는 필드이름이 있는 경우, 필요할 수도 있다. if (short !== null && typeof short !== "undefined") { this.#from += " AS " + this.#bq(short); } return this; } insert(table) { this.#comm = "INSERT "; this.#from = "INTO " + this.#bq(table); return this; } delete(table) { this.#comm = "DELETE "; this.#from = "FROM " + this.#bq(table); return this; } leftJoin(table, short = null, onClause = null, isQuery = false) { return this.#joinFunc(table, short, "LEFT", onClause, isQuery); } rightJoin(table, short = null, onClause = null, isQuery = false) { return this.#joinFunc(table, short, "RIGHT", onClause, isQuery); } join(table, short = null, onClause = null, isQuery = false) { return this.#joinFunc(table, short, null, onClause, isQuery); } values(cols, values) { var i; this.#cols += "("; for (i = 0; i < cols.length; i++) { this.#cols += this.#bq(cols[i]); if (i < cols.length - 1) { this.#cols += ", "; } } this.#cols += ")"; this.#values += " VALUES ("; for (i = 0; i < values.length; i++) { this.#values += this.#esc(values[i]); if (i < values.length - 1) { this.#values += ", "; } } this.#values += ")"; return this; } addValues(values) { this.#values += ",("; for (var i = 0; i < values.length; i++) { this.#values += this.#esc(values[i]); if (i < values.length - 1) { this.#values += ", "; } } this.#values += ")"; return this; } get(get, alias = null, isFunc = false) { if (this.#get.length !== 0) { this.#get += ",\n "; } this.#get += isFunc ? get : this.#bq(get); // add 'alias' from basagee@gmail.com, 2019.08.27 if (alias !== null && typeof alias !== "undefined") { this.#get += " AS " + this.#bq(alias); } return this; } getString(str) { if (this.#get.length !== 0) { this.#get += ",\n "; } this.#get += str; return this; } set(set, value) { if (this.#set.length !== 0) { this.#set += ",\n "; } else { this.#set += " \nSET "; } this.#set += this.#bq(set) + " = " + this.#esc(value); return this; } where(a, b, c = null) { if (this.#where.length !== 0) { this.#where += " \n AND "; } else { this.#where += " \nWHERE "; } this.#where += this.#makeWhereClause(a, b, c); return this; } whereOr(a, b, c = null) { if (this.#where.length !== 0) { this.#where += "\n OR "; } else { this.#where += "\nWHERE "; } this.#where += this.#makeWhereClause(a, b, c); return this; } whereString(str) { if (this.#where.length !== 0) { this.#where += "\n AND "; } else { this.#where += "\nWHERE "; } this.#where += str; return this; } whereOrString(str) { if (this.#where.length !== 0) { this.#where += "\n OR "; } else { this.#where += "\nWHERE "; } this.#where += str; return this; } limit(a, b = null) { if (this.#limit.length === 0) { this.#limit += "\nLIMIT "; } if (!b) { this.#limit += a; } else { this.#limit += a + ", " + b; } return this; } orderBy(col, sort) { if (this.#order.length !== 0) { this.#order += ", "; } if (this.#order.length === 0) { this.#order += " \nORDER BY "; } this.#order += this.#bq(col) + " " + sort; return this; } groupBy(col) { if (this.#group.length !== 0) { this.#group += ", " + this.#bq(col); } else { this.#group += " \nGROUP BY " + this.#bq(col); } return this; } duplicate = (cols) => { if (cols === null || typeof cols === "undefined" || cols.length === 0) { return this; } if (this.#duplicate.length !== 0) { this.#duplicate += ", "; } if (this.#duplicate.length === 0) { this.#duplicate += " \nON DUPLICATE KEY UPDATE \n"; } for (var i = 0; i < cols.length; i++) { this.#duplicate += " " + this.#bq(cols[i]) + "=VALUES(" + this.#bq(cols[i]) + ")"; if (i < cols.length - 1) { this.#duplicate += ", \n"; } else { // last values this.#duplicate += "\n"; } } return this; }; duplicateWithValues(cols, vals) { if (cols === null || typeof cols === "undefined" || cols.length === 0) { return this; } if (this.#duplicate.length !== 0) { this.#duplicate += ", "; } if (this.#duplicate.length === 0) { this.#duplicate += " \nON DUPLICATE KEY UPDATE \n"; } for (var i = 0; i < cols.length; i++) { this.#duplicate += " " + this.#bq(cols[i]) + "=" + this.#esc(vals[i]); if (i < cols.length - 1) { this.#duplicate += ", \n"; } else { // last values this.#duplicate += "\n"; } } return this; } build(semicolon = true) { if (this.#comm === "SELECT ") { this.#qs = this.#comm + (this.#get.length === 0 ? "*" : this.#get) + "\n" + "" + this.#from + this.#join + this.#where + this.#group + this.#order + this.#limit; if (semicolon) { this.#qs += ";"; } } else if (this.#comm === "UPDATE ") { this.#qs = this.#comm + this.#from + this.#set + this.#where; if (semicolon) { this.#qs += ";"; } } else if (this.#comm === "INSERT ") { this.#qs = this.#comm + // " " + this.#from + "\n " + this.#cols + "\n " + this.#values + "\n " + this.#duplicate; if (semicolon) { this.#qs += ";"; } } else if (this.#comm === "DELETE ") { this.#qs = this.#comm + " " + this.#from + this.#where; if (semicolon) { this.#qs += ";"; } } return this; } printObject() { console.log(this); return this; } printString() { console.log(this.#qs); return this; } returnString() { return this.#qs; } } module.exports = qsb;