|
1 | | -import { QueryCompiled } from "../../core"; |
2 | | -import { Utils, ValueType } from "../../core/utils"; |
3 | | -import { MapperColumn } from "../../mapper-column"; |
4 | | -import { MapperTable } from "../../mapper-table"; |
| 1 | +import { DatabaseBuilderError, QueryCompiled } from "../core"; |
| 2 | +import { Utils, ValueType } from "../core/utils"; |
| 3 | +import { MapperColumn } from "../mapper-column"; |
| 4 | +import { MapperTable } from "../mapper-table"; |
5 | 5 |
|
6 | 6 | export class CommanderBuilder { |
7 | 7 |
|
@@ -55,18 +55,31 @@ export class CommanderBuilder { |
55 | 55 |
|
56 | 56 | public static batchInsert(tableName: string, columnsNames: string[], values: Array<ValueType[]>) |
57 | 57 | : QueryCompiled[] { |
58 | | - return this.splitChunks(values, this.LIMIT_VARIABLES_INSERT).map(valuesChunk => { |
59 | | - return { |
60 | | - params: [].concat(...valuesChunk), |
61 | | - query: Utils.normalizeSqlString( |
62 | | - `INSERT INTO ${tableName} |
| 58 | + if (this.validValues(values)) { |
| 59 | + return this.splitChunks(values, this.LIMIT_VARIABLES_INSERT).map(valuesChunk => { |
| 60 | + return { |
| 61 | + params: [].concat(...valuesChunk), |
| 62 | + query: Utils.normalizeSqlString( |
| 63 | + `INSERT INTO ${tableName} |
63 | 64 | (${columnsNames.join(", ")}) |
64 | 65 | VALUES ${valuesChunk |
65 | | - .map(a => `(${a.map(() => "?").join(", ")})`) |
66 | | - .join(", ")}` |
67 | | - ), |
68 | | - }; |
69 | | - }); |
| 66 | + .map(a => `(${a.map(() => "?").join(", ")})`) |
| 67 | + .join(", ")}` |
| 68 | + ), |
| 69 | + }; |
| 70 | + }); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static validValues(values: Array<ValueType[]>): boolean { |
| 75 | + if (values.length < 1) |
| 76 | + throw new DatabaseBuilderError(`Values not informed`); |
| 77 | + const sizeInnerArray = values?.[0].length; |
| 78 | + if (sizeInnerArray < 1) |
| 79 | + throw new DatabaseBuilderError(`Inner values not informed`); |
| 80 | + if (!values.every(x => x.length === sizeInnerArray)) |
| 81 | + throw new DatabaseBuilderError(`Values with different size not suportted, values: ${JSON.stringify(values)}`); |
| 82 | + return true; |
70 | 83 | } |
71 | 84 |
|
72 | 85 | public static batchInsertColumn<T>(tableName: string, columns: MapperColumn[], models: Array<T>) |
|
0 commit comments