aboutsummaryrefslogtreecommitdiff
path: root/src/dbrw.h
blob: 1ff1056aaf357cfaffc1eb2c7e0bcad02590646e (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
/* This file is part of vmod-dbrw
   Copyright (C) 2013-2018 Sergey Poznyakoff

   Vmod-dbrw 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.

   Vmod-dbrw 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 vmod-dbrw.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <regex.h>

#include "vcl.h"
#include "vrt.h"
#include "vcc_if.h"
#include "cache/cache.h"
#define WSPTR(s) ((s)->ws)

struct dbrw_connection;

struct dbrw_backend {
	char *name;
	
	int (*sql_init) (struct dbrw_connection *);
	void (*sql_destroy) (struct dbrw_connection *);
	int (*sql_connect) (struct dbrw_connection *);
	int (*sql_disconnect) (struct dbrw_connection *);
	char *(*sql_escape) (struct dbrw_connection *, const char *);
	int (*sql_query) (struct dbrw_connection *, const char *);
	unsigned (*sql_num_tuples) (struct dbrw_connection *);
	unsigned (*sql_num_fields) (struct dbrw_connection *);
	int (*sql_free_result) (struct dbrw_connection *);
	const char *(*sql_get_column)(struct dbrw_connection *, size_t, size_t);
	int (*sql_idle_timeout)(struct dbrw_connection *);
};

enum {
	state_init,
	state_connected,
	state_result,
	state_error,
	state_disabled
};

#define HTTP_STATUS_LEN 3

struct dbrw_config {
	unsigned magic;
#define DBRW_CONFIG_MAGIC 0x67636667
	int debug_level;
	struct dbrw_backend *backend;
	char **param;
	char *param_str;
	char *query;
	int qdisp;
	int regflags;
	char status[HTTP_STATUS_LEN+1];
	int idle_timeout;
	VTAILQ_ENTRY(dbrw_config) list;
};

struct dbrw_connection {
	unsigned magic;
#define DBRW_CONNECTION_MAGIC 0x6773716c
	int state;                 /* Connection state */
	int busy:1;
	struct dbrw_config *conf;  /* Pointer to the configuration data */
	regmatch_t *matches;       /* Match map */
	size_t matchsize;          /* Total number of entries in match map */
	time_t timestamp;          /* Last used at */           
	void *data;                /* Backend-specific data */
	VTAILQ_ENTRY(dbrw_connection) list;
};

void dbrw_debug(const char *fmt, ...);
void dbrw_error(const char *fmt, ...);

#define debug(c,n,a) do { if ((c)->debug_level>=(n)) dbrw_debug a; } while (0)

#ifdef USE_SQL_MYSQL
struct dbrw_backend mysql_backend;
#endif
#ifdef USE_SQL_PGSQL
struct dbrw_backend pgsql_backend;
#endif

struct dbrw_backend *dbrw_backend_select(const char *name);

int sql_init(struct dbrw_connection *);
int sql_connect(struct dbrw_connection *pd);
void sql_disconnect(struct dbrw_connection *pd);
char *sql_escape(struct dbrw_connection *pd, const char *input);
int sql_query(struct dbrw_connection *pd, const char *input);
unsigned sql_num_tuples(struct dbrw_connection *pd);
unsigned sql_num_fields(struct dbrw_connection *pd);
void sql_free_result(struct dbrw_connection *pd);
void sql_destroy(struct dbrw_connection *pd);
const char *sql_get_column(struct dbrw_connection *pd, unsigned row, unsigned col);
int sql_idle_timeout(struct dbrw_connection *conn);

char *findparam(char **params, char *name);

Return to:

Send suggestions and report system problems to the System administrator.