@@ -11,28 +11,25 @@ function Execute(id, parameters, charsetNumber) {
11
11
this . encoding = CharsetToEncoding [ charsetNumber ] ;
12
12
}
13
13
14
- var pad = '000000000000' ;
15
- function leftPad ( num , value ) {
16
- var s = value . toString ( ) ;
17
- // if we don't need to pad
18
- if ( s . length >= num ) {
19
- return s ;
20
- }
21
- return ( pad + s ) . slice ( - num ) ;
22
- }
23
-
24
- function toMysqlDateTime ( date ) {
25
- return [ date . getFullYear ( ) , date . getMonth ( ) + 1 , date . getDate ( ) ] . join ( '-' ) + ' ' +
26
- [ date . getHours ( ) , date . getMinutes ( ) , date . getSeconds ( ) ] . join ( ':' ) + '.' +
27
- leftPad ( 3 , date . getMilliseconds ( ) ) ;
28
- }
29
-
30
14
function isJSON ( value ) {
31
15
return Array . isArray ( value ) ||
32
16
value . constructor === Object ||
33
17
( typeof value . toJSON === 'function' && ! Buffer . isBuffer ( value ) ) ;
34
18
}
35
19
20
+ function toMysqlDateBuffer ( value ) {
21
+ var bufferValue = Buffer . allocUnsafe ( 12 ) ;
22
+ bufferValue . writeUInt8 ( 11 , 0 ) ;
23
+ bufferValue . writeUInt16LE ( value . getFullYear ( ) , 1 ) ;
24
+ bufferValue . writeUInt8 ( value . getMonth ( ) + 1 , 3 ) ;
25
+ bufferValue . writeUInt8 ( value . getDate ( ) , 4 ) ;
26
+ bufferValue . writeUInt8 ( value . getHours ( ) , 5 ) ;
27
+ bufferValue . writeUInt8 ( value . getMinutes ( ) , 6 ) ;
28
+ bufferValue . writeUInt8 ( value . getSeconds ( ) , 7 ) ;
29
+ bufferValue . writeUInt32LE ( value . getMilliseconds ( ) * 1000 , 8 ) ;
30
+ return bufferValue ;
31
+ }
32
+
36
33
/**
37
34
* Converts a value to an object describing type, String/Buffer representation and length
38
35
* @param {* } value
@@ -43,6 +40,9 @@ function toParameter(value, encoding) {
43
40
var fixed = false ;
44
41
if ( value !== null ) {
45
42
switch ( typeof value ) {
43
+ case 'undefined' :
44
+ throw new TypeError ( 'Bind parameters must not contain undefined' ) ;
45
+
46
46
case 'number' :
47
47
type = Types . DOUBLE ;
48
48
fixed = true ;
@@ -61,7 +61,9 @@ function toParameter(value, encoding) {
61
61
62
62
case 'object' :
63
63
if ( Object . prototype . toString . call ( value ) == '[object Date]' ) {
64
- value = toMysqlDateTime ( value ) ;
64
+ type = Types . DATETIME ;
65
+ fixed = true ;
66
+ value = toMysqlDateBuffer ( value ) ;
65
67
} else if ( isJSON ( value ) ) {
66
68
type = Types . JSON ;
67
69
value = JSON . stringify ( value ) ;
0 commit comments