1 | import { EventEmitter } from 'events';
|
2 | import Connection, { type InternalConnectionOptions } from './connection';
|
3 | import { Transform } from 'stream';
|
4 | import { type DataType, type Parameter } from './data-type';
|
5 | import { Collation } from './collation';
|
6 |
|
7 |
|
8 |
|
9 | interface InternalOptions {
|
10 | checkConstraints: boolean;
|
11 | fireTriggers: boolean;
|
12 | keepNulls: boolean;
|
13 | lockTable: boolean;
|
14 | order: {
|
15 | [columnName: string]: 'ASC' | 'DESC';
|
16 | };
|
17 | }
|
18 | export interface Options {
|
19 | |
20 |
|
21 |
|
22 |
|
23 |
|
24 | checkConstraints?: InternalOptions['checkConstraints'] | undefined;
|
25 | |
26 |
|
27 |
|
28 | fireTriggers?: InternalOptions['fireTriggers'] | undefined;
|
29 | |
30 |
|
31 |
|
32 | keepNulls?: InternalOptions['keepNulls'] | undefined;
|
33 | |
34 |
|
35 |
|
36 | lockTable?: InternalOptions['lockTable'] | undefined;
|
37 | |
38 |
|
39 |
|
40 | order?: InternalOptions['order'] | undefined;
|
41 | }
|
42 | export type Callback =
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | (err: Error | undefined | null, rowCount?: number) => void;
|
49 | interface Column extends Parameter {
|
50 | objName: string;
|
51 | collation: Collation | undefined;
|
52 | }
|
53 | interface ColumnOptions {
|
54 | output?: boolean;
|
55 | |
56 |
|
57 |
|
58 | length?: number;
|
59 | |
60 |
|
61 |
|
62 | precision?: number;
|
63 | |
64 |
|
65 |
|
66 | scale?: number;
|
67 | |
68 |
|
69 |
|
70 | objName?: string;
|
71 | |
72 |
|
73 |
|
74 | nullable?: boolean;
|
75 | }
|
76 | declare class RowTransform extends Transform {
|
77 | |
78 |
|
79 |
|
80 | columnMetadataWritten: boolean;
|
81 | |
82 |
|
83 |
|
84 | bulkLoad: BulkLoad;
|
85 | |
86 |
|
87 |
|
88 | mainOptions: BulkLoad['options'];
|
89 | |
90 |
|
91 |
|
92 | columns: BulkLoad['columns'];
|
93 | |
94 |
|
95 |
|
96 | constructor(bulkLoad: BulkLoad);
|
97 | /**
|
98 | * @private
|
99 | */
|
100 | _transform(row: Array<unknown> | {
|
101 | [colName: string]: unknown;
|
102 | }, _encoding: string, callback: (error?: Error) => void): void;
|
103 | /**
|
104 | * @private
|
105 | */
|
106 | _flush(callback: () => void): void;
|
107 | }
|
108 | /**
|
109 | * A BulkLoad instance is used to perform a bulk insert.
|
110 | *
|
111 | * Use [[Connection.newBulkLoad]] to create a new instance, and [[Connection.execBulkLoad]] to execute it.
|
112 | *
|
113 | * Example of BulkLoad Usages:
|
114 | *
|
115 | * ```js
|
116 | * // optional BulkLoad options
|
117 | * const options = { keepNulls: true };
|
118 | *
|
119 | *
|
120 | * const bulkLoad = connection.newBulkLoad('MyTable', options, (error, rowCount) => {
|
121 | * console.log('inserted %d rows', rowCount);
|
122 | * });
|
123 | *
|
124 | *
|
125 | * bulkLoad.addColumn('myInt', TYPES.Int, { nullable: false });
|
126 | * bulkLoad.addColumn('myString', TYPES.NVarChar, { length: 50, nullable: true });
|
127 | *
|
128 | *
|
129 | * connection.execBulkLoad(bulkLoad, [
|
130 | * { myInt: 7, myString: 'hello' },
|
131 | * { myInt: 23, myString: 'world' }
|
132 | * ]);
|
133 | * ```
|
134 | */
|
135 | declare class BulkLoad extends EventEmitter {
|
136 | /**
|
137 | * @private
|
138 | */
|
139 | error: Error | undefined;
|
140 | /**
|
141 | * @private
|
142 | */
|
143 | canceled: boolean;
|
144 | /**
|
145 | * @private
|
146 | */
|
147 | executionStarted: boolean;
|
148 | /**
|
149 | * @private
|
150 | */
|
151 | streamingMode: boolean;
|
152 | /**
|
153 | * @private
|
154 | */
|
155 | table: string;
|
156 | /**
|
157 | * @private
|
158 | */
|
159 | timeout: number | undefined;
|
160 | /**
|
161 | * @private
|
162 | */
|
163 | options: InternalConnectionOptions;
|
164 | /**
|
165 | * @private
|
166 | */
|
167 | callback: Callback;
|
168 | /**
|
169 | * @private
|
170 | */
|
171 | columns: Array<Column>;
|
172 | /**
|
173 | * @private
|
174 | */
|
175 | columnsByName: {
|
176 | [name: string]: Column;
|
177 | };
|
178 | /**
|
179 | * @private
|
180 | */
|
181 | firstRowWritten: boolean;
|
182 | /**
|
183 | * @private
|
184 | */
|
185 | rowToPacketTransform: RowTransform;
|
186 | /**
|
187 | * @private
|
188 | */
|
189 | bulkOptions: InternalOptions;
|
190 | /**
|
191 | * @private
|
192 | */
|
193 | connection: Connection | undefined;
|
194 | /**
|
195 | * @private
|
196 | */
|
197 | rows: Array<any> | undefined;
|
198 | /**
|
199 | * @private
|
200 | */
|
201 | rst: Array<any> | undefined;
|
202 | /**
|
203 | * @private
|
204 | */
|
205 | rowCount: number | undefined;
|
206 | collation: Collation | undefined;
|
207 | /**
|
208 | * @private
|
209 | */
|
210 | constructor(table: string, collation: Collation | undefined, connectionOptions: InternalConnectionOptions, { checkConstraints, fireTriggers, keepNulls, lockTable, order, }: Options, callback: Callback);
|
211 | /**
|
212 | * Adds a column to the bulk load.
|
213 | *
|
214 | * The column definitions should match the table you are trying to insert into.
|
215 | * Attempting to call addColumn after the first row has been added will throw an exception.
|
216 | *
|
217 | * ```js
|
218 | * bulkLoad.addColumn('MyIntColumn', TYPES.Int, { nullable: false });
|
219 | * ```
|
220 | *
|
221 | * @param name The name of the column.
|
222 | * @param type One of the supported `data types`.
|
223 | * @param __namedParameters Additional column type information. At a minimum, `nullable` must be set to true or false.
|
224 | * @param length For VarChar, NVarChar, VarBinary. Use length as `Infinity` for VarChar(max), NVarChar(max) and VarBinary(max).
|
225 | * @param nullable Indicates whether the column accepts NULL values.
|
226 | * @param objName If the name of the column is different from the name of the property found on `rowObj` arguments passed to [[addRow]] or [[Connection.execBulkLoad]], then you can use this option to specify the property name.
|
227 | * @param precision For Numeric, Decimal.
|
228 | * @param scale For Numeric, Decimal, Time, DateTime2, DateTimeOffset.
|
229 | */
|
230 | addColumn(name: string, type: DataType, { output, length, precision, scale, objName, nullable }: ColumnOptions): void;
|
231 | /**
|
232 | * @private
|
233 | */
|
234 | getOptionsSql(): string;
|
235 | /**
|
236 | * @private
|
237 | */
|
238 | getBulkInsertSql(): string;
|
239 | /**
|
240 | * This is simply a helper utility function which returns a `CREATE TABLE SQL` statement based on the columns added to the bulkLoad object.
|
241 | * This may be particularly handy when you want to insert into a temporary table (a table which starts with `#`).
|
242 | *
|
243 | * ```js
|
244 | * var sql = bulkLoad.getTableCreationSql();
|
245 | * ```
|
246 | *
|
247 | * A side note on bulk inserting into temporary tables: if you want to access a local temporary table after executing the bulk load,
|
248 | * you'll need to use the same connection and execute your requests using [[Connection.execSqlBatch]] instead of [[Connection.execSql]]
|
249 | */
|
250 | getTableCreationSql(): string;
|
251 | /**
|
252 | * @private
|
253 | */
|
254 | getColMetaData(): Buffer;
|
255 | /**
|
256 | * Sets a timeout for this bulk load.
|
257 | *
|
258 | * ```js
|
259 | * bulkLoad.setTimeout(timeout);
|
260 | * ```
|
261 | *
|
262 | * @param timeout The number of milliseconds before the bulk load is considered failed, or 0 for no timeout.
|
263 | * When no timeout is set for the bulk load, the [[ConnectionOptions.requestTimeout]] of the Connection is used.
|
264 | */
|
265 | setTimeout(timeout?: number): void;
|
266 | /**
|
267 | * @private
|
268 | */
|
269 | createDoneToken(): Buffer;
|
270 | /**
|
271 | * @private
|
272 | */
|
273 | cancel(): void;
|
274 | }
|
275 | export default BulkLoad;
|
276 |
|
\ | No newline at end of file |