# HG changeset patch # User jbe # Date 1287243808 -7200 # Node ID 56648d7917b18800933874280b8c6ed24100f16d # Parent 3480a11da8e86a9bfcc141d13b224ecbf458a34f Added method :add_with(...) to create queries WITH RECURSIVE diff -r 3480a11da8e8 -r 56648d7917b1 libraries/mondelefant/mondelefant.lua --- a/libraries/mondelefant/mondelefant.lua Wed Oct 13 20:48:44 2010 +0200 +++ b/libraries/mondelefant/mondelefant.lua Sat Oct 16 17:43:28 2010 +0200 @@ -58,6 +58,7 @@ local function init_selector(self, db_conn) self._db_conn = db_conn self._mode = "list" + self._with = { sep = ", " } self._fields = { sep = ", " } self._distinct = false self._distinct_on = {sep = ", ", expression} @@ -141,6 +142,21 @@ --//-- --[[-- +db_selector = +:add_with( + expression = expression, + selector = selector +) + +Adds an WITH RECURSIVE expression to the selector. The selector is modified and returned. +--]]-- +function selector_prototype:add_with(expression, selector) + add(self._with, {"$ AS ($)", {expression}, {selector}}) + return self +end +--//-- + +--[[-- db_selector = -- same selector returned :add_distinct_on( expression -- expression as passed to "assemble_command" @@ -649,6 +665,9 @@ function selector_metatable:__tostring() local parts = {sep = " "} + if #self._with > 0 then + add(parts, {"WITH RECURSIVE $", self._with}) + end add(parts, "SELECT") if self._distinct then add(parts, "DISTINCT")