webmcp

diff libraries/mondelefant/mondelefant.lua @ 39:56648d7917b1

Added method <db_selector>:add_with(...) to create queries WITH RECURSIVE
author jbe
date Sat Oct 16 17:43:28 2010 +0200 (2010-10-16)
parents a02c25eb3517
children 791849c58105
line diff
     1.1 --- a/libraries/mondelefant/mondelefant.lua	Wed Oct 13 20:48:44 2010 +0200
     1.2 +++ b/libraries/mondelefant/mondelefant.lua	Sat Oct 16 17:43:28 2010 +0200
     1.3 @@ -58,6 +58,7 @@
     1.4  local function init_selector(self, db_conn)
     1.5    self._db_conn = db_conn
     1.6    self._mode = "list"
     1.7 +  self._with = { sep = ", " }
     1.8    self._fields = { sep = ", " }
     1.9    self._distinct = false
    1.10    self._distinct_on = {sep = ", ", expression}
    1.11 @@ -141,6 +142,21 @@
    1.12  --//--
    1.13  
    1.14  --[[--
    1.15 +db_selector =
    1.16 +<db_selector>:add_with(
    1.17 +  expression = expression,
    1.18 +  selector   = selector
    1.19 +)
    1.20 +
    1.21 +Adds an WITH RECURSIVE expression to the selector. The selector is modified and returned.
    1.22 +--]]--
    1.23 +function selector_prototype:add_with(expression, selector)
    1.24 +  add(self._with, {"$ AS ($)", {expression}, {selector}})
    1.25 +  return self
    1.26 +end
    1.27 +--//--
    1.28 +
    1.29 +--[[--
    1.30  db_selector =                   -- same selector returned
    1.31  <db_selector>:add_distinct_on(
    1.32    expression                    -- expression as passed to "assemble_command"
    1.33 @@ -649,6 +665,9 @@
    1.34  
    1.35  function selector_metatable:__tostring()
    1.36    local parts = {sep = " "}
    1.37 +  if #self._with > 0 then
    1.38 +    add(parts, {"WITH RECURSIVE $", self._with})
    1.39 +  end
    1.40    add(parts, "SELECT")
    1.41    if self._distinct then
    1.42      add(parts, "DISTINCT")

Impressum / About Us