webmcp

diff libraries/mondelefant/example.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children 3d43a5cf17c1
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libraries/mondelefant/example.lua	Sun Oct 25 12:00:00 2009 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +#!/usr/bin/env lua
     1.5 +require("mondelefant")
     1.6 +
     1.7 +-- Standarddatenbankverbindung ist globale Variable 'db'
     1.8 +function mondelefant.class_prototype:get_db_conn() return db end
     1.9 +
    1.10 +-- Verbindung aufbauen
    1.11 +db = assert(mondelefant.connect{engine='postgresql', dbname='test'})
    1.12 +
    1.13 +Product = mondelefant.new_class{ table = "product" }
    1.14 +ProductVariant = mondelefant.new_class{ table = "product_variant" }
    1.15 +
    1.16 +Product:add_reference{
    1.17 +  mode     = "1m",
    1.18 +  to       = ProductVariant,
    1.19 +  this_key = "number",
    1.20 +  that_key = "product_number",
    1.21 +  ref      = "product_variants",
    1.22 +  back_ref = "product",
    1.23 +  --default_order = '"number"'
    1.24 +}
    1.25 +
    1.26 +ProductVariant:add_reference{
    1.27 +  mode     = "m1",
    1.28 +  to       = Product,
    1.29 +  this_key = "product_number",
    1.30 +  that_key = "number",
    1.31 +  ref      = "product",
    1.32 +  back_ref = nil,
    1.33 +  --default_order = '"id"'
    1.34 +}
    1.35 +
    1.36 +p = Product:new_selector():single_object_mode():add_where{"name=?", "Noodles"}:exec()
    1.37 +
    1.38 +
    1.39 +--[[
    1.40 +-- Neue Datenbankklasse definieren
    1.41 +Product = mondelefant.new_class{ table = '"product"' }
    1.42 +
    1.43 +-- Methode der Klasse, um sofort eine alphabetische Liste aller Produkte
    1.44 +-- zu bekommen
    1.45 +function Product:get_all_ordered_by_name()
    1.46 +  local selector = self:new_selector()
    1.47 +  selector:add_order_by('"name"')
    1.48 +  selector:add_order_by('"id"')
    1.49 +  return selector:exec()
    1.50 +end
    1.51 +
    1.52 +function Product.object_get:name_length(key)
    1.53 +  local value = #self.name
    1.54 +  self._data.name_length = value
    1.55 +  return value
    1.56 +end
    1.57 +
    1.58 +function Product.object_set:quality(value)
    1.59 +  if value == "perfect" or value == "good" or value == "trash" then
    1.60 +    self._data.quality = value
    1.61 +  else
    1.62 +    self._data.quality = nil
    1.63 +  end
    1.64 +  self._dirty.quality = true
    1.65 +end
    1.66 +
    1.67 +-- Methode der Listen, um sie auszugeben
    1.68 +function Product.list:print()
    1.69 +  for i, product in ipairs(self) do
    1.70 +    print(product.id, product.name, product.name_length)
    1.71 +  end
    1.72 +end
    1.73 +
    1.74 +products = Product:get_all_ordered_by_name()
    1.75 +products:print()
    1.76 +products[1].quality = "perfect"
    1.77 +print(products[1].quality)
    1.78 +products[2].quality = "I don't know."
    1.79 +print(products[2].quality)
    1.80 +products[3].name = "Produkt Eins!"
    1.81 +products[3].quality = "perfect"
    1.82 +products[3]:save()
    1.83 +
    1.84 +sel = db:new_selector()
    1.85 +sel:from('"product_variant"')
    1.86 +sel:add_field("*")
    1.87 +sel:attach("m1", products, "product_id", "id", "product", "product_variants")
    1.88 +product_variants = sel:exec()
    1.89 +--]]
    1.90 \ No newline at end of file

Impressum / About Us