不要怂,就是干,撸起袖子干!

Commit f70f068e by Jan Aagaard Meier

DIE ;'s, die

1 parent 641624eb
Showing with 75 additions and 75 deletions
var STRING = function(length, binary) { var STRING = function(length, binary) {
if (this instanceof STRING) { if (this instanceof STRING) {
this._binary = !!binary; this._binary = !!binary
if (typeof length === 'number') { if (typeof length === 'number') {
this._length = length; this._length = length
} else { } else {
this._length = 255; this._length = 255
} }
} else { } else {
return new STRING(length, binary); return new STRING(length, binary)
} }
}; }
STRING.prototype = { STRING.prototype = {
get BINARY() { get BINARY() {
this._binary = true; this._binary = true
return this; return this
}, },
get type() { get type() {
return this.toString(); return this.toString()
}, },
toString: function() { toString: function() {
return 'VARCHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : ''); return 'VARCHAR(' + this._length + ')' + ((this._binary) ? ' BINARY' : '')
} }
}; }
Object.defineProperty(STRING, 'BINARY', { Object.defineProperty(STRING, 'BINARY', {
get: function() { get: function() {
return new STRING(undefined, true); return new STRING(undefined, true)
} }
}); })
var INTEGER = function() { var INTEGER = function() {
return INTEGER.prototype.construct.apply(this, [INTEGER].concat(Array.prototype.slice.apply(arguments))); return INTEGER.prototype.construct.apply(this, [INTEGER].concat(Array.prototype.slice.apply(arguments)))
}; }
var BIGINT = function() { var BIGINT = function() {
return BIGINT.prototype.construct.apply(this, [BIGINT].concat(Array.prototype.slice.apply(arguments))); return BIGINT.prototype.construct.apply(this, [BIGINT].concat(Array.prototype.slice.apply(arguments)))
}; }
var FLOAT = function() { var FLOAT = function() {
return FLOAT.prototype.construct.apply(this, [FLOAT].concat(Array.prototype.slice.apply(arguments))); return FLOAT.prototype.construct.apply(this, [FLOAT].concat(Array.prototype.slice.apply(arguments)))
}; }
var BLOB = function() { var BLOB = function() {
return BLOB.prototype.construct.apply(this, [BLOB].concat(Array.prototype.slice.apply(arguments))); return BLOB.prototype.construct.apply(this, [BLOB].concat(Array.prototype.slice.apply(arguments)))
}; }
FLOAT._type = FLOAT; FLOAT._type = FLOAT
FLOAT._typeName = 'FLOAT'; FLOAT._typeName = 'FLOAT'
INTEGER._type = INTEGER; INTEGER._type = INTEGER
INTEGER._typeName = 'INTEGER'; INTEGER._typeName = 'INTEGER'
BIGINT._type = BIGINT; BIGINT._type = BIGINT
BIGINT._typeName = 'BIGINT'; BIGINT._typeName = 'BIGINT'
STRING._type = STRING; STRING._type = STRING
STRING._typeName = 'VARCHAR'; STRING._typeName = 'VARCHAR'
BLOB._type = BLOB BLOB._type = BLOB
BLOB._typeName = 'BLOB' BLOB._typeName = 'BLOB'
BLOB.toString = STRING.toString = INTEGER.toString = FLOAT.toString = BIGINT.toString = function() { BLOB.toString = STRING.toString = INTEGER.toString = FLOAT.toString = BIGINT.toString = function() {
return new this._type().toString(); return new this._type().toString()
}; }
BLOB.prototype = { BLOB.prototype = {
...@@ -77,7 +77,7 @@ BLOB.prototype = { ...@@ -77,7 +77,7 @@ BLOB.prototype = {
}, },
get type() { get type() {
return this.toString(); return this.toString()
}, },
toString: function() { toString: function() {
...@@ -98,84 +98,84 @@ FLOAT.prototype = BIGINT.prototype = INTEGER.prototype = { ...@@ -98,84 +98,84 @@ FLOAT.prototype = BIGINT.prototype = INTEGER.prototype = {
construct: function(RealType, length, decimals, unsigned, zerofill) { construct: function(RealType, length, decimals, unsigned, zerofill) {
if (this instanceof RealType) { if (this instanceof RealType) {
this._typeName = RealType._typeName; this._typeName = RealType._typeName
this._unsigned = !!unsigned; this._unsigned = !!unsigned
this._zerofill = !!zerofill; this._zerofill = !!zerofill
if (typeof length === 'number') { if (typeof length === 'number') {
this._length = length; this._length = length
} }
if (typeof decimals === 'number') { if (typeof decimals === 'number') {
this._decimals = decimals; this._decimals = decimals
} }
} else { } else {
return new RealType(length, decimals, unsigned, zerofill); return new RealType(length, decimals, unsigned, zerofill)
} }
}, },
get type() { get type() {
return this.toString(); return this.toString()
}, },
get UNSIGNED() { get UNSIGNED() {
this._unsigned = true; this._unsigned = true
return this; return this
}, },
get ZEROFILL() { get ZEROFILL() {
this._zerofill = true; this._zerofill = true
return this; return this
}, },
toString: function() { toString: function() {
var result = this._typeName; var result = this._typeName
if (this._length) { if (this._length) {
result += '(' + this._length; result += '(' + this._length
if (typeof this._decimals === 'number') { if (typeof this._decimals === 'number') {
result += ',' + this._decimals; result += ',' + this._decimals
} }
result += ')'; result += ')'
} }
if (this._unsigned) { if (this._unsigned) {
result += ' UNSIGNED'; result += ' UNSIGNED'
} }
if (this._zerofill) { if (this._zerofill) {
result += ' ZEROFILL'; result += ' ZEROFILL'
} }
return result; return result
} }
}; }
var unsignedDesc = { var unsignedDesc = {
get: function() { get: function() {
return new this._type(undefined, undefined, true); return new this._type(undefined, undefined, true)
} }
}; }
var zerofillDesc = { var zerofillDesc = {
get: function() { get: function() {
return new this._type(undefined, undefined, undefined, true); return new this._type(undefined, undefined, undefined, true)
} }
}; }
var typeDesc = { var typeDesc = {
get: function() { get: function() {
return new this._type().toString(); return new this._type().toString()
} }
}; }
Object.defineProperty(STRING, 'type', typeDesc); Object.defineProperty(STRING, 'type', typeDesc)
Object.defineProperty(INTEGER, 'type', typeDesc); Object.defineProperty(INTEGER, 'type', typeDesc)
Object.defineProperty(BIGINT, 'type', typeDesc); Object.defineProperty(BIGINT, 'type', typeDesc)
Object.defineProperty(FLOAT, 'type', typeDesc); Object.defineProperty(FLOAT, 'type', typeDesc)
Object.defineProperty(BLOB, 'type', typeDesc) Object.defineProperty(BLOB, 'type', typeDesc)
Object.defineProperty(INTEGER, 'UNSIGNED', unsignedDesc); Object.defineProperty(INTEGER, 'UNSIGNED', unsignedDesc)
Object.defineProperty(BIGINT, 'UNSIGNED', unsignedDesc); Object.defineProperty(BIGINT, 'UNSIGNED', unsignedDesc)
Object.defineProperty(FLOAT, 'UNSIGNED', unsignedDesc); Object.defineProperty(FLOAT, 'UNSIGNED', unsignedDesc)
Object.defineProperty(INTEGER, 'ZEROFILL', zerofillDesc); Object.defineProperty(INTEGER, 'ZEROFILL', zerofillDesc)
Object.defineProperty(BIGINT, 'ZEROFILL', zerofillDesc); Object.defineProperty(BIGINT, 'ZEROFILL', zerofillDesc)
Object.defineProperty(FLOAT, 'ZEROFILL', zerofillDesc); Object.defineProperty(FLOAT, 'ZEROFILL', zerofillDesc)
module.exports = { module.exports = {
STRING: STRING, STRING: STRING,
......
...@@ -29,7 +29,7 @@ module.exports = (function() { ...@@ -29,7 +29,7 @@ module.exports = (function() {
this.options.logging('Executing: ' + this.sql) this.options.logging('Executing: ' + this.sql)
} }
var columnTypes = {}; var columnTypes = {}
this.database.serialize(function() { this.database.serialize(function() {
var executeSql = function() { var executeSql = function() {
self.database[getDatabaseMethod.call(self)](self.sql, function(err, results) { self.database[getDatabaseMethod.call(self)](self.sql, function(err, results) {
...@@ -48,16 +48,16 @@ module.exports = (function() { ...@@ -48,16 +48,16 @@ module.exports = (function() {
self.database.all("PRAGMA table_info(" + tableName + ")", function(err, results) { self.database.all("PRAGMA table_info(" + tableName + ")", function(err, results) {
if (!err) { if (!err) {
for (var i=0, l=results.length; i<l; i++) { for (var i=0, l=results.length; i<l; i++) {
columnTypes[results[i].name] = results[i].type; columnTypes[results[i].name] = results[i].type
} }
} }
executeSql(); executeSql()
}); });
} else { } else {
executeSql(); executeSql()
} }
} else { } else {
executeSql(); executeSql()
} }
}) })
...@@ -91,12 +91,12 @@ module.exports = (function() { ...@@ -91,12 +91,12 @@ module.exports = (function() {
if (result.hasOwnProperty(name) && metaData.columnTypes[name]) { if (result.hasOwnProperty(name) && metaData.columnTypes[name]) {
if (metaData.columnTypes[name] === 'DATETIME') { if (metaData.columnTypes[name] === 'DATETIME') {
// we need to convert the timestamps into actual date objects // we need to convert the timestamps into actual date objects
var val = result[name]; var val = result[name]
if (val !== null) { if (val !== null) {
result[name] = new Date(val+'Z'); // Z means UTC result[name] = new Date(val+'Z') // Z means UTC
} }
} else if (metaData.columnTypes[name].lastIndexOf('BLOB') !== -1) { } else if (metaData.columnTypes[name].lastIndexOf('BLOB') !== -1) {
result[name] = new Buffer(result[name]); result[name] = new Buffer(result[name])
} }
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!