Skip to content

Commit 2f2fa14

Browse files
committed
Add pgut-spi files.
1 parent 5ee8240 commit 2f2fa14

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

lib/pgut/pgut-spi.c

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* pgut-spi.c
4+
*
5+
* Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
6+
*
7+
*-------------------------------------------------------------------------
8+
*/
9+
10+
#include "postgres.h"
11+
#include "pgut-spi.h"
12+
13+
static void
14+
termStringInfo(StringInfo str)
15+
{
16+
if (str && str->data)
17+
pfree(str->data);
18+
}
19+
20+
/* simple execute */
21+
void
22+
execute(int expected, const char *sql)
23+
{
24+
int ret = SPI_execute(sql, false, 0);
25+
if ((expected > 0 && ret != expected) || ret < 0)
26+
elog(ERROR, "query failed: (sql=%s, code=%d, expected=%d)", sql, ret, expected);
27+
}
28+
29+
/* execute prepared plan */
30+
void
31+
execute_plan(int expected, SPIPlanPtr plan, Datum *values, const char *nulls)
32+
{
33+
int ret = SPI_execute_plan(plan, values, nulls, false, 0);
34+
if ((expected > 0 && ret != expected) || ret < 0)
35+
elog(ERROR, "query failed: (code=%d, expected=%d)", ret, expected);
36+
}
37+
38+
/* execute sql with format */
39+
void
40+
execute_with_format(int expected, const char *format, ...)
41+
{
42+
va_list ap;
43+
StringInfoData sql;
44+
int ret;
45+
46+
initStringInfo(&sql);
47+
va_start(ap, format);
48+
appendStringInfoVA(&sql, format, ap);
49+
va_end(ap);
50+
51+
ret = SPI_exec(sql.data, 0);
52+
if ((expected > 0 && ret != expected) || ret < 0)
53+
elog(ERROR, "query failed: (sql=%s, code=%d, expected=%d)", sql.data, ret, expected);
54+
55+
termStringInfo(&sql);
56+
}
57+
58+
void
59+
execute_with_args(int expected, const char *src, int nargs, Oid argtypes[], Datum values[], const bool nulls[])
60+
{
61+
int ret;
62+
int i;
63+
char c_nulls[FUNC_MAX_ARGS];
64+
65+
for (i = 0; i < nargs; i++)
66+
c_nulls[i] = (nulls[i] ? 'n' : ' ');
67+
68+
ret = SPI_execute_with_args(src, nargs, argtypes, values, c_nulls, false, 0);
69+
if ((expected > 0 && ret != expected) || ret < 0)
70+
elog(ERROR, "query failed: (sql=%s, code=%d, expected=%d)", src, ret, expected);
71+
}
72+
73+
void
74+
execute_with_format_args(int expected, const char *format, int nargs, Oid argtypes[], Datum values[], const bool nulls[], ...)
75+
{
76+
va_list ap;
77+
StringInfoData sql;
78+
79+
initStringInfo(&sql);
80+
va_start(ap, nulls);
81+
appendStringInfoVA(&sql, format, ap);
82+
va_end(ap);
83+
84+
execute_with_args(expected, sql.data, nargs, argtypes, values, nulls);
85+
86+
termStringInfo(&sql);
87+
}
88+
89+
90+
#if PG_VERSION_NUM < 80400
91+
92+
int
93+
SPI_execute_with_args(const char *src,
94+
int nargs, Oid *argtypes,
95+
Datum *values, const char *nulls,
96+
bool read_only, long tcount)
97+
{
98+
SPIPlanPtr plan;
99+
int ret;
100+
101+
plan = SPI_prepare(src, nargs, argtypes);
102+
if (plan == NULL)
103+
return SPI_result;
104+
ret = SPI_execute_plan(plan, values, nulls, read_only, tcount);
105+
SPI_freeplan(plan);
106+
return ret;
107+
}
108+
109+
#endif

lib/pgut/pgut-spi.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* pgut-spi.h
4+
*
5+
* Copyright (c) 2009, NIPPON TELEGRAPH AND TELEPHONE CORPORATION
6+
*
7+
*-------------------------------------------------------------------------
8+
*/
9+
10+
#ifndef PGUT_SPI_H
11+
#define PGUT_SPI_H
12+
13+
#include "executor/spi.h"
14+
15+
extern void execute(int expected, const char *sql);
16+
extern void execute_plan(int expected, SPIPlanPtr plan, Datum *values, const char *nulls);
17+
extern void execute_with_format(int expected, const char *format, ...)
18+
__attribute__((format(printf, 2, 3)));
19+
extern void execute_with_args(int expected, const char *src, int nargs, Oid argtypes[], Datum values[], const bool nulls[]);
20+
extern void execute_with_format_args(int expected, const char *format, int nargs, Oid argtypes[], Datum values[], const bool nulls[], ...)
21+
__attribute__((format(printf, 2, 7)));
22+
23+
#if PG_VERSION_NUM < 80400
24+
25+
extern int SPI_execute_with_args(const char *src, int nargs, Oid *argtypes,
26+
Datum *values, const char *nulls, bool read_only, long tcount);
27+
28+
#endif
29+
30+
#endif /* PGUT_SPI_H */

0 commit comments

Comments
 (0)