webmcp
diff libraries/mondelefant/mondelefant_native.c @ 436:1dbbe4c62f08
Methods :create_object() and :create_list() ignore any additional table argument; "_class" attribute is always set; "_col" proxy respects foreign keys
author | jbe |
---|---|
date | Wed Jan 20 20:43:23 2016 +0100 (2016-01-20) |
parents | 553a1a9dbc4c |
children | 66d7a0ac9c9d |
line diff
1.1 --- a/libraries/mondelefant/mondelefant_native.c Sat Jan 16 04:44:16 2016 +0100 1.2 +++ b/libraries/mondelefant/mondelefant_native.c Wed Jan 20 20:43:23 2016 +0100 1.3 @@ -534,14 +534,9 @@ 1.4 static int mondelefant_conn_create_list(lua_State *L) { 1.5 // ensure that first argument is a database connection: 1.6 luaL_checkudata(L, 1, MONDELEFANT_CONN_MT_REGKEY); 1.7 - // if no second argument is given, use an empty table: 1.8 - if (lua_isnoneornil(L, 2)) { 1.9 - lua_settop(L, 1); 1.10 - lua_newtable(L); // 2 1.11 - } else { 1.12 - luaL_checktype(L, 2, LUA_TTABLE); 1.13 - lua_settop(L, 2); 1.14 - } 1.15 + // create new table on stack position 2: 1.16 + lua_settop(L, 1); 1.17 + lua_newtable(L); // 2 1.18 // set meta-table for database result lists/objects: 1.19 luaL_setmetatable(L, MONDELEFANT_RESULT_MT_REGKEY); 1.20 // set "_connection" attribute to self: 1.21 @@ -550,6 +545,9 @@ 1.22 // set "_type" attribute to string "list": 1.23 lua_pushliteral(L, "list"); // 3 1.24 lua_setfield(L, 2, "_type"); 1.25 + // set "_class" attribute to default class: 1.26 + lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CLASS_PROTO_REGKEY); // 3 1.27 + lua_setfield(L, 2, "_class"); 1.28 // return created database result list: 1.29 return 1; 1.30 } 1.31 @@ -558,14 +556,9 @@ 1.32 static int mondelefant_conn_create_object(lua_State *L) { 1.33 // ensure that first argument is a database connection: 1.34 luaL_checkudata(L, 1, MONDELEFANT_CONN_MT_REGKEY); 1.35 - // if no second argument is given, use an empty table: 1.36 - if (lua_isnoneornil(L, 2)) { 1.37 - lua_settop(L, 1); 1.38 - lua_newtable(L); // 2 1.39 - } else { 1.40 - luaL_checktype(L, 2, LUA_TTABLE); 1.41 - lua_settop(L, 2); 1.42 - } 1.43 + // create new table on stack position 2: 1.44 + lua_settop(L, 1); 1.45 + lua_newtable(L); // 2 1.46 // set meta-table for database result lists/objects: 1.47 luaL_setmetatable(L, MONDELEFANT_RESULT_MT_REGKEY); 1.48 // set "_connection" attribute to self: 1.49 @@ -574,6 +567,9 @@ 1.50 // set "_type" attribute to string "object": 1.51 lua_pushliteral(L, "object"); // 3 1.52 lua_setfield(L, 2, "_type"); // "object" or "list" 1.53 + // set "_class" attribute to default class: 1.54 + lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CLASS_PROTO_REGKEY); // 3 1.55 + lua_setfield(L, 2, "_class"); 1.56 // create empty tables for "_data", "_dirty" and "_ref" attributes: 1.57 lua_newtable(L); // 3 1.58 lua_setfield(L, 2, "_data"); 1.59 @@ -1596,13 +1592,9 @@ 1.60 if (lua_type(L, 2) != LUA_TSTRING || lua_tostring(L, 2)[0] == '_') { 1.61 return 0; 1.62 } 1.63 - // value of "_class" attribute or default class on stack position 3: 1.64 + // class on stack position 3: 1.65 lua_settop(L, 2); 1.66 lua_getfield(L, 1, "_class"); // 3 1.67 - if (!lua_toboolean(L, 3)) { 1.68 - lua_settop(L, 2); 1.69 - lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CLASS_PROTO_REGKEY); // 3 1.70 - } 1.71 // get value of "_type" attribute: 1.72 lua_getfield(L, 1, "_type"); // 4 1.73 result_type = lua_tostring(L, 4); 1.74 @@ -1734,13 +1726,9 @@ 1.75 lua_rawset(L, 1); 1.76 return 1; 1.77 } 1.78 - // value of "_class" attribute or default class on stack position 4: 1.79 + // class on stack position 4: 1.80 lua_settop(L, 3); 1.81 lua_getfield(L, 1, "_class"); // 4 1.82 - if (!lua_toboolean(L, 4)) { 1.83 - lua_settop(L, 3); 1.84 - lua_getfield(L, LUA_REGISTRYINDEX, MONDELEFANT_CLASS_PROTO_REGKEY); // 4 1.85 - } 1.86 // get value of "_type" attribute: 1.87 lua_getfield(L, 1, "_type"); // 5 1.88 result_type = lua_tostring(L, 5); 1.89 @@ -1845,6 +1833,37 @@ 1.90 luaL_checktype(L, 1, LUA_TTABLE); 1.91 lua_settop(L, 2); 1.92 lua_rawgetp(L, 1, MONDELEFANT_COLUMNS_RESULT_LUKEY); // 3 1.93 + // try primary keys of referenced objects: 1.94 + lua_pushcfunction(L, 1.95 + mondelefant_class_get_foreign_key_reference_name 1.96 + ); // 4 1.97 + lua_getfield(L, 3, "_class"); // 5 1.98 + lua_pushvalue(L, 2); // 6 1.99 + lua_call(L, 2, 1); // 4 1.100 + if (!lua_isnil(L, 4)) { 1.101 + // reference name at stack position 4 1.102 + lua_pushcfunction(L, mondelefant_class_get_reference); // 5 1.103 + lua_getfield(L, 3, "_class"); // 6 1.104 + lua_pushvalue(L, 4); // 7 1.105 + lua_call(L, 2, 1); // reference info at stack position 5 1.106 + lua_getfield(L, 3, "_ref"); // 6 1.107 + lua_getfield(L, 5, "ref"); // 7 1.108 + lua_gettable(L, 6); // 7 1.109 + if (!lua_isnil(L, 7)) { 1.110 + if (lua_toboolean(L, 7)) { 1.111 + lua_getfield(L, 5, "that_key"); // 8 1.112 + if (lua_isnil(L, 8)) { 1.113 + return luaL_error(L, "Missing 'that_key' entry in model reference."); 1.114 + } 1.115 + lua_gettable(L, 7); // 8 1.116 + } else { 1.117 + lua_pushnil(L); 1.118 + } 1.119 + return 1; 1.120 + } 1.121 + } 1.122 + lua_settop(L, 3); 1.123 + // if not successful, use _data table: 1.124 lua_getfield(L, 3, "_data"); // 4 1.125 lua_pushvalue(L, 2); // 5 1.126 lua_gettable(L, 4); // 5 1.127 @@ -1856,6 +1875,21 @@ 1.128 luaL_checktype(L, 1, LUA_TTABLE); 1.129 lua_settop(L, 3); 1.130 lua_rawgetp(L, 1, MONDELEFANT_COLUMNS_RESULT_LUKEY); // 4 1.131 + // reset reference cache, if neccessary: 1.132 + lua_pushcfunction(L, 1.133 + mondelefant_class_get_foreign_key_reference_name 1.134 + ); // 5 1.135 + lua_getfield(L, 4, "_class"); // 6 1.136 + lua_pushvalue(L, 2); // 7 1.137 + lua_call(L, 2, 1); // 5 1.138 + if (!lua_isnil(L, 5)) { 1.139 + lua_getfield(L, 4, "_ref"); // 6 1.140 + lua_pushvalue(L, 5); // 7 1.141 + lua_pushnil(L); // 8 1.142 + lua_settable(L, 6); 1.143 + } 1.144 + lua_settop(L, 4); 1.145 + // set _data and _dirty: 1.146 lua_getfield(L, 4, "_data"); // 5 1.147 lua_getfield(L, 4, "_dirty"); // 6 1.148 lua_pushvalue(L, 2);