webmcp

diff libraries/mondelefant/mondelefant_native.c @ 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
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_native.c	Sat Jan 09 17:32:30 2016 +0100
     1.2 +++ b/libraries/mondelefant/mondelefant_native.c	Sat Jan 09 19:29:36 2016 +0100
     1.3 @@ -23,6 +23,14 @@
     1.4  #define MONDELEFANT_CLASS_MT_REGKEY (MONDELEFANT_REGKEY "class")
     1.5  // registry key of default prototype for models/classes:
     1.6  #define MONDELEFANT_CLASS_PROTO_REGKEY (MONDELEFANT_REGKEY "class_proto")
     1.7 +// registry key of meta-table for column proxy:
     1.8 +#define MONDELEFANT_COLUMNS_MT_REGKEY (MONDELEFANT_REGKEY "columns")
     1.9 +// table (lightuserdata) key to store result object in column proxy:
    1.10 +// (address of struct used as unique reference)
    1.11 +#define MONDELEFANT_COLUMNS_RESULT_LUKEY ((void *)&mondelefant_columns_result_lukey_dummy)
    1.12 +
    1.13 +// dummy variable for MONDELEFANT_COLUMNS_RESULT_LUKEY reference:
    1.14 +char mondelefant_columns_result_lukey_dummy;
    1.15  
    1.16  // C-structure for database connection userdata:
    1.17  typedef struct {
    1.18 @@ -573,7 +581,13 @@
    1.19    lua_setfield(L, 2, "_dirty");
    1.20    lua_newtable(L);  // 3
    1.21    lua_setfield(L, 2, "_ref");  // nil=no info, false=nil, else table
    1.22 -  // return created database result object:
    1.23 +  // create column proxy (field "columns" in result object):
    1.24 +  lua_newtable(L);  // 3
    1.25 +  luaL_setmetatable(L, MONDELEFANT_COLUMNS_MT_REGKEY);
    1.26 +  lua_pushvalue(L, 2);  // 4
    1.27 +  lua_rawsetp(L, 3, MONDELEFANT_COLUMNS_RESULT_LUKEY);
    1.28 +  lua_setfield(L, 2, "columns");
    1.29 +  // return created database result object (from stack position 2):
    1.30    return 1;
    1.31  }
    1.32  
    1.33 @@ -1825,6 +1839,33 @@
    1.34    }
    1.35  }
    1.36  
    1.37 +// meta-method "__index" of column proxy:
    1.38 +static int mondelefant_columns_index(lua_State *L) {
    1.39 +  luaL_checktype(L, 1, LUA_TTABLE);
    1.40 +  lua_settop(L, 2);
    1.41 +  lua_rawgetp(L, 1, MONDELEFANT_COLUMNS_RESULT_LUKEY);  // 3
    1.42 +  lua_getfield(L, 3, "_data");  // 4
    1.43 +  lua_pushvalue(L, 2);  // 5
    1.44 +  lua_gettable(L, 4);  // 5
    1.45 +  return 1;
    1.46 +}
    1.47 +
    1.48 +// meta-method "__newindex" of column proxy:
    1.49 +static int mondelefant_columns_newindex(lua_State *L) {
    1.50 +  luaL_checktype(L, 1, LUA_TTABLE);
    1.51 +  lua_settop(L, 3);
    1.52 +  lua_rawgetp(L, 1, MONDELEFANT_COLUMNS_RESULT_LUKEY);  // 4
    1.53 +  lua_getfield(L, 4, "_data");  // 5
    1.54 +  lua_getfield(L, 4, "_dirty");  // 6
    1.55 +  lua_pushvalue(L, 2);
    1.56 +  lua_pushvalue(L, 3);
    1.57 +  lua_settable(L, 5);
    1.58 +  lua_pushvalue(L, 2);
    1.59 +  lua_pushboolean(L, 1);
    1.60 +  lua_settable(L, 6);
    1.61 +  return 0;
    1.62 +}
    1.63 +
    1.64  // meta-method "__index" of classes (models):
    1.65  static int mondelefant_class_index(lua_State *L) {
    1.66    // perform lookup in prototype:
    1.67 @@ -1888,7 +1929,7 @@
    1.68    {NULL, NULL}
    1.69  };
    1.70  
    1.71 -// registration information for methods of database result lists/objects:
    1.72 +// registration information for meta-methods of classes (models):
    1.73  static const struct luaL_Reg mondelefant_class_mt_functions[] = {
    1.74    {"__index", mondelefant_class_index},
    1.75    {NULL, NULL}
    1.76 @@ -1913,9 +1954,21 @@
    1.77    {NULL, NULL}
    1.78  };
    1.79  
    1.80 +// registration information for meta-methods of column proxy:
    1.81 +static const struct luaL_Reg mondelefant_columns_mt_functions[] = {
    1.82 +  {"__index", mondelefant_columns_index},
    1.83 +  {"__newindex", mondelefant_columns_newindex},
    1.84 +  {NULL, NULL}
    1.85 +};
    1.86 +
    1.87  // luaopen function to initialize/register library:
    1.88  int luaopen_mondelefant_native(lua_State *L) {
    1.89    lua_settop(L, 0);
    1.90 +
    1.91 +  lua_newtable(L);  // meta-table for columns proxy
    1.92 +  luaL_setfuncs(L, mondelefant_columns_mt_functions, 0);
    1.93 +  lua_setfield(L, LUA_REGISTRYINDEX, MONDELEFANT_COLUMNS_MT_REGKEY);
    1.94 +
    1.95    lua_newtable(L);  // module at stack position 1
    1.96    luaL_setfuncs(L, mondelefant_module_functions, 0);
    1.97  

Impressum / About Us