This repository was archived by the owner on Apr 29, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +24
-14
lines changed Expand file tree Collapse file tree 2 files changed +24
-14
lines changed Original file line number Diff line number Diff line change 1
- import type { Connection , PoolConnection } from 'mysql2/promise' ;
1
+ import type { Connection , PoolConnection , TypeCast } from 'mysql2/promise' ;
2
2
import { scheduleTick } from '../utils/scheduleTick' ;
3
3
import { sleep } from '../utils/sleep' ;
4
4
import { pool } from './pool' ;
5
5
import type { CFXParameters } from 'types' ;
6
+ import { typeCastExecute } from 'utils/typeCast' ;
6
7
7
8
( Symbol as any ) . dispose ??= Symbol ( 'Symbol.dispose' ) ;
8
9
@@ -34,7 +35,11 @@ export class MySql {
34
35
async execute ( query : string , values : CFXParameters = [ ] ) {
35
36
scheduleTick ( ) ;
36
37
37
- const [ result ] = await this . connection . execute ( query , values ) ;
38
+ const [ result ] = await this . connection . execute ( {
39
+ sql : query ,
40
+ values : values ,
41
+ typeCast : typeCastExecute as TypeCast ,
42
+ } ) ;
38
43
return result ;
39
44
}
40
45
Original file line number Diff line number Diff line change 1
- import { FieldPacket } from 'mysql2' ;
1
+ import type { TypeCastField , TypeCastNext } from 'mysql2/promise ' ;
2
2
3
3
const BINARY_CHARSET = 63 ;
4
4
5
- type Field = {
6
- type : string ;
7
- length : number ;
8
- packet : FieldPacket ;
5
+ interface Field extends TypeCastField {
9
6
charset : number ;
10
- string : ( ) => string ;
11
- buffer : ( ) => number [ ] ;
12
- } ;
7
+ }
13
8
14
- export const typeCast = ( field : Field , next : ( ) => void ) => {
9
+ /**
10
+ * node-mysql2 v3.9.0 introduced (breaking) typecasting for execute methods.
11
+ */
12
+ export function typeCastExecute ( field : Field , next : TypeCastNext ) {
13
+ return next ( ) ;
14
+ }
15
+
16
+ /**
17
+ * mysql-async compatible typecasting.
18
+ */
19
+ export function typeCast ( field : Field , next : TypeCastNext ) {
15
20
switch ( field . type ) {
16
21
case 'DATETIME' :
17
22
case 'DATETIME2' :
18
23
case 'TIMESTAMP' :
19
24
case 'TIMESTAMP2' :
20
25
case 'NEWDATE' :
21
- return new Date ( field . string ( ) ) . getTime ( ) ;
26
+ return new Date ( field . string ( ) || '' ) . getTime ( ) ;
22
27
case 'DATE' :
23
28
return new Date ( field . string ( ) + ' 00:00:00' ) . getTime ( ) ;
24
29
case 'TINY' :
25
30
return field . length === 1 ? field . string ( ) === '1' : next ( ) ;
26
31
case 'BIT' :
27
- return field . length === 1 ? field . buffer ( ) [ 0 ] === 1 : field . buffer ( ) [ 0 ] ;
32
+ return field . length === 1 ? field . buffer ( ) ?. [ 0 ] === 1 : field . buffer ( ) ?. [ 0 ] ;
28
33
case 'TINY_BLOB' :
29
34
case 'MEDIUM_BLOB' :
30
35
case 'LONG_BLOB' :
@@ -38,4 +43,4 @@ export const typeCast = (field: Field, next: () => void) => {
38
43
default :
39
44
return next ( ) ;
40
45
}
41
- } ;
46
+ }
You can’t perform that action at this time.
0 commit comments