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