@@ -4,19 +4,28 @@ local lmdb = require("resty.lmdb")
4
4
local marshaller = require (" kong.db.declarative.marshaller" )
5
5
6
6
7
-
8
7
local kong = kong
9
8
local fmt = string.format
10
9
local type = type
11
10
local next = next
11
+ local sort = table.sort
12
12
local pairs = pairs
13
+ local match = string.match
14
+ local assert = assert
13
15
local tostring = tostring
14
16
local tonumber = tonumber
15
17
local encode_base64 = ngx .encode_base64
16
18
local decode_base64 = ngx .decode_base64
17
- local null = ngx .null
18
- local unmarshall = marshaller .unmarshall
19
- local lmdb_get = lmdb .get
19
+ local null = ngx .null
20
+ local unmarshall = marshaller .unmarshall
21
+ local lmdb_get = lmdb .get
22
+ local get_workspace_id = workspaces .get_workspace_id
23
+
24
+
25
+ local PROCESS_AUTO_FIELDS_OPTS = {
26
+ no_defaults = true ,
27
+ show_ws_id = true ,
28
+ }
20
29
21
30
22
31
local off = {}
@@ -26,8 +35,8 @@ local _mt = {}
26
35
_mt .__index = _mt
27
36
28
37
29
- local function ws (self , options )
30
- if not self . schema .workspaceable then
38
+ local function ws (schema , options )
39
+ if not schema .workspaceable then
31
40
return " "
32
41
end
33
42
@@ -39,7 +48,8 @@ local function ws(self, options)
39
48
return options .workspace
40
49
end
41
50
end
42
- return workspaces .get_workspace_id () or kong .default_workspace
51
+
52
+ return get_workspace_id () or kong .default_workspace
43
53
end
44
54
45
55
@@ -97,7 +107,7 @@ local function get_entity_ids_tagged(key, tag_names, tags_cond)
97
107
len = len + 1
98
108
arr [len ] = entity_id
99
109
end
100
- table. sort (arr ) -- consistency when paginating results
110
+ sort (arr ) -- consistency when paginating results
101
111
102
112
return arr
103
113
end
@@ -142,7 +152,8 @@ local function page_for_key(self, key, size, offset, options)
142
152
end
143
153
144
154
local ret = {}
145
- local schema_name = self .schema .name
155
+ local schema = self .schema
156
+ local schema_name = schema .name
146
157
147
158
local item
148
159
for i = offset , offset + size - 1 do
@@ -158,7 +169,7 @@ local function page_for_key(self, key, size, offset, options)
158
169
-- For example "admin|services|<a service uuid>"
159
170
-- This loop transforms each individual string into tables.
160
171
if schema_name == " tags" then
161
- local tag_name , entity_name , uuid = string. match (item , " ^([^|]+)|([^|]+)|(.+)$" )
172
+ local tag_name , entity_name , uuid = match (item , " ^([^|]+)|([^|]+)|(.+)$" )
162
173
if not tag_name then
163
174
return nil , " Could not parse tag from cache: " .. tostring (item )
164
175
end
@@ -178,12 +189,7 @@ local function page_for_key(self, key, size, offset, options)
178
189
return nil , " stale data detected while paginating"
179
190
end
180
191
181
- item = self .schema :process_auto_fields (item , " select" , true , {
182
- no_defaults = true ,
183
- show_ws_id = true ,
184
- })
185
-
186
- ret [i - offset + 1 ] = item
192
+ ret [i - offset + 1 ] = schema :process_auto_fields (item , " select" , true , PROCESS_AUTO_FIELDS_OPTS )
187
193
end
188
194
189
195
if offset then
@@ -194,33 +200,32 @@ local function page_for_key(self, key, size, offset, options)
194
200
end
195
201
196
202
197
- local function select_by_key (self , key )
203
+ local function select_by_key (schema , key )
198
204
local entity , err = unmarshall (lmdb_get (key ))
199
205
if not entity then
200
206
return nil , err
201
207
end
202
208
203
- entity = self .schema :process_auto_fields (entity , " select" , true , {
204
- no_defaults = true ,
205
- show_ws_id = true ,
206
- })
209
+ entity = schema :process_auto_fields (entity , " select" , true , PROCESS_AUTO_FIELDS_OPTS )
207
210
208
211
return entity
209
212
end
210
213
211
214
212
215
local function page (self , size , offset , options )
213
- local ws_id = ws (self , options )
214
- local key = self .schema .name .. " |" .. ws_id .. " |@list"
216
+ local schema = self .schema
217
+ local ws_id = ws (schema , options )
218
+ local key = schema .name .. " |" .. ws_id .. " |@list"
215
219
return page_for_key (self , key , size , offset , options )
216
220
end
217
221
218
222
219
223
local function select (self , pk , options )
220
- local ws_id = ws (self , options )
221
- local id = declarative_config .pk_string (self .schema , pk )
222
- local key = self .schema .name .. " :" .. id .. " :::::" .. ws_id
223
- return select_by_key (self , key )
224
+ local schema = self .schema
225
+ local ws_id = ws (schema , options )
226
+ local id = declarative_config .pk_string (schema , pk )
227
+ local key = schema .name .. " :" .. id .. " :::::" .. ws_id
228
+ return select_by_key (schema , key )
224
229
end
225
230
226
231
@@ -230,28 +235,30 @@ local function select_by_field(self, field, value, options)
230
235
_ , value = next (value )
231
236
end
232
237
233
- local ws_id = ws (self , options )
238
+ local schema = self .schema
239
+ local ws_id = ws (schema , options )
234
240
235
241
local key
236
242
if field ~= " cache_key" then
237
- local unique_across_ws = self . schema .fields [field ].unique_across_ws
243
+ local unique_across_ws = schema .fields [field ].unique_across_ws
238
244
if unique_across_ws then
239
245
ws_id = " "
240
246
end
241
247
242
248
-- only accept global query by field if field is unique across workspaces
243
249
assert (not options or options .workspace ~= null or unique_across_ws )
244
250
245
- key = self . schema .name .. " |" .. ws_id .. " |" .. field .. " :" .. value
251
+ key = schema .name .. " |" .. ws_id .. " |" .. field .. " :" .. value
246
252
247
253
else
248
254
-- if select_by_cache_key, use the provided cache_key as key directly
249
255
key = value
250
256
end
251
257
252
- return select_by_key (self , key )
258
+ return select_by_key (schema , key )
253
259
end
254
260
261
+
255
262
do
256
263
local unsupported = function (operation )
257
264
return function (self )
262
269
end
263
270
264
271
local unsupported_by = function (operation )
265
-
266
272
return function (self , field_name )
267
273
local err = fmt (" cannot %s '%s' entities by '%s' when not using a database" ,
268
274
operation , self .schema .name , ' %s' )
285
291
_mt .page_for_key = page_for_key
286
292
end
287
293
294
+
288
295
function off .new (connector , schema , errors )
289
296
local self = {
290
297
connector = connector , -- instance of kong.db.strategies.off.connector
@@ -299,14 +306,13 @@ function off.new(connector, schema, errors)
299
306
kong .default_workspace = " 00000000-0000-0000-0000-000000000000"
300
307
end
301
308
302
- local name = self . schema .name
309
+ local name = schema .name
303
310
for fname , fdata in schema :each_field () do
304
311
if fdata .type == " foreign" then
305
312
local entity = fdata .reference
306
313
local method = " page_for_" .. fname
307
314
self [method ] = function (_ , foreign_key , size , offset , options )
308
- local ws_id = ws (self , options )
309
-
315
+ local ws_id = ws (schema , options )
310
316
local key = name .. " |" .. ws_id .. " |" .. entity .. " |" .. foreign_key .id .. " |@list"
311
317
return page_for_key (self , key , size , offset , options )
312
318
end
0 commit comments