diff options
Diffstat (limited to 'src/include/taler_db_lib.h')
-rw-r--r-- | src/include/taler_db_lib.h | 90 |
1 files changed, 72 insertions, 18 deletions
diff --git a/src/include/taler_db_lib.h b/src/include/taler_db_lib.h index 41b46264e..6e2b2b2c0 100644 --- a/src/include/taler_db_lib.h +++ b/src/include/taler_db_lib.h | |||
@@ -13,13 +13,12 @@ | |||
13 | You should have received a copy of the GNU General Public License along with | 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/> | 14 | TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> |
15 | */ | 15 | */ |
16 | |||
17 | |||
18 | /** | 16 | /** |
19 | * @file include/taler_db_lib.h | 17 | * @file include/taler_db_lib.h |
20 | * @brief helper functions for DB interactions | 18 | * @brief helper functions for DB interactions |
21 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> | 19 | * @author Sree Harsha Totakura <sreeharsha@totakura.in> |
22 | * @author Florian Dold | 20 | * @author Florian Dold |
21 | * @author Christian Grothoff | ||
23 | */ | 22 | */ |
24 | 23 | ||
25 | #ifndef TALER_DB_LIB_H_ | 24 | #ifndef TALER_DB_LIB_H_ |
@@ -28,16 +27,6 @@ | |||
28 | #include <libpq-fe.h> | 27 | #include <libpq-fe.h> |
29 | #include "taler_util.h" | 28 | #include "taler_util.h" |
30 | 29 | ||
31 | #define TALER_DB_QUERY_PARAM_END { NULL, 0, 0 } | ||
32 | #define TALER_DB_QUERY_PARAM_PTR(x) { (x), sizeof (*(x)), 1 } | ||
33 | #define TALER_DB_QUERY_PARAM_PTR_SIZED(x, s) { (x), (s), 1 } | ||
34 | |||
35 | |||
36 | #define TALER_DB_RESULT_SPEC_END { NULL, 0, NULL } | ||
37 | #define TALER_DB_RESULT_SPEC(name, dst) { (void *) (dst), sizeof (*(dst)), (name) } | ||
38 | #define TALER_DB_RESULT_SPEC_SIZED(name, dst, s) { (void *) (dst), (s), (name) } | ||
39 | |||
40 | |||
41 | /** | 30 | /** |
42 | * Description of a DB query parameter. | 31 | * Description of a DB query parameter. |
43 | */ | 32 | */ |
@@ -47,10 +36,12 @@ struct TALER_DB_QueryParam | |||
47 | * Data or NULL | 36 | * Data or NULL |
48 | */ | 37 | */ |
49 | const void *data; | 38 | const void *data; |
39 | |||
50 | /** | 40 | /** |
51 | * Size of 'data' | 41 | * Size of @e data |
52 | */ | 42 | */ |
53 | size_t size; | 43 | size_t size; |
44 | |||
54 | /** | 45 | /** |
55 | * Non-null if this is not the last parameter. | 46 | * Non-null if this is not the last parameter. |
56 | * This allows for null as sentinal value. | 47 | * This allows for null as sentinal value. |
@@ -58,6 +49,27 @@ struct TALER_DB_QueryParam | |||
58 | int more; | 49 | int more; |
59 | }; | 50 | }; |
60 | 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 | |||
61 | 73 | ||
62 | /** | 74 | /** |
63 | * Description of a DB result cell. | 75 | * Description of a DB result cell. |
@@ -70,7 +82,9 @@ struct TALER_DB_ResultSpec | |||
70 | void *dst; | 82 | void *dst; |
71 | 83 | ||
72 | /** | 84 | /** |
73 | * Allowed size for the data. | 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). | ||
74 | */ | 88 | */ |
75 | size_t dst_size; | 89 | size_t dst_size; |
76 | 90 | ||
@@ -78,10 +92,48 @@ struct TALER_DB_ResultSpec | |||
78 | * Field name of the desired result. | 92 | * Field name of the desired result. |
79 | */ | 93 | */ |
80 | char *fname; | 94 | char *fname; |
95 | |||
96 | /** | ||
97 | * Actual size of the result. | ||
98 | */ | ||
99 | size_t *result_size; | ||
100 | |||
81 | }; | 101 | }; |
82 | 102 | ||
83 | 103 | ||
84 | /** | 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 | /** | ||
85 | * Execute a prepared statement. | 137 | * Execute a prepared statement. |
86 | */ | 138 | */ |
87 | PGresult * | 139 | PGresult * |
@@ -96,12 +148,14 @@ TALER_DB_exec_prepared (PGconn *db_conn, | |||
96 | * is returned. | 148 | * is returned. |
97 | * | 149 | * |
98 | * @return | 150 | * @return |
99 | * GNUNET_YES if all results could be extracted | 151 | * #GNUNET_YES if all results could be extracted |
100 | * GNUNET_NO if at least one result was NULL | 152 | * #GNUNET_NO if at least one result was NULL |
101 | * GNUNET_SYSERR if a result was invalid (non-existing field) | 153 | * #GNUNET_SYSERR if a result was invalid (non-existing field) |
102 | */ | 154 | */ |
103 | int | 155 | int |
104 | TALER_DB_extract_result (PGresult *result, struct TALER_DB_ResultSpec *rs, int row); | 156 | TALER_DB_extract_result (PGresult *result, |
157 | struct TALER_DB_ResultSpec *rs, | ||
158 | int row); | ||
105 | 159 | ||
106 | 160 | ||
107 | int | 161 | int |