Repository navigation
Expand file tree
/
Copy pathdao.lua
More file actions
418 lines (364 loc) · 13.6 KB
/
Copy pathdao.lua
File metadata and controls
418 lines (364 loc) · 13.6 KB
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
409
410
411
412
413
414
415
416
417
418
--- Operates over entities of a given type in a database table.
-- An instance of this class is to be instanciated for each entity, and can interact
-- with the table representing the entity in the database.
--
-- Instanciations of this class are managed by the DAO Factory.
--
-- This class provides an abstraction for various databases (PostgreSQL, Cassandra)
-- and is responsible for propagating clustering events related to data invalidation,
-- as well as foreign constraints when the underlying database does not support them
-- (as with Cassandra).
-- @copyright Copyright 2016-2017 Kong Inc. All rights reserved.
-- @license [Apache 2.0](https://opensource.org/licenses/Apache-2.0)
-- @module kong.dao
local Object = require "kong.vendor.classic"
local utils = require "kong.tools.utils"
local Errors = require "kong.dao.errors"
local schemas_validation = require "kong.dao.schemas_validation"
local fmt = string.format
local new_tab
do
local ok
ok, new_tab = pcall(require, "table.new")
if not ok then
new_tab = function(narr, nrec) return {} end
end
end
local RANDOM_VALUE = utils.random_string()
local function check_arg(arg, arg_n, exp_type)
if type(arg) ~= exp_type then
local info = debug.getinfo(2)
local err = string.format("bad argument #%d to '%s' (%s expected, got %s)",
arg_n, info.name, exp_type, type(arg))
error(err, 3)
end
end
local function check_not_empty(tbl, arg_n)
if next(tbl) == nil then
local info = debug.getinfo(2)
local err = string.format("bad argument #%d to '%s' (expected table to not be empty)",
arg_n, info.name)
error(err, 3)
end
end
local function check_utf8(tbl, arg_n)
for k, v in pairs(tbl) do
if not utils.validate_utf8(v) then
tbl[k] = RANDOM_VALUE -- Force a random string
end
end
end
local function ret_error(db_name, res, err, ...)
if type(err) == "table" then
err.db_name = db_name
elseif type(err) == "string" then
local e = Errors.db(err)
e.db_name = db_name
err = tostring(e)
end
return res, err, ...
end
local DAO = Object:extend()
DAO.ret_error = ret_error
--- Instanciate a DAO.
-- The DAO Factory is responsible for instanciating DAOs for each entity.
-- This method is only documented for clarity.
-- @param db An instance of the underlying database object (`cassandra_db` or `postgres_db`)
-- @param model_mt The related model metatable. Such metatables contain, among other things, validation methods.
-- @param schema The schema of the entity for which this DAO is instanciated. The schema contains crucial informations about how to interact with the database (fields type, table name, etc...)
-- @param constraints A table of contraints built by the DAO Factory. Such constraints are mostly useful for databases without support for foreign keys. SQL databases handle those contraints natively.
-- @return self
function DAO:new(db, model_mt, schema, constraints)
self.db = db
self.model_mt = model_mt
self.schema = schema
self.table = schema.table
self.constraints = constraints
end
function DAO:cache_key(arg1, arg2, arg3, arg4, arg5)
return fmt("%s:%s:%s:%s:%s:%s", self.table,
arg1 == nil and "" or arg1,
arg2 == nil and "" or arg2,
arg3 == nil and "" or arg3,
arg4 == nil and "" or arg4,
arg5 == nil and "" or arg5)
end
function DAO:entity_cache_key(entity)
local schema = self.schema
local cache_key = schema.cache_key
if not cache_key then
return
end
local n = #cache_key
local keys = new_tab(n, 0)
keys.n = n
for i = 1, n do
keys[i] = entity[cache_key[i]]
end
return self:cache_key(utils.unpack(keys))
end
--- Insert a row.
-- Insert a given Lua table as a row in the related table.
-- @param[type=table] tbl Table to insert as a row.
-- @param[type=table] options Options to use for this insertion. (`ttl`: Time-to-live for this row, in seconds, `quiet`: does not send event)
-- @treturn table res A table representing the insert row (with fields created during the insertion).
-- @treturn table err If an error occured, a table describing the issue.
function DAO:insert(tbl, options)
options = options or {}
check_arg(tbl, 1, "table")
check_arg(options, 2, "table")
local model = self.model_mt(tbl)
local ok, err = model:validate {dao = self}
if not ok then
return ret_error(self.db.name, nil, err)
end
for col, field in pairs(model.__schema.fields) do
if field.dao_insert_value and model[col] == nil then
local f = self.db.dao_insert_values[field.type]
if f then
model[col] = f()
end
end
end
local res, err = self.db:insert(self.table, self.schema, model, self.constraints, options)
if not err and not options.quiet then
if self.events then
local ok, err = self.events.post_local("dao:crud", "create", {
schema = self.schema,
operation = "create",
entity = res,
})
if not ok then
ngx.log(ngx.ERR, "could not propagate CRUD operation: ", err)
end
end
end
return ret_error(self.db.name, res, err)
end
--- Find a row.
-- Find a row by its given, mandatory primary key. All other fields are ignored.
-- @param[type=table] tbl A table containing the primary key field(s) for this row.
-- @treturn table row The row, or nil if none could be found.
-- @treturn table err If an error occured, a table describing the issue.
function DAO:find(tbl)
check_arg(tbl, 1, "table")
check_utf8(tbl, 1)
local model = self.model_mt(tbl)
if not model:has_primary_keys() then
error("Missing PRIMARY KEY field", 2)
end
local primary_keys, _, _, err = model:extract_keys()
if err then
return ret_error(self.db.name, nil, Errors.schema(err))
end
return ret_error(self.db.name, self.db:find(self.table, self.schema, primary_keys))
end
--- Find all rows.
-- Find all rows in the table, eventually matching the values in the given fields.
-- @param[type=table] tbl (optional) A table containing the fields and values to search for.
-- @treturn rows An array of rows.
-- @treturn table err If an error occured, a table describing the issue.
function DAO:find_all(tbl)
if tbl ~= nil then
check_arg(tbl, 1, "table")
check_utf8(tbl, 1)
check_not_empty(tbl, 1)
local ok, err = schemas_validation.is_schema_subset(tbl, self.schema)
if not ok then
return ret_error(self.db.name, nil, Errors.schema(err))
end
end
return ret_error(self.db.name, self.db:find_all(self.table, tbl, self.schema))
end
--- Find a paginated set of rows.
-- Find a pginated set of rows eventually matching the values in the given fields.
-- @param[type=table] tbl (optional) A table containing the fields and values to filter for.
-- @param page_offset Offset at which to resume pagination.
-- @param page_size Size of the page to retrieve (number of rows).
-- @treturn table rows An array of rows.
-- @treturn table err If an error occured, a table describing the issue.
function DAO:find_page(tbl, page_offset, page_size)
if tbl ~= nil then
check_arg(tbl, 1, "table")
check_not_empty(tbl, 1)
local ok, err = schemas_validation.is_schema_subset(tbl, self.schema)
if not ok then
return ret_error(self.db.name, nil, Errors.schema(err))
end
end
if page_size == nil then
page_size = 100
end
check_arg(page_size, 3, "number")
return ret_error(self.db.name, self.db:find_page(self.table, tbl, page_offset, page_size, self.schema))
end
--- Count the number of rows.
-- Count the number of rows matching the given values.
-- @param[type=table] tbl (optional) A table containing the fields and values to filter for.
-- @treturn number count The total count of rows matching the given filter, or total count of rows if no filter was given.
-- @treturn table err If an error occured, a table describing the issue.
function DAO:count(tbl)
if tbl ~= nil then
check_arg(tbl, 1, "table")
check_not_empty(tbl, 1)
local ok, err = schemas_validation.is_schema_subset(tbl, self.schema)
if not ok then
return ret_error(self.db.name, nil, Errors.schema(err))
end
end
if tbl ~= nil and next(tbl) == nil then
tbl = nil
end
return ret_error(self.db.name, self.db:count(self.table, tbl, self.schema))
end
local function fix(old, new, schema)
for col, field in pairs(schema.fields) do
if old[col] ~= nil and new[col] ~= nil and field.schema ~= nil then
local f_schema, err
if type(field.schema) == "function" then
f_schema, err = field.schema(old)
if err then
error(err)
end
else
f_schema = field.schema
end
for f_k in pairs(f_schema.fields) do
if new[col][f_k] == nil and old[col][f_k] ~= nil then
new[col][f_k] = old[col][f_k]
elseif new[col][f_k] == ngx.null then
new[col][f_k] = nil
end
end
fix(old[col], new[col], f_schema)
end
end
end
--- Update a row.
-- Update a row in the related table. Performe a partial update by default (only fields in `tbl` will)
-- be updated. If asked, can perform a "full" update, replacing the entire entity (assuming it is valid)
-- with the one specified in `tbl` at once.
-- @param[type=table] tbl A table containing the new values for this row.
-- @param[type=table] filter_keys A table which must contain the primary key(s) to select the row to be updated.
-- @param[type=table] options Options to use for this update. (`full`: performs a full update of the entity, `quiet`: does not send event).
-- @treturn table res A table representing the updated entity.
-- @treturn table err If an error occured, a table describing the issue.
function DAO:update(tbl, filter_keys, options)
options = options or {}
check_arg(tbl, 1, "table")
check_not_empty(tbl, 1)
check_arg(filter_keys, 2, "table")
check_not_empty(filter_keys, 2)
check_arg(options, 3, "table")
for k, v in pairs(filter_keys) do
if tbl[k] == nil then
tbl[k] = v
end
end
local model = self.model_mt(tbl)
local ok, err = model:validate {dao = self, update = true, full_update = options.full}
if not ok then
return ret_error(self.db.name, nil, err)
end
local primary_keys, values, nils, err = model:extract_keys()
if err then
return ret_error(self.db.name, nil, Errors.schema(err))
end
local old, err = self.db:find(self.table, self.schema, primary_keys)
if err then
return ret_error(self.db.name, nil, err)
elseif old == nil then
return
end
if not options.full then
fix(old, values, self.schema)
end
local res, err = self.db:update(self.table, self.schema, self.constraints, primary_keys, values, nils, options.full, model, options)
if err then
return ret_error(self.db.name, nil, err)
elseif res then
if not options.quiet then
if self.events then
local ok, err = self.events.post_local("dao:crud", "update", {
schema = self.schema,
operation = "update",
entity = res,
old_entity = old,
})
if not ok then
ngx.log(ngx.ERR, "could not propagate CRUD operation: ", err)
end
end
end
return setmetatable(res, nil)
end
end
--- Delete a row.
-- Delete a row in table related to this instance. Also deletes all rows with a relashionship to the deleted row
-- (via foreign key relations). For SQL databases such as PostgreSQL, the underlying implementation
-- leverages "FOREIGN KEY" constraints, but for others such as Cassandra, such operations are executed
-- manually.
-- @param[type=table] tbl A table containing the primary key field(s) for this row.
-- @treturn table row A table representing the deleted row
-- @treturn table err If an error occured, a table describing the issue.
function DAO:delete(tbl, options)
options = options or {}
check_arg(tbl, 1, "table")
check_arg(options, 2, "table")
local model = self.model_mt(tbl)
if not model:has_primary_keys() then
error("Missing PRIMARY KEY field", 2)
end
local primary_keys, _, _, err = model:extract_keys()
if err then
return ret_error(self.db.name, nil, Errors.schema(err))
end
-- Find associated entities
local associated_entites = {}
if self.constraints.cascade ~= nil then
for f_entity, cascade in pairs(self.constraints.cascade) do
local f_fetch_keys = {[cascade.f_col] = tbl[cascade.col]}
local rows, err = self.db:find_all(cascade.table, f_fetch_keys, cascade.schema)
if err then
return ret_error(self.db.name, nil, err)
end
associated_entites[cascade.table] = {
schema = cascade.schema,
entities = rows
}
end
end
local row, err = self.db:delete(self.table, self.schema, primary_keys, self.constraints)
if not err and row ~= nil and not options.quiet then
if self.events then
local ok, err = self.events.post_local("dao:crud", "delete", {
schema = self.schema,
operation = "delete",
entity = row,
})
if not ok then
ngx.log(ngx.ERR, "could not propagate CRUD operation: ", err)
end
end
-- Also propagate the deletion for the associated entities
for k, v in pairs(associated_entites) do
for _, entity in ipairs(v.entities) do
if self.events then
local ok, err = self.events.post_local("dao:crud", "delete", {
schema = v.schema,
operation = "delete",
entity = entity,
})
if not ok then
ngx.log(ngx.ERR, "could not propagate CRUD operation: ", err)
end
end
end
end
end
return ret_error(self.db.name, row, err)
end
function DAO:truncate()
return ret_error(self.db.name, self.db:truncate_table(self.table))
end
return DAO