5
5
6
6
const QUOTE = 0x27 ;
7
7
8
+ // Cache common GeoJSON types
9
+ const GEO_TYPES = new Set ( [
10
+ 'Point' ,
11
+ 'LineString' ,
12
+ 'Polygon' ,
13
+ 'MultiPoint' ,
14
+ 'MultiLineString' ,
15
+ 'MultiPolygon' ,
16
+ 'GeometryCollection'
17
+ ] ) ;
18
+
19
+ // Optimized function to pad numbers with leading zeros
8
20
const formatDigit = function ( val , significantDigit ) {
9
- let res = `${ val } ` ;
10
- while ( res . length < significantDigit ) res = '0' + res ;
11
- return res ;
21
+ const str = `${ val } ` ;
22
+ return str . length < significantDigit ? '0' . repeat ( significantDigit - str . length ) + str : str ;
12
23
} ;
13
24
14
25
class TextEncoder {
@@ -51,27 +62,17 @@ class TextEncoder {
51
62
out . writeStringAscii ( 'NULL' ) ;
52
63
} else TextEncoder . writeParam ( out , value [ i ] , opts , info ) ;
53
64
}
65
+
54
66
if ( opts . arrayParenthesis ) {
55
67
out . writeStringAscii ( ')' ) ;
56
68
}
57
69
} else {
58
- if (
59
- value . type != null &&
60
- [
61
- 'Point' ,
62
- 'LineString' ,
63
- 'Polygon' ,
64
- 'MultiPoint' ,
65
- 'MultiLineString' ,
66
- 'MultiPolygon' ,
67
- 'GeometryCollection'
68
- ] . includes ( value . type )
69
- ) {
70
+ if ( value . type != null && GEO_TYPES . has ( value . type ) ) {
70
71
//GeoJSON format.
71
- let prefix =
72
- ( info . isMariaDB ( ) && info . hasMinVersion ( 10 , 1 , 4 ) ) || ( ! info . isMariaDB ( ) && info . hasMinVersion ( 5 , 7 , 6 ) )
73
- ? 'ST_'
74
- : '' ;
72
+ const isMariaDb = info . isMariaDB ( ) ;
73
+ const prefix =
74
+ ( isMariaDb && info . hasMinVersion ( 10 , 1 , 4 ) ) || ( ! isMariaDb && info . hasMinVersion ( 5 , 7 , 6 ) ) ? 'ST_' : '' ;
75
+
75
76
switch ( value . type ) {
76
77
case 'Point' :
77
78
out . writeStringAscii (
@@ -136,15 +137,18 @@ class TextEncoder {
136
137
} else {
137
138
if ( opts . permitSetMultiParamEntries ) {
138
139
let first = true ;
139
- for ( let key in value ) {
140
+ for ( const key in value ) {
140
141
const val = value [ key ] ;
141
142
if ( typeof val === 'function' ) continue ;
143
+
142
144
if ( first ) {
143
145
first = false ;
144
146
} else {
145
147
out . writeStringAscii ( ',' ) ;
146
148
}
149
+
147
150
out . writeString ( '`' + key + '`' ) ;
151
+
148
152
if ( val == null ) {
149
153
out . writeStringAscii ( '=NULL' ) ;
150
154
} else {
@@ -164,33 +168,38 @@ class TextEncoder {
164
168
165
169
static geometricCollectionToString ( geo ) {
166
170
if ( ! geo ) return '' ;
171
+
172
+ const len = geo . length ;
167
173
let st = '' ;
168
- for ( let i = 0 ; i < geo . length ; i ++ ) {
174
+
175
+ for ( let i = 0 ; i < len ; i ++ ) {
176
+ const item = geo [ i ] ;
169
177
//GeoJSON format.
170
- st += i !== 0 ? ',' : '' ;
171
- switch ( geo [ i ] . type ) {
178
+ if ( i !== 0 ) st += ',' ;
179
+
180
+ switch ( item . type ) {
172
181
case 'Point' :
173
- st += `POINT(${ TextEncoder . geoPointToString ( geo [ i ] . coordinates ) } )` ;
182
+ st += `POINT(${ TextEncoder . geoPointToString ( item . coordinates ) } )` ;
174
183
break ;
175
184
176
185
case 'LineString' :
177
- st += `LINESTRING(${ TextEncoder . geoArrayPointToString ( geo [ i ] . coordinates ) } )` ;
186
+ st += `LINESTRING(${ TextEncoder . geoArrayPointToString ( item . coordinates ) } )` ;
178
187
break ;
179
188
180
189
case 'Polygon' :
181
- st += `POLYGON(${ TextEncoder . geoMultiArrayPointToString ( geo [ i ] . coordinates ) } )` ;
190
+ st += `POLYGON(${ TextEncoder . geoMultiArrayPointToString ( item . coordinates ) } )` ;
182
191
break ;
183
192
184
193
case 'MultiPoint' :
185
- st += `MULTIPOINT(${ TextEncoder . geoArrayPointToString ( geo [ i ] . coordinates ) } )` ;
194
+ st += `MULTIPOINT(${ TextEncoder . geoArrayPointToString ( item . coordinates ) } )` ;
186
195
break ;
187
196
188
197
case 'MultiLineString' :
189
- st += `MULTILINESTRING(${ TextEncoder . geoMultiArrayPointToString ( geo [ i ] . coordinates ) } )` ;
198
+ st += `MULTILINESTRING(${ TextEncoder . geoMultiArrayPointToString ( item . coordinates ) } )` ;
190
199
break ;
191
200
192
201
case 'MultiPolygon' :
193
- st += `MULTIPOLYGON(${ TextEncoder . geoMultiPolygonToString ( geo [ i ] . coordinates ) } )` ;
202
+ st += `MULTIPOLYGON(${ TextEncoder . geoMultiPolygonToString ( item . coordinates ) } )` ;
194
203
break ;
195
204
}
196
205
}
@@ -199,59 +208,73 @@ class TextEncoder {
199
208
200
209
static geoMultiPolygonToString ( coords ) {
201
210
if ( ! coords ) return '' ;
202
- let st = '' ;
203
- for ( let i = 0 ; i < coords . length ; i ++ ) {
204
- st += ( i !== 0 ? ',(' : '(' ) + TextEncoder . geoMultiArrayPointToString ( coords [ i ] ) + ')' ;
211
+
212
+ const len = coords . length ;
213
+ if ( len === 0 ) return '' ;
214
+
215
+ let st = '(' ;
216
+
217
+ for ( let i = 0 ; i < len ; i ++ ) {
218
+ if ( i !== 0 ) st += ',(' ;
219
+ st += TextEncoder . geoMultiArrayPointToString ( coords [ i ] ) + ')' ;
205
220
}
221
+
206
222
return st ;
207
223
}
208
224
209
225
static geoMultiArrayPointToString ( coords ) {
210
226
if ( ! coords ) return '' ;
211
- let st = '' ;
212
- for ( let i = 0 ; i < coords . length ; i ++ ) {
213
- st += ( i !== 0 ? ',(' : '(' ) + TextEncoder . geoArrayPointToString ( coords [ i ] ) + ')' ;
227
+
228
+ const len = coords . length ;
229
+ if ( len === 0 ) return '' ;
230
+
231
+ let st = '(' ;
232
+
233
+ for ( let i = 0 ; i < len ; i ++ ) {
234
+ if ( i !== 0 ) st += ',(' ;
235
+ st += TextEncoder . geoArrayPointToString ( coords [ i ] ) + ')' ;
214
236
}
237
+
215
238
return st ;
216
239
}
217
240
218
241
static geoArrayPointToString ( coords ) {
219
242
if ( ! coords ) return '' ;
243
+
244
+ const len = coords . length ;
245
+ if ( len === 0 ) return '' ;
246
+
220
247
let st = '' ;
221
- for ( let i = 0 ; i < coords . length ; i ++ ) {
222
- st += ( i !== 0 ? ',' : '' ) + TextEncoder . geoPointToString ( coords [ i ] ) ;
248
+
249
+ for ( let i = 0 ; i < len ; i ++ ) {
250
+ if ( i !== 0 ) st += ',' ;
251
+ st += TextEncoder . geoPointToString ( coords [ i ] ) ;
223
252
}
253
+
224
254
return st ;
225
255
}
226
256
227
257
static geoPointToString ( coords ) {
228
258
if ( ! coords ) return '' ;
229
- return ( isNaN ( coords [ 0 ] ) ? '' : coords [ 0 ] ) + ' ' + ( isNaN ( coords [ 1 ] ) ? '' : coords [ 1 ] ) ;
259
+ const x = isNaN ( coords [ 0 ] ) ? '' : coords [ 0 ] ;
260
+ const y = isNaN ( coords [ 1 ] ) ? '' : coords [ 1 ] ;
261
+ return x + ' ' + y ;
230
262
}
231
263
232
264
static getLocalDate ( date ) {
265
+ const year = date . getFullYear ( ) ;
266
+ const month = date . getMonth ( ) + 1 ;
267
+ const day = date . getDate ( ) ;
268
+ const hours = date . getHours ( ) ;
269
+ const minutes = date . getMinutes ( ) ;
270
+ const seconds = date . getSeconds ( ) ;
233
271
const ms = date . getMilliseconds ( ) ;
234
272
235
- //return 'YYYY-MM-DD HH:MM:SS' datetime format
236
- //see https://mariadb.com/kb/en/library/datetime/
237
- let d =
238
- "'" +
239
- date . getFullYear ( ) +
240
- '-' +
241
- ( date . getMonth ( ) + 1 ) +
242
- '-' +
243
- date . getDate ( ) +
244
- ' ' +
245
- date . getHours ( ) +
246
- ':' +
247
- date . getMinutes ( ) +
248
- ':' +
249
- date . getSeconds ( ) ;
273
+ const d = "'" + year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds ;
274
+
250
275
if ( ms === 0 ) return d + "'" ;
251
276
252
- let res = `${ ms } ` ;
253
- while ( res . length < 3 ) res = '0' + res ;
254
- return d + '.' + res + "'" ;
277
+ return d + '.' + ( ms < 10 ? '00' : ms < 100 ? '0' : '' ) + ms + "'" ;
255
278
}
256
279
257
280
static getFixedFormatDate ( date ) {
@@ -263,9 +286,7 @@ class TextEncoder {
263
286
const sec = date . getSeconds ( ) ;
264
287
const ms = date . getMilliseconds ( ) ;
265
288
266
- //return 'YYYY-MM-DD HH:MM:SS' datetime format
267
- //see https://mariadb.com/kb/en/library/datetime/
268
- return (
289
+ let result =
269
290
"'" +
270
291
formatDigit ( year , 4 ) +
271
292
'-' +
@@ -277,10 +298,13 @@ class TextEncoder {
277
298
':' +
278
299
formatDigit ( min , 2 ) +
279
300
':' +
280
- formatDigit ( sec , 2 ) +
281
- ( ms > 0 ? '.' + formatDigit ( ms , 3 ) : '' ) +
282
- "'"
283
- ) ;
301
+ formatDigit ( sec , 2 ) ;
302
+
303
+ if ( ms > 0 ) {
304
+ result += '.' + formatDigit ( ms , 3 ) ;
305
+ }
306
+
307
+ return result + "'" ;
284
308
}
285
309
}
286
310
0 commit comments