2014-03-30 23:54:35 +02:00
|
|
|
/*
|
2021-01-11 23:46:08 +01:00
|
|
|
* Copyright (c) 2014-2021 Joris Vink <joris@coders.se>
|
2014-03-30 23:54:35 +02:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _H_KORE_PGSQL
|
|
|
|
#define _H_KORE_PGSQL
|
|
|
|
|
|
|
|
#include <libpq-fe.h>
|
|
|
|
|
2014-09-28 21:39:16 +02:00
|
|
|
#define KORE_PGSQL_FORMAT_TEXT 0
|
|
|
|
#define KORE_PGSQL_FORMAT_BINARY 1
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
#define KORE_PGSQL_SYNC 0x0001
|
|
|
|
#define KORE_PGSQL_ASYNC 0x0002
|
2017-01-29 10:34:53 +01:00
|
|
|
#define KORE_PGSQL_SCHEDULED 0x0004
|
2016-01-04 11:12:43 +01:00
|
|
|
|
2019-06-01 23:14:50 +02:00
|
|
|
#define KORE_PGSQL_PARAM_BINARY(v, l) v, l, 1
|
|
|
|
#define KORE_PGSQL_PARAM_TEXT_LEN(v, l) v, l, 0
|
|
|
|
#define KORE_PGSQL_PARAM_TEXT(v) v, strlen(v), 0
|
|
|
|
|
2015-04-01 13:25:10 +02:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-08-14 14:34:23 +02:00
|
|
|
struct pgsql_conn {
|
2018-10-09 19:34:40 +02:00
|
|
|
struct kore_event evt;
|
2014-08-14 14:34:23 +02:00
|
|
|
u_int8_t flags;
|
2016-01-04 11:12:43 +01:00
|
|
|
char *name;
|
2014-08-14 14:34:23 +02:00
|
|
|
|
|
|
|
PGconn *db;
|
|
|
|
struct pgsql_job *job;
|
|
|
|
TAILQ_ENTRY(pgsql_conn) list;
|
|
|
|
};
|
|
|
|
|
2016-01-04 11:12:43 +01:00
|
|
|
struct pgsql_db {
|
|
|
|
char *name;
|
|
|
|
char *conn_string;
|
2017-08-21 14:25:09 +02:00
|
|
|
u_int16_t conn_max;
|
|
|
|
u_int16_t conn_count;
|
2016-01-04 11:12:43 +01:00
|
|
|
|
|
|
|
LIST_ENTRY(pgsql_db) rlist;
|
|
|
|
};
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
struct kore_pgsql {
|
|
|
|
u_int8_t state;
|
2016-01-04 11:12:43 +01:00
|
|
|
int flags;
|
2014-03-30 23:54:35 +02:00
|
|
|
char *error;
|
|
|
|
PGresult *result;
|
2014-08-14 14:34:23 +02:00
|
|
|
struct pgsql_conn *conn;
|
2014-08-14 22:05:34 +02:00
|
|
|
|
2018-07-18 11:38:17 +02:00
|
|
|
struct {
|
|
|
|
char *channel;
|
|
|
|
char *extra;
|
|
|
|
} notify;
|
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
struct http_request *req;
|
|
|
|
void *arg;
|
|
|
|
void (*cb)(struct kore_pgsql *, void *);
|
|
|
|
|
2014-08-14 22:05:34 +02:00
|
|
|
LIST_ENTRY(kore_pgsql) rlist;
|
2014-03-30 23:54:35 +02:00
|
|
|
};
|
|
|
|
|
2014-07-04 09:14:05 +02:00
|
|
|
extern u_int16_t pgsql_conn_max;
|
2018-02-13 13:21:27 +01:00
|
|
|
extern u_int32_t pgsql_queue_limit;
|
2014-04-14 08:41:41 +02:00
|
|
|
|
2017-03-24 12:53:07 +01:00
|
|
|
void kore_pgsql_sys_init(void);
|
2017-02-06 11:40:33 +01:00
|
|
|
void kore_pgsql_sys_cleanup(void);
|
2017-03-24 12:53:07 +01:00
|
|
|
void kore_pgsql_init(struct kore_pgsql *);
|
|
|
|
void kore_pgsql_bind_request(struct kore_pgsql *, struct http_request *);
|
|
|
|
void kore_pgsql_bind_callback(struct kore_pgsql *,
|
|
|
|
void (*cb)(struct kore_pgsql *, void *), void *);
|
|
|
|
int kore_pgsql_setup(struct kore_pgsql *, const char *, int);
|
2014-09-19 12:32:49 +02:00
|
|
|
void kore_pgsql_handle(void *, int);
|
|
|
|
void kore_pgsql_cleanup(struct kore_pgsql *);
|
2017-03-24 12:53:07 +01:00
|
|
|
void kore_pgsql_continue(struct kore_pgsql *);
|
2019-06-01 23:14:50 +02:00
|
|
|
int kore_pgsql_query(struct kore_pgsql *, const void *);
|
2016-01-04 11:12:43 +01:00
|
|
|
int kore_pgsql_query_params(struct kore_pgsql *,
|
2019-06-01 23:14:50 +02:00
|
|
|
const void *, int, int, ...);
|
2016-06-07 13:12:31 +02:00
|
|
|
int kore_pgsql_v_query_params(struct kore_pgsql *,
|
2019-06-01 23:14:50 +02:00
|
|
|
const void *, int, int, va_list);
|
|
|
|
int kore_pgsql_query_param_fields(struct kore_pgsql *, const void *,
|
2019-04-25 23:13:13 +02:00
|
|
|
int, int, const char **, int *, int *);
|
2016-01-04 11:12:43 +01:00
|
|
|
int kore_pgsql_register(const char *, const char *);
|
2014-09-19 12:32:49 +02:00
|
|
|
int kore_pgsql_ntuples(struct kore_pgsql *);
|
2017-02-06 11:40:33 +01:00
|
|
|
int kore_pgsql_nfields(struct kore_pgsql *);
|
2014-09-19 12:32:49 +02:00
|
|
|
void kore_pgsql_logerror(struct kore_pgsql *);
|
2017-02-06 11:40:33 +01:00
|
|
|
char *kore_pgsql_fieldname(struct kore_pgsql *, int);
|
2014-09-19 12:32:49 +02:00
|
|
|
char *kore_pgsql_getvalue(struct kore_pgsql *, int, int);
|
2014-09-28 23:03:49 +02:00
|
|
|
int kore_pgsql_getlength(struct kore_pgsql *, int, int);
|
2019-04-25 23:13:13 +02:00
|
|
|
int kore_pgsql_column_binary(struct kore_pgsql *, int);
|
2014-04-01 21:44:22 +02:00
|
|
|
|
2015-04-01 13:25:10 +02:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-03-30 23:54:35 +02:00
|
|
|
#define KORE_PGSQL_STATE_INIT 1
|
|
|
|
#define KORE_PGSQL_STATE_WAIT 2
|
|
|
|
#define KORE_PGSQL_STATE_RESULT 3
|
|
|
|
#define KORE_PGSQL_STATE_ERROR 4
|
|
|
|
#define KORE_PGSQL_STATE_DONE 5
|
|
|
|
#define KORE_PGSQL_STATE_COMPLETE 6
|
2018-07-18 11:38:17 +02:00
|
|
|
#define KORE_PGSQL_STATE_NOTIFY 7
|
2014-03-30 23:54:35 +02:00
|
|
|
|
|
|
|
#endif
|