| 85 | return sql |
| 86 | |
| 87 | function Sql(handler) { |
| 88 | handler.debug = options.debug |
| 89 | |
| 90 | Object.entries(options.types).reduce((acc, [name, type]) => { |
| 91 | acc[name] = (x) => new Parameter(x, type.to) |
| 92 | return acc |
| 93 | }, typed) |
| 94 | |
| 95 | Object.assign(sql, { |
| 96 | types: typed, |
| 97 | typed, |
| 98 | unsafe, |
| 99 | notify, |
| 100 | array, |
| 101 | json, |
| 102 | file |
| 103 | }) |
| 104 | |
| 105 | return sql |
| 106 | |
| 107 | function typed(value, type) { |
| 108 | return new Parameter(value, type) |
| 109 | } |
| 110 | |
| 111 | function sql(strings, ...args) { |
| 112 | const query = strings && Array.isArray(strings.raw) |
| 113 | ? new Query(strings, args, handler, cancel) |
| 114 | : typeof strings === 'string' && !args.length |
| 115 | ? new Identifier(options.transform.column.to ? options.transform.column.to(strings) : strings) |
| 116 | : new Builder(strings, args) |
| 117 | return query |
| 118 | } |
| 119 | |
| 120 | function unsafe(string, args = [], options = {}) { |
| 121 | arguments.length === 2 && !Array.isArray(args) && (options = args, args = []) |
| 122 | const query = new Query([string], args, handler, cancel, { |
| 123 | prepare: false, |
| 124 | ...options, |
| 125 | simple: 'simple' in options ? options.simple : args.length === 0 |
| 126 | }) |
| 127 | return query |
| 128 | } |
| 129 | |
| 130 | function file(path, args = [], options = {}) { |
| 131 | arguments.length === 2 && !Array.isArray(args) && (options = args, args = []) |
| 132 | const query = new Query([], args, (query) => { |
| 133 | fs.readFile(path, 'utf8', (err, string) => { |
| 134 | if (err) |
| 135 | return query.reject(err) |
| 136 | |
| 137 | query.strings = [string] |
| 138 | handler(query) |
| 139 | }) |
| 140 | }, cancel, { |
| 141 | ...options, |
| 142 | simple: 'simple' in options ? options.simple : args.length === 0 |
| 143 | }) |
| 144 | return query |