From 46eef2b5f54522607bf971c9f82e6f7b6694850d Mon Sep 17 00:00:00 2001 From: oof1lab Date: Sat, 18 Nov 2017 13:11:25 +0530 Subject: [PATCH] refactor : move independent methods in xsql to data.helper --- lib/util/data.helper.js | 46 +++++++++++++++++++++++++++++++ lib/xsql.js | 60 +++++++---------------------------------- 2 files changed, 56 insertions(+), 50 deletions(-) diff --git a/lib/util/data.helper.js b/lib/util/data.helper.js index 75f3dcf490..d0fb251c20 100644 --- a/lib/util/data.helper.js +++ b/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; +} + + diff --git a/lib/xsql.js b/lib/xsql.js index 46b64e7d76..c9e284b783 100644 --- a/lib/xsql.js +++ b/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; -}