aboutsummaryrefslogtreecommitdiffstats
path: root/main/pllua/fix-postgres10.patch
blob: c77c00f65cb979905fecb13b3983164b892fdfb7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
From 3fcf91f71b8a8a6ceb46b59b132ed8d7cc81eb63 Mon Sep 17 00:00:00 2001
From: eugwne <eugeney.sergeev@gmail.com>
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})