webmcp

diff libraries/mondelefant/mondelefant_native.c @ 376:4bcedf32b089

Allow direct (proxy) access to fields of JSON object in a special column (set in the model) (write access not implemented yet)
author jbe
date Sat Nov 14 17:07:03 2015 +0100 (2015-11-14)
parents fb98b17056e5
children ecafddf7ac93
line diff
     1.1 --- a/libraries/mondelefant/mondelefant_native.c	Sat Nov 14 15:44:53 2015 +0100
     1.2 +++ b/libraries/mondelefant/mondelefant_native.c	Sat Nov 14 17:07:03 2015 +0100
     1.3 @@ -1490,80 +1490,87 @@
     1.4    // different lookup for lists and objects:
     1.5    if (result_type && !strcmp(result_type, "object")) {  // object
     1.6      lua_settop(L, 3);
     1.7 -    // try inherited attributes, methods or getter functions:
     1.8 -    while (lua_toboolean(L, 3)) {
     1.9 -      lua_getfield(L, 3, "object");  // 4
    1.10 -      lua_pushvalue(L, 2);  // 5
    1.11 -      lua_gettable(L, 4);  // 5
    1.12 -      if (!lua_isnil(L, 5)) return 1;
    1.13 -      lua_settop(L, 3);
    1.14 -      lua_getfield(L, 3, "object_get");  // 4
    1.15 -      lua_pushvalue(L, 2);  // 5
    1.16 -      lua_gettable(L, 4);  // 5
    1.17 -      if (lua_toboolean(L, 5)) {
    1.18 -        lua_pushvalue(L, 1);  // 6
    1.19 -        lua_call(L, 1, 1);  // 5
    1.20 +    lua_pushnil(L);
    1.21 +    lua_insert(L, 3);  // optional document column name on stack position 3
    1.22 +    // try inherited attributes, methods or getter functions
    1.23 +    // (and determine optional document column, if existent):
    1.24 +    while (lua_toboolean(L, 4)) {  // class on stack position 4 (due to insert)
    1.25 +      lua_getfield(L, 4, "object");  // 5
    1.26 +      lua_pushvalue(L, 2);  // 6
    1.27 +      lua_gettable(L, 5);  // 6
    1.28 +      if (!lua_isnil(L, 6)) return 1;
    1.29 +      lua_settop(L, 4);
    1.30 +      lua_getfield(L, 4, "object_get");  // 5
    1.31 +      lua_pushvalue(L, 2);  // 6
    1.32 +      lua_gettable(L, 5);  // 6
    1.33 +      if (lua_toboolean(L, 6)) {
    1.34 +        lua_pushvalue(L, 1);  // 7
    1.35 +        lua_call(L, 1, 1);  // 6
    1.36          return 1;
    1.37        }
    1.38 -      lua_settop(L, 3);
    1.39 -      lua_pushliteral(L, "prototype");  // 4
    1.40 -      lua_rawget(L, 3);  // 4
    1.41 -      lua_replace(L, 3);
    1.42 +      lua_settop(L, 4);
    1.43 +      if (lua_isnil(L, 3)) {
    1.44 +        lua_getfield(L, 4, "document_column");  // 5
    1.45 +        lua_replace(L, 3);
    1.46 +      }
    1.47 +      lua_pushliteral(L, "prototype");  // 5
    1.48 +      lua_rawget(L, 4);  // 5
    1.49 +      lua_replace(L, 4);
    1.50      }
    1.51 -    lua_settop(L, 2);
    1.52 +    lua_settop(L, 3);
    1.53      // try primary keys of referenced objects:
    1.54      lua_pushcfunction(L,
    1.55        mondelefant_class_get_foreign_key_reference_name
    1.56 -    );  // 3
    1.57 -    lua_getfield(L, 1, "_class");  // 4
    1.58 -    lua_pushvalue(L, 2);  // 5
    1.59 -    lua_call(L, 2, 1);  // 3
    1.60 -    if (!lua_isnil(L, 3)) {
    1.61 -      // reference name at stack position 3
    1.62 -      lua_pushcfunction(L, mondelefant_class_get_reference);  // 4
    1.63 -      lua_getfield(L, 1, "_class");  // 5
    1.64 -      lua_pushvalue(L, 3);  // 6
    1.65 -      lua_call(L, 2, 1);  // reference info at stack position 4
    1.66 -      lua_getfield(L, 1, "_ref");  // 5
    1.67 -      lua_getfield(L, 4, "ref");  // 6
    1.68 -      lua_gettable(L, 5);  // 6
    1.69 -      if (!lua_isnil(L, 6)) {
    1.70 -        if (lua_toboolean(L, 6)) {
    1.71 -          lua_getfield(L, 4, "that_key");  // 7
    1.72 -          if (lua_isnil(L, 7)) {
    1.73 +    );  // 4
    1.74 +    lua_getfield(L, 1, "_class");  // 5
    1.75 +    lua_pushvalue(L, 2);  // 6
    1.76 +    lua_call(L, 2, 1);  // 4
    1.77 +    if (!lua_isnil(L, 4)) {
    1.78 +      // reference name at stack position 4
    1.79 +      lua_pushcfunction(L, mondelefant_class_get_reference);  // 5
    1.80 +      lua_getfield(L, 1, "_class");  // 6
    1.81 +      lua_pushvalue(L, 4);  // 7
    1.82 +      lua_call(L, 2, 1);  // reference info at stack position 5
    1.83 +      lua_getfield(L, 1, "_ref");  // 6
    1.84 +      lua_getfield(L, 4, "ref");  // 7
    1.85 +      lua_gettable(L, 6);  // 7
    1.86 +      if (!lua_isnil(L, 7)) {
    1.87 +        if (lua_toboolean(L, 7)) {
    1.88 +          lua_getfield(L, 5, "that_key");  // 8
    1.89 +          if (lua_isnil(L, 8)) {
    1.90              return luaL_error(L, "Missing 'that_key' entry in model reference.");
    1.91            }
    1.92 -          lua_gettable(L, 6);  // 7
    1.93 +          lua_gettable(L, 7);  // 8
    1.94          } else {
    1.95            lua_pushnil(L);
    1.96          }
    1.97          return 1;
    1.98        }
    1.99      }
   1.100 -    lua_settop(L, 2);
   1.101 +    lua_settop(L, 3);
   1.102      // try normal data field info:
   1.103 -    lua_getfield(L, 1, "_data");  // 3
   1.104 -    lua_pushvalue(L, 2);  // 4
   1.105 -    lua_gettable(L, 3);  // 4
   1.106 -    if (!lua_isnil(L, 4)) return 1;
   1.107 -    lua_settop(L, 2);
   1.108 +    lua_getfield(L, 1, "_data");  // 4
   1.109 +    lua_pushvalue(L, 2);  // 5
   1.110 +    lua_gettable(L, 4);  // 5
   1.111 +    if (!lua_isnil(L, 5)) return 1;
   1.112 +    lua_settop(L, 3);  // TODO: keep _data table on stack
   1.113      // try cached referenced object (or cached NULL reference):
   1.114 -    lua_getfield(L, 1, "_ref");  // 3
   1.115 -    lua_pushvalue(L, 2);  // 4
   1.116 -    lua_gettable(L, 3);  // 4
   1.117 -    if (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) {
   1.118 +    lua_getfield(L, 1, "_ref");  // 4
   1.119 +    lua_pushvalue(L, 2);  // 5
   1.120 +    lua_gettable(L, 4);  // 5
   1.121 +    if (lua_isboolean(L, 5) && !lua_toboolean(L, 5)) {
   1.122        lua_pushnil(L);
   1.123        return 1;
   1.124 -    } else if (!lua_isnil(L, 4)) {
   1.125 +    } else if (!lua_isnil(L, 5)) {
   1.126        return 1;
   1.127      }
   1.128 -    lua_settop(L, 2);
   1.129 +    lua_settop(L, 3);
   1.130      // try to load a referenced object:
   1.131 -    lua_pushcfunction(L, mondelefant_class_get_reference);  // 3
   1.132 -    lua_getfield(L, 1, "_class");  // 4
   1.133 -    lua_pushvalue(L, 2);  // 5
   1.134 -    lua_call(L, 2, 1);  // 3
   1.135 -    if (!lua_isnil(L, 3)) {
   1.136 +    lua_pushcfunction(L, mondelefant_class_get_reference);  // 4
   1.137 +    lua_getfield(L, 1, "_class");  // 5
   1.138 +    lua_pushvalue(L, 2);  // 6
   1.139 +    lua_call(L, 2, 1);  // 4
   1.140 +    if (!lua_isnil(L, 4)) {
   1.141        lua_settop(L, 2);
   1.142        lua_getfield(L, 1, "load");  // 3
   1.143        lua_pushvalue(L, 1);  // 4 (self)
   1.144 @@ -1576,6 +1583,18 @@
   1.145        if (lua_isboolean(L, 4) && !lua_toboolean(L, 4)) lua_pushnil(L);  // TODO: use special object instead of false
   1.146        return 1;
   1.147      }
   1.148 +    lua_settop(L, 3);
   1.149 +    // try proxy access to document in special column:
   1.150 +    if (lua_toboolean(L, 3)) {
   1.151 +      lua_getfield(L, 1, "_data");  // 4  TODO: keep on stack from earlier
   1.152 +      lua_insert(L, 3);  // column name to stack position 4
   1.153 +      lua_gettable(L, 3);  // 4
   1.154 +      if (!lua_isnil(L, 4)) {
   1.155 +        lua_pushvalue(L, 2);  // 5
   1.156 +        lua_gettable(L, 4);  // 5
   1.157 +        if (!lua_isnil(L, 5)) return 1;
   1.158 +      }
   1.159 +    }
   1.160      return 0;
   1.161    } else if (result_type && !strcmp(result_type, "list")) {  // list
   1.162      lua_settop(L, 3);

Impressum / About Us