summaryrefslogtreecommitdiff
path: root/src/backenddb/merchant-0004.sql
blob: eebe2889c1f8313152a8cab4b7380d98f079bd1c (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
--
-- This file is part of TALER
-- Copyright (C) 2021 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 merchant-004.sql
 * @brief database helper functions for postgres used by the merchant and funtion for plugin_merchantdb_postgres.c
 * @author Priscilla Huang
 */

-- Everything in one big transaction
BEGIN;

-- Check patch versioning is in place.
SELECT _v.register_patch('merchant-0004', NULL, NULL);

SET search_path TO merchant;

-- create table here!

CREATE TABLE IF NOT EXISTS merchant_template
  (template_serial BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY
  ,merchant_serial BIGINT NOT NULL
    REFERENCES merchant_instances (merchant_serial) ON DELETE CASCADE
  ,template_id VARCHAR NOT NULL
  ,template_description VARCHAR NOT NULL
  ,image BYTEA
  ,template_contract VARCHAR NOT NULL -- in JSON format
  ,UNIQUE (merchant_serial, template_id)
  );
COMMENT ON TABLE merchant_template
  IS 'template used by the merchant (may be incomplete, frontend can override)';
COMMENT ON COLUMN merchant_template.template_description
  IS 'Human-readable template description';
COMMENT ON COLUMN merchant_template.image
  IS 'NOT NULL, but can be 0 bytes; must contain an ImageDataUrl';
COMMENT ON COLUMN merchant_template.template_contract
  IS 'The template contract will contains some additional information.'


-- C CODE

/**
 * Delete information about a template.
 *
 * @param cls closure
 * @param instance_id instance to delete product of
 * @param template_id template to delete
 * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
 *           if template unknow.
 */
static enum GNUNET_DB_QueryStatus
postgres_delete_template (void *cls,
                         const char *instance_id,
                         const char *template_id)
{
  struct PostgresClosure *pg = cls;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_string (template_id),
    GNUNET_PQ_query_param_end
  };

  check_connection (pg);
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "delete_template",
                                             params);
}

/**
 * Insert details about a particular template.
 *
 * @param cls closure
 * @param instance_id instance to insert template for
 * @param template_id template identifier of template to insert
 * @param pd the template details to insert
 * @return database result code
 */
static enum GNUNET_DB_QueryStatus
postgres_insert_template (void *cls,
                         const char *instance_id,
                         const char *template_id,
                         const struct TALER_MERCHANTDB_TemplateDetails *pd)
{
  struct PostgresClosure *pg = cls;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_string (template_id),
    GNUNET_PQ_query_param_string (pd->template_description),
    GNUNET_PQ_query_param_string (pd->image),
    GNUNET_PQ_query_param_string (pd->template_contract),
    GNUNET_PQ_query_param_end

  };

  check_connection (pg);
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "insert_template",
                                             params);
}

/**
 * Update details about a particular template.
 *
 * @param cls closure
 * @param instance_id instance to update template for
 * @param template_id template to update
 * @param pd update to the template details on success, can be NULL
 *             (in that case we only want to check if the template exists)
 * @return database result code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the template
 *         does not yet exist.
 */
static enum GNUNET_DB_QueryStatus
postgres_update_template (void *cls,
                         const char *instance_id,
                         const char *template_id,
                         const struct TALER_MERCHANTDB_TemplateDetails *pd)
{
  struct PostgresClosure *pg = cls;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_string (template_id),
    GNUNET_PQ_query_param_string (pd->template_description),
    GNUNET_PQ_query_param_string (pd->image),
    GNUNET_PQ_query_param_string (pd->template_contract),
    GNUNET_PQ_query_param_end
  };

  {
    GNUNET_break (0);
    return GNUNET_DB_STATUS_HARD_ERROR;
  }
  check_connection (pg);
  return GNUNET_PQ_eval_prepared_non_select (pg->conn,
                                             "update_template",
                                             params);
}




/**
 * Context used for postgres_lookup_template().
 */
struct LookupTemplateContext
{
  /**
   * Function to call with the results.
   */
  TALER_MERCHANTDB_TemplateCallback 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 template.
 *
 * @param[in,out] cls of type `struct LookupTemplateContext *`
 * @param result the postgres result
 * @param num_results the number of results in @a result
 */
static void
lookup_templates_cb (void *cls,
                    PGresult *result,
                    unsigned int num_results)
{
  struct LookupTemplateContext *tlc = cls;

  for (unsigned int i = 0; i < num_results; i++)
  {
    char *template_id;
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_string ("template_id",
                                    &template_id),
      GNUNET_PQ_result_spec_string ("template_description",
                                   &template_description),
      GNUNET_PQ_result_spec_end
    };

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


/**
 * Lookup all of the templates the given instance has configured.
 *
 * @param cls closure
 * @param instance_id instance to lookup template for
 * @param cb function to call on all template found
 * @param cb_cls closure for @a cb
 * @return database result code
 */
static enum GNUNET_DB_QueryStatus
postgres_lookup_templates (void *cls,
                          const char *instance_id,
                          TALER_MERCHANTDB_TemplateCallback cb,
                          void *cb_cls)
{
  struct PostgresClosure *pg = cls;
  struct LookupTemplateContext tlc = {
    .cb = cb,
    .cb_cls = cb_cls,
    /* Can be overwritten by the lookup_template_cb */
    .extract_failed = false,
  };
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_end
  };
  enum GNUNET_DB_QueryStatus qs;

  check_connection (pg);
  qs = GNUNET_PQ_eval_prepared_multi_select (pg->conn,
                                             "lookup_templates",
                                             params,
                                             &lookup_templates_cb,
                                             &tlc);
  /* If there was an error inside lookup_template_cb, return a hard error. */
  if (tlc.extract_failed)
    return GNUNET_DB_STATUS_HARD_ERROR;
  return qs;
}

/**
 * Lookup details about a particular template.
 *
 * @param cls closure
 * @param instance_id instance to lookup template for
 * @param template_id template to lookup
 * @param[out] pd set to the template details on success, can be NULL
 *             (in that case we only want to check if the template exists)
 * @return database result code
 */
static enum GNUNET_DB_QueryStatus
postgres_lookup_template (void *cls,
                         const char *instance_id,
                         const char *template_id,
                         struct TALER_MERCHANTDB_TemplateDetails *pd)
{
  struct PostgresClosure *pg = cls;
  struct GNUNET_PQ_QueryParam params[] = {
    GNUNET_PQ_query_param_string (instance_id),
    GNUNET_PQ_query_param_string (template_id),
    GNUNET_PQ_query_param_end
  };

  if (NULL == pd)
  {
    struct GNUNET_PQ_ResultSpec rs_null[] = {
      GNUNET_PQ_result_spec_end
    };

    check_connection (pg);
    return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                     "lookup_template",
                                                     params,
                                                     rs_null);
  }
  else
  {
    struct GNUNET_PQ_ResultSpec rs[] = {
      GNUNET_PQ_result_spec_string ("template_description",
                                    &pd->template_description),
      GNUNET_PQ_result_spec_string ("image",
                                    &pd->image),
      GNUNET_PQ_result_spec_string ("template_contract",
                                    &pd->template_contract),
      GNUNET_PQ_result_spec_end
    };

    check_connection (pg);
    return GNUNET_PQ_eval_prepared_singleton_select (pg->conn,
                                                     "lookup_template",
                                                     params,
                                                     rs);
  }
}



------------------------------API--------------------------------

/**
 * Typically called by `lookup_templates`.
 *
 * @param cls a `json_t *` JSON array to build
 * @param template_id ID of the template
 */
typedef void
(*TALER_MERCHANTDB_TemplatesCallback)(void *cls,
                                     const char *template_id,
                                     const char *template_description);


/**
 * Details about a template.
 */
struct TALER_MERCHANTDB_TemplateDetails
{
  /**
   * Description of the template.
   */
  const char *template_description;

  /**
   * Base64-encoded product image, or NULL.
   */
  const char *image;

  /**
   * In this template contract, we can have additional information.
   */
  const json_t *template_contract;
};



/**
 * Lookup all of the templates the given instance has configured.
 *
 * @param cls closure
 * @param instance_id instance to lookup template for
 * @param cb function to call on all template found
 * @param cb_cls closure for @a cb
 * @return database result code
 */
enum GNUNET_DB_QueryStatus
(*lookup_templates) (void *cls,
                          const char *instance_id,
                          TALER_MERCHANTDB_TemplateCallback cb,
                          void *cb_cls);


/**
 * Lookup details about a particular template.
 *
 * @param cls closure
 * @param instance_id instance to lookup template for
 * @param template_id template to lookup
 * @param[out] pd set to the template details on success, can be NULL
 *             (in that case we only want to check if the template exists)
 * @return database result code
 */
enum GNUNET_DB_QueryStatus
(*lookup_template) (void *cls,
                         const char *instance_id,
                         const char *template_id,
                         struct TALER_MERCHANTDB_TemplateDetails *pd);

/**
 * Delete information about a template.
 *
 * @param cls closure
 * @param instance_id instance to delete product of
 * @param template_id template to delete
 * @return DB status code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS
 *           if template unknow.
 */
enum GNUNET_DB_QueryStatus
(*delete_template) (void *cls,
                         const char *instance_id,
                         const char *template_id);



/**
 * Insert details about a particular template.
 *
 * @param cls closure
 * @param instance_id instance to insert template for
 * @param template_id template identifier of template to insert
 * @param pd the template details to insert
 * @return database result code
 */
enum GNUNET_DB_QueryStatus
(*insert_template) (void *cls,
                         const char *instance_id,
                         const char *template_id,
                         const struct TALER_MERCHANTDB_TemplateDetails *pd);



/**
 * Update details about a particular template.
 *
 * @param cls closure
 * @param instance_id instance to update template for
 * @param template_id template to update
 * @param pd update to the template details on success, can be NULL
 *             (in that case we only want to check if the template exists)
 * @return database result code, #GNUNET_DB_STATUS_SUCCESS_NO_RESULTS if the template
 *         does not yet exist.
 */

enum GNUNET_DB_QueryStatus
(*update_template) (void *cls,
                         const char *instance_id,
                         const char *template_id,
                         const struct TALER_MERCHANTDB_TemplateDetails *pd)