From 3fcf91f71b8a8a6ceb46b59b132ed8d7cc81eb63 Mon Sep 17 00:00:00 2001 From: eugwne Date: Mon, 2 Oct 2017 20:13:36 +0300 Subject: [PATCH] text pgtype to oid Patch-Source: https://github.com/pllua/pllua/commit/3fcf91f71b8a8a6ceb46b59b132ed8d7cc81eb63 diff --git a/expected/error_info.out b/expected/error_info.out index f29edb0..32be0fa 100644 --- a/expected/error_info.out +++ b/expected/error_info.out @@ -117,3 +117,10 @@ CONTEXT: stack traceback(trusted): [C]: in function 'error' [string "anonymous"]:2: in main chunk +do $$ +local status, err = subtransaction(function() local _ = fromstring('no_type_text','qwerty') end) +if (err) then + print(err) +end +$$ language pllua +INFO: type "no_type_text" does not exist diff --git a/expected/pgfunctest.out b/expected/pgfunctest.out index 81a225e..fc058c4 100644 --- a/expected/pgfunctest.out +++ b/expected/pgfunctest.out @@ -52,14 +52,12 @@ begin raise exception '%', $1; end $$ language plpgsql; +\set VERBOSITY 'terse' do $$ pgfunc('pg_temp.throw_error(text)',{only_internal=false})("exception test") $$ language pllua; ERROR: exception test -CONTEXT: -stack traceback(trusted): - [C]: ? - [string "anonymous"]:2: in main chunk +\set VERBOSITY 'default' do $$ local f = pgfunc('pg_temp.throw_error(text)',{only_internal=false}) print(pcall(f, "exception test")) diff --git a/pllua.c b/pllua.c index 4ef2f3b..2139eb3 100644 --- a/pllua.c +++ b/pllua.c @@ -150,7 +150,7 @@ void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { } #endif -int pg_to_regtype(char *typ_name) +Oid pg_to_regtype(const char *typ_name) { Oid result; @@ -161,14 +161,17 @@ int pg_to_regtype(char *typ_name) */ #if PG_VERSION_NUM < 90400 - parseTypeString(typ_name, &result, &typmod); + PG_TRY(); + { + parseTypeString(typ_name, &result, &typmod); + } + PG_CATCH(); + { + result = InvalidOid; + } + PG_END_TRY(); #else - parseTypeString(typ_name, &result, &typmod, true); + parseTypeString(typ_name, &result, &typmod, true); #endif - - - if (OidIsValid(result)) - return result; - else - return -1; + return result; } diff --git a/pllua.h b/pllua.h index 0a9fe99..d8d5df6 100644 --- a/pllua.h +++ b/pllua.h @@ -40,7 +40,6 @@ void luaP_pushtuple_trg (lua_State *L, TupleDesc desc, HeapTuple tuple, HeapTuple luaP_totuple (lua_State *L); HeapTuple luaP_casttuple (lua_State *L, TupleDesc tupdesc); /* SPI */ -Oid luaP_gettypeoid (const char *type_name); void luaP_pushdesctable(lua_State *L, TupleDesc desc); void luaP_registerspi(lua_State *L); void luaP_pushcursor (lua_State *L, Portal cursor); diff --git a/plluaapi.c b/plluaapi.c index d0a1b4d..adbe9e4 100644 --- a/plluaapi.c +++ b/plluaapi.c @@ -452,16 +452,22 @@ static int luaP_warning (lua_State *L) { #undef PLLUA_REPORT static int luaP_fromstring (lua_State *L) { - int oid = luaP_gettypeoid(luaL_checkstring(L, 1)); - const char *s = luaL_checkstring(L, 2); - luaP_Typeinfo *ti = luaP_gettypeinfo(L, oid); - int inoid = oid; - Datum v; - /* from getTypeIOParam in lsyscache.c */ - if (ti->type == TYPTYPE_BASE && OidIsValid(ti->elem)) inoid = ti->elem; - v = InputFunctionCall(&ti->input, (char *) s, inoid, 0); /* typmod = 0 */ - luaP_pushdatum(L, v, oid); - return 1; + const char *type_name = luaL_checkstring(L, 1); + Oid oid = pg_to_regtype(type_name); + if (oid == InvalidOid) { + return luaL_error(L,"type \"%s\" does not exist",type_name); + } else { + const char *s = luaL_checkstring(L, 2); + luaP_Typeinfo *ti = luaP_gettypeinfo(L, oid); + Oid inoid = oid; + Datum v; + + /* from getTypeIOParam in lsyscache.c */ + if (ti->type == TYPTYPE_BASE && OidIsValid(ti->elem)) inoid = ti->elem; + v = InputFunctionCall(&ti->input, (char *) s, inoid, 0); /* typmod = 0 */ + luaP_pushdatum(L, v, oid); + } + return 1; } #ifdef PLLUA_DEBUG diff --git a/plluacommon.h b/plluacommon.h index dc7a5fa..c2f9852 100644 --- a/plluacommon.h +++ b/plluacommon.h @@ -93,6 +93,6 @@ int pllua_getmaster_index(lua_State *L); ___m = MemoryContextSwitchTo(___mcxt) #define MTOPG MemoryContextSwitchTo(___m);} -int pg_to_regtype(char *typ_name); +Oid pg_to_regtype(const char *typ_name); #endif // PLLUACOMMON_H diff --git a/plluaspi.c b/plluaspi.c index c50d107..f87280f 100644 --- a/plluaspi.c +++ b/plluaspi.c @@ -958,21 +958,6 @@ static int luaP_rowsplan (lua_State *L) { /* ======= SPI ======= */ - -Oid luaP_gettypeoid (const char *type_name) { -#if PG_VERSION_NUM < 80300 - List *namelist = stringToQualifiedNameList(type_name, NULL); - HeapTuple typetup = typenameType(NULL, makeTypeNameFromNameList(namelist)); -#else - List *namelist = stringToQualifiedNameList(type_name); - HeapTuple typetup = typenameType(NULL, makeTypeNameFromNameList(namelist), NULL); -#endif - Oid typeoid = HeapTupleGetOid(typetup); - ReleaseSysCache(typetup); - list_free(namelist); - return typeoid; -} - static int luaP_prepare (lua_State *L) { int nargs, cursoropt; const char *q = luaL_checkstring(L, 1); @@ -995,7 +980,7 @@ static int luaP_prepare (lua_State *L) { int k = lua_tointeger(L, -2); if (k > 0) { const char *s = luaL_checkstring(L, -1); - Oid type = luaP_gettypeoid(s); + Oid type = pg_to_regtype(s); if (type == InvalidOid) return luaL_error(L, "invalid type to plan: %s", s); p->type[k - 1] = type; diff --git a/sql/error_info.sql b/sql/error_info.sql index 3a8ed85..aef7e7c 100644 --- a/sql/error_info.sql +++ b/sql/error_info.sql @@ -71,3 +71,10 @@ do $$ error() $$language pllua; +do $$ +local status, err = subtransaction(function() local _ = fromstring('no_type_text','qwerty') end) +if (err) then + print(err) +end +$$ language pllua + diff --git a/sql/pgfunctest.sql b/sql/pgfunctest.sql index 40c5350..65eaf33 100644 --- a/sql/pgfunctest.sql +++ b/sql/pgfunctest.sql @@ -47,9 +47,11 @@ raise exception '%', $1; end $$ language plpgsql; +\set VERBOSITY 'terse' do $$ pgfunc('pg_temp.throw_error(text)',{only_internal=false})("exception test") $$ language pllua; +\set VERBOSITY 'default' do $$ local f = pgfunc('pg_temp.throw_error(text)',{only_internal=false})