@@ -8,17 +8,37 @@ for (var t in Types) {
8
8
typeNames [ Types [ t ] ] = t ;
9
9
}
10
10
11
+
11
12
function compile ( fields , options , config ) {
12
13
14
+ // node-mysql typeCast compatibility wrapper
15
+ // see https://github.com/mysqljs/mysql/blob/96fdd0566b654436624e2375c7b6604b1f50f825/lib/protocol/packets/Field.js
16
+ function wrap ( field , type , packet ) {
17
+ return {
18
+ type : type ,
19
+ length : field . columnLength ,
20
+ db : field . schema ,
21
+ table : field . table ,
22
+ name : field . name ,
23
+ string : function ( ) { return packet . readLengthCodedString ( ) ; } ,
24
+ buffer : function ( ) { return this . parser . parseLengthCodedBuffer ( ) ; } ,
25
+ geometry : function ( ) { return this . parser . parseGeometryValue ( ) ; }
26
+ } ;
27
+ } ;
28
+
13
29
var result = [ ] ;
14
30
var i = 0 ;
15
31
var lvalue = '' ;
16
32
17
- result . push ( '(function() { return function TextRow(packet) {' ) ;
33
+ result . push ( '(function() { return function TextRow(packet, fields, options ) {' ) ;
18
34
if ( options . rowsAsArray ) {
19
35
result . push ( ' var result = new Array(' + fields . length + ')' ) ;
20
36
}
21
37
38
+ if ( typeof options . typeCast === 'function' ) {
39
+ result . push ( ' var wrap = ' + wrap . toString ( ) ) ;
40
+ }
41
+
22
42
var resultTables = { } ;
23
43
var resultTablesArray = [ ] ;
24
44
@@ -48,7 +68,14 @@ function compile (fields, options, config) {
48
68
} else {
49
69
lvalue = ' this[' + srcEscape ( fields [ i ] . name ) + ']' ;
50
70
}
51
- result . push ( lvalue + ' = ' + readCodeFor ( fields [ i ] . columnType , fields [ i ] . characterSet , config ) ) ;
71
+ var readCode = readCodeFor ( fields [ i ] . columnType , fields [ i ] . characterSet , config ) ;
72
+ if ( typeof options . typeCast === 'function' ) {
73
+ result . push ( lvalue + ' = options.typeCast(wrap(fields[' + i + '], ' + srcEscape ( typeNames [ fields [ i ] . columnType ] ) + ', packet), function() { return ' + readCode + ';})' ) ;
74
+ } else if ( options . typeCast === false ) {
75
+ result . push ( lvalue + ' = packet.readLengthCodedBuffer();' ) ;
76
+ } else {
77
+ result . push ( lvalue + ' = ' + readCode ) ;
78
+ }
52
79
}
53
80
54
81
if ( options . rowsAsArray ) {
@@ -59,6 +86,7 @@ function compile (fields, options, config) {
59
86
var src = result . join ( '\n' ) ;
60
87
if ( config . debug ) {
61
88
console . log ( ' Compiled text protocol row parser:\n\n' ) ;
89
+ console . log ( src ) ;
62
90
var cardinal = require ( 'cardinal' ) ;
63
91
console . log ( cardinal . highlight ( src ) + '\n\n' ) ;
64
92
}
@@ -72,45 +100,45 @@ function readCodeFor (type, charset, config) {
72
100
case Types . LONG :
73
101
case Types . INT24 :
74
102
case Types . YEAR :
75
- return 'packet.parseLengthCodedInt(); ' ;
103
+ return 'packet.parseLengthCodedInt()' ;
76
104
case Types . LONGLONG :
77
105
if ( config . supportBigNumbers && config . bigNumberStrings ) {
78
- return 'packet.parseLengthCodedIntString(); ' ;
106
+ return 'packet.parseLengthCodedIntString()' ;
79
107
}
80
- return 'packet.parseLengthCodedInt(); ' ;
108
+ return 'packet.parseLengthCodedInt()' ;
81
109
case Types . FLOAT :
82
110
case Types . DOUBLE :
83
- return 'packet.parseLengthCodedFloat(); ' ;
111
+ return 'packet.parseLengthCodedFloat()' ;
84
112
case Types . NULL :
85
- return 'null; packet.skip(1); ' ;
113
+ return 'null; packet.skip(1)' ;
86
114
case Types . DECIMAL :
87
115
case Types . NEWDECIMAL :
88
116
if ( config . decimalNumbers ) {
89
- return 'packet.parseLengthCodedFloat(); ' ;
117
+ return 'packet.parseLengthCodedFloat()' ;
90
118
}
91
- return 'packet.readLengthCodedString(); //' + type + ' ' + charset ;
119
+ return 'packet.readLengthCodedString()' ;
92
120
case Types . DATE :
93
121
if ( config . dateStrings ) {
94
122
return 'packet.readLengthCodedString()' ;
95
123
}
96
- return 'packet.parseDate(); ' ;
124
+ return 'packet.parseDate()' ;
97
125
case Types . DATETIME :
98
126
case Types . TIMESTAMP :
99
127
if ( config . dateStrings ) {
100
128
return 'packet.readLengthCodedString()' ;
101
129
}
102
- return 'packet.parseDateTime(); ' ;
130
+ return 'packet.parseDateTime()' ;
103
131
case Types . TIME :
104
132
return 'packet.readLengthCodedString()' ;
105
133
case Types . GEOMETRY :
106
- return 'packet.parseGeometryValue(); ' ;
134
+ return 'packet.parseGeometryValue()' ;
107
135
case Types . JSON :
108
- return 'JSON.parse(packet.readLengthCodedString()); ' ;
136
+ return 'JSON.parse(packet.readLengthCodedString())' ;
109
137
default :
110
138
if ( charset == Charsets . BINARY ) {
111
- return 'packet.readLengthCodedBuffer(); ' ;
139
+ return 'packet.readLengthCodedBuffer()' ;
112
140
} else {
113
- return 'packet.readLengthCodedString(); //' + type + ' ' + charset ;
141
+ return 'packet.readLengthCodedString()' ;
114
142
}
115
143
}
116
144
}
0 commit comments