webmcp
diff libraries/mondelefant/mondelefant.lua @ 4:5e32ef998acf
Version 1.0.4
ui.link{...} with POST target can now be parameterized with BOTH content and text to allow HTML content for JavaScript browsers and a text-only version for accessiblity
Changes related to database selectors:
- Support for row-based locking
- New method :count(), caching and returning the number of rows, which WOULD have been returned by :exec()
- Bugfix: WHERE and HAVING expressions are now enclosed in parenthesis to avoid problems with operator precedence
ui.script{...} now supports external .js files
Changes in langtool.lua to cope with escaped new-line chars (\n)
ui.link{...} with POST target can now be parameterized with BOTH content and text to allow HTML content for JavaScript browsers and a text-only version for accessiblity
Changes related to database selectors:
- Support for row-based locking
- New method :count(), caching and returning the number of rows, which WOULD have been returned by :exec()
- Bugfix: WHERE and HAVING expressions are now enclosed in parenthesis to avoid problems with operator precedence
ui.script{...} now supports external .js files
Changes in langtool.lua to cope with escaped new-line chars (\n)
author | jbe/bsw |
---|---|
date | Fri Dec 25 12:00:00 2009 +0100 (2009-12-25) |
parents | 9fdfb27f8e67 |
children | 5cba83b3f411 |
line diff
1.1 --- a/libraries/mondelefant/mondelefant.lua Thu Dec 10 12:00:00 2009 +0100 1.2 +++ b/libraries/mondelefant/mondelefant.lua Fri Dec 25 12:00:00 2009 +0100 1.3 @@ -62,17 +62,15 @@ 1.4 self._distinct = false 1.5 self._distinct_on = {sep = ", ", expression} 1.6 self._from = { sep = " " } 1.7 - self._where = { sep = " AND " } 1.8 + self._where = { sep = ") AND (" } 1.9 self._group_by = { sep = ", " } 1.10 - self._having = { sep = " AND " } 1.11 + self._having = { sep = ") AND (" } 1.12 self._combine = { sep = " " } 1.13 self._order_by = { sep = ", " } 1.14 self._limit = nil 1.15 self._offset = nil 1.16 - --[[ 1.17 - self._lock = nil 1.18 - self._lock_tables = { sep = ", " } 1.19 - --]] 1.20 + self._read_lock = { sep = ", " } 1.21 + self._write_lock = { sep = ", " } 1.22 self._class = nil 1.23 self._attach = nil 1.24 return self 1.25 @@ -193,6 +191,26 @@ 1.26 return self 1.27 end 1.28 1.29 +function selector_prototype:for_share() 1.30 + self._read_lock.all = true 1.31 + return self 1.32 +end 1.33 + 1.34 +function selector_prototype:for_share_of(expression) 1.35 + add(self._read_lock, expression) 1.36 + return self 1.37 +end 1.38 + 1.39 +function selector_prototype:for_update() 1.40 + self._write_lock.all = true 1.41 + return self 1.42 +end 1.43 + 1.44 +function selector_prototype:for_update_of(expression) 1.45 + add(self._write_lock, expression) 1.46 + return self 1.47 +end 1.48 + 1.49 function selector_prototype:reset_fields() 1.50 for idx in ipairs(self._fields) do 1.51 self._fields[idx] = nil 1.52 @@ -324,13 +342,13 @@ 1.53 if #self._mode == "empty_list" then 1.54 add(parts, "WHERE FALSE") 1.55 elseif #self._where > 0 then 1.56 - add(parts, {"WHERE $", self._where}) 1.57 + add(parts, {"WHERE ($)", self._where}) 1.58 end 1.59 if #self._group_by > 0 then 1.60 add(parts, {"GROUP BY $", self._group_by}) 1.61 end 1.62 if #self._having > 0 then 1.63 - add(parts, {"HAVING $", self._having}) 1.64 + add(parts, {"HAVING ($)", self._having}) 1.65 end 1.66 for i, v in ipairs(self._combine) do 1.67 add(parts, v) 1.68 @@ -348,6 +366,18 @@ 1.69 if self._offset then 1.70 add(parts, "OFFSET " .. self._offset) 1.71 end 1.72 + if self._write_lock.all then 1.73 + add(parts, "FOR UPDATE") 1.74 + else 1.75 + if self._read_lock.all then 1.76 + add(parts, "FOR SHARE") 1.77 + elseif #self._read_lock > 0 then 1.78 + add(parts, {"FOR SHARE OF $", self._read_lock}) 1.79 + end 1.80 + if #self._write_lock > 0 then 1.81 + add(parts, {"FOR UPDATE OF $", self._write_lock}) 1.82 + end 1.83 + end 1.84 return self._db_conn:assemble_command{"$", parts} 1.85 end 1.86 1.87 @@ -390,6 +420,18 @@ 1.88 end 1.89 end 1.90 1.91 +-- NOTE: This function caches the result! 1.92 +function selector_prototype:count() 1.93 + if not self._count then 1.94 + local count_selector = self:get_db_conn():new_selector() 1.95 + count_selector:add_field('count(1)') 1.96 + count_selector:add_from(self) 1.97 + count_selector:single_object_mode() 1.98 + self._count = count_selector:exec().count 1.99 + end 1.100 + return self._count 1.101 +end 1.102 + 1.103 1.104 1.105 -----------------