ee-mysql-connection
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -11,10 +11,17 @@ !function(){ | ||
, typeCast = require('./typeCast') | ||
, Connection = require('ee-db-connection') //*/require('../../ee-db-connection') | ||
, Connection = /*require('ee-db-connection') //*/require('../../ee-db-connection') | ||
, QueryBuilder = require('ee-mysql-query-builder'); //*/require('../../ee-mysql-query-builder'); | ||
var debug = argv.has('debug-sql'); | ||
var debug = argv.has('debug-sql') || process.env.debug_sql === true | ||
, debugErrors = argv.has('debug-orm-errors') | ||
, debugSlow = argv.has('debug-slow-queries') | ||
, slowDebugTime = debugSlow && type.string(argv.get('debug-slow-queries')) ? argv.get('debug-slow-queries') : 200; | ||
module.exports = new Class({ | ||
@@ -158,19 +165,29 @@ inherits: Connection | ||
*/ | ||
, _query: function(sql, callback) { | ||
var start; | ||
, _query: function(sql, values, callback) { | ||
var start; | ||
if (debug) { | ||
log(sql); | ||
start = Date.now(); | ||
} | ||
if (debug || debugSlow) start = Date.now(); | ||
this.connection.query(sql, values, function(err, results) { | ||
this.connection.query(sql, function(err, results) { | ||
if (debug){ | ||
log.debug('query took «'+(Date.now()-start)+'» msec ...'); | ||
if (results && results.length !== undefined) log.debug('query returned «'+(results ? (results.length || 0) : 0 )+'» rows ...'); | ||
if (results && results.affectedRows !== undefined) log.debug('query affected «'+(results ? (results.affectedRows || 0) : 0 )+'» rows ...'); | ||
log(err); | ||
} | ||
if (debug){ | ||
log.debug('['+this.id+'] ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ QUERY DEBUGGER ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼'); | ||
log.debug('['+this.id+'] Query returned %s rows (%s msec) ...', (results && results.rows ? results.rows.length : 0 ), (Date.now()-start)); | ||
log.info(('['+this.id+'] ').grey + sql.white); | ||
if (values && values.length) log(values); | ||
log.debug('['+this.id+'] ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ QUERY DEBUGGER ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲'); | ||
} | ||
if (debugSlow && (Date.now()-start) > slowDebugTime) { | ||
log.debug('['+this.id+'] ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼ SLOW QUERY ▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼'); | ||
log.debug('['+this.id+'] Query returned %s rows (%s msec) ...', (results && results.rows ? results.rows.length : 0 ), (Date.now()-start)); | ||
log.info(('['+this.id+'] ').grey + sql.white); | ||
if (values && values.length) log(values); | ||
log.debug('['+this.id+'] ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲ SLOW QUERY ▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲'); | ||
} | ||
if (err && debugErrors) { | ||
log.error('Failed to execute Query: '+err.message, sql); | ||
} | ||
if (err) callback(err); | ||
@@ -196,2 +213,33 @@ else { | ||
/* | ||
* bring the query into the correcto format | ||
* | ||
* @param <String> SQL | ||
* @param <Mixed> object, array, null, undefined query parameters | ||
*/ | ||
, _paramterizeQuery: function(SQLString, parameters) { | ||
var values = [] | ||
, reg = /\?([a-z0-9_-]+)/gi | ||
, match; | ||
// get a list of parameters from the string | ||
while (match = reg.exec(SQLString)) { | ||
//log(match[1]); | ||
values.push(parameters[match[1]]); | ||
} | ||
// replace | ||
reg.lastIndex = 0; | ||
return { | ||
SQL: SQLString.replace(reg, '?') | ||
, values: values | ||
}; | ||
} | ||
/** | ||
@@ -203,3 +251,3 @@ * the _describe() method returns a detailed description of all | ||
*/ | ||
, _describe: function(databases, callback){ | ||
, _describe: function(databases, callback) { | ||
@@ -228,3 +276,3 @@ // get definition for each database | ||
// clean up results | ||
, function(err, results){ | ||
, function(err, results) { | ||
if(err) callback(err); | ||
@@ -284,14 +332,3 @@ else { | ||
table.columns[definition.COLUMN_NAME] = { | ||
name: definition.COLUMN_NAME | ||
, type: definition.DATA_TYPE | ||
, length: definition.CHARACTER_MAXIMUM_LENGTH || definition.NUMERIC_PRECISION | ||
, nullable: definition.IS_NULLABLE === 'YES' | ||
, isPrimary: false | ||
, isUnique: false | ||
, isForeignKey: false | ||
, isReferenced: false | ||
, mapsTo: [] | ||
, belongsTo: [] | ||
}; | ||
table.columns[definition.COLUMN_NAME] = this._mapTypes(definition); | ||
}.bind(this)); | ||
@@ -452,2 +489,152 @@ | ||
/* | ||
* translate mysql type definition to standard orm type definition | ||
* | ||
* @param <Object> mysql column description | ||
* | ||
* @returns <Object> standardized type object | ||
*/ | ||
, _mapTypes: function(mysqlDefinition) { | ||
var ormType = {}; | ||
// column identifier | ||
ormType.name = mysqlDefinition.COLUMN_NAME.trim(); | ||
// type conversion | ||
switch (mysqlDefinition.DATA_TYPE) { | ||
case 'int': | ||
case 'tinyint': | ||
case 'smallint': | ||
case 'mediumint': | ||
case 'bigint': | ||
ormType.type = 'integer'; | ||
ormType.jsTypeMapping = 'number'; | ||
ormType.variableLength = false; | ||
if (mysqlDefinition.EXTRA === 'auto_increment') ormType.isAutoIncrementing = true; | ||
else if (type.string(mysqlDefinition.COLUMN_DEFAULT)) ormType.defaultValue = parseInt(mysqlDefinition.COLUMN_DEFAULT, 10); | ||
if (mysqlDefinition.DATA_TYPE === 'int') ormType.bitLength = 32; | ||
else if (mysqlDefinition.DATA_TYPE === 'tinyint') ormType.bitLength = 8; | ||
else if (mysqlDefinition.DATA_TYPE === 'smallint') ormType.bitLength = 16; | ||
else if (mysqlDefinition.DATA_TYPE === 'mediumint') ormType.bitLength = 24; | ||
else if (mysqlDefinition.DATA_TYPE === 'bigint') ormType.bitLength = 64; | ||
break; | ||
case 'bit': | ||
ormType.type = 'bit'; | ||
ormType.jsTypeMapping = 'arrayBuffer'; | ||
ormType.variableLength = false; | ||
ormType.bitLength = mysqlDefinition.NUMERIC_PRECISION; | ||
break; | ||
case 'date': | ||
ormType.type = 'date'; | ||
ormType.jsTypeMapping = 'date'; | ||
ormType.variableLength = false; | ||
break; | ||
case 'character': | ||
ormType.type = 'string'; | ||
ormType.jsTypeMapping = 'string'; | ||
ormType.variableLength = false; | ||
ormType.length = mysqlDefinition.CHARACTER_MAXIMUM_LENGTH; | ||
break; | ||
case 'varchar': | ||
case 'text': | ||
case 'tinytext': | ||
case 'mediumtext': | ||
case 'longtext': | ||
ormType.type = 'string'; | ||
ormType.jsTypeMapping = 'string'; | ||
ormType.variableLength = true; | ||
ormType.maxLength = mysqlDefinition.character_maximum_length; | ||
break; | ||
case 'numeric': | ||
case 'decimal': | ||
case 'double': | ||
ormType.type = 'decimal'; | ||
ormType.jsTypeMapping = 'string'; | ||
ormType.variableLength = false; | ||
ormType.length = this._scalarToBits(mysqlDefinition.NUMERIC_PRECISION); | ||
break; | ||
case 'float': | ||
ormType.type = 'float'; | ||
ormType.jsTypeMapping = 'number'; | ||
ormType.variableLength = false; | ||
ormType.bitLength = (parseInt(mysqlDefinition.NUMERIC_PRECISION, 10) < 24 ) ? 32 : 64; | ||
break; | ||
case 'datetime': | ||
ormType.type = 'datetime'; | ||
ormType.withTimeZone = true; | ||
ormType.jsTypeMapping = 'date'; | ||
break; | ||
case 'timestamp': | ||
ormType.type = 'datetime'; | ||
ormType.withTimeZone = false; | ||
ormType.jsTypeMapping = 'date'; | ||
break; | ||
case 'time': | ||
ormType.type = 'time'; | ||
ormType.withTimeZone = true; | ||
ormType.jsTypeMapping = 'string'; | ||
break; | ||
} | ||
// is null allowed | ||
ormType.nullable = mysqlDefinition.IS_NULLABLE === 'YES'; | ||
// autoincrementing? | ||
if (!ormType.isAutoIncrementing) ormType.isAutoIncrementing = false; | ||
// has a default value? | ||
if (type.undefined(ormType.defaultValue)) { | ||
if (type.string(mysqlDefinition.COLUMN_DEFAULT)) ormType.defaultValue = mysqlDefinition.COLUMN_DEFAULT; | ||
else ormType.defaultValue = null; | ||
} | ||
// will be set later | ||
ormType.isPrimary = false; | ||
ormType.isUnique = false; | ||
ormType.isReferenced = false; | ||
ormType.isForeignKey = false; | ||
// the native type, should not be used by the users, differs for every db | ||
ormType.nativeType = mysqlDefinition.DATA_TYPE; | ||
// will be filled later | ||
ormType.mapsTo = []; | ||
ormType.belongsTo = []; | ||
return ormType; | ||
} | ||
/* | ||
* compute how many bits (bytes) are required to store a certain scalar value | ||
*/ | ||
, _scalarToBits: function(value) { | ||
var byteLength = 0; | ||
value = Array.apply(null, {length: parseInt(value, 10)+1}).join('9'); | ||
while(value/Math.pow(2, ((byteLength+1)*8)) > 1) byteLength++; | ||
return byteLength*8; | ||
} | ||
, describeTables: function(databaseName, callback){ | ||
@@ -460,3 +647,3 @@ this.query({ | ||
, from: 'columns' | ||
, select: ['TABLE_SCHEMA', 'TABLE_NAME', 'COLUMN_NAME', 'COLUMN_DEFAULT', 'IS_NULLABLE', 'DATA_TYPE', 'CHARACTER_MAXIMUM_LENGTH', 'NUMERIC_PRECISION'] | ||
, select: ['TABLE_SCHEMA', 'TABLE_NAME', 'COLUMN_NAME', 'COLUMN_DEFAULT', 'IS_NULLABLE', 'DATA_TYPE', 'CHARACTER_MAXIMUM_LENGTH', 'NUMERIC_PRECISION', 'EXTRA'] | ||
}, callback); | ||
@@ -463,0 +650,0 @@ } |
{ | ||
"name" : "ee-mysql-connection" | ||
, "description" : "mysql connection abstraction for ee-orm" | ||
, "version" : "0.3.2" | ||
, "version" : "0.4.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-mysql-connection" | ||
@@ -28,3 +28,3 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)" | ||
, "ee-mysql-query-builder" : "0.1.x" | ||
, "ee-db-connection" : "^0.1.13" | ||
, "ee-db-connection" : "0.2.x" | ||
, "moment" : "2.5.x" | ||
@@ -31,0 +31,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
34043
852
1
+ Addedee-db-connection@0.2.0(transitive)
- Removedee-db-connection@0.1.18(transitive)
Updatedee-db-connection@0.2.x