-
Notifications
You must be signed in to change notification settings - Fork 25
/
sql-bricks.d.ts
408 lines (357 loc) · 17.8 KB
/
sql-bricks.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Type definitions for sql-bricks 2.0
// Project: http://csnw.github.io/sql-bricks
// Definitions by: Narcisse Assogba <https://github.com/adn05>
// Paleo <https://github.com/paleo>
declare namespace SqlBricks {
/**
* Statement is an abstract base class for all statements (SELECT, INSERT, UPDATE, DELETE)
* and should never be instantiated directly. It is exposed because it can be used with the
* instanceof operator to easily determine whether something is a SQL Bricks statement: my_var instanceof Statement.
*/
interface Statement {
/**
* Clones a statement so that subsequent modifications do not affect the original statement.
*/
clone(): this
/**
* Returns the non-parameterized SQL for the statement. This is called implicitly by Javascript when using a Statement anywhere that a string is expected (string concatenation, Array.join(), etc).
* While toString() is easy to use, it is not recommended in most cases because:
* It doesn't provide robust protection against SQL injection attacks (it just does basic escaping)
* It doesn't provide as much support for complex data types (objects, arrays, etc, are "stringified" before being passed to your database driver, which then has to interpret them correctly)
* It does not provide the same level of detail in error messages (see this issue)
* For the above reasons, it is usually better to use toParams().
*/
toString(): string
/**
* Returns an object with two properties: a parameterized text string and a values array. The values are populated with anything on the right-hand side
* of a WHERE criteria,as well as any values passed into an insert() or update() (they can be passed explicitly with val() or opted out of with sql())
* @param options A placeholder option of '?%d' can be passed to generate placeholders compatible with node-sqlite3 (%d is replaced with the parameter #):
* @example
* update('person', {'first_name': 'Fred'}).where({'last_name': 'Flintstone'}).toParams({placeholder: '?%d'});
* // {"text": "UPDATE person SET first_name = ?1 WHERE last_name = ?2", "values": ["Fred", "Flintstone"]}
*/
toParams(options?: { placeholder: string }): SqlBricksParam
}
interface SqlBricksParam {
text: string
values: any[]
}
type TableName = string | SelectStatement
interface OnCriteria {
[column: string]: string
}
interface WhereObject {
[column: string]: any
}
interface WhereGroup {
op?: string
expressions: WhereExpression[]
}
interface WhereBinary {
op: string
col: string | SelectStatement
val: any
quantifier: string
}
/**
* When a non-expression object is passed somewhere a whereExpression is expected,
* each key/value pair will be ANDed together:
*/
type WhereExpression = WhereGroup | WhereBinary | WhereObject | string
/**
* A SELECT statement
*/
interface SelectStatement extends Statement {
/**
* Appends additional columns to an existing query.
* @param columns can be passed as multiple arguments, a comma-delimited string or an array.
*/
select(...columns: Array<string | SelectStatement>): SelectStatement
/**
* Appends additional columns to an existing query.
* @param columns can be passed as multiple arguments, a comma-delimited string or an array.
*/
select(columns: string[] | SelectStatement[]): SelectStatement
as(alias: string): SelectStatement
distinct(...columns: Array<string | SelectStatement>): SelectStatement
distinct(columns: string[] | SelectStatement[]): SelectStatement
/**
* Makes the query a SELECT ... INTO query (which creates a new table with the results of the query).
* @alias intoTable
* @param tbl new table to create
*/
into(tbl: TableName): SelectStatement
/**
* Makes the query a SELECT ... INTO query (which creates a new table with the results of the query).
* @alias into
* @param tbl new table to create
*/
intoTable(tbl: TableName): SelectStatement
intoTemp(tbl: TableName): SelectStatement
intoTempTable(tbl: TableName): SelectStatement
/**
* Table names can be passed in as multiple string arguments, a comma-delimited string or an array.
* @param tbls table names
*/
from(...tbls: TableName[]): SelectStatement
/**
* Table names can be passed in as multiple string arguments, a comma-delimited string or an array.
* @param tbls array of table names
*/
from(tbls: TableName[]): SelectStatement
/**
* Adds the specified join to the query.
* @alias innerJoin
* @param tbl can include an alias after a space or after the 'AS' keyword ('my_table my_alias').
* @param onCriteria is optional if a joinCriteria function has been supplied.
*/
join(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
join(tbl: string, onCol1: string, onCol2: string): SelectStatement
join(firstTbl: string, ...otherTbls: string[]): SelectStatement
leftJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
leftJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
leftJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
rightJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
rightJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
rightJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
fullJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
fullJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
fullJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
crossJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
crossJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
crossJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
innerJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
innerJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
innerJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
leftOuterJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
leftOuterJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
leftOuterJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
rightOuterJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
rightOuterJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
rightOuterJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
fullOuterJoin(tbl: string, criteria?: OnCriteria | string[] | WhereExpression): SelectStatement
fullOuterJoin(tbl: string, onCol1: string, onCol2: string): SelectStatement
fullOuterJoin(firstTbl: string, ...otherTbls: string[]): SelectStatement
on(onCriteria: OnCriteria | WhereExpression): SelectStatement
on(onCol1: string, onCol2: string): SelectStatement
/**
* Joins using USING instead of ON.
* @param columnList columnList can be passed in as one or more string arguments, a comma-delimited string, or an array.
* @example
* select('*').from('person').join('address').using('address_id', 'country_id');
* // SELECT * FROM person INNER JOIN address USING (address_id, country_id)
*/
using(...columnList: string[]): SelectStatement
using(columnList: string[]): SelectStatement
/**
* Adds the specified natural join to the query.
* @param tbl can include an alias after a space or after the 'AS' keyword ('my_table my_alias').
*/
naturalJoin(tbl: string): SelectStatement
naturalLeftJoin(tbl: string): SelectStatement
naturalRightJoin(tbl: string): SelectStatement
naturalFullJoin(tbl: string): SelectStatement
naturalInnerJoin(tbl: string): SelectStatement
naturalLeftOuterJoin(tbl: string): SelectStatement
naturalRightOuterJoin(tbl: string): SelectStatement
naturalFullOuterJoin(tbl: string): SelectStatement
where(column?: string | null, value?: any): SelectStatement
where(...whereExpr: WhereExpression[]): SelectStatement
and(...options: any[]): SelectStatement
/**
* Sets or extends the GROUP BY columns.
* @param columns can take multiple arguments, a single comma-delimited string or an array.
*/
groupBy(...columns: string[]): SelectStatement
groupBy(columns: string[]): SelectStatement
having(column: string, value: string): SelectStatement
having(whereExpr: WhereExpression): SelectStatement
/**
* Sets or extends the list of columns in the ORDER BY clause.
* @param columns can be passed as multiple arguments, a single comma-delimited string or an array.
*/
orderBy(...columns: string[]): SelectStatement
orderBy(columns: string[]): SelectStatement
order(...columns: string[]): SelectStatement
order(columns: string[]): SelectStatement
forUpdate(...tbls: string[]): SelectStatement
of(tlb: string): SelectStatement
noWait(): SelectStatement
union(...stmt: Statement[]): SelectStatement
intersect(...stmt: Statement[]): SelectStatement
minus(...stmt: Statement[]): SelectStatement
except(...stmt: Statement[]): SelectStatement
}
/**
* An INSERT statement
*/
interface InsertStatement extends Statement {
into(tbl: TableName, ...columns: any[]): InsertStatement
intoTable(tbl: TableName, ...columns: any[]): InsertStatement
select(...columns: Array<string | SelectStatement>): InsertStatement
select(columns: string[] | SelectStatement[]): InsertStatement
values(...values: any[]): InsertStatement
}
/**
* An UPDATE statement
*/
interface UpdateStatement extends Statement {
values(...values: any[]): UpdateStatement
set(...values: any[]): UpdateStatement
where(column?: string | null, value?: any): UpdateStatement
where(...whereExpr: WhereExpression[]): UpdateStatement
and(column?: string | null, value?: any): UpdateStatement
and(...whereExpr: WhereExpression[]): UpdateStatement
}
/**
* A DELETE statement
*/
interface DeleteStatement extends Statement {
from(...tbls: string[]): DeleteStatement
using(...columnList: string[]): DeleteStatement
using(columnList: string[]): DeleteStatement
where(column?: string | null, value?: any): DeleteStatement
where(...whereExpr: WhereExpression[]): DeleteStatement
and(column?: string | null, value?: any): DeleteStatement
and(...whereExpr: WhereExpression[]): DeleteStatement
}
}
interface SqlBricksFn {
(...params: any[]): any
/**
* Wraps a value (user-supplied string, number, boolean, etc) so that it can be passed into SQL Bricks
* anywhere that a column is expected (the left-hand side of WHERE criteria and many other SQL Bricks APIs)
* @param value value to be wraped
*/
val(value: any): any
/**
* Returns a new INSERT statement. It can be used with or without the new operator.
* @alias insertInto
* @param tbl table name
* @param values a values object or a columns list. Passing a set of columns (as multiple arguments, a comma-delimited string or an array)
* will put the statement into split keys/values mode, where a matching array of values is expected in values()
* @example
* insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});
* // INSERT INTO person (first_name, last_name) VALUES ('Fred', 'Flintstone')
*/
insert(tbl?: string, ...values: any[]): SqlBricks.InsertStatement
/**
* Returns a new INSERT statement. It can be used with or without the new operator.
* @alias insert
* @param tbl table name
* @param values a values object or a columns list. Passing a set of columns (as multiple arguments, a comma-delimited string or an array)
* will put the statement into split keys/values mode, where a matching array of values is expected in values()
* @example
* insert('person', {'first_name': 'Fred', 'last_name': 'Flintstone'});
* // INSERT INTO person (first_name, last_name) VALUES ('Fred', 'Flintstone')
*/
insertInto(tbl?: string, ...values: any[]): SqlBricks.InsertStatement
/**
* Returns a new select statement, seeded with a set of columns. It can be used with or without the new keyword.
* @param columns it can be passed in here (or appended later via sel.select() or sel.distinct()) via multiple arguments
* or a comma-delimited string or an array. If no columns are specified, toString() will default to SELECT *.
*/
select(...columns: Array<string | SqlBricks.SelectStatement>): SqlBricks.SelectStatement
select(columns: string[] | SqlBricks.SelectStatement[]): SqlBricks.SelectStatement
/**
* Returns a new UPDATE statement. It can be used with or without the new operator.
* @param tbl table name
* @param values
*/
update(tbl: string, ...values: any[]): SqlBricks.UpdateStatement
/**
* Returns a new DELETE statement. It can be used with or without the new operator.
* @alias deleteFrom
* @param tbl table name
*/
delete(tbl?: string): SqlBricks.DeleteStatement
/**
* Returns a new DELETE statement. It can be used with or without the new operator.
* @alias delete
* @param tbl table name
*/
deleteFrom(tbl?: string): SqlBricks.DeleteStatement
/**
* Registers a set of frequently-used table aliases with SQL Bricks.
* These table aliases can then be used by themselves in from(), join(), etc
* and SQL Bricks will automatically expand them to include the table name as well as the alias.
* @param expansions
* @example
* sql.aliasExpansions({'psn': 'person', 'addr': 'address', 'zip': 'zipcode', 'usr': 'user'});
* select().from('psn').join('addr', {'psn.addr_id': 'addr.id'});
* // SELECT * FROM person psn INNER JOIN address addr ON psn.addr_id = addr.id
*/
aliasExpansions(expansions: { [tbl: string]: string }): void
/**
* Sets a user-supplied function to automatically generate the .on() criteria for joins whenever it is not supplied explicitly.
* @param func
*/
joinCriteria(func?: (...args: any[]) => SqlBricks.OnCriteria): any
_extension(): any
prop: number
conversions: any
//////////////////////////////////////////
////// Where Expression functions //////
//////////////////////////////////////////
/**
* Joins the passed expressions with AND
* @param whereExprs
*/
and(...whereExprs: SqlBricks.WhereExpression[]): SqlBricks.WhereGroup
/**
* Joins the passed expressions with OR:
* @param whereExprs
*/
or(...whereExprs: SqlBricks.WhereExpression[]): SqlBricks.WhereGroup
/**
* Negates the expression by wrapping it in NOT (...)
* (if it is at the top level, the parentheses are unnecessary and will be omitted)
* @param whereExpr
*/
not(whereExpr: SqlBricks.WhereExpression): SqlBricks.WhereGroup
/**
* Generates a BETWEEN
* @param column
* @param value1
* @param value2
*/
between(column: string, value1: any, value2: any): SqlBricks.WhereExpression
isNull(column: string): SqlBricks.WhereExpression
isNotNull(column: string): SqlBricks.WhereExpression
like(column: string, value: any, escapeStr?: string): SqlBricks.WhereExpression
exists(stmt: any): SqlBricks.WhereExpression
in(column: string, stmt: SqlBricks.SelectStatement): SqlBricks.WhereExpression
in(column: string, ...values: any[]): SqlBricks.WhereExpression
/**
* Generates the appropriate relational operator (=, <>, <, <=, > or >=).
* @param column column name or query result
* @param value column value
*/
eq(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
equal(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
notEq(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
lt(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
lte(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gt(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gte(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
eqAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
notEqAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
ltAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
lteAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gtAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gteAll(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
eqAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
notEqAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
ltAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
lteAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gtAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gteAny(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
eqSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
notEqSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
ltSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
lteSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gtSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
gteSome(column: string | SqlBricks.SelectStatement, value?: any): SqlBricks.WhereBinary
}
declare const SqlBricks: SqlBricksFn
export = SqlBricks