817
817
--- Perform unflatten, skipping, filtering, limiting of objects. This is the
818
818
--- core of the `select_internal` function.
819
819
---
820
+ --- @tparam table self accessor_general instance
821
+ ---
820
822
--- @tparam table state table variable where the function holds state accross
821
823
--- invokes; fields:
822
824
---
844
846
---
845
847
--- Nothing returned, but after necessary count of invokes `state.objs` will
846
848
--- hold list of resulting objects.
847
- local function process_tuple (state , tuple , opts )
849
+ local function process_tuple (self , state , tuple , opts )
848
850
local limit = opts .limit
849
851
local filter = opts .filter
850
852
local do_filter = opts .do_filter
@@ -865,7 +867,7 @@ local function process_tuple(state, tuple, opts)
865
867
local resolveField = opts .resolveField
866
868
867
869
-- convert tuple -> object
868
- local obj = opts .unflatten_tuple (collection_name , tuple ,
870
+ local obj = opts .unflatten_tuple (self , collection_name , tuple ,
869
871
{ use_tomap = opts .use_tomap }, opts .default_unflatten_tuple )
870
872
871
873
-- skip all items before pivot (the item pointed by offset)
@@ -947,8 +949,8 @@ local function perform_primary_key_operation(self, collection_name, schema_name,
947
949
end
948
950
-- XXX: we can pass a tuple corresponding the object to update_tuple /
949
951
-- delete_tuple to save one get operation
950
- local new_tuple = self .funcs [operation ](collection_name , key , ... )
951
- local new_object = self .funcs .unflatten_tuple (collection_name ,
952
+ local new_tuple = self .funcs [operation ](self , collection_name , key , ... )
953
+ local new_object = self .funcs .unflatten_tuple (self , collection_name ,
952
954
new_tuple , {use_tomap = self .collection_use_tomap [collection_name ]},
953
955
self .default_unflatten_tuple [schema_name ])
954
956
table.insert (new_objects , new_object )
@@ -980,36 +982,28 @@ end
980
982
---
981
983
--- @treturn table list of matching objects
982
984
local function select_internal (self , collection_name , from , filter , args , extra )
983
- assert (type (self ) == ' table' ,
984
- ' self must be a table, got ' .. type (self ))
985
- assert (type (collection_name ) == ' string' ,
986
- ' collection_name must be a string, got ' ..
987
- type (collection_name ))
985
+ check (self , ' self' , ' table' )
986
+ check (collection_name , ' collection_name' , ' string' )
988
987
check (from , ' from' , ' table' )
989
- assert (type (filter ) == ' table' ,
990
- ' filter must be a table, got ' .. type (filter ))
991
- assert (type (args ) == ' table' ,
992
- ' args must be a table, got ' .. type (args ))
993
- assert (args .limit == nil or type (args .limit ) == ' number' ,
994
- ' args.limit must be a number of nil, got ' .. type (args .limit ))
995
- -- XXX: save type at parsing and check here
996
- -- assert(args.offset == nil or type(args.offset) == 'number',
997
- -- 'args.offset must be a number of nil, got ' .. type(args.offset))
998
- assert (args .pcre == nil or type (args .pcre ) == ' table' ,
999
- ' args.pcre must be nil or a table, got ' .. type (args .pcre ))
988
+ check (filter , ' filter' , ' table' )
989
+ check (args , ' args' , ' table' )
990
+ check (args .limit , ' args.limit' , ' number' , ' nil' )
991
+ -- XXX: save type of args.offset at parsing and check here
992
+ -- check(args.offset, 'args.offset', ...)
993
+ check (args .pcre , ' args.pcre' , ' table' , ' nil' )
1000
994
1001
995
local collection = self .collections [collection_name ]
1002
996
assert (collection ~= nil ,
1003
997
(' cannot find the collection "%s"' ):format (
1004
998
collection_name ))
1005
- assert (self .funcs .is_collection_exists (collection_name ),
999
+ assert (self .funcs .is_collection_exists (self , collection_name ),
1006
1000
(' cannot find collection "%s"' ):format (collection_name ))
1007
1001
1008
1002
-- search for suitable index
1009
1003
local full_match , index_name , filter , index_value , pivot = get_index_name (
1010
1004
self , collection_name , from , filter , args ) -- we redefine filter here
1011
1005
local index = index_name ~= nil and
1012
- self .funcs .get_index (collection_name , index_name ) or nil
1006
+ self .funcs .get_index (self , collection_name , index_name ) or nil
1013
1007
if from .collection_name ~= nil then
1014
1008
-- allow fullscan only for a top-level object
1015
1009
assert (index ~= nil ,
@@ -1052,11 +1046,13 @@ local function select_internal(self, collection_name, from, filter, args, extra)
1052
1046
1053
1047
if index == nil then
1054
1048
-- fullscan
1055
- local primary_index = self .funcs .get_primary_index (collection_name )
1049
+ local primary_index = self .funcs .get_primary_index (self ,
1050
+ collection_name )
1056
1051
for _ , tuple in primary_index :pairs () do
1057
1052
assert (pivot == nil ,
1058
1053
' offset for top-level objects must use a primary index' )
1059
- local continue = process_tuple (select_state , tuple , select_opts )
1054
+ local continue = process_tuple (self , select_state , tuple ,
1055
+ select_opts )
1060
1056
if not continue then break end
1061
1057
end
1062
1058
else
@@ -1093,7 +1089,8 @@ local function select_internal(self, collection_name, from, filter, args, extra)
1093
1089
end
1094
1090
1095
1091
for _ , tuple in index :pairs (index_value , iterator_opts ) do
1096
- local continue = process_tuple (select_state , tuple , select_opts )
1092
+ local continue = process_tuple (self , select_state , tuple ,
1093
+ select_opts )
1097
1094
if not continue then break end
1098
1095
end
1099
1096
end
@@ -1135,16 +1132,17 @@ local function insert_internal(self, collection_name, from, filter, args, extra)
1135
1132
assert (default_flatten_object ~= nil ,
1136
1133
(' cannot find default_flatten_object ' ..
1137
1134
' for collection "%s"' ):format (collection_name ))
1138
- local tuple = self .funcs .flatten_object (collection_name , object , {
1135
+ local tuple = self .funcs .flatten_object (self , collection_name , object , {
1139
1136
service_fields_defaults =
1140
1137
self .service_fields_defaults [schema_name ],
1141
1138
}, default_flatten_object )
1142
1139
1143
1140
-- insert tuple & tuple -> object (with default values set before)
1144
- local new_tuple = self .funcs .insert_tuple (collection_name , tuple )
1145
- local new_object = self .funcs .unflatten_tuple (collection_name , new_tuple , {
1146
- use_tomap = self .collection_use_tomap [collection_name ]
1147
- }, self .default_unflatten_tuple [schema_name ])
1141
+ local new_tuple = self .funcs .insert_tuple (self , collection_name , tuple )
1142
+ local use_tomap = self .collection_use_tomap [collection_name ]
1143
+ local new_object = self .funcs .unflatten_tuple (self , collection_name ,
1144
+ new_tuple , {use_tomap = use_tomap },
1145
+ self .default_unflatten_tuple [schema_name ])
1148
1146
1149
1147
return {new_object }
1150
1148
end
@@ -1177,7 +1175,7 @@ local function update_internal(self, collection_name, extra, selected)
1177
1175
assert (default_xflatten ~= nil ,
1178
1176
(' cannot find default_xflatten ' ..
1179
1177
' for collection "%s"' ):format (collection_name ))
1180
- local statements = self .funcs .xflatten (collection_name , xobject , {
1178
+ local statements = self .funcs .xflatten (self , collection_name , xobject , {
1181
1179
service_fields_defaults =
1182
1180
self .service_fields_defaults [schema_name ],
1183
1181
}, default_xflatten )
@@ -1279,7 +1277,7 @@ local function gen_default_object_tuple_map_funcs(models)
1279
1277
local default_flatten_object = {}
1280
1278
local default_xflatten = {}
1281
1279
for schema_name , model in pairs (models ) do
1282
- default_unflatten_tuple [schema_name ] = function (_ , tuple , opts )
1280
+ default_unflatten_tuple [schema_name ] = function (_ , _ , tuple , opts )
1283
1281
local opts = opts or {}
1284
1282
check (opts , ' opts' , ' table' )
1285
1283
@@ -1288,7 +1286,7 @@ local function gen_default_object_tuple_map_funcs(models)
1288
1286
schema_name , tostring (obj )))
1289
1287
return obj
1290
1288
end
1291
- default_flatten_object [schema_name ] = function (_ , obj , opts )
1289
+ default_flatten_object [schema_name ] = function (_ , _ , obj , opts )
1292
1290
local opts = opts or {}
1293
1291
check (opts , ' opts' , ' table' )
1294
1292
local sf_defaults = opts .service_fields_defaults or {}
@@ -1299,7 +1297,7 @@ local function gen_default_object_tuple_map_funcs(models)
1299
1297
schema_name , tostring (tuple )))
1300
1298
return tuple
1301
1299
end
1302
- default_xflatten [schema_name ] = function (_ , xobject , opts )
1300
+ default_xflatten [schema_name ] = function (_ , _ , xobject , opts )
1303
1301
local opts = opts or {}
1304
1302
check (opts , ' opts' , ' table' )
1305
1303
0 commit comments