static int luaP_tupleindex (lua_State *L) { luaP_Tuple *t = (luaP_Tuple *) lua_touserdata(L, 1); const char *name = luaL_checkstring(L, 2); int i; lua_pushinteger(L, (int) t->relid); lua_rawget(L, LUA_REGISTRYINDEX); lua_getfield(L, -1, name); i = luaL_optinteger(L, -1, -1); if (i >= 0) { if (t->changed == -1) { /* read-only? */ bool isnull; Datum v = heap_getattr(t->tuple, t->desc->attrs[i]->attnum, t->desc, &isnull); if (!isnull) luaP_pushdatum(L, v, t->desc->attrs[i]->atttypid); else lua_pushnil(L); } else { if (!t->null[i]) luaP_pushdatum(L, t->value[i], t->desc->attrs[i]->atttypid); else lua_pushnil(L); } } else lua_pushnil(L); return 1; }
static int pg_callable_func(lua_State *L) { MemoryContext m; int i; FunctionCallInfoData fcinfo; Lua_pgfunc *fi; fi = (Lua_pgfunc *) lua_touserdata(L, lua_upvalueindex(1)); InitFunctionCallInfoData(fcinfo, &fi->fi, fi->numargs, InvalidOid, NULL, NULL); if(tmpcontext_usage> RESET_CONTEXT_AFTER ){ MemoryContextReset(tmpcontext); tmpcontext_usage = 0; } ++tmpcontext_usage; m = MemoryContextSwitchTo(tmpcontext); for (i=0; i<fi->numargs; ++i){ fcinfo.arg[i] = luaP_todatum(L, fi->argtypes[i], 0, &fcinfo.argnull[i], i+1); } if(!fi->options.only_internal && fi->options.throwable){ SPI_push(); PG_TRY(); { Datum d = FunctionCallInvoke(&fcinfo); MemoryContextSwitchTo(m); if (fcinfo.isnull) { lua_pushnil(L); } else { luaP_pushdatum(L, d, fi->prorettype); } SPI_pop(); } PG_CATCH(); { lua_pop(L, lua_gettop(L)); push_spi_error(L, m); /*context switch to m inside push_spi_error*/ SPI_pop(); return lua_error(L); }PG_END_TRY(); }else{ Datum d = FunctionCallInvoke(&fcinfo); MemoryContextSwitchTo(m); if (fcinfo.isnull) { lua_pushnil(L); } else { luaP_pushdatum(L, d, fi->prorettype); } } return 1; }
static int hs_delete(lua_State *L) { if (lua_gettop(L)!= 2){ return luaL_error(L, "wrong args length in hstore.delete(hstore, text)"); } if (hstore_oid >0){ bool isnull; Datum a; Datum b; if ((lua_type(L, 2) != LUA_TSTRING)|| (lua_type(L, 1) != LUA_TUSERDATA) ){ return luaL_error(L, "wrong args types for hstore.delete(hstore, text)"); } lua_getmetatable (L, 1); luaP_getfield(L, hstore_type_name); if (lua_equal (L, -1, -2)!= 1){ return luaL_error(L, "hstore.delete(hstore, text) first arg is not hstore"); } a = luaP_todatum(L, hstore_oid, 0, &isnull, 1); b = luaP_todatum(L, TEXTOID, 0, &isnull, 2); luaP_pushdatum(L, call_delete_hstore_text(a,b), hstore_oid); return 1; } return luaL_error(L, "hstore is not registered //hstore.delete"); }
static int pgfunc_rowsaux (lua_State *L) { ReturnSetInfo *rsinfo; FunctionCallInfoData *fcinfo; Datum d; Oid prorettype; Lua_pgfunc_srf *srfi; srfi = (Lua_pgfunc_srf *) lua_touserdata(L, lua_upvalueindex(1)); rsinfo = &srfi->rsinfo; fcinfo = &srfi->fcinfo; prorettype = srfi->prorettype; d = FunctionCallInvoke(fcinfo); if ((!fcinfo->isnull)&&(rsinfo->isDone != ExprEndResult)){ luaP_pushdatum(L, d, prorettype); return 1; } lua_pushnil(L); return 1; }