diff options
Diffstat (limited to 'src/pq/db_pq.h')
-rw-r--r-- | src/pq/db_pq.h | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/src/pq/db_pq.h b/src/pq/db_pq.h new file mode 100644 index 000000000..6e2b2b2c0 --- /dev/null +++ b/src/pq/db_pq.h | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | This file is part of TALER | ||
3 | (C) 2014 Christian Grothoff (and other contributing authors) | ||
4 | |||
5 | TALER is free software; you can redistribute it and/or modify it under the | ||
6 | terms of the GNU General Public License as published by the Free Software | ||
7 | Foundation; either version 3, or (at your option) any later version. | ||
8 | |||
9 | TALER is distributed in the hope that it will be useful, but WITHOUT ANY | ||
10 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR | ||
11 | A PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
12 | |||
13 | You should have received a copy of the GNU General Public License along with | ||
14 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> | ||
15 | */ | ||
16 | /** | ||
17 | * @file include/taler_db_lib.h | ||
18 | * @brief helper functions for DB interactions | ||
19 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | ||
20 | * @author Florian Dold | ||
21 | * @author Christian Grothoff | ||
22 | */ | ||
23 | |||
24 | #ifndef TALER_DB_LIB_H_ | ||
25 | #define TALER_DB_LIB_H_ | ||
26 | |||
27 | #include <libpq-fe.h> | ||
28 | #include "taler_util.h" | ||
29 | |||
30 | /** | ||
31 | * Description of a DB query parameter. | ||
32 | */ | ||
33 | struct TALER_DB_QueryParam | ||
34 | { | ||
35 | /** | ||
36 | * Data or NULL | ||
37 | */ | ||
38 | const void *data; | ||
39 | |||
40 | /** | ||
41 | * Size of @e data | ||
42 | */ | ||
43 | size_t size; | ||
44 | |||
45 | /** | ||
46 | * Non-null if this is not the last parameter. | ||
47 | * This allows for null as sentinal value. | ||
48 | */ | ||
49 | int more; | ||
50 | }; | ||
51 | |||
52 | /** | ||
53 | * End of query parameter specification. | ||
54 | */ | ||
55 | #define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 } | ||
56 | |||
57 | /** | ||
58 | * Generate fixed-size query parameter with size given explicitly. | ||
59 | * | ||
60 | * @param x pointer to the query parameter to pass | ||
61 | * @param s number of bytes of @a x to use for the query | ||
62 | */ | ||
63 | #define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 } | ||
64 | |||
65 | /** | ||
66 | * Generate fixed-size query parameter with size determined | ||
67 | * by variable type. | ||
68 | * | ||
69 | * @param x pointer to the query parameter to pass. | ||
70 | */ | ||
71 | #define TALER_DB_QUERY_PARAM_PTR(x) TALER_DB_QUERY_PARAM_PTR_SIZED(x, sizeof (*(x))) | ||
72 | |||
73 | |||
74 | /** | ||
75 | * Description of a DB result cell. | ||
76 | */ | ||
77 | struct TALER_DB_ResultSpec | ||
78 | { | ||
79 | /** | ||
80 | * Destination for the data. | ||
81 | */ | ||
82 | void *dst; | ||
83 | |||
84 | /** | ||
85 | * Allowed size for the data, 0 for variable-size | ||
86 | * (in this case, the type of @e dst is a `void **` | ||
87 | * and we need to allocate a buffer of the right size). | ||
88 | */ | ||
89 | size_t dst_size; | ||
90 | |||
91 | /** | ||
92 | * Field name of the desired result. | ||
93 | */ | ||
94 | char *fname; | ||
95 | |||
96 | /** | ||
97 | * Actual size of the result. | ||
98 | */ | ||
99 | size_t *result_size; | ||
100 | |||
101 | }; | ||
102 | |||
103 | |||
104 | /** | ||
105 | * End of result parameter specification. | ||
106 | */ | ||
107 | #define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL, NULL } | ||
108 | |||
109 | /** | ||
110 | * We expect a fixed-size result, with size given explicitly | ||
111 | * | ||
112 | * @param name name of the field in the table | ||
113 | * @param dst point to where to store the result | ||
114 | * @param s number of bytes we should use in @a dst | ||
115 | */ | ||
116 | #define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name), NULL } | ||
117 | |||
118 | /** | ||
119 | * We expect a fixed-size result, with size determined by the type of `* dst` | ||
120 | * | ||
121 | * @param name name of the field in the table | ||
122 | * @param dst point to where to store the result, type fits expected result size | ||
123 | */ | ||
124 | #define TALER_DB_RESULT_SPEC(name, dst) TALER_DB_RESULT_SPEC_SIZED(name, dst, sizeof (*(dst))) | ||
125 | |||
126 | /** | ||
127 | * Variable-size result expected. | ||
128 | * | ||
129 | * @param name name of the field in the table | ||
130 | * @param dst where to store the result (of type void **), to be allocated | ||
131 | * @param sptr pointer to a `size_t` for where to store the size of @a dst | ||
132 | */ | ||
133 | #define TALER_DB_RESULT_SPEC_VAR(name, dst, sptr) { (void *) (dst), 0, (name), sptr } | ||
134 | |||
135 | |||
136 | /** | ||
137 | * Execute a prepared statement. | ||
138 | */ | ||
139 | PGresult * | ||
140 | TALER_DB_exec_prepared (PGconn *db_conn, | ||
141 | const char *name, | ||
142 | const struct TALER_DB_QueryParam *params); | ||
143 | |||
144 | |||
145 | /** | ||
146 | * Extract results from a query result according to the given specification. | ||
147 | * If colums are NULL, the destination is not modified, and GNUNET_NO | ||
148 | * is returned. | ||
149 | * | ||
150 | * @return | ||
151 | * #GNUNET_YES if all results could be extracted | ||
152 | * #GNUNET_NO if at least one result was NULL | ||
153 | * #GNUNET_SYSERR if a result was invalid (non-existing field) | ||
154 | */ | ||
155 | int | ||
156 | TALER_DB_extract_result (PGresult *result, | ||
157 | struct TALER_DB_ResultSpec *rs, | ||
158 | int row); | ||
159 | |||
160 | |||
161 | int | ||
162 | TALER_DB_field_isnull (PGresult *result, | ||
163 | int row, | ||
164 | const char *fname); | ||
165 | |||
166 | |||
167 | int | ||
168 | TALER_DB_extract_amount_nbo (PGresult *result, | ||
169 | int row, | ||
170 | const char *val_name, | ||
171 | const char *frac_name, | ||
172 | const char *curr_name, | ||
173 | struct TALER_AmountNBO *r_amount_nbo); | ||
174 | |||
175 | |||
176 | int | ||
177 | TALER_DB_extract_amount (PGresult *result, | ||
178 | int row, | ||
179 | const char *val_name, | ||
180 | const char *frac_name, | ||
181 | const char *curr_name, | ||
182 | struct TALER_Amount *r_amount); | ||
183 | |||
184 | #endif /* TALER_DB_LIB_H_ */ | ||
185 | |||
186 | /* end of include/taler_db_lib.h */ | ||