@@ -19,10 +19,10 @@ M.sqlvalue = function(v)
1919 return type (v ) == " boolean" and (v == true and 1 or 0 ) or (v == nil and " null" or v )
2020end
2121
22- M .luavalue = function (v , schema_type )
23- if schema_type == " luatable" or schema_type == " json" then
22+ M .luavalue = function (v , key_type )
23+ if key_type == " luatable" or key_type == " json" then
2424 return json .decode (v )
25- elseif schema_type == " boolean" then
25+ elseif key_type == " boolean" then
2626 return v == 0 and false or true
2727 end
2828
@@ -393,16 +393,21 @@ M.create = function(tbl, defs)
393393 local items = {}
394394
395395 tbl = defs .ensure and " if not exists " .. tbl or tbl
396- defs .ensure = nil
397396
398397 for k , v in u .opairs (defs ) do
399- local t = type (v )
400- if t == " boolean" then
401- tinsert (items , k .. " integer not null primary key" )
402- elseif t ~= " table" then
403- tinsert (items , string.format (" %s %s" , k , v ))
404- else
405- tinsert (items , (" %s %s" ):format (k , opts_to_str (v )))
398+ if k ~= " ensure" then
399+ local t = type (v )
400+ if t == " boolean" then
401+ tinsert (items , k .. " integer not null primary key" )
402+ elseif t ~= " table" then
403+ tinsert (items , string.format (" %s %s" , k , v ))
404+ else
405+ if u .is_list (v ) then
406+ tinsert (items , (" %s %s" ):format (k , tconcat (v , " " )))
407+ else
408+ tinsert (items , (" %s %s" ):format (k , opts_to_str (v )))
409+ end
410+ end
406411 end
407412 end
408413 return (" create table %s(%s)" ):format (tbl , tconcat (items , " , " ))
@@ -425,11 +430,12 @@ M.pre_insert = function(rows, schema)
425430 local res = {}
426431 rows = u .is_nested (rows ) and rows or { rows }
427432 for i , row in ipairs (rows ) do
428- u .foreach (schema .req , function (k )
429- a .missing_req_key (row [k ], k )
430- end )
433+ -- u.foreach(schema.req, function(k)
434+ -- a.missing_req_key(row[k], k)
435+ -- end)
431436 res [i ] = u .map (row , function (v , k )
432- local is_json = schema .types [k ] == " luatable" or schema .types [k ] == " json"
437+ a .missing_req_key (v , schema [k ].required )
438+ local is_json = schema [k ].type == " luatable" or schema [k ].type == " json"
433439 return is_json and json .encode (v ) or M .sqlvalue (v )
434440 end )
435441 end
@@ -442,13 +448,18 @@ end
442448--- @param schema table tbl schema
443449--- @return table pre processed rows
444450--- @TODO support boolean values.
445- M .post_select = function (rows , types )
451+ M .post_select = function (rows , schema )
446452 local is_nested = u .is_nested (rows )
447453 rows = is_nested and rows or { rows }
448454
449455 for _ , row in ipairs (rows ) do
450456 for k , v in pairs (row ) do
451- row [k ] = M .luavalue (v , types [k ])
457+ local info = schema [k ]
458+ if info then
459+ row [k ] = M .luavalue (v , info .type )
460+ else
461+ row [k ] = v
462+ end
452463 end
453464 end
454465
0 commit comments