webmcp
view libraries/mondelefant/mondelefant.lua @ 471:a9fea293b2d6
New function format.file_path_element(...)
| author | jbe | 
|---|---|
| date | Thu May 25 02:46:23 2017 +0200 (2017-05-25) | 
| parents | 8c19fa7950f6 | 
| children | 6c819040ef6f | 
 line source
     1 #!/usr/bin/env lua
     4 ---------------------------
     5 -- module initialization --
     6 ---------------------------
     8 local _G              = _G
     9 local _VERSION        = _VERSION
    10 local assert          = assert
    11 local error           = error
    12 local getmetatable    = getmetatable
    13 local ipairs          = ipairs
    14 local next            = next
    15 local pairs           = pairs
    16 local print           = print
    17 local rawequal        = rawequal
    18 local rawget          = rawget
    19 local rawlen          = rawlen
    20 local rawset          = rawset
    21 local select          = select
    22 local setmetatable    = setmetatable
    23 local tonumber        = tonumber
    24 local tostring        = tostring
    25 local type            = type
    27 local math            = math
    28 local string          = string
    29 local table           = table
    31 local add             = table.insert
    33 local _M = require("mondelefant_native")
    34 if _ENV then
    35   _ENV = _M
    36 else
    37   _G[...] = _M
    38   setfenv(1, _M)
    39 end
    41 -- TODO: remove following downward-compatibility code
    42 -- for mondelefant.connect{...} function:
    44 do
    45   local original_connect_function = connect
    46   function connect(args)
    47     if args.engine == "postgresql" then
    48       local newargs = {}
    49       for k, v in pairs(args) do
    50         if k ~= "engine" then newargs[k] = v end
    51       end
    52       return original_connect_function(newargs)
    53     else
    54       return original_connect_function(args)
    55     end
    56   end
    57 end
    61 ---------------
    62 -- selectors --
    63 ---------------
    65 selector_metatable = {}
    66 selector_prototype = {}
    67 selector_metatable.__index = selector_prototype
    69 local function init_selector(self, db_conn)
    70   self._db_conn = db_conn
    71   self._mode = "list"
    72   self._with = { sep = ", " }
    73   self._fields = { sep = ", " }
    74   self._distinct = false
    75   self._distinct_on = {sep = ", ", expression}
    76   self._from = { sep = " " }
    77   self._where = { sep = ") AND (" }
    78   self._group_by = { sep = ", " }
    79   self._having = { sep = ") AND (" }
    80   self._combine = { sep = " " }
    81   self._order_by = { sep = ", " }
    82   self._limit = nil
    83   self._offset = nil
    84   self._read_lock = { sep = ", " }
    85   self._write_lock = { sep = ", " }
    86   self._class = nil
    87   self._attach = nil
    88   return self
    89 end
    91 --[[--
    92 selector =                  -- new selector
    93 <db_handle>:new_selector()
    95 Creates a new selector to operate on the given database handle.
    96 --]]--
    97 function connection_prototype:new_selector()
    98   return init_selector(setmetatable({}, selector_metatable), self)
    99 end
   100 --//--
   102 --[[--
   103 db_handle =                  -- handle of database connection
   104 <db_selector>:get_db_conn()
   106 Returns the database connection handle used by a selector.
   108 --]]--
   109 function selector_prototype:get_db_conn()
   110   return self._db_conn
   111 end
   112 --//--
   114 -- TODO: selector clone?
   116 --[[--
   117 db_selector =                       -- same selector returned
   118 <db_selector>:single_object_mode()
   120 Sets selector to single object mode (mode "object" passed to "query" method of database handle). The selector is modified and returned.
   122 --]]--
   123 function selector_prototype:single_object_mode()
   124   self._mode = "object"
   125   return self
   126 end
   127 --//--
   129 --[[--
   130 db_selector =                         -- same selector returned
   131 <db_selector>:optional_object_mode()
   133 Sets selector to single object mode (mode "opt_object" passed to "query" method of database handle). The selector is modified and returned.
   135 --]]--
   136 function selector_prototype:optional_object_mode()
   137   self._mode = "opt_object"
   138   return self
   139 end
   140 --//--
   142 --[[--
   143 db_selector =                    -- same selector returned
   144 <db_selector>:empty_list_mode()
   146 Sets selector to empty list mode. The selector is modified and returned. When using the selector, no SQL query will be issued, but instead an empty database result list is returned.
   148 --]]--
   149 function selector_prototype:empty_list_mode()
   150   self._mode = "empty_list"
   151   return self
   152 end
   153 --//--
   155 --[[--
   156 db_selector =
   157 <db_selector>:add_with(
   158   expression = expression,
   159   selector   = selector
   160 )
   162 Adds an WITH RECURSIVE expression to the selector. The selector is modified and returned.
   163 --]]--
   164 function selector_prototype:add_with(expression, selector)
   165   add(self._with, {"$ AS ($)", {expression}, {selector}})
   166   return self
   167 end
   168 --//--
   170 --[[--
   171 db_selector =                   -- same selector returned
   172 <db_selector>:add_distinct_on(
   173   expression                    -- expression as passed to "assemble_command"
   174 )
   176 Adds an DISTINCT ON expression to the selector. The selector is modified and returned.
   178 --]]--
   179 function selector_prototype:add_distinct_on(expression)
   180   if self._distinct then
   181     error("Can not combine DISTINCT with DISTINCT ON.")
   182   end
   183   add(self._distinct_on, expression)
   184   return self
   185 end
   186 --//--
   188 --[[--
   189 db_selector =                -- same selector returned
   190 <db_selector>:set_distinct()
   192 Sets selector to perform a SELECT DISTINCT instead of SELECT (ALL). The selector is modified and returned. This mode can not be combined with DISTINCT ON.
   194 --]]--
   195 function selector_prototype:set_distinct()
   196   if #self._distinct_on > 0 then
   197     error("Can not combine DISTINCT with DISTINCT ON.")
   198   end
   199   self._distinct = true
   200   return self
   201 end
   202 --//--
   204 --[[--
   205 db_selector =             -- same selector returned
   206 <db_selector>:add_from(
   207   expression,             -- expression as passed to "assemble_command"
   208   alias,                  -- optional alias expression as passed to "assemble_command"
   209   condition               -- optional condition expression as passed to "assemble_command"
   210 )
   212 Adds expressions for FROM clause to the selector. The selector is modified and returned. If an additional condition is given, an INNER JOIN will be used, otherwise a CROSS JOIN.
   214 This method is identical to "join".
   216 --]]--
   217 function selector_prototype:add_from(expression, alias, condition)
   218   local first = (#self._from == 0)
   219   if not first then
   220     if condition then
   221       add(self._from, "INNER JOIN")
   222     else
   223       add(self._from, "CROSS JOIN")
   224     end
   225   end
   226   if getmetatable(expression) == selector_metatable then
   227     if alias then
   228       add(self._from, {'($) AS "$"', {expression}, {alias}})
   229     else
   230       add(self._from, {'($) AS "subquery"', {expression}})
   231     end
   232   else
   233     if alias then
   234       add(self._from, {'$ AS "$"', {expression}, {alias}})
   235     else
   236       add(self._from, expression)
   237     end
   238   end
   239   if condition then
   240     if first then
   241       self:add_where(condition)
   242     else
   243       add(self._from, "ON")
   244       add(self._from, condition)
   245     end
   246   end
   247   return self
   248 end
   249 --//--
   251 --[[--
   252 db_selector =             -- same selector returned
   253 <db_selector>:add_where(
   254   expression              -- expression as passed to "assemble_command"
   255 )
   257 Adds expressions for WHERE clause to the selector. The selector is modified and returned. Multiple calls cause expressions to be AND-combined.
   259 --]]--
   260 function selector_prototype:add_where(expression)
   261   add(self._where, expression)
   262   return self
   263 end
   264 --//--
   266 --[[--
   267 db_selector =                -- same selector returned
   268 <db_selector>:add_group_by(
   269   expression                 -- expression as passed to "assemble_command"
   270 )
   272 Adds expressions for GROUP BY clause to the selector. The selector is modified and returned.
   274 --]]--
   275 function selector_prototype:add_group_by(expression)
   276   add(self._group_by, expression)
   277   return self
   278 end
   279 --//--
   281 --[[--
   282 db_selector =              -- same selector returned
   283 <db_selector>:add_having(
   284   expression               -- expression as passed to "assemble_command"
   285 )
   287 Adds expressions for HAVING clause to the selector. The selector is modified and returned. Multiple calls cause expressions to be AND-combined.
   289 --]]--
   290 function selector_prototype:add_having(expression)
   291   add(self._having, expression)
   292   return self
   293 end
   294 --//--
   296 --[[--
   297 db_selector =               -- same selector returned
   298 <db_selector>:add_combine(
   299   expression                -- expression as passed to "assemble_command"
   300 )
   302 This function is used for UNION/INTERSECT/EXCEPT clauses. It does not need to be called directly. Use "union", "union_all", "intersect", "intersect_all", "except" and "except_all" instead.
   304 --]]--
   305 function selector_prototype:add_combine(expression)
   306   add(self._combine, expression)
   307   return self
   308 end
   309 --//--
   311 --[[--
   312 db_selector =                -- same selector returned
   313 <db_selector>:add_order_by(
   314   expression                 -- expression as passed to "assemble_command"
   315 )
   317 Adds expressions for ORDER BY clause to the selector. The selector is modified and returned.
   319 --]]--
   320 function selector_prototype:add_order_by(expression)
   321   add(self._order_by, expression)
   322   return self
   323 end
   324 --//--
   326 --[[--
   327 db_selector =         -- same selector returned
   328 <db_selector>:limit(
   329   count               -- integer used as LIMIT
   330 )
   332 Limits the number of rows to a given number, by using LIMIT. The selector is modified and returned.
   334 --]]--
   335 function selector_prototype:limit(count)
   336   if type(count) ~= "number" or count % 1 ~= 0 then
   337     error("LIMIT must be an integer.")
   338   end
   339   self._limit = count
   340   return self
   341 end
   342 --//--
   344 --[[--
   345 db_selector =          -- same selector returned
   346 <db_selector>:offset(
   347   count                -- integer used as OFFSET
   348 )
   350 Skips a given number of rows, by using OFFSET. The selector is modified and returned.
   352 --]]--
   353 function selector_prototype:offset(count)
   354   if type(count) ~= "number" or count % 1 ~= 0 then
   355     error("OFFSET must be an integer.")
   356   end
   357   self._offset = count
   358   return self
   359 end
   360 --//--
   362 --[[--
   363 db_selector =              -- same selector returned
   364 <db_selector>:for_share()
   366 Adds FOR SHARE to the statement, to share-lock all rows read. The selector is modified and returned.
   368 --]]--
   369 function selector_prototype:for_share()
   370   self._read_lock.all = true
   371   return self
   372 end
   373 --//--
   375 --[[--
   376 db_selector =                -- same selector returned
   377 <db_selector>:for_share_of(
   378   expression                 -- expression as passed to "assemble_command"
   379 )
   381 Adds FOR SHARE OF to the statement, to share-lock all rows read by the named table(s). The selector is modified and returned.
   383 --]]--
   384 function selector_prototype:for_share_of(expression)
   385   add(self._read_lock, expression)
   386   return self
   387 end
   388 --//--
   390 --[[--
   391 db_selector =               -- same selector returned
   392 <db_selector>:for_update()
   394 Adds FOR UPDATE to the statement, to exclusivly lock all rows read. The selector is modified and returned.
   396 --]]--
   397 function selector_prototype:for_update()
   398   self._write_lock.all = true
   399   return self
   400 end
   401 --//--
   403 --[[--
   404 db_selector =                 -- same selector returned
   405 <db_selector>:for_update_of(
   406   expression                  -- expression as passed to "assemble_command"
   407 )
   409 Adds FOR SHARE OF to the statement, to exclusivly lock all rows read by the named table(s). The selector is modified and returned.
   411 --]]--
   412 function selector_prototype:for_update_of(expression)
   413   add(self._write_lock, expression)
   414   return self
   415 end
   416 --//--
   418 --[[--
   419 db_selector =                 -- same selector returned
   420 <db_selector>:reset_fields()
   422 This method removes all fields added by method "add_field". The selector is modified and returned.
   424 --]]--
   425 function selector_prototype:reset_fields()
   426   for idx in ipairs(self._fields) do
   427     self._fields[idx] = nil
   428   end
   429   return self
   430 end
   431 --//--
   433 --[[--
   434 db_selector =             -- same selector returned
   435 <db_selector>:add_field(
   436   expression,             -- expression as passed to "assemble_command"
   437   alias,                  -- optional alias expression as passed to "assemble_command"
   438   option_list             -- optional list of options (may contain strings "distinct" or "grouped")
   439 )
   441 Adds fields to the selector. The selector is modified and returned. The third argument can be a list of options. If option "distinct" is given, then "add_distinct_on" will be executed for the given field or alias. If option "grouped" is given, then "add_group_by" will be executed for the given field or alias.
   443 --]]--
   444 function selector_prototype:add_field(expression, alias, options)
   445   if alias then
   446     add(self._fields, {'$ AS "$"', {expression}, {alias}})
   447   else
   448     add(self._fields, expression)
   449   end
   450   if options then
   451     for i, option in ipairs(options) do
   452       if option == "distinct" then
   453         if alias then
   454           self:add_distinct_on('"' .. alias .. '"')
   455         else
   456           self:add_distinct_on(expression)
   457         end
   458       elseif option == "grouped" then
   459         if alias then
   460           self:add_group_by('"' .. alias .. '"')
   461         else
   462           self:add_group_by(expression)
   463         end
   464       else
   465         error("Unknown option '" .. option .. "' to add_field method.")
   466       end
   467     end
   468   end
   469   return self
   470 end
   471 --//--
   473 --[[--
   474 db_selector =        -- same selector returned
   475 <db_selector>:join(
   476   expression,        -- expression as passed to "assemble_command"
   477   alias,             -- optional alias expression as passed to "assemble_command"
   478   condition          -- optional condition expression as passed to "assemble_command"
   479 )
   481 Adds expressions for FROM clause to the selector. The selector is modified and returned. If an additional condition is given, an INNER JOIN will be used, otherwise a CROSS JOIN.
   483 This method is identical to "add_from".
   485 --]]--
   486 function selector_prototype:join(...)  -- NOTE: alias for add_from
   487   return self:add_from(...)
   488 end
   489 --//--
   491 --[[--
   492 db_selector =        -- same selector returned
   493 <db_selector>:from(
   494   expression,        -- expression as passed to "assemble_command"
   495   alias,             -- optional alias expression as passed to "assemble_command"
   496   condition          -- optional condition expression as passed to "assemble_command"
   497 )
   499 Adds the first expression for FROM clause to the selector. The selector is modified and returned. If an additional condition is given, an INNER JOIN will be used, otherwise a CROSS JOIN.
   501 This method is identical to "add_from" or "join", except that an error is thrown, if there is already any FROM expression existent.
   503 --]]--
   504 function selector_prototype:from(expression, alias, condition)
   505   if #self._from > 0 then
   506     error("From-clause already existing (hint: try join).")
   507   end
   508   return self:join(expression, alias, condition)
   509 end
   510 --//--
   512 --[[--
   513 db_selector =             -- same selector returned
   514 <db_selector>:left_join(
   515   expression,             -- expression as passed to "assemble_command"
   516   alias,                  -- optional alias expression as passed to "assemble_command"
   517   condition               -- optional condition expression as passed to "assemble_command"
   518 )
   520 Adds expressions for FROM clause to the selector using a LEFT OUTER JOIN. The selector is modified and returned.
   522 --]]--
   523 function selector_prototype:left_join(expression, alias, condition)
   524   local first = (#self._from == 0)
   525   if not first then
   526     add(self._from, "LEFT OUTER JOIN")
   527   end
   528   if alias then
   529     add(self._from, {'$ AS "$"', {expression}, {alias}})
   530   else
   531     add(self._from, expression)
   532   end
   533   if condition then
   534     if first then
   535       self:add_where(condition)
   536     else
   537       add(self._from, "ON")
   538       add(self._from, condition)
   539     end
   540   end
   541   return self
   542 end
   543 --//--
   545 --[[--
   546 db_selector =         -- same selector returned
   547 <db_selector>:union(
   548   expression          -- expression or selector without ORDER BY, LIMIT, FOR UPDATE or FOR SHARE
   549 )
   551 This method adds a UNION clause to the given selector. The selector is modified and returned. The selector (or expression) passed as argument to this function shall not contain any ORDER BY, LIMIT, FOR UPDATE or FOR SHARE clauses.
   553 --]]--
   554 function selector_prototype:union(expression)
   555   self:add_combine{"UNION $", {expression}}
   556   return self
   557 end
   558 --//--
   560 --[[--
   561 db_selector =             -- same selector returned
   562 <db_selector>:union_all(
   563   expression              -- expression or selector without ORDER BY, LIMIT, FOR UPDATE or FOR SHARE
   564 )
   566 This method adds a UNION ALL clause to the given selector. The selector is modified and returned. The selector (or expression) passed as argument to this function shall not contain any ORDER BY, LIMIT, FOR UPDATE or FOR SHARE clauses.
   568 --]]--
   569 function selector_prototype:union_all(expression)
   570   self:add_combine{"UNION ALL $", {expression}}
   571   return self
   572 end
   573 --//--
   575 --[[--
   576 db_selector =             -- same selector returned
   577 <db_selector>:intersect(
   578   expression              -- expression or selector without ORDER BY, LIMIT, FOR UPDATE or FOR SHARE
   579 )
   581 This method adds an INTERSECT clause to the given selector. The selector is modified and returned. The selector (or expression) passed as argument to this function shall not contain any ORDER BY, LIMIT, FOR UPDATE or FOR SHARE clauses.
   583 --]]--
   584 function selector_prototype:intersect(expression)
   585   self:add_combine{"INTERSECT $", {expression}}
   586   return self
   587 end
   588 --//--
   590 --[[--
   591 db_selector =                 -- same selector returned
   592 <db_selector>:intersect_all(
   593   expression                  -- expression or selector without ORDER BY, LIMIT, FOR UPDATE or FOR SHARE
   594 )
   596 This method adds an INTERSECT ALL clause to the given selector. The selector is modified and returned. The selector (or expression) passed as argument to this function shall not contain any ORDER BY, LIMIT, FOR UPDATE or FOR SHARE clauses.
   598 --]]--
   599 function selector_prototype:intersect_all(expression)
   600   self:add_combine{"INTERSECT ALL $", {expression}}
   601   return self
   602 end
   603 --//--
   605 --[[--
   606 db_selector =          -- same selector returned
   607 <db_selector>:except(
   608   expression           -- expression or selector without ORDER BY, LIMIT, FOR UPDATE or FOR SHARE
   609 )
   611 This method adds an EXCEPT clause to the given selector. The selector is modified and returned. The selector (or expression) passed as argument to this function shall not contain any ORDER BY, LIMIT, FOR UPDATE or FOR SHARE clauses.
   613 --]]--
   614 function selector_prototype:except(expression)
   615   self:add_combine{"EXCEPT $", {expression}}
   616   return self
   617 end
   618 --//--
   620 --[[--
   621 db_selector =              -- same selector returned
   622 <db_selector>:except_all(
   623   expression               -- expression or selector without ORDER BY, LIMIT, FOR UPDATE or FOR SHARE
   624 )
   626 This method adds an EXCEPT ALL clause to the given selector. The selector is modified and returned. The selector (or expression) passed as argument to this function shall not contain any ORDER BY, LIMIT, FOR UPDATE or FOR SHARE clauses.
   628 --]]--
   629 function selector_prototype:except_all(expression)
   630   self:add_combine{"EXCEPT ALL $", {expression}}
   631   return self
   632 end
   633 --//--
   635 --[[--
   636 db_selector =             -- same selector returned
   637 <db_selector>:set_class(
   638   class                   -- database class (model)
   639 )
   641 This method makes the selector to return database result lists or objects of the given database class (model). The selector is modified and returned.
   643 --]]--
   644 function selector_prototype:set_class(class)
   645   self._class = class
   646   return self
   647 end
   648 --//--
   650 --[[--
   651 db_selector =          -- same selector returned
   652 <db_selector>:attach(
   653   mode,                -- attachment type: "11" one to one, "1m" one to many, "m1" many to one
   654   data2,               -- other database result list or object, the results of this selector shall be attached with
   655   field1,              -- field name(s) in result list or object of this selector used for attaching
   656   field2,              -- field name(s) in "data2" used for attaching
   657   ref1,                -- name of reference field in the results of this selector after attaching
   658   ref2                 -- name of reference field in "data2" after attaching
   659 )
   661 This method causes database result lists or objects of this selector to be attached with other database result lists after execution. This method does not need to be called directly.
   663 --]]--
   664 function selector_prototype:attach(mode, data2, field1, field2, ref1, ref2)
   665   self._attach = {
   666     mode = mode,
   667     data2 = data2,
   668     field1 = field1,
   669     field2 = field2,
   670     ref1 = ref1,
   671     ref2 = ref2
   672   }
   673   return self
   674 end
   675 --//--
   677 function selector_metatable:__tostring()
   678   local parts = {sep = " "}
   679   if #self._with > 0 then
   680     add(parts, {"WITH RECURSIVE $", self._with})
   681   end
   682   add(parts, "SELECT")
   683   if self._distinct then
   684     add(parts, "DISTINCT")
   685   elseif #self._distinct_on > 0 then
   686     add(parts, {"DISTINCT ON ($)", self._distinct_on})
   687   end
   688   add(parts, {"$", self._fields})
   689   if #self._from > 0 then
   690     add(parts, {"FROM $", self._from})
   691   end
   692   if #self._mode == "empty_list" then
   693     add(parts, "WHERE FALSE")
   694   elseif #self._where > 0 then
   695     add(parts, {"WHERE ($)", self._where})
   696   end
   697   if #self._group_by > 0 then
   698     add(parts, {"GROUP BY $", self._group_by})
   699   end
   700   if #self._having > 0 then
   701     add(parts, {"HAVING ($)", self._having})
   702   end
   703   for i, v in ipairs(self._combine) do
   704     add(parts, v)
   705   end
   706   if #self._order_by > 0 then
   707     add(parts, {"ORDER BY $", self._order_by})
   708   end
   709   if self._mode == "empty_list" then
   710     add(parts, "LIMIT 0")
   711   elseif self._mode ~= "list" then
   712     add(parts, "LIMIT 1")
   713   elseif self._limit then
   714     add(parts, "LIMIT " .. self._limit)
   715   end
   716   if self._offset then
   717     add(parts, "OFFSET " .. self._offset)
   718   end
   719   if self._write_lock.all then
   720     add(parts, "FOR UPDATE")
   721   else
   722     if self._read_lock.all then
   723       add(parts, "FOR SHARE")
   724     elseif #self._read_lock > 0 then
   725       add(parts, {"FOR SHARE OF $", self._read_lock})
   726     end
   727     if #self._write_lock > 0 then
   728       add(parts, {"FOR UPDATE OF $", self._write_lock})
   729     end
   730   end
   731   return self._db_conn:assemble_command{"$", parts}
   732 end
   734 --[[--
   735 db_error,                 -- database error object, or nil in case of success
   736 result =                  -- database result list or object
   737 <db_selector>:try_exec()
   739 This method executes the selector on its database. First return value is an error object or nil in case of success. Second return value is the result list or object.
   741 --]]--
   742 function selector_prototype:try_exec()
   743   if self._mode == "empty_list" then
   744     if self._class then
   745       return nil, self._class:create_list()
   746     else
   747        return nil, self._db_conn:create_list()
   748     end
   749   end
   750   local db_error, db_result = self._db_conn:try_query(self, self._mode)
   751   if db_error then
   752     return db_error
   753   elseif db_result then
   754     if self._class then set_class(db_result, self._class) end
   755     if self._attach then
   756       attach(
   757         self._attach.mode,
   758         db_result,
   759         self._attach.data2,
   760         self._attach.field1,
   761         self._attach.field2,
   762         self._attach.ref1,
   763         self._attach.ref2
   764       )
   765     end
   766     return nil, db_result
   767   else
   768     return nil
   769   end
   770 end
   771 --//--
   773 --[[--
   774 result =              -- database result list or object
   775 <db_selector>:exec()
   777 This method executes the selector on its database. The result list or object is returned on success, otherwise an error is thrown.
   779 --]]--
   780 function selector_prototype:exec()
   781   local db_error, result = self:try_exec()
   782   if db_error then
   783     db_error:escalate()
   784   else
   785     return result
   786   end
   787 end
   788 --//--
   790 --[[--
   791 count =                -- number of rows returned
   792 <db_selector>:count()
   794 This function wraps the given selector inside a subquery to count the number of rows returned by the database. NOTE: The result is cached inside the selector, thus the selector should NOT be modified afterwards.
   796 --]]--
   797 function selector_prototype:count()
   798   if not self._count then
   799     local count_selector = self:get_db_conn():new_selector()
   800     count_selector:add_field('count(1)')
   801     count_selector:add_from(self)
   802     count_selector:single_object_mode()
   803     self._count = count_selector:exec().count
   804   end
   805   return self._count
   806 end
   807 --//--
   811 -----------------
   812 -- attachments --
   813 -----------------
   815 local function attach_key(row, fields)
   816   local t = type(fields)
   817   if t == "string" then
   818     return tostring(row[fields])
   819   elseif t == "table" then
   820     local r = {}
   821     for idx, field in ipairs(fields) do
   822       r[idx] = string.format("%q", row[field])
   823     end
   824     return table.concat(r)
   825   else
   826     error("Field information for 'mondelefant.attach' is neither a string nor a table.")
   827   end
   828 end
   830 --[[--
   831 mondelefant.attach(
   832   mode,              -- attachment type: "11" one to one, "1m" one to many, "m1" many to one
   833   data1,             -- first database result list or object
   834   data2,             -- second database result list or object
   835   key1,              -- field name(s) in first result list or object used for attaching
   836   key2,              -- field name(s) in second result list or object used for attaching
   837   ref1,              -- name of reference field to be set in first database result list or object
   838   ref2               -- name of reference field to be set in second database result list or object
   839 )
   841 This function attaches database result lists/objects with each other. It does not need to be called directly.
   843 --]]--
   844 function attach(mode, data1, data2, key1, key2, ref1, ref2)
   845   local many1, many2
   846   if mode == "11" then
   847     many1 = false
   848     many2 = false
   849   elseif mode == "1m" then
   850     many1 = false
   851     many2 = true
   852   elseif mode == "m1" then
   853     many1 = true
   854     many2 = false
   855   elseif mode == "mm" then
   856     many1 = true
   857     many2 = true
   858   else
   859     error("Unknown mode specified for 'mondelefant.attach'.")
   860   end
   861   local list1, list2
   862   if data1._type == "object" then
   863     list1 = { data1 }
   864   elseif data1._type == "list" then
   865     list1 = data1
   866   else
   867     error("First result data given to 'mondelefant.attach' is invalid.")
   868   end
   869   if data2._type == "object" then
   870     list2 = { data2 }
   871   elseif data2._type == "list" then
   872     list2 = data2
   873   else
   874     error("Second result data given to 'mondelefant.attach' is invalid.")
   875   end
   876   local hash1 = {}
   877   local hash2 = {}
   878   if ref2 then
   879     for i, row in ipairs(list1) do
   880       local key = attach_key(row, key1)
   881       local list = hash1[key]
   882       if not list then list = {}; hash1[key] = list end
   883       list[#list + 1] = row
   884     end
   885   end
   886   if ref1 then
   887     for i, row in ipairs(list2) do
   888       local key = attach_key(row, key2)
   889       local list = hash2[key]
   890       if not list then list = {}; hash2[key] = list end
   891       list[#list + 1] = row
   892     end
   893     for i, row in ipairs(list1) do
   894       local key = attach_key(row, key1)
   895       local matching_rows = hash2[key]
   896       if many2 then
   897         local list = data2._connection:create_list(matching_rows)
   898         list._class = data2._class
   899         row._ref[ref1] = list
   900       elseif matching_rows and #matching_rows == 1 then
   901         row._ref[ref1] = matching_rows[1]
   902       else
   903         row._ref[ref1] = false
   904       end
   905     end
   906   end
   907   if ref2 then
   908     for i, row in ipairs(list2) do
   909       local key = attach_key(row, key2)
   910       local matching_rows = hash1[key]
   911       if many1 then
   912         local list = data1._connection:create_list(matching_rows)
   913         list._class = data1._class
   914         row._ref[ref2] = list
   915       elseif matching_rows and #matching_rows == 1 then
   916         row._ref[ref2] = matching_rows[1]
   917       else
   918         row._ref[ref2] = false
   919       end
   920     end
   921   end
   922 end
   923 --//--
   927 ------------------
   928 -- model system --
   929 ------------------
   931 --[[--
   932 <db_class>.primary_key
   934 Primary key of a database class (model). Defaults to "id".
   936 If the primary key is a tuple, then a sequence (table with integer keys mapped to the column names) must be used. If the primary key is contained in a JSON document within a table column, then a special object with the following fields is expected: {json_doc = "column_name", key = "field_name_within_json_object", type = "postgresql_type"}.
   938 --]]--
   939 class_prototype.primary_key = "id"
   940 --//--
   942 --[[--
   943 <db_class>.document_column
   945 Optional column name to redirect key lookups to. This can be used to allow for an easier access to fields of a JSON document.
   947 --]]--
   948 class_prototype.document_column = nil
   949 --//--
   951 --[[--
   952 db_handle =               -- database connection handle used by this class
   953 <db_class>:get_db_conn()
   955 By implementing this method for a particular model or overwriting it in the default prototype "mondelefant.class_prototype", classes are connected with a particular database. This method needs to return a database connection handle. If it is not overwritten, an error is thrown, when invoking this method.
   957 --]]--
   958 function class_prototype:get_db_conn()
   959   error(
   960     "Method mondelefant class(_prototype):get_db_conn() " ..
   961     "has to be implemented."
   962   )
   963 end
   964 --//--
   966 --[[--
   967 string =                          -- string of form '"schemaname"."tablename"' or '"tablename"'
   968 <db_class>:get_qualified_table()
   970 This method returns a string with the (double quoted) qualified table name used to store objects of this class.
   972 --]]--
   973 function class_prototype:get_qualified_table()
   974   if not self.table then error "Table unknown." end
   975   if self.schema then
   976     return '"' .. self.schema .. '"."' .. self.table .. '"'
   977   else
   978     return '"' .. self.table .. '"'
   979   end
   980 end
   981 --]]--
   983 --[[--
   984 string =                                  -- single quoted string of form "'schemaname.tablename'" or "'tablename'"
   985 <db_class>:get_qualified_table_literal()
   987 This method returns a string with an SQL literal representing the given table. It causes ambiguities when the table name contains a dot (".") character.
   989 --]]--
   990 function class_prototype:get_qualified_table_literal()
   991   if not self.table then error "Table unknown." end
   992   if self.schema then
   993     return self.schema .. '.' .. self.table
   994   else
   995     return self.table
   996   end
   997 end
   998 --//--
  1000 --[[--
  1001 list =                             -- list of column names of primary key
  1002 <db_class>:get_primary_key_list()
  1004 This method returns a list of column names of the primary key.
  1006 --]]--
  1007 function class_prototype:get_primary_key_list()
  1008   local primary_key = self.primary_key
  1009   if type(primary_key) == "string" then
  1010     return {primary_key}
  1011   else
  1012     return primary_key
  1013   end
  1014 end
  1015 --//--
  1017 --[[--
  1018 columns =                 -- list of columns
  1019 <db_class>:get_columns()
  1021 This method returns a list of column names of the table used for the class.
  1023 --]]--
  1024 function class_prototype:get_columns()
  1025   if self._columns then
  1026     return self._columns
  1027   end
  1028   local selector = self:get_db_conn():new_selector()
  1029   selector:set_class(self)
  1030   selector:from(self:get_qualified_table())
  1031   selector:add_field("*")
  1032   selector:add_where("FALSE")
  1033   local db_result = selector:exec()
  1034   local connection = db_result._connection
  1035   local columns = {}
  1036   for idx, info in ipairs(db_result._column_info) do
  1037     local key   = info.field_name
  1038     local value = {
  1039       name = key,
  1040       type = connection.type_mappings[info.type]
  1041     }
  1042     columns[key] = value
  1043     table.insert(columns, value)
  1044   end
  1045   self._columns = columns
  1046   return columns
  1047 end
  1048 --//--
  1050 --[[--
  1051 selector =                -- new selector for selecting objects of this class
  1052 <db_class>:new_selector(
  1053   db_conn                 -- optional(!) database connection handle, defaults to result of :get_db_conn()
  1054 )
  1056 This method creates a new selector for selecting objects of the class.
  1058 --]]--
  1059 function class_prototype:new_selector(db_conn)
  1060   local selector = (db_conn or self:get_db_conn()):new_selector()
  1061   selector:set_class(self)
  1062   selector:from(self:get_qualified_table())
  1063   selector:add_field(self:get_qualified_table() .. ".*")
  1064   return selector
  1065 end
  1066 --//--
  1068 --[[--
  1069 db_list =                 -- database result being an empty list
  1070 <db_class>:create_list()
  1072 Creates an empty database result representing a list of objects of the given class.
  1074 --]]--
  1075 function class_prototype:create_list()
  1076   local list = self:get_db_conn():create_list()
  1077   list._class = self
  1078   return list
  1079 end
  1080 --//--
  1082 --[[--
  1083 db_object =       -- database object (instance of model)
  1084 <db_class>:new()
  1086 Creates a new object of the given class.
  1088 --]]--
  1089 function class_prototype:new()
  1090   local object = self:get_db_conn():create_object()
  1091   object._class = self
  1092   object._new = true
  1093   return object
  1094 end
  1095 --//--
  1097 --[[--
  1098 <db_object>:upsert_mode()
  1100 Enables UPSERT mode for an existing (new) database object. Note that only new objects can use the UPSERT mode, i.e. it is not possible to call this method on objects returned from a database query.
  1102 --]]--
  1103 function class_prototype.object:upsert_mode()
  1104   if not self._new then
  1105     error("Upsert mode requires a new object and cannot be used on objects returned from a database query.")
  1106   end
  1107   self._upsert = true
  1108   return self
  1109 end
  1110 --//--
  1112 --[[--
  1113 db_error =              -- database error object, or nil in case of success
  1114 <db_object>:try_save()
  1116 This method saves changes to an object in the database. Returns nil on success, otherwise an error object is returned.
  1118 --]]--
  1119 function class_prototype.object:try_save()
  1120   if not self._class then
  1121     error("Cannot save object: No class information available.")
  1122   end
  1123   local primary_key = self._class:get_primary_key_list()
  1124   if self._new then
  1125     local fields = {sep = ", "}
  1126     local values = {sep = ", "}
  1127     for key in pairs(self._dirty or {}) do
  1128       add(fields, '"' .. key .. '"')
  1129       add(values, {'?', self._col[key]})
  1130     end
  1131     local returning = { sep = ", " }
  1132     if primary_key.json_doc then
  1133       returning[1] = {
  1134         '("$"->>?)::$ AS "json_key"',
  1135         {primary_key.json_doc}, primary_key.key, {primary_key.type}
  1136       }
  1137     else
  1138       for idx, value in ipairs(primary_key) do
  1139         returning[idx] = '"' .. value .. '"'
  1140       end
  1141     end
  1142     local db_error, db_result
  1143     if self._upsert then
  1144       local upsert_keys = {sep = ", "}
  1145       if primary_key.json_doc then
  1146         upsert_keys[1] = {
  1147           '("$"->>?)::$',
  1148           {primary_key.json_doc}, primary_key.key, {primary_key.type}
  1149         }
  1150       else
  1151         for idx, value in ipairs(primary_key) do
  1152           upsert_keys[idx] = '"' .. value .. '"'
  1153         end
  1154       end
  1155       if #fields == 0 then
  1156         db_error, db_result = self._connection:try_query(
  1157           {
  1158             'INSERT INTO $ DEFAULT VALUES ON CONFLICT ($) DO NOTHING $',
  1159             {self._class:get_qualified_table()},
  1160             upsert_keys,
  1161             returning
  1162           },
  1163           "object"
  1164         )
  1165       else
  1166         local upsert_sets = {sep = ", "}
  1167         for key in pairs(self._dirty) do
  1168           add(upsert_sets, {'"$" = ?', {key}, self._col[key]})
  1169         end
  1170         db_error, db_result = self._connection:try_query(
  1171           {
  1172             'INSERT INTO $ ($) VALUES ($) ON CONFLICT ($) DO UPDATE SET $ RETURNING $',
  1173             {self._class:get_qualified_table()},
  1174             fields,
  1175             values,
  1176             upsert_keys,
  1177             upsert_sets,
  1178             returning
  1179           },
  1180           "object"
  1181         )
  1182       end
  1183     else
  1184       if #fields == 0 then
  1185         db_error, db_result = self._connection:try_query(
  1186           {
  1187             'INSERT INTO $ DEFAULT VALUES RETURNING $',
  1188             {self._class:get_qualified_table()},
  1189             returning
  1190           },
  1191           "object"
  1192         )
  1193       else
  1194         db_error, db_result = self._connection:try_query(
  1195           {
  1196             'INSERT INTO $ ($) VALUES ($) RETURNING $',
  1197             {self._class:get_qualified_table()},
  1198             fields,
  1199             values,
  1200             returning
  1201           },
  1202           "object"
  1203         )
  1204       end
  1205     end
  1206     if db_error then
  1207       return db_error
  1208     end
  1209     if primary_key.json_doc then
  1210       self._col[primary_key.json_doc][primary_key.key] = db_result.json_key
  1211     else
  1212       for idx, value in ipairs(primary_key) do
  1213         self[value] = db_result[value]
  1214       end
  1215     end
  1216     if not self._upsert then
  1217       self._new = false
  1218     end
  1219   else
  1220     local update_sets = {sep = ", "}
  1221     for key, mutability_state in pairs(self._dirty or {}) do
  1222       if
  1223         mutability_state == true or (
  1224           verify_mutability_state and
  1225           verify_mutability_state(self._col[key], mutability_state)
  1226         )
  1227       then
  1228         add(update_sets, {'"$" = ?', {key}, self._col[key]})
  1229         self._dirty[key] = true  -- always dirty in case of later error
  1230       end
  1231     end
  1232     if #update_sets >= 1 then
  1233       local primary_key_compare = {sep = " AND "}
  1234       if primary_key.json_doc then
  1235         primary_key_compare[1] = {
  1236           '("$"->>?)::$ = ?',
  1237           {primary_key.json_doc}, primary_key.key, {primary_key.type},
  1238           self._col[primary_key.json_doc][primary_key.key]
  1239         }
  1240       else
  1241         for idx, value in ipairs(primary_key) do
  1242           primary_key_compare[idx] = {
  1243             "$ = ?",
  1244             {'"' .. value .. '"'},
  1245             self[value]
  1246           }
  1247         end
  1248       end
  1249       local db_error = self._connection:try_query{
  1250         'UPDATE $ SET $ WHERE $',
  1251         {self._class:get_qualified_table()},
  1252         update_sets,
  1253         primary_key_compare
  1254       }
  1255       if db_error then
  1256         return db_error
  1257       end
  1258     end
  1259   end
  1260   for key in pairs(self._dirty or {}) do
  1261     if save_mutability_state then
  1262       self._dirty[key] =
  1263         save_mutability_state and save_mutability_state(self._col[key]) or nil
  1264     end
  1265   end
  1266   return nil
  1267 end
  1268 --//--
  1270 --[[--
  1271 <db_object>:save()
  1273 This method saves changes to an object in the database. Throws error, unless successful.
  1275 --]]--
  1276 function class_prototype.object:save()
  1277   local db_error = self:try_save()
  1278   if db_error then
  1279     db_error:escalate()
  1280   end
  1281   return self
  1282 end
  1283 --//--
  1285 --[[--
  1286 db_error =                 -- database error object, or nil in case of success
  1287 <db_object>:try_destroy()
  1289 This method deletes an object in the database. Returns nil on success, otherwise an error object is returned.
  1291 --]]--
  1292 function class_prototype.object:try_destroy()
  1293   if not self._class then
  1294     error("Cannot destroy object: No class information available.")
  1295   end
  1296   local primary_key = self._class:get_primary_key_list()
  1297   local primary_key_compare = {sep = " AND "}
  1298   if primary_key.json_doc then
  1299     primary_key_compare[1] = {
  1300       '("$"->>?)::$ = ?',
  1301       {primary_key.json_doc}, primary_key.key, {primary_key.type},
  1302       self._col[primary_key.json_doc][primary_key.key]
  1303     }
  1304   else
  1305     for idx, value in ipairs(primary_key) do
  1306       primary_key_compare[idx] = {
  1307         "$ = ?",
  1308         {'"' .. value .. '"'},
  1309         self[value]
  1310       }
  1311     end
  1312   end
  1313   return self._connection:try_query{
  1314     'DELETE FROM $ WHERE $',
  1315     {self._class:get_qualified_table()},
  1316     primary_key_compare
  1317   }
  1318 end
  1319 --//--
  1321 --[[--
  1322 <db_object>:destroy()
  1324 This method deletes an object in the database. Throws error, unless successful.
  1326 --]]--
  1327 function class_prototype.object:destroy()
  1328   local db_error = self:try_destroy()
  1329   if db_error then
  1330     db_error:escalate()
  1331   end
  1332   return self
  1333 end
  1334 --//--
  1336 --[[--
  1337 db_selector =
  1338 <db_list>:get_reference_selector(
  1339   ref_name,                        -- name of reference (e.g. "children")
  1340   options,                         -- table options passed to the reference loader (e.g. { order = ... })
  1341   ref_alias,                       -- optional alias for the reference (e.g. "ordered_children")
  1342   back_ref_alias                   -- back reference name (e.g. "parent")
  1343 )
  1345 This method returns a special selector for selecting referenced objects. It is prepared in a way, that on execution of the selector, all returned objects are attached with the objects of the existent list. The "ref" and "back_ref" arguments passed to "add_reference" are used for the attachment, unless aliases are given with "ref_alias" and "back_ref_alias". If "options" are set, these options are passed to the reference loader. The default reference loader supports only one option named "order". If "order" is set to nil, the default order is used, if "order" is set to false, no ORDER BY statment is included in the selector, otherwise the given expression is used for ordering.
  1347 This method is not only available for database result lists but also for database result objects.
  1349 --]]--
  1350 function class_prototype.list:get_reference_selector(
  1351   ref_name, options, ref_alias, back_ref_alias
  1352 )
  1353   local ref_info = self._class.references[ref_name]
  1354   if not ref_info then
  1355     error('Reference with name "' .. ref_name .. '" not found.')
  1356   end
  1357   local selector = ref_info.selector_generator(self, options or {})
  1358   local mode = ref_info.mode
  1359   if mode == "mm" or mode == "1m" then
  1360     mode = "m1"
  1361   elseif mode == "m1" then
  1362     mode = "1m"
  1363   end
  1364   local ref_alias = ref_alias
  1365   if ref_alias == false then
  1366     ref_alias = nil
  1367   elseif ref_alias == nil then
  1368     ref_alias = ref_name
  1369   end
  1370   local back_ref_alias
  1371   if back_ref_alias == false then
  1372     back_ref_alias = nil
  1373   elseif back_ref_alias == nil then
  1374     back_ref_alias = ref_info.back_ref
  1375   end
  1376   selector:attach(
  1377     mode,
  1378     self,
  1379     ref_info.that_key,                   ref_info.this_key,
  1380     back_ref_alias or ref_info.back_ref, ref_alias or ref_name
  1381   )
  1382   return selector
  1383 end
  1384 --//--
  1386 --[[--
  1387 db_list_or_object =
  1388 <db_list>:load(
  1389   ref_name,          -- name of reference (e.g. "children")
  1390   options,           -- table options passed to the reference loader (e.g. { order = ... })
  1391   ref_alias,         -- optional alias for the reference (e.g. "ordered_children")
  1392   back_ref_alias     -- back reference name (e.g. "parent")
  1393 )
  1395 This method loads referenced objects and attaches them with the objects of the existent list. The "ref" and "back_ref" arguments passed to "add_reference" are used for the attachment, unless aliases are given with "ref_alias" and "back_ref_alias". If "options" are set, these options are passed to the reference loader. The default reference loader supports only one option named "order". If "order" is set to nil, the default order is used, if "order" is set to false, no ORDER BY statment is included in the selector, otherwise the given expression is used for ordering.
  1397 This method is not only available for database result lists but also for database result objects.
  1399 --]]--
  1400 function class_prototype.list.load(...)
  1401   return class_prototype.list.get_reference_selector(...):exec()
  1402 end
  1403 --//--
  1405 --[[--
  1406 db_object =
  1407 <db_object>:get_reference_selector(
  1408   ref_name,                          -- name of reference (e.g. "children")
  1409   options,                           -- table options passed to the reference loader (e.g. { order = ... })
  1410   ref_alias,                         -- optional alias for the reference (e.g. "ordered_children")
  1411   back_ref_alias                     -- back reference name (e.g. "parent")
  1412 )
  1414 This method returns a special selector for selecting referenced objects. It is prepared in a way, that on execution of the selector, all returned objects are attached with the objects of the existent list. The "ref" and "back_ref" arguments passed to "add_reference" are used for the attachment, unless aliases are given with "ref_alias" and "back_ref_alias". If "options" are set, these options are passed to the reference loader. The default reference loader supports only one option named "order". If "order" is set to nil, the default order is used, if "order" is set to false, no ORDER BY statment is included in the selector, otherwise the given expression is used for ordering.
  1416 This method is not only available for database result objects but also for database result lists.
  1418 --]]--
  1419 function class_prototype.object:get_reference_selector(...)
  1420   local list = self._class:create_list()
  1421   list[1] = self
  1422   return list:get_reference_selector(...)
  1423 end
  1424 --//--
  1426 --[[--
  1427 db_list_or_object =
  1428 <db_object>:load(
  1429   ref_name,          -- name of reference (e.g. "children")
  1430   options,           -- table options passed to the reference loader (e.g. { order = ... })
  1431   ref_alias,         -- optional alias for the reference (e.g. "ordered_children")
  1432   back_ref_alias     -- back reference name (e.g. "parent")
  1433 )
  1435 This method loads referenced objects and attaches them with the objects of the existent list. The "ref" and "back_ref" arguments passed to "add_reference" are used for the attachment, unless aliases are given with "ref_alias" and "back_ref_alias". If "options" are set, these options are passed to the reference loader. The default reference loader supports only one option named "order". If "order" is set to nil, the default order is used, if "order" is set to false, no ORDER BY statment is included in the selector, otherwise the given expression is used for ordering.
  1437 This method is not only available for database result objects but also for database result lists. Calling this method for objects is unneccessary, unless additional options and/or an alias is used.
  1439 --]]--
  1440 function class_prototype.object.load(...)
  1441   return class_prototype.object.get_reference_selector(...):exec()
  1442 end
  1443 --//--
  1445 --[[--
  1446 db_class =                                        -- same class returned
  1447 <db_class>:add_reference{
  1448   mode                  = mode,                   -- "11", "1m", "m1", or "mm" (one/many to one/many)
  1449   to                    = to,                     -- referenced class (model), optionally as string or function returning the value (avoids autoload)
  1450   this_key              = this_key,               -- name of key in this class (model)
  1451   that_key              = that_key,               -- name of key in the other class (model) ("to" argument)
  1452   ref                   = ref,                    -- name of reference in this class, referring to the other class
  1453   back_ref              = back_ref,               -- name of reference in other class, referring to this class
  1454   default_order         = default_order,          -- expression as passed to "assemble_command" used for sorting
  1455   selector_generator    = selector_generator,     -- alternative function used as selector generator (use only, when you know what you are doing)
  1456   connected_by_table    = connected_by_table,     -- connecting table used for many to many relations
  1457   connected_by_this_key = connected_by_this_key,  -- key in connecting table referring to "this_key" of this class (model)
  1458   connected_by_that_key = connected_by_that_key   -- key in connecting table referring to "that_key" in other class (model) ("to" argument)
  1459 }
  1461 Denotes a reference from one database class to another database class (model to model relation). There are 4 possible types of references: one-to-one (mode = "11"), one-to-many (mode = "1m"), many-to-one ("m1"), and many-to-many ("mm"). References usually should be defined in both models, which are related to each other, with mirrored mode (i.e. "1m" in one model, and "m1" in the other). One-to-one and one-to-many references may have a "back_ref" setting, which causes that loaded objects of the referenced class, refer back to the originating object. One-to-many and many-to-many references may have a "default_order" setting, which selects the default order for selected objects. When adding a many-to-many reference, the argument "connected_by_table", "connected_by_this_key" and "connected_by_that_key" must be set additionally.
  1463 --]]--
  1464 function class_prototype:add_reference(args)
  1465   local selector_generator    = args.selector_generator
  1466   local mode                  = args.mode
  1467   local to                    = args.to
  1468   local this_key              = args.this_key
  1469   local that_key              = args.that_key
  1470   local connected_by_table    = args.connected_by_table  -- TODO: split to table and schema
  1471   local connected_by_this_key = args.connected_by_this_key
  1472   local connected_by_that_key = args.connected_by_that_key
  1473   local ref                   = args.ref
  1474   local back_ref              = args.back_ref
  1475   local default_order         = args.default_order
  1476   local model
  1477   local function get_model()
  1478     if not model then
  1479       if type(to) == "string" then
  1480         model = _G
  1481         for path_element in string.gmatch(to, "[^.]+") do
  1482           model = model[path_element]
  1483         end
  1484       elseif type(to) == "function" then
  1485         model = to()
  1486       else
  1487         model = to
  1488       end
  1489     end
  1490     if not model or model == _G then
  1491       error("Could not get model for reference.")
  1492     end
  1493     return model
  1494   end
  1495   self.references[ref] = {
  1496     mode     = mode,
  1497     this_key = this_key,
  1498     that_key = connected_by_table and "mm_ref_" or that_key,
  1499     ref      = ref,
  1500     back_ref = back_ref,
  1501     selector_generator = selector_generator or function(list, options)
  1502       -- TODO: support tuple keys
  1503       local options = options or {}
  1504       local model = get_model()
  1505       -- TODO: too many records cause PostgreSQL command stack overflow
  1506       local ids = { sep = ", " }
  1507       for i, object in ipairs(list) do
  1508         local id = object[this_key]
  1509         if id ~= nil then
  1510           ids[#ids+1] = {"?", id}
  1511         end
  1512       end
  1513       if #ids == 0 then
  1514         return model:new_selector():empty_list_mode()
  1515       end
  1516       local selector = model:new_selector()
  1517       if connected_by_table then
  1518         selector:join(
  1519           connected_by_table,
  1520           nil,
  1521           {
  1522             '$."$" = $."$"',
  1523             {connected_by_table},
  1524             {connected_by_that_key},
  1525             {model:get_qualified_table()},
  1526             {that_key}
  1527           }
  1528         )
  1529         selector:add_field(
  1530           {
  1531             '$."$"',
  1532             {connected_by_table},
  1533             {connected_by_this_key}
  1534           },
  1535           'mm_ref_'
  1536         )
  1537         selector:add_where{
  1538           '$."$" IN ($)',
  1539           {connected_by_table},
  1540           {connected_by_this_key},
  1541           ids
  1542         }
  1543       else
  1544         selector:add_where{'$."$" IN ($)', {model:get_qualified_table()}, {that_key}, ids}
  1545       end
  1546       if options.order == nil and default_order then
  1547         selector:add_order_by(default_order)
  1548       elseif options.order then
  1549         selector:add_order_by(options.order)
  1550       end
  1551       return selector
  1552     end
  1553   }
  1554   if mode == "m1" or mode == "11" then
  1555     self.foreign_keys[this_key] = ref
  1556   end
  1557   return self
  1558 end
  1559 --//--
  1561 return _M
