summaryrefslogtreecommitdiff
path: root/src/backenddb/pg_lookup_orders.c
blob: 63494c3aa6c186b090112d973c2e3798966f7409 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
   This file is part of TALER
   Copyright (C) 2022,2023 Taler Systems SA

   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, see <http://www.gnu.org/licenses/>
 */
/**
 * @file backenddb/pg_lookup_orders.c
 * @brief Implementation of the lookup_orders function for Postgres
 * @author Iván Ávalos
 * @author Christian Grothoff
 */
#include "platform.h"
#include <taler/taler_error_codes.h>
#include <taler/taler_dbevents.h>
#include <taler/taler_pq_lib.h>
#include "pg_lookup_orders.h"
#include "pg_helper.h"

/**
 * Context used for TMH_PG_lookup_orders().
 */
struct LookupOrdersContext
{
  /**
   * Function to call with the results.
   */
  TALER_MERCHANTDB_OrdersCallback cb;

  /**
   * Closure for @a cb.
   */
  void *cb_cls;

  /**
   * Did database result extraction fail?
   */
  bool extract_failed;
};


/**
 * Function to be called with the results of a SELECT statement
 * that has returned @a num_results results about orders.
 *
 * @param[in,out] cls of type `struct LookupOrdersContext *`
 * @param result the postgres result
 * @param num_results the number of results in @a result
 */
static void
lookup_orders_cb (void *cls,
                  PGresult *result,
                  unsigned int num_results)
{
  struct LookupOrdersContext *plc = cls;

  for (unsigned int i = 0; i < num_results; i++)
  {
    char *order_id;
    uint64_t order_serial;
    struct GNUNET_TIME_Timestamp ts;
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_string ("order_id",
                                    &order_id),
      GNUNET_PQ_result_spec_uint64 ("order_serial",
                                    &order_serial),
      GNUNET_PQ_result_spec_timestamp ("creation_time",
                                       &ts),
      GNUNET_PQ_result_spec_end
    };

    if (GNUNET_OK !=
        GNUNET_PQ_extract_result (result,
                                  rs,
                                  i))
    {
      GNUNET_break (0);
      plc->extract_failed = true;
      return;
    }
    plc->cb (plc->cb_cls,
             order_id,
             order_serial,
             ts);
    GNUNET_PQ_cleanup_result (rs);
  }
}


enum GNUNET_DB_QueryStatus
TMH_PG_lookup_orders (void *cls,
                      const char *instance_id,
                      const struct TALER_MERCHANTDB_OrderFilter *of,
                      TALER_MERCHANTDB_OrdersCallback cb,
                      void *cb_cls)
{
  struct PostgresClosure *pg = cls;
  struct LookupOrdersContext plc = {
    .cb = cb,
    .cb_cls = cb_cls
  };
  uint64_t limit = (of->delta > 0) ? of->delta : -of->delta;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_uint64 (&limit),
    GNUNET_PQ_query_param_uint64 (&of->start_row),
    GNUNET_PQ_query_param_timestamp (&of->date),
    GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == of->paid)),
    GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == of->paid)),
    GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == of->refunded)),
    GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == of->refunded)),
    GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_ALL == of->wired)),
    GNUNET_PQ_query_param_bool ((TALER_EXCHANGE_YNA_YES == of->wired)),
    GNUNET_PQ_query_param_bool (NULL == of->session_id),
    NULL == of->session_id
    ? GNUNET_PQ_query_param_null ()
    : GNUNET_PQ_query_param_string (of->session_id),
    GNUNET_PQ_query_param_bool (NULL == of->fulfillment_url),
    NULL == of->fulfillment_url
    ? GNUNET_PQ_query_param_null ()
    : GNUNET_PQ_query_param_string (of->fulfillment_url),
    GNUNET_PQ_query_param_end
  };
  enum GNUNET_DB_QueryStatus qs;
  char stmt[128];

  GNUNET_snprintf (stmt,
                   sizeof (stmt),
                   "lookup_orders_%s",
                   (of->delta > 0) ? "inc" : "dec");
  PREPARE (pg,
           "lookup_orders_dec",
           "(SELECT"
           " order_id"
           ",order_serial"
           ",creation_time"
           " FROM merchant_orders"
           " WHERE merchant_orders.merchant_serial="
           "     (SELECT merchant_serial "
           "        FROM merchant_instances"
           "        WHERE merchant_id=$1)"
           "   AND"
           "    order_serial < $3"
           "   AND"
           "    creation_time < $4"
           "   AND ($5 OR "
           "    NOT CAST($6 as BOOL))" /* unclaimed orders are never paid */
           "   AND ($7 OR "
           "    NOT CAST($8 as BOOL))"/* unclaimed orders are never refunded */
           "   AND ($9 OR "
           "    NOT CAST($10 as BOOL))" /* unclaimed orders are never wired */
           "   AND"
           "    order_serial NOT IN"
           "     (SELECT order_serial"
           "      FROM merchant_contract_terms)" /* only select unclaimed orders */
           "   AND ($11 OR "
           "     ($12 = session_id))"
           "   AND ($13 OR "
           "     ($14 = fulfillment_url))"
           " ORDER BY order_serial DESC"
           " LIMIT $2)"
           "UNION " /* union ensures elements are distinct! */
           "(SELECT"
           " order_id"
           ",order_serial"
           ",creation_time"
           " FROM merchant_contract_terms"
           " WHERE merchant_contract_terms.merchant_serial="
           "     (SELECT merchant_serial "
           "        FROM merchant_instances"
           "        WHERE merchant_id=$1)"
           "   AND"
           "    order_serial < $3"
           "   AND"
           "    creation_time < $4"
           "   AND ($5 OR "
           "    (BOOL($6) = paid))"
           "   AND ($7 OR "
           "    (BOOL($8) = (order_serial IN"
           "     (SELECT order_serial "
           "      FROM merchant_refunds))))"
           "   AND ($9 OR"
           "     (BOOL($10) = wired))"
           "   AND ($11 OR "
           "     ($12 = session_id))"
           "   AND ($13 OR "
           "     ($14 = fulfillment_url))"
           " ORDER BY order_serial DESC"
           " LIMIT $2)"
           " ORDER BY order_serial DESC"
           " LIMIT $2");
  
  PREPARE (pg,
           "lookup_orders_inc",
           "(SELECT"
           " order_id"
           ",order_serial"
           ",creation_time"
           " FROM merchant_orders"
           " WHERE merchant_orders.merchant_serial="
           "     (SELECT merchant_serial "
           "        FROM merchant_instances"
           "        WHERE merchant_id=$1)"
           "   AND"
           "    order_serial > $3"
           "   AND"
           "    creation_time > $4"
           "   AND ($5 OR "
           "    NOT CAST($6 as BOOL))" /* unclaimed orders are never paid */
           "   AND ($7 OR "
           "    NOT CAST($8 as BOOL))"/* unclaimed orders are never refunded */
           "   AND ($9 OR "
           "    NOT CAST($10 as BOOL))" /* unclaimed orders are never wired */
           "   AND"
           "    (order_serial NOT IN"
           "     (SELECT order_serial"
           "      FROM merchant_contract_terms))" /* only select unclaimed orders */
           "   AND ($11 OR "
           "     ($12 = session_id))"
           "   AND ($13 OR "
           "     ($14 = fulfillment_url))"
           " ORDER BY order_serial ASC"
           " LIMIT $2)"
           "UNION " /* union ensures elements are distinct! */
           "(SELECT"
           " order_id"
           ",order_serial"
           ",creation_time"
           " FROM merchant_contract_terms"
           " WHERE merchant_contract_terms.merchant_serial="
           "     (SELECT merchant_serial "
           "        FROM merchant_instances"
           "        WHERE merchant_id=$1)"
           "   AND"
           "    order_serial > $3"
           "   AND"
           "    creation_time > $4"
           "   AND ($5 OR "
           "    (BOOL($6) = paid))"
           "   AND ($7 OR "
           "    (BOOL($8) = (order_serial IN"
           "     (SELECT order_serial "
           "      FROM merchant_refunds))))"
           "   AND ($9 OR"
           "    (BOOL($10) = wired))"
           "   AND ($11 OR "
           "     ($12 = session_id))"
           "   AND ($13 OR "
           "     ($14 = fulfillment_url))"
           " ORDER BY order_serial ASC"
           " LIMIT $2)"
           " ORDER BY order_serial ASC"
           " LIMIT $2");
  
  qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
                                             stmt,
                                             params,
                                             &lookup_orders_cb,
                                             &plc);
  if (plc.extract_failed)
    return GNUNET_DB_STATUS_HARD_ERROR;
  return qs;
}