Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Implement initial type casting support

  • Loading branch information...
commit 878b434bfd795b35791fde82b6320d8473648548 1 parent 0d04bde
@felixge authored
View
2  benchmark/parse-100k-blog-rows.js
@@ -90,7 +90,7 @@ function mergeBuffers(buffers) {
function benchmark(buffers) {
var protocol = new Protocol();
protocol._handshakeInitializationPacket = true;
- protocol.query({sql: 'SELECT ...'});
+ protocol.query({typeCast: false, sql: 'SELECT ...'});
var start = +new Date;
View
2  benchmark/select-100k-blog-rows.js
@@ -1,5 +1,5 @@
var common = require('../test/common');
-var client = common.createConnection();
+var client = common.createConnection({typeCast: false});
var rowsPerRun = 100000;
client.connect(function(err) {
View
3  lib/Config.js
@@ -7,6 +7,9 @@ function MysqlClientConfig(options) {
this.user = options.user || undefined;
this.password = options.password || undefined;
this.database = options.database;
+ this.typeCast = (options.typeCast === undefined)
+ ? true
+ : options.typeCast;
this.maxPacketSize = 0x01000000;
this.charsetNumber = Constants.UTF8_UNICODE_CI;
View
3  lib/Connection.js
@@ -31,7 +31,8 @@ Connection.prototype.connect = function(cb) {
Connection.prototype.query = function(sql, cb) {
return this._protocol.query({
- sql: sql,
+ sql : sql,
+ typeCast : this.config.typeCast,
}, cb);
};
View
42 lib/protocol/packets/RowDataPacket.js
@@ -1,10 +1,46 @@
+var Types = require('../constants/types');
+
module.exports = RowDataPacket;
function RowDataPacket() {
}
-RowDataPacket.prototype.parse = function(parser, fieldPackets) {
+RowDataPacket.prototype.parse = function(parser, fieldPackets, typeCast) {
for (var i = 0; i < fieldPackets.length; i++) {
- var fieldPacket = fieldPackets[i];
- this[fieldPacket.name] = parser.parseLengthCodedString();
+ var fieldPacket = fieldPackets[i];
+ var value = parser.parseLengthCodedString();
+
+ if (typeCast) {
+ value = this._typeCast(value, fieldPacket.type);
+ }
+
+ this[fieldPacket.name] = value;
+ }
+};
+
+RowDataPacket.prototype._typeCast = function(value, type) {
+ // This is a separate function, otherwise v8 will bail with:
+ // Bailout in HGraphBuilder: @"RowDataPacket.parse": SwitchStatement: non-literal switch label
+
+ switch (fieldPacket.type) {
+ case Types.TIMESTAMP:
+ case Types.DATE:
+ case Types.DATETIME:
+ case Types.NEWDATE:
+ value = new Date(value);
+ break;
+ case Types.TINY:
+ case Types.SHORT:
+ case Types.LONG:
+ case Types.LONGLONG:
+ case Types.INT24:
+ case Types.YEAR:
+ value = parseInt(value, 10);
+ break;
+ case Types.FLOAT:
+ case Types.DOUBLE:
+ // decimal types cannot be parsed as floats because
+ // V8 Numbers have less precision than some MySQL Decimals
+ value = parseFloat(value);
+ break;
}
};
View
14 lib/protocol/sequences/Query.js
@@ -8,6 +8,10 @@ function Query(options, callback) {
Sequence.call(this, callback);
this._sql = options.sql;
+ this._typeCast = (options.typeCast === undefined)
+ ? true
+ : options.typeCast;
+
this._resultSetHeaderPacket = null;
this._fieldPackets = [];
@@ -33,14 +37,6 @@ Query.prototype.determinePacket = function(firstByte) {
: Packets.RowDataPacket;
};
-Query.prototype._parse = function(packet) {
- if (packet.constructor === Packets.RowDataPacket) {
- packet.parse(this._parser, this._fieldPackets);
- } else {
- packet.parse(this._parser);
- }
-};
-
Query.prototype['ResultSetHeaderPacket'] = function(packet) {
this._resultSetHeaderPacket = packet;
};
@@ -58,7 +54,7 @@ Query.prototype['EofPacket'] = function(packet) {
};
Query.prototype['RowDataPacket'] = function(packet, parser) {
- packet.parse(parser, this._fieldPackets);
+ packet.parse(parser, this._fieldPackets, this._typeCast);
if (this._callback) {
this._rows.push(packet);
Please sign in to comment.
Something went wrong with that request. Please try again.