webmcp

changeset 416:046927075270

Proxy table to directly access column-values of a database row (e.g. if document_column is set or for reserved method names)
author jbe
date Sat Jan 09 19:29:36 2016 +0100 (2016-01-09)
parents 0dff5e2f659c
children 03f4f905a41a
files libraries/mondelefant/mondelefant_native.autodoc.lua libraries/mondelefant/mondelefant_native.c
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_native.autodoc.lua	Sat Jan 09 17:32:30 2016 +0100
     1.2 +++ b/libraries/mondelefant/mondelefant_native.autodoc.lua	Sat Jan 09 19:29:36 2016 +0100
     1.3 @@ -331,3 +331,15 @@
     1.4  -- static int mondelefant_class_get_reference(lua_State *L)
     1.5  --//--
     1.6  
     1.7 +
     1.8 +--[[--
     1.9 +<db_object>.columns  -- proxy table mapping column names to their values
    1.10 +
    1.11 +This attribute can be used to access column values directly (helpful if <db_class>:document_column is set). Currently pairs(...) and ipairs(...) are not implemented on this proxy table.
    1.12 +
    1.13 +--]]--
    1.14 +-- implemented in mondelefant_native.c as
    1.15 +-- static const struct luaL_Reg mondelefant_columns_mt_functions[]
    1.16 +--
    1.17 +--//--
    1.18 +
     2.1 --- a/libraries/mondelefant/mondelefant_native.c	Sat Jan 09 17:32:30 2016 +0100
     2.2 +++ b/libraries/mondelefant/mondelefant_native.c	Sat Jan 09 19:29:36 2016 +0100
     2.3 @@ -23,6 +23,14 @@
     2.4  #define MONDELEFANT_CLASS_MT_REGKEY (MONDELEFANT_REGKEY "class")
     2.5  // registry key of default prototype for models/classes:
     2.6  #define MONDELEFANT_CLASS_PROTO_REGKEY (MONDELEFANT_REGKEY "class_proto")
     2.7 +// registry key of meta-table for column proxy:
     2.8 +#define MONDELEFANT_COLUMNS_MT_REGKEY (MONDELEFANT_REGKEY "columns")
     2.9 +// table (lightuserdata) key to store result object in column proxy:
    2.10 +// (address of struct used as unique reference)
    2.11 +#define MONDELEFANT_COLUMNS_RESULT_LUKEY ((void *)&mondelefant_columns_result_lukey_dummy)
    2.12 +
    2.13 +// dummy variable for MONDELEFANT_COLUMNS_RESULT_LUKEY reference:
    2.14 +char mondelefant_columns_result_lukey_dummy;
    2.15  
    2.16  // C-structure for database connection userdata:
    2.17  typedef struct {
    2.18 @@ -573,7 +581,13 @@
    2.19    lua_setfield(L, 2, "_dirty");
    2.20    lua_newtable(L);  // 3
    2.21    lua_setfield(L, 2, "_ref");  // nil=no info, false=nil, else table
    2.22 -  // return created database result object:
    2.23 +  // create column proxy (field "columns" in result object):
    2.24 +  lua_newtable(L);  // 3
    2.25 +  luaL_setmetatable(L, MONDELEFANT_COLUMNS_MT_REGKEY);
    2.26 +  lua_pushvalue(L, 2);  // 4
    2.27 +  lua_rawsetp(L, 3, MONDELEFANT_COLUMNS_RESULT_LUKEY);
    2.28 +  lua_setfield(L, 2, "columns");
    2.29 +  // return created database result object (from stack position 2):
    2.30    return 1;
    2.31  }
    2.32  
    2.33 @@ -1825,6 +1839,33 @@
    2.34    }
    2.35  }
    2.36  
    2.37 +// meta-method "__index" of column proxy:
    2.38 +static int mondelefant_columns_index(lua_State *L) {
    2.39 +  luaL_checktype(L, 1, LUA_TTABLE);
    2.40 +  lua_settop(L, 2);
    2.41 +  lua_rawgetp(L, 1, MONDELEFANT_COLUMNS_RESULT_LUKEY);  // 3
    2.42 +  lua_getfield(L, 3, "_data");  // 4
    2.43 +  lua_pushvalue(L, 2);  // 5
    2.44 +  lua_gettable(L, 4);  // 5
    2.45 +  return 1;
    2.46 +}
    2.47 +
    2.48 +// meta-method "__newindex" of column proxy:
    2.49 +static int mondelefant_columns_newindex(lua_State *L) {
    2.50 +  luaL_checktype(L, 1, LUA_TTABLE);
    2.51 +  lua_settop(L, 3);
    2.52 +  lua_rawgetp(L, 1, MONDELEFANT_COLUMNS_RESULT_LUKEY);  // 4
    2.53 +  lua_getfield(L, 4, "_data");  // 5
    2.54 +  lua_getfield(L, 4, "_dirty");  // 6
    2.55 +  lua_pushvalue(L, 2);
    2.56 +  lua_pushvalue(L, 3);
    2.57 +  lua_settable(L, 5);
    2.58 +  lua_pushvalue(L, 2);
    2.59 +  lua_pushboolean(L, 1);
    2.60 +  lua_settable(L, 6);
    2.61 +  return 0;
    2.62 +}
    2.63 +
    2.64  // meta-method "__index" of classes (models):
    2.65  static int mondelefant_class_index(lua_State *L) {
    2.66    // perform lookup in prototype:
    2.67 @@ -1888,7 +1929,7 @@
    2.68    {NULL, NULL}
    2.69  };
    2.70  
    2.71 -// registration information for methods of database result lists/objects:
    2.72 +// registration information for meta-methods of classes (models):
    2.73  static const struct luaL_Reg mondelefant_class_mt_functions[] = {
    2.74    {"__index", mondelefant_class_index},
    2.75    {NULL, NULL}
    2.76 @@ -1913,9 +1954,21 @@
    2.77    {NULL, NULL}
    2.78  };
    2.79  
    2.80 +// registration information for meta-methods of column proxy:
    2.81 +static const struct luaL_Reg mondelefant_columns_mt_functions[] = {
    2.82 +  {"__index", mondelefant_columns_index},
    2.83 +  {"__newindex", mondelefant_columns_newindex},
    2.84 +  {NULL, NULL}
    2.85 +};
    2.86 +
    2.87  // luaopen function to initialize/register library:
    2.88  int luaopen_mondelefant_native(lua_State *L) {
    2.89    lua_settop(L, 0);
    2.90 +
    2.91 +  lua_newtable(L);  // meta-table for columns proxy
    2.92 +  luaL_setfuncs(L, mondelefant_columns_mt_functions, 0);
    2.93 +  lua_setfield(L, LUA_REGISTRYINDEX, MONDELEFANT_COLUMNS_MT_REGKEY);
    2.94 +
    2.95    lua_newtable(L);  // module at stack position 1
    2.96    luaL_setfuncs(L, mondelefant_module_functions, 0);
    2.97  

Impressum / About Us