Code

Initial import of PostRR -- the PostgreSQL Round-Robin Extension.
[postrr.git] / src / utils / pg_spi.c
1 /*
2  * PostRR - src/utils/pg_spi.c
3  * Copyright (C) 2012 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 /*
29  * Helpers for using the PostgreSQL Server Programming Interface (SPI).
30  */
32 #include "utils/pg_spi.h"
34 #include <postgres.h>
35 #include <executor/spi.h>
37 #include <stdarg.h>
39 /*
40  * public API
41  */
43 int
44 pg_spi_get_int(const char *query, int num_vals, ...)
45 {
46         int spi_rc;
47         int i;
49         va_list ap;
51         spi_rc = SPI_exec(query, /* max num rows = */ 1);
52         if (spi_rc != SPI_OK_SELECT) {
53                 errmsg("failed to execute query: %s",
54                         SPI_result_code_string(spi_rc));
55                 return PG_SPI_MAKE_RC(ERROR_EXEC_QUERY, spi_rc);
56         }
58         if (SPI_processed <= 0)
59                 return PG_SPI_MAKE_RC(ERROR_NO_VALUES, 0);
61         va_start(ap, num_vals);
62         /* column count starts at 1 */
63         for (i = 1; i <= num_vals; ++i) {
64                 char  *tmp;
65                 int32 *valp;
67                 valp = va_arg(ap, int32 *);
69                 tmp = SPI_getvalue(SPI_tuptable->vals[0],
70                                 SPI_tuptable->tupdesc, /* col = */ i);
72                 *valp = atoi(tmp);
73         }
74         va_end(ap);
75         return PG_SPI_OK;
76 } /* pg_spi_get_int */
78 int
79 pg_spi_ereport(int elevel, const char *context, int rc)
80 {
81         int status     = PG_SPI_GET_STATUS(rc);
82         int spi_status = PG_SPI_GET_SPI_STATUS(rc);
84         switch (status) {
85                 case PG_SPI_OK:
86                         return 0;
87                 case PG_SPI_ERROR_EXEC_QUERY:
88                         ereport(elevel, (
89                                                 errmsg("failed to %s: failed to execute query: %s",
90                                                         context, SPI_result_code_string(spi_status))
91                                         ));
92                 case PG_SPI_ERROR_NO_VALUES:
93                         ereport(elevel, (
94                                                 errmsg("failed to %s: "
95                                                         "query did not return any values",
96                                                         context)
97                                         ));
98         }
99         return 0;
100 } /* pg_spi_ereport */
102 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */