|
1 |
| -module.exports = { |
2 |
| - staticText(text) { |
3 |
| - return { |
4 |
| - sql: text, |
5 |
| - } |
6 |
| - }, |
7 |
| - whateverTheyPutIn(clausePartsJoinedBy, partsJoinedBy, ...expressions) { |
8 |
| - const { sql, values } = joinExpressions(expressions, partsJoinedBy) |
9 |
| - return { |
10 |
| - sql, |
11 |
| - values, |
12 |
| - joinedBy: clausePartsJoinedBy, |
13 |
| - } |
14 |
| - }, |
15 |
| - tableNameOrSubquery(table, alias) { |
16 |
| - if (hasABuildFunction(table)) { |
17 |
| - const result = table.build(`\n\t`) |
18 |
| - |
19 |
| - return { |
20 |
| - sql: combineWithAlias(`(\n\t${ result.sql }\n)`, alias), |
21 |
| - values: result.values, |
22 |
| - } |
23 |
| - } else { |
24 |
| - return { |
25 |
| - sql: combineWithAlias(table, alias), |
26 |
| - } |
27 |
| - } |
28 |
| - }, |
29 |
| - columnParam(joinedBy, opts, expression, comparator, value) { |
30 |
| - opts = opts || {} |
31 |
| - |
32 |
| - const expressionObject = expressionToObject(expression) |
33 |
| - |
34 |
| - if (comparator === undefined) { |
35 |
| - if (opts.like) { |
36 |
| - throw new Error(`You can't use a "like" comparison without passing in a value`) |
37 |
| - } |
38 |
| - return { joinedBy, ...expressionObject } |
39 |
| - } else if (value === undefined) { |
40 |
| - value = comparator |
41 |
| - comparator = undefined |
42 |
| - } |
43 |
| - |
44 |
| - const valueIsObject = (value && typeof value === `object` && value.values && typeof value.values === `object`) |
45 |
| - |
46 |
| - const valueParams = valueIsObject |
47 |
| - ? value.values |
48 |
| - : [ value ] |
49 |
| - |
50 |
| - const values = [ ...expressionObject.values, ...valueParams ] |
51 |
| - |
52 |
| - const comparatorAndValue = valueIsObject |
53 |
| - ? getComparison(opts.like, comparator) + ` ` + value.sql |
54 |
| - : getComparisonAndParameterString(value, opts.like, comparator) |
55 |
| - |
56 |
| - return { |
57 |
| - sql: `${ expressionObject.sql } ${ comparatorAndValue }`, |
58 |
| - values, |
59 |
| - joinedBy, |
60 |
| - } |
61 |
| - }, |
62 |
| - joinClauseHandler(type, table, alias, on) { |
63 |
| - if (!on) { |
64 |
| - on = alias |
65 |
| - alias = undefined |
66 |
| - } |
67 |
| - |
68 |
| - function joinString() { |
69 |
| - return `${ type }JOIN ` |
70 |
| - } |
71 |
| - |
72 |
| - let onParams = [] |
73 |
| - let onString = `` |
74 |
| - |
75 |
| - if (on) { |
76 |
| - if (on.values) { |
77 |
| - onParams = on.values |
78 |
| - onString = makeOn(on.sql) |
79 |
| - } else { |
80 |
| - onString = makeOn(on) |
81 |
| - } |
82 |
| - } |
83 |
| - |
84 |
| - |
85 |
| - if (hasABuildFunction(table)) { |
86 |
| - const result = table.build(`\n\t`) |
87 |
| - |
88 |
| - return { |
89 |
| - sql: joinString() + combineWithAlias(`(\n\t${ result.sql }\n)`, alias) + onString, |
90 |
| - values: [ ...result.values, ...onParams ], |
91 |
| - joinedBy: `\n`, |
92 |
| - } |
93 |
| - } else { |
94 |
| - return { |
95 |
| - sql: joinString() + combineWithAlias(table, alias) + onString, |
96 |
| - values: onParams, |
97 |
| - joinedBy: `\n`, |
98 |
| - } |
99 |
| - } |
100 |
| - }, |
101 |
| -} |
102 |
| - |
103 | 1 | const makeOn = on => on ? ` ON ${ on }` : ``
|
104 | 2 |
|
105 | 3 | const expressionToObject = expression => {
|
@@ -147,3 +45,114 @@ function hasABuildFunction(q) {
|
147 | 45 | function combineWithAlias(str, alias) {
|
148 | 46 | return alias ? (`${ str } AS ${ alias }`) : str
|
149 | 47 | }
|
| 48 | + |
| 49 | +export { |
| 50 | + staticText, |
| 51 | + whateverTheyPutIn, |
| 52 | + tableNameOrSubquery, |
| 53 | + columnParam, |
| 54 | + joinClauseHandler, |
| 55 | +} |
| 56 | + |
| 57 | +function staticText(text) { |
| 58 | + return { |
| 59 | + sql: text, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function whateverTheyPutIn(clausePartsJoinedBy, partsJoinedBy, ...expressions) { |
| 64 | + const { sql, values } = joinExpressions(expressions, partsJoinedBy) |
| 65 | + return { |
| 66 | + sql, |
| 67 | + values, |
| 68 | + joinedBy: clausePartsJoinedBy, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +function tableNameOrSubquery(table, alias) { |
| 73 | + if (hasABuildFunction(table)) { |
| 74 | + const result = table.build(`\n\t`) |
| 75 | + |
| 76 | + return { |
| 77 | + sql: combineWithAlias(`(\n\t${ result.sql }\n)`, alias), |
| 78 | + values: result.values, |
| 79 | + } |
| 80 | + } else { |
| 81 | + return { |
| 82 | + sql: combineWithAlias(table, alias), |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +function columnParam(joinedBy, opts, expression, comparator, value) { |
| 88 | + opts = opts || {} |
| 89 | + |
| 90 | + const expressionObject = expressionToObject(expression) |
| 91 | + |
| 92 | + if (comparator === undefined) { |
| 93 | + if (opts.like) { |
| 94 | + throw new Error(`You can't use a "like" comparison without passing in a value`) |
| 95 | + } |
| 96 | + return { joinedBy, ...expressionObject } |
| 97 | + } else if (value === undefined) { |
| 98 | + value = comparator |
| 99 | + comparator = undefined |
| 100 | + } |
| 101 | + |
| 102 | + const valueIsObject = (value && typeof value === `object` && value.values && typeof value.values === `object`) |
| 103 | + |
| 104 | + const valueParams = valueIsObject |
| 105 | + ? value.values |
| 106 | + : [ value ] |
| 107 | + |
| 108 | + const values = [ ...expressionObject.values, ...valueParams ] |
| 109 | + |
| 110 | + const comparatorAndValue = valueIsObject |
| 111 | + ? getComparison(opts.like, comparator) + ` ` + value.sql |
| 112 | + : getComparisonAndParameterString(value, opts.like, comparator) |
| 113 | + |
| 114 | + return { |
| 115 | + sql: `${ expressionObject.sql } ${ comparatorAndValue }`, |
| 116 | + values, |
| 117 | + joinedBy, |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function joinClauseHandler(type, table, alias, on) { |
| 122 | + if (!on) { |
| 123 | + on = alias |
| 124 | + alias = undefined |
| 125 | + } |
| 126 | + |
| 127 | + function joinString() { |
| 128 | + return `${ type }JOIN ` |
| 129 | + } |
| 130 | + |
| 131 | + let onParams = [] |
| 132 | + let onString = `` |
| 133 | + |
| 134 | + if (on) { |
| 135 | + if (on.values) { |
| 136 | + onParams = on.values |
| 137 | + onString = makeOn(on.sql) |
| 138 | + } else { |
| 139 | + onString = makeOn(on) |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + if (hasABuildFunction(table)) { |
| 144 | + const result = table.build(`\n\t`) |
| 145 | + |
| 146 | + return { |
| 147 | + sql: joinString() + combineWithAlias(`(\n\t${ result.sql }\n)`, alias) + onString, |
| 148 | + values: [ ...result.values, ...onParams ], |
| 149 | + joinedBy: `\n`, |
| 150 | + } |
| 151 | + } else { |
| 152 | + return { |
| 153 | + sql: joinString() + combineWithAlias(table, alias) + onString, |
| 154 | + values: onParams, |
| 155 | + joinedBy: `\n`, |
| 156 | + } |
| 157 | + } |
| 158 | +} |
0 commit comments