Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ exports.__defineGetter__(

exports.__defineGetter__('Types', () => require('./lib/constants/types.js'));

exports.__defineGetter__(
'TypedParameter',
() => require('./lib/packets/typed_parameter.js').types
);

exports.__defineGetter__('Charsets', () =>
require('./lib/constants/charsets.js')
);
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class Execute extends Command {
connection.config.timezone,
this._executeOptions.attributes,
clientFlags,
connection._isMariaDB
connection._isMariaDB,
this.statement.parameters
);
//For reasons why this try-catch is here, please see
// https://github.com/sidorares/node-mysql2/pull/689
Expand Down
1 change: 1 addition & 0 deletions lib/constants/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ module.exports = {
0x0e: 'NEWDATE', // aka ?
0x0f: 'VARCHAR', // aka VARCHAR (?)
0x10: 'BIT', // aka BIT, 1-8 byte
0xf2: 'VECTOR',
0xf5: 'JSON',
0xf6: 'NEWDECIMAL', // aka DECIMAL
0xf7: 'ENUM', // aka ENUM
Expand Down
30 changes: 28 additions & 2 deletions lib/packets/encode_parameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

const Types = require('../constants/types');
const Packet = require('../packets/packet');
const {
TypedParameter,
encodeTypedParameter,
integerHint,
} = require('./typed_parameter.js');
const FieldFlags = require('../constants/field_flags.js');

function isJSON(value) {
return (
Expand All @@ -11,7 +17,20 @@ function isJSON(value) {
);
}

function toParameter(value, encoding, timezone, jsonAsString) {
function toParameter(value, encoding, timezone, jsonAsString, hint) {
if (value instanceof TypedParameter) {
return encodeTypedParameter(value, encoding, timezone, jsonAsString);
}
if (hint) {
const hinted = integerHint(
value,
hint.columnType,
Boolean(hint.flags & FieldFlags.UNSIGNED)
);
if (hinted) {
return encodeTypedParameter(hinted, encoding, timezone, jsonAsString);
}
}
let type = Types.VAR_STRING;
let length;
let writer = function (value) {
Expand Down Expand Up @@ -71,7 +90,14 @@ function toParameter(value, encoding, timezone, jsonAsString) {
if (!length) {
length = Packet.lengthCodedStringLength(value, encoding);
}
return { value, type, length, writer };
return {
value,
type,
length,
writer,
unsigned: false,
isNull: type === Types.NULL,
};
}

module.exports = { toParameter, isJSON };
20 changes: 14 additions & 6 deletions lib/packets/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class Execute {
timezone,
attributes,
clientFlags,
jsonAsString
jsonAsString,
parameterDefinitions
) {
this.id = id;
this.parameters = parameters;
Expand All @@ -25,6 +26,7 @@ class Execute {
this.attributes = attributes;
this.clientFlags = clientFlags || 0;
this.jsonAsString = jsonAsString || false;
this.parameterDefinitions = parameterDefinitions || [];
}

static fromPacket(packet, encoding) {
Expand Down Expand Up @@ -124,8 +126,14 @@ class Execute {
if (totalParams > 0) {
const bindParams =
numParams > 0
? this.parameters.map((v) =>
toParameter(v, this.encoding, this.timezone, this.jsonAsString)
? this.parameters.map((v, i) =>
toParameter(
v,
this.encoding,
this.timezone,
this.jsonAsString,
this.parameterDefinitions[i]
)
)
: [];
const attrParams = attrNames.map((name) =>
Expand All @@ -137,7 +145,7 @@ class Execute {
let bitmap = 0;
let bitValue = 1;
allParams.forEach((parameter) => {
if (parameter.type === Types.NULL) {
if (parameter.isNull) {
bitmap += bitValue;
}
bitValue *= 2;
Expand All @@ -156,7 +164,7 @@ class Execute {
// types (and names for attributes)
for (let i = 0; i < allParams.length; i++) {
packet.writeInt8(allParams[i].type);
packet.writeInt8(0); // unsigned flag
packet.writeInt8(allParams[i].unsigned ? 0x80 : 0);
if (useQueryAttributes) {
const name = i < numParams ? '' : attrNames[i - numParams];
packet.writeLengthCodedString(name, this.encoding);
Expand All @@ -165,7 +173,7 @@ class Execute {

// values
allParams.forEach((parameter) => {
if (parameter.type !== Types.NULL) {
if (!parameter.isNull) {
parameter.writer.call(packet, parameter.value);
}
});
Expand Down
30 changes: 30 additions & 0 deletions lib/packets/packet.js
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,36 @@ class Packet {
this.offset += 8;
}

writeFloat(n) {
this.buffer.writeFloatLE(n, this.offset);
this.offset += 4;
}

writeUIntLE(n, bytes) {
if (bytes === 8) {
this.buffer.writeBigUInt64LE(n, this.offset);
} else {
this.buffer.writeUIntLE(Number(n), this.offset, bytes);
}
this.offset += bytes;
}

writeTime({ negative, days, hours, minutes, seconds, microseconds }) {
if (!days && !hours && !minutes && !seconds && !microseconds) {
this.writeInt8(0);
return;
}
this.writeInt8(microseconds ? 12 : 8);
this.writeInt8(negative ? 1 : 0);
this.writeInt32(days);
this.writeInt8(hours);
this.writeInt8(minutes);
this.writeInt8(seconds);
if (microseconds) {
this.writeInt32(microseconds);
}
}

writeBuffer(b) {
b.copy(this.buffer, this.offset);
this.offset += b.length;
Expand Down
Loading
Loading