mysql
Advanced tools
Comparing version 2.0.0-alpha5 to 2.0.0-alpha6
@@ -7,2 +7,8 @@ # Changes | ||
## v2.0.0-alpha6 (2013-01-31) | ||
* Add supportBigNumbers option (#381, #382) | ||
* Accept prebuilt Query object in connection.query | ||
* Bug fixes | ||
## v2.0.0-alpha5 (2012-12-03) | ||
@@ -9,0 +15,0 @@ |
@@ -10,4 +10,6 @@ var Connection = require('./lib/Connection'); | ||
exports.createQuery = Connection.createQuery; | ||
exports.Types = Types; | ||
exports.escape = SqlString.escape; | ||
exports.escapeId = SqlString.escapeId; |
@@ -5,2 +5,3 @@ var Net = require('net'); | ||
var SqlString = require('./protocol/SqlString'); | ||
var Query = require('./protocol/sequences/Query'); | ||
var EventEmitter = require('events').EventEmitter; | ||
@@ -21,2 +22,26 @@ var Util = require('util'); | ||
Connection.createQuery = function(sql, values, cb) { | ||
if (sql instanceof Query) { | ||
return sql; | ||
} | ||
var options = {}; | ||
if (typeof sql === 'object') { | ||
// query(options, cb) | ||
options = sql; | ||
cb = values; | ||
} else if (typeof values === 'function') { | ||
// query(sql, cb) | ||
cb = values; | ||
options.sql = sql; | ||
options.values = undefined; | ||
} else { | ||
// query(sql, values, cb) | ||
options.sql = sql; | ||
options.values = values; | ||
} | ||
return new Query(options, cb); | ||
}; | ||
Connection.prototype.connect = function(cb) { | ||
@@ -66,29 +91,12 @@ if (!this._connectCalled) { | ||
var options = {}; | ||
var query = Connection.createQuery(sql, values, cb); | ||
if (typeof sql === 'object') { | ||
// query(options, cb) | ||
options = sql; | ||
cb = values; | ||
values = options.values; | ||
delete options.values; | ||
} else if (typeof values === 'function') { | ||
// query(sql, cb) | ||
cb = values; | ||
options.sql = sql; | ||
values = undefined; | ||
} else { | ||
// query(sql, values, cb) | ||
options.sql = sql; | ||
options.values = values; | ||
if (!(typeof sql == 'object' && 'typeCast' in sql)) { | ||
query.typeCast = this.config.typeCast; | ||
} | ||
options.sql = this.format(options.sql, values || []); | ||
query.sql = this.format(query.sql, query.values || []); | ||
delete query.values; | ||
if (!('typeCast' in options)) { | ||
options.typeCast = this.config.typeCast; | ||
} | ||
return this._protocol.query(options, cb); | ||
return this._protocol._enqueue(query); | ||
}; | ||
@@ -95,0 +103,0 @@ |
@@ -11,14 +11,15 @@ var urlParse = require('url').parse; | ||
this.host = options.host || 'localhost'; | ||
this.port = options.port || 3306; | ||
this.socketPath = options.socketPath; | ||
this.user = options.user || undefined; | ||
this.password = options.password || undefined; | ||
this.database = options.database; | ||
this.insecureAuth = options.insecureAuth || false; | ||
this.debug = options.debug; | ||
this.timezone = options.timezone || 'local'; | ||
this.flags = options.flags || ''; | ||
this.queryFormat = options.queryFormat; | ||
this.typeCast = (options.typeCast === undefined) | ||
this.host = options.host || 'localhost'; | ||
this.port = options.port || 3306; | ||
this.socketPath = options.socketPath; | ||
this.user = options.user || undefined; | ||
this.password = options.password || undefined; | ||
this.database = options.database; | ||
this.insecureAuth = options.insecureAuth || false; | ||
this.supportBigNumbers = options.supportBigNumbers || false; | ||
this.debug = options.debug; | ||
this.timezone = options.timezone || 'local'; | ||
this.flags = options.flags || ''; | ||
this.queryFormat = options.queryFormat; | ||
this.typeCast = (options.typeCast === undefined) | ||
? true | ||
@@ -25,0 +26,0 @@ : options.typeCast; |
@@ -1,4 +0,5 @@ | ||
var Types = require('../constants/types'); | ||
var Charsets = require('../constants/charsets'); | ||
var Field = require('./Field'); | ||
var Types = require('../constants/types'); | ||
var Charsets = require('../constants/charsets'); | ||
var Field = require('./Field'); | ||
var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53); | ||
@@ -12,3 +13,3 @@ module.exports = RowDataPacket; | ||
var next = function () { | ||
return self._typeCast(fieldPacket, parser, connection.config.timezone); | ||
return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers); | ||
}; | ||
@@ -24,3 +25,3 @@ | ||
value = (typeCast) | ||
? this._typeCast(fieldPacket, parser, connection.config.timezone) | ||
? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers) | ||
: ( (fieldPacket.charsetNr === Charsets.BINARY) | ||
@@ -42,3 +43,3 @@ ? parser.parseLengthCodedBuffer() | ||
RowDataPacket.prototype._typeCast = function(field, parser, timeZone) { | ||
RowDataPacket.prototype._typeCast = function(field, parser, timeZone, supportBigNumbers) { | ||
switch (field.type) { | ||
@@ -75,3 +76,5 @@ case Types.TIMESTAMP: | ||
? numberString | ||
: Number(numberString); | ||
: ((supportBigNumbers && Number(numberString) > IEEE_754_BINARY_64_PRECISION) | ||
? numberString | ||
: Number(numberString)); | ||
case Types.BIT: | ||
@@ -78,0 +81,0 @@ return parser.parseLengthCodedBuffer(); |
var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53); | ||
var MAX_PACKET_LENGTH = Math.pow(2, 24) - 1; | ||
var PacketHeader = require('./PacketHeader'); | ||
var BigNumber = require("bignumber.js"); | ||
@@ -9,2 +10,3 @@ module.exports = Parser; | ||
this._supportBigNumbers = options.config && options.config.supportBigNumbers; | ||
this._buffer = new Buffer(0); | ||
@@ -149,27 +151,45 @@ this._longPacketBuffers = []; | ||
Parser.prototype.parseLengthCodedNumber = function() { | ||
var byte = this._buffer[this._offset++]; | ||
var bits = this._buffer[this._offset++]; | ||
if (byte <= 251) { | ||
return (byte === 251) | ||
if (bits <= 251) { | ||
return (bits === 251) | ||
? null | ||
: byte; | ||
: bits; | ||
} | ||
var length; | ||
if (byte === 252) { | ||
var bigNumber = false; | ||
var value = 0; | ||
if (bits === 252) { | ||
length = 2; | ||
} else if (byte === 253) { | ||
} else if (bits === 253) { | ||
length = 3; | ||
} else if (byte === 254) { | ||
} else if (bits === 254) { | ||
length = 8; | ||
if (this._supportBigNumbers) { | ||
if (this._buffer[this._offset + 6] > 31 || this._buffer[this._offset + 7]) { | ||
value = new BigNumber(0); | ||
bigNumber = true; | ||
} | ||
} | ||
} else { | ||
throw new Error('parseLengthCodedNumber: Unexpected first byte: ' + byte); | ||
throw new Error('parseLengthCodedNumber: Unexpected first byte: ' + bits); | ||
} | ||
var value = 0; | ||
for (var bytesRead = 0; bytesRead < length; bytesRead++) { | ||
var byte = this._buffer[this._offset++]; | ||
value += Math.pow(256, bytesRead) * byte; | ||
bits = this._buffer[this._offset++]; | ||
if (bigNumber) { | ||
value = value.plus((new BigNumber(256)).pow(bytesRead).times(bits)); | ||
} else { | ||
value += Math.pow(256, bytesRead) * bits; | ||
} | ||
} | ||
if (bigNumber) { | ||
return value.toString(); | ||
} | ||
if (value >= IEEE_754_BINARY_64_PRECISION) { | ||
@@ -199,3 +219,3 @@ throw new Error( | ||
var end = this._nullByteOffset(); | ||
var value = this._buffer.toString(this._encoding, this._offset, end) | ||
var value = this._buffer.toString(this._encoding, this._offset, end); | ||
this._offset = end + 1; | ||
@@ -202,0 +222,0 @@ |
@@ -19,3 +19,2 @@ var Parser = require('./Parser'); | ||
this._parser = new Parser({onPacket: this._parsePacket.bind(this)}); | ||
this._config = options.config || {}; | ||
@@ -30,2 +29,7 @@ this._connection = options.connection; | ||
this._handshakeInitializationPacket = null; | ||
this._parser = new Parser({ | ||
onPacket : this._parsePacket.bind(this), | ||
config : this._config | ||
}); | ||
} | ||
@@ -32,0 +36,0 @@ |
@@ -14,2 +14,3 @@ var Sequence = require('./Sequence'); | ||
this.sql = options.sql; | ||
this.values = options.values; | ||
this.typeCast = (options.typeCast === undefined) | ||
@@ -16,0 +17,0 @@ ? true |
@@ -77,3 +77,3 @@ var SqlString = exports; | ||
if (timeZone != 'local') { | ||
tz = convertTimezone(timeZone); | ||
var tz = convertTimezone(timeZone); | ||
@@ -80,0 +80,0 @@ dt.setTime(dt.getTime() + (dt.getTimezoneOffset() * 60000)); |
@@ -5,3 +5,3 @@ { | ||
"description": "A node.js driver for mysql. It is written in JavaScript, does not require compiling, and is 100% MIT licensed.", | ||
"version": "2.0.0-alpha5", | ||
"version": "2.0.0-alpha6", | ||
"repository": { | ||
@@ -18,3 +18,4 @@ "url": "" | ||
"dependencies": { | ||
"require-all": "0.0.3" | ||
"require-all": "0.0.3", | ||
"bignumber.js": "1.0.1" | ||
}, | ||
@@ -21,0 +22,0 @@ "devDependencies": { |
@@ -8,3 +8,3 @@ # node-mysql | ||
```bash | ||
npm install mysql@2.0.0-alpha5 | ||
npm install mysql@2.0.0-alpha6 | ||
``` | ||
@@ -149,5 +149,6 @@ | ||
* `queryFormat`: A custom query format function. See [Custom format](#custom-format). | ||
* `supportBigNumbers`: When dealing with big numbers in the database, you should enable this option. | ||
* `debug`: Prints protocol details to stdout. (Default: `false`) | ||
* `multipleStatements`: Allow multiple mysql statements per query. Be careful | ||
with this, it exposes you to SQL injection attacks. (Default: `false) | ||
with this, it exposes you to SQL injection attacks. (Default: `false`) | ||
* `flags`: List of connection flags to use other than the default ones. It is | ||
@@ -377,2 +378,9 @@ also possible to blacklist default ones. For more information, check [Connection Flags](#connection-flags). | ||
When dealing with big numbers (above JavaScript Number precision limit), you should | ||
consider enabling `supportBigNumbers` option to be able to read the insert id as a | ||
string, otherwise it will throw. | ||
This option is also required when fetching big numbers from the database, otherwise | ||
you will get values rounded to hundreds or thousands due to the precision limit. | ||
## Executing queries in parallel | ||
@@ -379,0 +387,0 @@ |
214048
119
5076
768
2
+ Addedbignumber.js@1.0.1
+ Addedbignumber.js@1.0.1(transitive)