webmcp
view 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 |
line source
1 #!/usr/bin/env lua
2 require("mondelefant")
4 -- Standarddatenbankverbindung ist globale Variable 'db'
5 function mondelefant.class_prototype:get_db_conn() return db end
7 -- Verbindung aufbauen
8 db = assert(mondelefant.connect{engine='postgresql', dbname='test'})
10 Product = mondelefant.new_class{ table = "product" }
11 ProductVariant = mondelefant.new_class{ table = "product_variant" }
13 Product:add_reference{
14 mode = "1m",
15 to = ProductVariant,
16 this_key = "number",
17 that_key = "product_number",
18 ref = "product_variants",
19 back_ref = "product",
20 --default_order = '"number"'
21 }
23 ProductVariant:add_reference{
24 mode = "m1",
25 to = Product,
26 this_key = "product_number",
27 that_key = "number",
28 ref = "product",
29 back_ref = nil,
30 --default_order = '"id"'
31 }
33 p = Product:new_selector():single_object_mode():add_where{"name=?", "Noodles"}:exec()
36 --[[
37 -- Neue Datenbankklasse definieren
38 Product = mondelefant.new_class{ table = '"product"' }
40 -- Methode der Klasse, um sofort eine alphabetische Liste aller Produkte
41 -- zu bekommen
42 function Product:get_all_ordered_by_name()
43 local selector = self:new_selector()
44 selector:add_order_by('"name"')
45 selector:add_order_by('"id"')
46 return selector:exec()
47 end
49 function Product.object_get:name_length(key)
50 local value = #self.name
51 self._data.name_length = value
52 return value
53 end
55 function Product.object_set:quality(value)
56 if value == "perfect" or value == "good" or value == "trash" then
57 self._data.quality = value
58 else
59 self._data.quality = nil
60 end
61 self._dirty.quality = true
62 end
64 -- Methode der Listen, um sie auszugeben
65 function Product.list:print()
66 for i, product in ipairs(self) do
67 print(product.id, product.name, product.name_length)
68 end
69 end
71 products = Product:get_all_ordered_by_name()
72 products:print()
73 products[1].quality = "perfect"
74 print(products[1].quality)
75 products[2].quality = "I don't know."
76 print(products[2].quality)
77 products[3].name = "Produkt Eins!"
78 products[3].quality = "perfect"
79 products[3]:save()
81 sel = db:new_selector()
82 sel:from('"product_variant"')
83 sel:add_field("*")
84 sel:attach("m1", products, "product_id", "id", "product", "product_variants")
85 product_variants = sel:exec()
86 --]]