webmcp
annotate libraries/mondelefant/example.lua @ 2:72860d232f32
Version 1.0.2
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
Fixed bug with explicit garbage collection (requests > 256kB caused an error)
Views prefixed with an underscore can't be called externally
ui.paginate now displays the last page, if the selected page number is too high.
author | jbe/bsw |
---|---|
date | Thu Dec 10 12:00:00 2009 +0100 (2009-12-10) |
parents | 9fdfb27f8e67 |
children | 3d43a5cf17c1 |
rev | line source |
---|---|
jbe/bsw@0 | 1 #!/usr/bin/env lua |
jbe/bsw@0 | 2 require("mondelefant") |
jbe/bsw@0 | 3 |
jbe/bsw@0 | 4 -- Standarddatenbankverbindung ist globale Variable 'db' |
jbe/bsw@0 | 5 function mondelefant.class_prototype:get_db_conn() return db end |
jbe/bsw@0 | 6 |
jbe/bsw@0 | 7 -- Verbindung aufbauen |
jbe/bsw@0 | 8 db = assert(mondelefant.connect{engine='postgresql', dbname='test'}) |
jbe/bsw@0 | 9 |
jbe/bsw@0 | 10 Product = mondelefant.new_class{ table = "product" } |
jbe/bsw@0 | 11 ProductVariant = mondelefant.new_class{ table = "product_variant" } |
jbe/bsw@0 | 12 |
jbe/bsw@0 | 13 Product:add_reference{ |
jbe/bsw@0 | 14 mode = "1m", |
jbe/bsw@0 | 15 to = ProductVariant, |
jbe/bsw@0 | 16 this_key = "number", |
jbe/bsw@0 | 17 that_key = "product_number", |
jbe/bsw@0 | 18 ref = "product_variants", |
jbe/bsw@0 | 19 back_ref = "product", |
jbe/bsw@0 | 20 --default_order = '"number"' |
jbe/bsw@0 | 21 } |
jbe/bsw@0 | 22 |
jbe/bsw@0 | 23 ProductVariant:add_reference{ |
jbe/bsw@0 | 24 mode = "m1", |
jbe/bsw@0 | 25 to = Product, |
jbe/bsw@0 | 26 this_key = "product_number", |
jbe/bsw@0 | 27 that_key = "number", |
jbe/bsw@0 | 28 ref = "product", |
jbe/bsw@0 | 29 back_ref = nil, |
jbe/bsw@0 | 30 --default_order = '"id"' |
jbe/bsw@0 | 31 } |
jbe/bsw@0 | 32 |
jbe/bsw@0 | 33 p = Product:new_selector():single_object_mode():add_where{"name=?", "Noodles"}:exec() |
jbe/bsw@0 | 34 |
jbe/bsw@0 | 35 |
jbe/bsw@0 | 36 --[[ |
jbe/bsw@0 | 37 -- Neue Datenbankklasse definieren |
jbe/bsw@0 | 38 Product = mondelefant.new_class{ table = '"product"' } |
jbe/bsw@0 | 39 |
jbe/bsw@0 | 40 -- Methode der Klasse, um sofort eine alphabetische Liste aller Produkte |
jbe/bsw@0 | 41 -- zu bekommen |
jbe/bsw@0 | 42 function Product:get_all_ordered_by_name() |
jbe/bsw@0 | 43 local selector = self:new_selector() |
jbe/bsw@0 | 44 selector:add_order_by('"name"') |
jbe/bsw@0 | 45 selector:add_order_by('"id"') |
jbe/bsw@0 | 46 return selector:exec() |
jbe/bsw@0 | 47 end |
jbe/bsw@0 | 48 |
jbe/bsw@0 | 49 function Product.object_get:name_length(key) |
jbe/bsw@0 | 50 local value = #self.name |
jbe/bsw@0 | 51 self._data.name_length = value |
jbe/bsw@0 | 52 return value |
jbe/bsw@0 | 53 end |
jbe/bsw@0 | 54 |
jbe/bsw@0 | 55 function Product.object_set:quality(value) |
jbe/bsw@0 | 56 if value == "perfect" or value == "good" or value == "trash" then |
jbe/bsw@0 | 57 self._data.quality = value |
jbe/bsw@0 | 58 else |
jbe/bsw@0 | 59 self._data.quality = nil |
jbe/bsw@0 | 60 end |
jbe/bsw@0 | 61 self._dirty.quality = true |
jbe/bsw@0 | 62 end |
jbe/bsw@0 | 63 |
jbe/bsw@0 | 64 -- Methode der Listen, um sie auszugeben |
jbe/bsw@0 | 65 function Product.list:print() |
jbe/bsw@0 | 66 for i, product in ipairs(self) do |
jbe/bsw@0 | 67 print(product.id, product.name, product.name_length) |
jbe/bsw@0 | 68 end |
jbe/bsw@0 | 69 end |
jbe/bsw@0 | 70 |
jbe/bsw@0 | 71 products = Product:get_all_ordered_by_name() |
jbe/bsw@0 | 72 products:print() |
jbe/bsw@0 | 73 products[1].quality = "perfect" |
jbe/bsw@0 | 74 print(products[1].quality) |
jbe/bsw@0 | 75 products[2].quality = "I don't know." |
jbe/bsw@0 | 76 print(products[2].quality) |
jbe/bsw@0 | 77 products[3].name = "Produkt Eins!" |
jbe/bsw@0 | 78 products[3].quality = "perfect" |
jbe/bsw@0 | 79 products[3]:save() |
jbe/bsw@0 | 80 |
jbe/bsw@0 | 81 sel = db:new_selector() |
jbe/bsw@0 | 82 sel:from('"product_variant"') |
jbe/bsw@0 | 83 sel:add_field("*") |
jbe/bsw@0 | 84 sel:attach("m1", products, "product_id", "id", "product", "product_variants") |
jbe/bsw@0 | 85 product_variants = sel:exec() |
jbe/bsw@0 | 86 --]] |