-
-
Notifications
You must be signed in to change notification settings - Fork 680
Expand file tree
/
Copy pathencode_parameter.js
More file actions
103 lines (95 loc) · 2.87 KB
/
Copy pathencode_parameter.js
File metadata and controls
103 lines (95 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
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 (
Array.isArray(value) ||
value.constructor === Object ||
(typeof value.toJSON === 'function' && !Buffer.isBuffer(value))
);
}
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) {
return Packet.prototype.writeLengthCodedString.call(this, value, encoding);
};
if (value !== null) {
switch (typeof value) {
case 'undefined':
throw new TypeError('Bind parameters must not contain undefined');
case 'number':
type = Types.DOUBLE;
length = 8;
writer = Packet.prototype.writeDouble;
break;
case 'boolean':
value = value | 0;
type = Types.TINY;
length = 1;
writer = Packet.prototype.writeInt8;
break;
case 'object':
if (Object.prototype.toString.call(value) === '[object Date]') {
type = Types.DATETIME;
length = 12;
writer = function (value) {
return Packet.prototype.writeDate.call(this, value, timezone);
};
} else if (isJSON(value)) {
value = JSON.stringify(value);
// MariaDB rejects the JSON parameter type with "Incorrect
// arguments to mysqld_stmt_execute"; it expects JSON values
// as plain strings
if (!jsonAsString) {
type = Types.JSON;
}
} else if (Buffer.isBuffer(value)) {
// send buffers as BLOB so servers treat the value as binary data
// rather than a string in the connection charset (MariaDB converts
// string parameters when storing into binary columns such as
// VECTOR, corrupting the value)
type = Types.BLOB;
length = Packet.lengthCodedNumberLength(value.length) + value.length;
writer = Packet.prototype.writeLengthCodedBuffer;
}
break;
default:
value = value.toString();
}
} else {
value = '';
type = Types.NULL;
}
if (!length) {
length = Packet.lengthCodedStringLength(value, encoding);
}
return {
value,
type,
length,
writer,
unsigned: false,
isNull: type === Types.NULL,
};
}
module.exports = { toParameter, isJSON };