commit 3fcf91f71b8a8a6ceb46b59b132ed8d7cc81eb63
Author: eugwne <eugeney.sergeev@gmail.com>
Date:   Mon Oct 2 20:13:36 2017 +0300

    text pgtype to oid
    
    Update .travis.yml

diff --git a/.travis.yml b/.travis.yml
index bbf08fa..17474ea 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -4,7 +4,7 @@ before_install:
   - sudo apt-get -y --purge remove postgresql libpq-dev libpq5 postgresql-client-common postgresql-common
   - sudo rm -rf /var/lib/postgresql
   - wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
-  - sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdg main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list"
+  - sudo sh -c "echo deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main $PGVERSION >> /etc/apt/sources.list.d/postgresql.list"
   - sudo apt-get update -qq
   - sudo apt-get -y -o Dpkg::Options::=--force-confdef -o Dpkg::Options::="--force-confnew" install postgresql-$PGVERSION postgresql-server-dev-$PGVERSION
   - sudo chmod 777 /etc/postgresql/$PGVERSION/main/pg_hba.conf
@@ -45,6 +45,12 @@ env:
     - PGVERSION=9.5 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_INCDIR=/usr/include/lua5.1 LUALIB=-llua5.1
     - PGVERSION=9.5 LUA=lua5.3 LUA_DEV=liblua5.3-dev LUA_INCDIR=/usr/include/lua5.3 LUALIB=-llua5.3
     - PGVERSION=9.5 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_INCDIR=/usr/include/luajit-2.0 LUALIB=-lluajit-5.1
+    - PGVERSION=9.6 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_INCDIR=/usr/include/lua5.1 LUALIB=-llua5.1
+    - PGVERSION=9.6 LUA=lua5.3 LUA_DEV=liblua5.3-dev LUA_INCDIR=/usr/include/lua5.3 LUALIB=-llua5.3
+    - PGVERSION=9.6 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_INCDIR=/usr/include/luajit-2.0 LUALIB=-lluajit-5.1
+    - PGVERSION=10 LUA=lua5.1 LUA_DEV=liblua5.1-dev LUA_INCDIR=/usr/include/lua5.1 LUALIB=-llua5.1
+    - PGVERSION=10 LUA=lua5.3 LUA_DEV=liblua5.3-dev LUA_INCDIR=/usr/include/lua5.3 LUALIB=-llua5.3
+    - PGVERSION=10 LUA=luajit LUA_DEV=libluajit-5.1-dev LUA_INCDIR=/usr/include/luajit-2.0 LUALIB=-lluajit-5.1
 
 
 language: c
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})
