UNPKG

5.04 kBtext/coffeescriptView Raw
1isRaw = (x) ->
2 x? and ('object' is typeof x) and ('function' is typeof x.sql)
3
4asRaw = (x) ->
5 if isRaw x
6 return x
7
8 unless 'string' is typeof x
9 throw new Exception 'raw or string expected'
10
11 {sql: x, params: []}
12
13insert =
14 sql: (mohair) ->
15 that = this
16
17 table = mohair._escape mohair._table
18 keys = Object.keys(that._data)
19 escapedKeys = keys.map (key) -> mohair._escape key
20 row = keys.map (key) ->
21 if isRaw that._data[key]
22 that._data[key].sql()
23 else
24 '?'
25 "INSERT INTO #{table}(#{escapedKeys.join ', '}) VALUES (#{row.join ', '})"
26 params: ->
27 that = this
28 params = []
29 Object.keys(that._data).map (key) ->
30 if isRaw that._data[key]
31 params = params.concat that._data[key].params()
32 else
33 params.push that._data[key]
34 params
35
36module.exports.insert = (data) ->
37 object = Object.create insert
38 object._data = data
39 object
40
41insertMany =
42 sql: (mohair) ->
43 that = this
44 table = mohair._escape mohair._table
45 first = that._array[0]
46 keys = Object.keys(first)
47 escapedKeys = keys.map (key) -> mohair._escape key
48 rows = that._array.map (data) ->
49 row = keys.map (key) ->
50 if isRaw data[key]
51 data[key].sql()
52 else
53 '?'
54 "(#{row.join ', '})"
55 "INSERT INTO #{table}(#{escapedKeys.join ', '}) VALUES #{rows.join ', '}"
56 params: (mohair) ->
57 that = this
58 firstKeys = Object.keys that._array[0]
59 params = []
60 that._array.forEach (data) ->
61 firstKeys.forEach (key) ->
62 if isRaw data[key]
63 params = params.concat data[key].params()
64 else
65 params.push data[key]
66 params
67
68module.exports.insertMany = (array) ->
69 object = Object.create insertMany
70 object._array = array
71 object
72
73select =
74 sql: (mohair) ->
75 that = this
76 table = mohair._escape mohair._table
77 sql = ''
78
79 if mohair._with?
80 sql += 'WITH '
81 parts = []
82 parts = Object.keys(mohair._with).map (key) ->
83 key + ' AS (' + asRaw(mohair._with[key]).sql() + ')'
84 sql += parts.join(', ')
85 sql += ' '
86
87 sql += "SELECT #{that._sql} FROM #{table}"
88 mohair._joins.forEach (join) ->
89 sql += " #{join.sql}"
90 sql += " AND (#{join.criterion.sql()})" if join.criterion?
91 sql += " WHERE #{mohair._where.sql()}" if mohair._where?
92 sql += " GROUP BY #{mohair._group}" if mohair._group?
93 sql += " ORDER BY #{mohair._order}" if mohair._order?
94 sql += " LIMIT ?" if mohair._limit?
95 sql += " OFFSET ?" if mohair._offset?
96 sql
97 params: (mohair) ->
98 that = this
99 params = []
100
101 if mohair._with?
102 Object.keys(mohair._with).forEach (key) ->
103 params = params.concat asRaw(mohair._with[key]).params()
104
105 params = params.concat that._params if that._params?
106
107 mohair._joins.forEach (join) ->
108 if join.criterion?
109 params = params.concat join.criterion.params()
110
111 params = params.concat mohair._where.params() if mohair._where?
112 params.push mohair._limit if mohair._limit?
113 params.push mohair._offset if mohair._offset?
114 params
115
116module.exports.select = (sql, params) ->
117 object = Object.create select
118 object._sql = sql
119 object._params = params
120 object
121
122update =
123 sql: (mohair) ->
124 that = this
125 table = mohair._escape mohair._table
126 keys = Object.keys that._updates
127
128 updates = keys.map (key) ->
129 escapedKey = mohair._escape key
130 if isRaw that._updates[key]
131 "#{escapedKey} = #{that._updates[key].sql()}"
132 else
133 "#{escapedKey} = ?"
134 sql = "UPDATE #{table} SET #{updates.join ', '}"
135 sql += " WHERE #{mohair._where.sql()}" if mohair._where?
136 sql
137 params: (mohair) ->
138 that = this
139 params = []
140 Object.keys(that._updates).forEach (key) ->
141 if isRaw that._updates[key]
142 params = params.concat that._updates[key].params()
143 else
144 params.push that._updates[key]
145 params = params.concat mohair._where.params() if mohair._where?
146 params
147
148module.exports.update = (updates) ->
149 object = Object.create update
150 object._updates = updates
151 object
152
153deletePrototype =
154 sql: (mohair) ->
155 that = this
156 table = mohair._escape mohair._table
157 sql = "DELETE FROM #{table}"
158 sql += " WHERE #{mohair._where.sql()}" if mohair._where?
159 sql
160 params: (mohair) ->
161 if mohair._where?
162 mohair._where.params()
163 else []
164
165module.exports.delete = ->
166 Object.create deletePrototype