@@ -2,61 +2,63 @@ import { DatabaseBuilderError, QueryCompiled } from "../core";
2
2
import { Utils , ValueType } from "../core/utils" ;
3
3
import { MapperColumn } from "../mapper-column" ;
4
4
import { MapperTable } from "../mapper-table" ;
5
+ import { ConfigCommander } from "./config-commander" ;
5
6
6
7
export class CommanderBuilder {
7
8
8
- public static delete ( tableName : string )
9
+ constructor ( private _config : ConfigCommander ) {
10
+ }
11
+
12
+ public delete ( tableName : string )
9
13
: QueryCompiled {
10
14
return {
11
15
params : [ ] ,
12
16
query : `DELETE FROM ${ tableName } ` ,
13
17
} ;
14
18
}
15
19
16
- public static deleteMapper < T > ( mapper : MapperTable )
20
+ public deleteMapper < T > ( mapper : MapperTable )
17
21
: QueryCompiled {
18
22
return this . delete ( mapper . tableName ) ;
19
23
}
20
24
21
- public static update ( tableName : string , columnsNames : string [ ] , params : ValueType [ ] )
25
+ public update ( tableName : string , columnsNames : string [ ] , params : ValueType [ ] )
22
26
: QueryCompiled {
23
27
return {
24
28
params : [ ] . concat ( params ) ,
25
29
query : `UPDATE ${ tableName } SET ${ columnsNames . join ( ", " ) } ` ,
26
30
} ;
27
31
}
28
32
29
- public static updateColumn < T > ( tableName : string , columns : MapperColumn [ ] , model : T )
33
+ public updateColumn < T > ( tableName : string , columns : MapperColumn [ ] , model : T )
30
34
: QueryCompiled {
31
35
return this . update ( tableName , columns . map ( x => x . column ) , columns . map ( column => Utils . getValue < any , any > ( model , column . fieldReference ) ?. [ 0 ] ) ) ;
32
36
}
33
37
34
- public static updateMapper < T > ( mapper : MapperTable , model : T )
38
+ public updateMapper < T > ( mapper : MapperTable , model : T )
35
39
: QueryCompiled {
36
40
return this . updateColumn ( mapper . tableName , mapper . columns , model ) ;
37
41
}
38
42
39
- public static insert ( tableName : string , columnsNames : string [ ] , params : ValueType [ ] )
43
+ public insert ( tableName : string , columnsNames : string [ ] , params : ValueType [ ] )
40
44
: QueryCompiled {
41
45
return this . batchInsert ( tableName , columnsNames , [ params ] ) [ 0 ] ;
42
46
}
43
47
44
- public static insertColumn < T > ( tableName : string , columns : MapperColumn [ ] , model : T )
48
+ public insertColumn < T > ( tableName : string , columns : MapperColumn [ ] , model : T )
45
49
: QueryCompiled {
46
50
return this . batchInsertColumn ( tableName , columns , [ model ] ) [ 0 ] ;
47
51
}
48
52
49
- public static insertMapper < T > ( mapper : MapperTable , model : T )
53
+ public insertMapper < T > ( mapper : MapperTable , model : T )
50
54
: QueryCompiled {
51
55
return this . batchInsertMapper ( mapper , [ model ] ) [ 0 ] ;
52
56
}
53
57
54
- private static LIMIT_VARIABLES_INSERT = 10000 ;
55
-
56
- public static batchInsert ( tableName : string , columnsNames : string [ ] , values : Array < ValueType [ ] > )
58
+ public batchInsert ( tableName : string , columnsNames : string [ ] , values : Array < ValueType [ ] > )
57
59
: QueryCompiled [ ] {
58
60
if ( this . validValues ( values ) ) {
59
- return this . splitChunks ( values , this . LIMIT_VARIABLES_INSERT ) . map ( valuesChunk => {
61
+ return this . splitChunks ( values , Math . floor ( this . _config . sqliteLimitVariables / columnsNames . length ) ) . map ( valuesChunk => {
60
62
return {
61
63
params : [ ] . concat ( ...valuesChunk ) ,
62
64
query : Utils . normalizeSqlString (
@@ -71,7 +73,7 @@ export class CommanderBuilder {
71
73
}
72
74
}
73
75
74
- private static validValues ( values : Array < ValueType [ ] > ) : boolean {
76
+ private validValues ( values : Array < ValueType [ ] > ) : boolean {
75
77
if ( values . length < 1 )
76
78
throw new DatabaseBuilderError ( `Values not informed` ) ;
77
79
const sizeInnerArray = values ?. [ 0 ] . length ;
@@ -82,7 +84,7 @@ export class CommanderBuilder {
82
84
return true ;
83
85
}
84
86
85
- public static batchInsertColumn < T > ( tableName : string , columns : MapperColumn [ ] , models : Array < T > )
87
+ public batchInsertColumn < T > ( tableName : string , columns : MapperColumn [ ] , models : Array < T > )
86
88
: QueryCompiled [ ] {
87
89
return this . batchInsert ( tableName , columns . map ( x => x . column ) ,
88
90
models . map ( model => {
@@ -91,12 +93,12 @@ export class CommanderBuilder {
91
93
) ;
92
94
}
93
95
94
- public static batchInsertMapper < T > ( mapper : MapperTable , models : Array < T > )
96
+ public batchInsertMapper < T > ( mapper : MapperTable , models : Array < T > )
95
97
: QueryCompiled [ ] {
96
98
return this . batchInsertColumn ( mapper . tableName , mapper . columns , models ) ;
97
99
}
98
100
99
- private static splitChunks ( sourceArray : any [ ] , chunkSize : number ) : any [ ] [ ] {
101
+ private splitChunks ( sourceArray : any [ ] , chunkSize : number ) : any [ ] [ ] {
100
102
const result : any [ ] [ ] = [ ] ;
101
103
for ( var i = 0 ; i < sourceArray . length ; i += chunkSize ) {
102
104
result [ i / chunkSize ] = sourceArray . slice ( i , i + chunkSize ) ;
0 commit comments