webmcp
diff 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 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/framework/env/param/update.lua Sun Oct 25 12:00:00 2009 +0100 1.3 @@ -0,0 +1,40 @@ 1.4 +--[[-- 1.5 +param.update( 1.6 + record, -- database record to be updated 1.7 + key_and_field_name1, -- name of CGI parameter and record field 1.8 + key_and_field_name2, -- another name of a CGI parameter and record field 1.9 + { 1.10 + key3, -- name of CGI parameter 1.11 + field_name3 -- name of record field 1.12 + } 1.13 +) 1.14 + 1.15 +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). 1.16 +--]]-- 1.17 + 1.18 +function param.update(record, mapping_info, ...) 1.19 + if not mapping_info then 1.20 + return 1.21 + end 1.22 + assert(record, "No record given for param.update(...).") 1.23 + assert(record._class, "Record passed to param.update(...) has no _class attribute.") 1.24 + local key, field_name 1.25 + if type(mapping_info) == "string" then 1.26 + key = mapping_info 1.27 + field_name = mapping_info 1.28 + else 1.29 + key = mapping_info[1] 1.30 + field_name = mapping_info[2] 1.31 + end 1.32 + assert(key, "No key given in parameter of param.update(...).") 1.33 + assert(field_name, "No field name given in parameter of param.update(...).") 1.34 + local column_info = record._class:get_columns()[field_name] 1.35 + if not column_info then 1.36 + error('Type of column "' .. field_name .. '" is unknown.') 1.37 + end 1.38 + local new_value = param.get(key, column_info.type) 1.39 + if new_value ~= record[field_name] then 1.40 + record[field_name] = new_value 1.41 + end 1.42 + return param.update(record, ...) -- recursivly process following arguments 1.43 +end