Browse Source

refactor : move independent methods in xsql to data.helper

pull/13/head
oof1lab 7 years ago
parent
commit
46eef2b5f5
  1. 46
      lib/util/data.helper.js
  2. 60
      lib/xsql.js

46
lib/util/data.helper.js

@ -163,3 +163,49 @@ exports.getChartQuery = function () {
return 'select ? as ??, count(*) as _count from ?? where ?? between ? and ? '
}
exports.getDataType = function(colType, typesArr) {
// console.log(colType,typesArr);
for (let i = 0; i < typesArr.length; ++i) {
if (colType.indexOf(typesArr[i]) !== -1) {
return 1;
}
}
return 0;
}
exports.getColumnType = function (column) {
let strTypes = ['varchar', 'text', 'char', 'tinytext', 'mediumtext', 'longtext', 'blob', 'mediumblob', 'longblob', 'tinyblob', 'binary', 'varbinary'];
let intTypes = ['int', 'long', 'smallint', 'mediumint', 'bigint', 'tinyint'];
let flatTypes = ['float', 'double', 'decimal'];
let dateTypes = ['date', 'datetime', 'timestamp', 'time', 'year'];
//console.log(column);
if (this.getDataType(column['data_type'], strTypes)) {
return "string"
} else if (this.getDataType(column['data_type'], intTypes)) {
return "int"
} else if (this.getDataType(column['data_type'], flatTypes)) {
return "float"
} else if (this.getDataType(column['data_type'], dateTypes)) {
return "date"
} else {
return "string"
}
}
exports.getType = function(colType, typesArr) {
for (let i = 0; i < typesArr.length; ++i) {
// if (typesArr[i].indexOf(colType) !== -1) {
// return 1;
// }
if (colType.indexOf(typesArr[i]) !== -1) {
return 1;
}
}
return 0;
}

60
lib/xsql.js

@ -22,6 +22,8 @@ class Xsql {
}
/**************** START : Cache functions ****************/
init(cbk) {
this.dbCacheInitAsync((err, results) => {
cbk(err, results)
@ -137,6 +139,8 @@ class Xsql {
}
}
}
/**************** END : Cache functions ****************/
exec(query, params) {
@ -162,13 +166,13 @@ class Xsql {
const flatTypes = ['float', 'double', 'decimal'];
const dateTypes = ['date', 'datetime', 'timestamp', 'time', 'year'];
if (getType(Type, strTypes)) {
if (dataHelp.getType(Type, strTypes)) {
return "string"
} else if (getType(Type, intTypes)) {
} else if (dataHelp.getType(Type, intTypes)) {
return "int"
} else if (getType(Type, flatTypes)) {
} else if (dataHelp.getType(Type, flatTypes)) {
return "float"
} else if (getType(Type, dateTypes)) {
} else if (dataHelp.getType(Type, dateTypes)) {
return "date"
} else {
return "unknown"
@ -466,7 +470,7 @@ class Xsql {
// get a where clause out of the above columnNames and their values
for (let i = 0; i < pks.length; ++i) {
let type = getColumnType(pks[i]);
let type = dataHelp.getColumnType(pks[i]);
whereCol = pks[i]['column_name']
@ -503,7 +507,7 @@ class Xsql {
let fks = this.metaDb.tables[childTable].foreignKeys;
let fk = dataHelp.findObjectInArrayByKey('referenced_table_name', parentTable, fks);
let whereCol = fk['column_name']
let colType = getColumnType(fk);
let colType = dataHelp.getColumnType(fk);
if (colType === 'string') {
whereValue = mysql.escape(parentId)
@ -1005,47 +1009,3 @@ class Xsql {
module.exports = Xsql;
function getDataType(colType, typesArr) {
// console.log(colType,typesArr);
for (let i = 0; i < typesArr.length; ++i) {
if (colType.indexOf(typesArr[i]) !== -1) {
return 1;
}
}
return 0;
}
function getColumnType(column) {
let strTypes = ['varchar', 'text', 'char', 'tinytext', 'mediumtext', 'longtext', 'blob', 'mediumblob', 'longblob', 'tinyblob', 'binary', 'varbinary'];
let intTypes = ['int', 'long', 'smallint', 'mediumint', 'bigint', 'tinyint'];
let flatTypes = ['float', 'double', 'decimal'];
let dateTypes = ['date', 'datetime', 'timestamp', 'time', 'year'];
//console.log(column);
if (getDataType(column['data_type'], strTypes)) {
return "string"
} else if (getDataType(column['data_type'], intTypes)) {
return "int"
} else if (getDataType(column['data_type'], flatTypes)) {
return "float"
} else if (getDataType(column['data_type'], dateTypes)) {
return "date"
} else {
return "string"
}
}
function getType(colType, typesArr) {
for (let i = 0; i < typesArr.length; ++i) {
// if (typesArr[i].indexOf(colType) !== -1) {
// return 1;
// }
if (colType.indexOf(typesArr[i]) !== -1) {
return 1;
}
}
return 0;
}

Loading…
Cancel
Save