webmcp

annotate framework/env/param/update.lua @ 0:9fdfb27f8e67

Version 1.0.0
author jbe/bsw
date Sun Oct 25 12:00:00 2009 +0100 (2009-10-25)
parents
children
rev   line source
jbe/bsw@0 1 --[[--
jbe/bsw@0 2 param.update(
jbe/bsw@0 3 record, -- database record to be updated
jbe/bsw@0 4 key_and_field_name1, -- name of CGI parameter and record field
jbe/bsw@0 5 key_and_field_name2, -- another name of a CGI parameter and record field
jbe/bsw@0 6 {
jbe/bsw@0 7 key3, -- name of CGI parameter
jbe/bsw@0 8 field_name3 -- name of record field
jbe/bsw@0 9 }
jbe/bsw@0 10 )
jbe/bsw@0 11
jbe/bsw@0 12 This function can update several fields of a database record using GET/POST request parameters (or internal/exchanged parameters). The type of each parameter is automatically determined by the class of the record (_class:get_colums()[field].type).
jbe/bsw@0 13 --]]--
jbe/bsw@0 14
jbe/bsw@0 15 function param.update(record, mapping_info, ...)
jbe/bsw@0 16 if not mapping_info then
jbe/bsw@0 17 return
jbe/bsw@0 18 end
jbe/bsw@0 19 assert(record, "No record given for param.update(...).")
jbe/bsw@0 20 assert(record._class, "Record passed to param.update(...) has no _class attribute.")
jbe/bsw@0 21 local key, field_name
jbe/bsw@0 22 if type(mapping_info) == "string" then
jbe/bsw@0 23 key = mapping_info
jbe/bsw@0 24 field_name = mapping_info
jbe/bsw@0 25 else
jbe/bsw@0 26 key = mapping_info[1]
jbe/bsw@0 27 field_name = mapping_info[2]
jbe/bsw@0 28 end
jbe/bsw@0 29 assert(key, "No key given in parameter of param.update(...).")
jbe/bsw@0 30 assert(field_name, "No field name given in parameter of param.update(...).")
jbe/bsw@0 31 local column_info = record._class:get_columns()[field_name]
jbe/bsw@0 32 if not column_info then
jbe/bsw@0 33 error('Type of column "' .. field_name .. '" is unknown.')
jbe/bsw@0 34 end
jbe/bsw@0 35 local new_value = param.get(key, column_info.type)
jbe/bsw@0 36 if new_value ~= record[field_name] then
jbe/bsw@0 37 record[field_name] = new_value
jbe/bsw@0 38 end
jbe/bsw@0 39 return param.update(record, ...) -- recursivly process following arguments
jbe/bsw@0 40 end

Impressum / About Us