summaryrefslogtreecommitdiff
path: root/src/util/db.c
blob: a0b234a06d842345fc4f43fbe62be56290867c09 (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
/*
  This file is part of TALER
  (C) 2014 Christian Grothoff (and other contributing authors)

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU General Public License as published by the Free Software
  Foundation; either version 3, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU General Public License for more details.

  You should have received a copy of the GNU General Public License along with
  TALER; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
*/


/**
 * @file util/db.c
 * @brief helper functions for DB interactions
 * @author Sree Harsha Totakura <sreeharsha@totakura.in>
 * @author Florian Dold
 */

#include "platform.h"
#include <gnunet/gnunet_util_lib.h>
#include "taler_db_lib.h"


/**
 * Execute a prepared statement.
 */
PGresult *
TALER_DB_exec_prepared (PGconn *db_conn,
                        const char *name,
                        const struct TALER_DB_QueryParam *params)
{
  unsigned len;
  unsigned i;

  /* count the number of parameters */

  {
    const struct TALER_DB_QueryParam *x;
    for (len = 0, x = params;
         x->more;
         len +=1, x += 1);
  }

  /* new scope to allow stack allocation without alloca */

  {
    void *param_values[len];
    int param_lengths[len];
    int param_formats[len];

    for (i = 0; i < len; i += 1)
    {
      param_values[i] = (void *) params[i].data;
      param_lengths[i] = params[i].size;
      param_formats[i] = 1;
    }
    return PQexecPrepared (db_conn, name, len,
                           (const char **) param_values, param_lengths, param_formats, 1);
  }
}


/**
 * Extract results from a query result according to the given specification.
 * If colums are NULL, the destination is not modified, and GNUNET_NO
 * is returned.
 *
 * @return
 *   GNUNET_YES if all results could be extracted
 *   GNUNET_NO if at least one result was NULL
 *   GNUNET_SYSERR if a result was invalid (non-existing field)
 */
int
TALER_DB_extract_result (PGresult *result,
                         struct TALER_DB_ResultSpec *rs,
                         int row)
{
  int had_null = GNUNET_NO;

  for (; NULL != rs->fname; rs += 1)
  {
    int fnum;
    fnum = PQfnumber (result, rs->fname);
    if (fnum < 0)
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' does not exist in result\n", rs->fname);
      return GNUNET_SYSERR;
    }

    /* if a field is null, continue but
     * remember that we now return a different result */

    if (PQgetisnull (result, row, fnum))
    {
      had_null = GNUNET_YES;
      continue;
    }
    const char *res;
    if (rs->dst_size != PQgetlength (result, row, fnum))
    {
      GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "field '%s' has wrong size (got %u, expected %u)\n",
                  rs->fname, (int) PQgetlength (result, row, fnum), (int) rs->dst_size);
      return GNUNET_SYSERR;
    }
    res = PQgetvalue (result, row, fnum);
    GNUNET_assert (NULL != res);
    memcpy (rs->dst, res, rs->dst_size);
  }
  if (GNUNET_YES == had_null)
    return GNUNET_NO;
  return GNUNET_YES;
}


int
TALER_DB_field_isnull (PGresult *result,
                       int row,
                       const char *fname)
{
  int fnum;
  fnum = PQfnumber (result, fname);
  GNUNET_assert (fnum >= 0);
  if (PQgetisnull (result, row, fnum))
    return GNUNET_YES;
  return GNUNET_NO;
}


int
TALER_DB_extract_amount_nbo (PGresult *result,
                             int row,
                             const char *val_name,
                             const char *frac_name,
                             const char *curr_name,
                             struct TALER_AmountNBO *r_amount_nbo)
{
  int val_num;
  int frac_num;
  int curr_num;
  int len;

  GNUNET_assert (NULL != strstr (val_name, "_val"));
  GNUNET_assert (NULL != strstr (frac_name, "_frac"));
  GNUNET_assert (NULL != strstr (curr_name, "_curr"));

  val_num = PQfnumber (result, val_name);
  GNUNET_assert (val_num >= 0);
  frac_num = PQfnumber (result, frac_name);
  GNUNET_assert (frac_num >= 0);
  curr_num = PQfnumber (result, curr_name);
  GNUNET_assert (curr_num >= 0);

  r_amount_nbo->value = *(uint32_t *) PQgetvalue (result, row, val_num);
  r_amount_nbo->fraction = *(uint32_t *) PQgetvalue (result, row, frac_num);
  memset (r_amount_nbo->currency, 0, TALER_CURRENCY_LEN);
  // FIXME: overflow?
  len = PQgetlength (result, row, curr_num);
  len = GNUNET_MIN (TALER_CURRENCY_LEN, len);
  memcpy (r_amount_nbo->currency, PQgetvalue (result, row, curr_num), len);
  r_amount_nbo->currency[TALER_CURRENCY_LEN - 1] = '\0';

  return GNUNET_OK;
}


int
TALER_DB_extract_amount (PGresult *result,
                         int row,
                         const char *val_name,
                         const char *frac_name,
                         const char *curr_name,
                         struct TALER_Amount *r_amount)
{
  struct TALER_AmountNBO amount_nbo;

  (void)
      TALER_DB_extract_amount_nbo (result,
                                   row,
                                   val_name,
                                   frac_name,
                                   curr_name,
                                   &amount_nbo);
  r_amount->value = ntohl (amount_nbo.value);
  r_amount->fraction = ntohl (amount_nbo.fraction);
  (void) strncpy (r_amount->currency, amount_nbo.currency, TALER_CURRENCY_LEN);

  return GNUNET_OK;
}

/* end of util/db.c */