summaryrefslogtreecommitdiff
path: root/ngx_postgres-1.0/src/ngx_postgres_module.h
blob: 03f49fdbc74af12ff0ed991a500e49a4136ca4ac (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
/*
 * Copyright (c) 2010, FRiCKLE Piotr Sikora <info@frickle.com>
 * Copyright (c) 2009-2010, Xiaozhe Wang <chaoslawful@gmail.com>
 * Copyright (c) 2009-2010, Yichun Zhang <agentzh@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef _NGX_POSTGRES_MODULE_H_
#define _NGX_POSTGRES_MODULE_H_

#include <nginx.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <libpq-fe.h>


extern ngx_module_t  ngx_postgres_module;


typedef struct {
    ngx_http_script_code_pt             code;
    ngx_uint_t                          empty;
} ngx_postgres_escape_t;

typedef struct {
    ngx_uint_t                          key;
    ngx_str_t                           sv;
    ngx_http_complex_value_t           *cv;
} ngx_postgres_mixed_t;

typedef struct {
    ngx_uint_t                          key;
    ngx_int_t                           status;
} ngx_postgres_rewrite_t;

typedef struct {
    ngx_int_t                           row;
    ngx_int_t                           column;
    u_char                             *col_name;
    ngx_uint_t                          required;
} ngx_postgres_value_t;

typedef struct {
    ngx_uint_t                          idx;
    ngx_http_variable_t                *var;
    ngx_postgres_value_t                value;
} ngx_postgres_variable_t;

typedef struct {
    ngx_uint_t                          methods_set;
    ngx_array_t                        *methods; /* method-specific */
    ngx_postgres_mixed_t               *def;     /* default */
} ngx_postgres_query_conf_t;

typedef struct ngx_postgres_rewrite_conf_s ngx_postgres_rewrite_conf_t;

typedef ngx_int_t (*ngx_postgres_rewrite_handler_pt)
    (ngx_http_request_t *, ngx_postgres_rewrite_conf_t *);

struct ngx_postgres_rewrite_conf_s {
    /* condition */
    ngx_uint_t                          key;
    ngx_postgres_rewrite_handler_pt     handler;
    /* methods */
    ngx_uint_t                          methods_set;
    ngx_array_t                        *methods; /* method-specific */
    ngx_postgres_rewrite_t             *def;     /* default */
};

typedef struct {
    ngx_str_t                           name;
    ngx_uint_t                          key;
    ngx_postgres_rewrite_handler_pt     handler;
} ngx_postgres_rewrite_enum_t;

typedef ngx_int_t (*ngx_postgres_output_handler_pt)
    (ngx_http_request_t *, PGresult *);

typedef struct {
    ngx_str_t                           name;
    unsigned                            binary:1;
    ngx_postgres_output_handler_pt      handler;
} ngx_postgres_output_enum_t;

typedef struct {
#if defined(nginx_version) && (nginx_version >= 8022)
    ngx_addr_t                         *addrs;
#else
    ngx_peer_addr_t                    *addrs;
#endif
    ngx_uint_t                          naddrs;
    in_port_t                           port;
    ngx_str_t                           dbname;
    ngx_str_t                           user;
    ngx_str_t                           password;
} ngx_postgres_upstream_server_t;

typedef struct {
    struct sockaddr                    *sockaddr;
    socklen_t                           socklen;
    ngx_str_t                           name;
    ngx_str_t                           host;
    in_port_t                           port;
    ngx_str_t                           dbname;
    ngx_str_t                           user;
    ngx_str_t                           password;
} ngx_postgres_upstream_peer_t;

typedef struct {
    ngx_uint_t                          single;
    ngx_uint_t                          number;
    ngx_str_t                          *name;
    ngx_postgres_upstream_peer_t        peer[1];
} ngx_postgres_upstream_peers_t;

typedef struct {
    ngx_postgres_upstream_peers_t      *peers;
    ngx_uint_t                          current;
    ngx_array_t                        *servers;
    ngx_pool_t                         *pool;
    /* keepalive */
    ngx_flag_t                          single;
    ngx_queue_t                         free;
    ngx_queue_t                         cache;
    ngx_uint_t                          active_conns;
    ngx_uint_t                          max_cached;
    ngx_uint_t                          reject;
} ngx_postgres_upstream_srv_conf_t;

typedef struct {
    /* upstream */
    ngx_http_upstream_conf_t            upstream;
    ngx_http_complex_value_t           *upstream_cv;
    /* queries */
    ngx_postgres_query_conf_t           query;
    /* rewrites */
    ngx_array_t                        *rewrites;
    /* output */
    ngx_postgres_output_handler_pt      output_handler;
    unsigned                            output_binary:1;
    /* custom variables */
    ngx_array_t                        *variables;
} ngx_postgres_loc_conf_t;

typedef struct {
    ngx_chain_t                        *response;
    ngx_int_t                           var_cols;
    ngx_int_t                           var_rows;
    ngx_int_t                           var_affected;
    ngx_str_t                           var_query;
    ngx_array_t                        *variables;
    ngx_int_t                           status;
} ngx_postgres_ctx_t;


ngx_int_t   ngx_postgres_add_variables(ngx_conf_t *);
void       *ngx_postgres_create_upstream_srv_conf(ngx_conf_t *);
void       *ngx_postgres_create_loc_conf(ngx_conf_t *);
char       *ngx_postgres_merge_loc_conf(ngx_conf_t *, void *, void *);
char       *ngx_postgres_conf_server(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_keepalive(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_pass(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_query(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_rewrite(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_output(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_set(ngx_conf_t *, ngx_command_t *, void *);
char       *ngx_postgres_conf_escape(ngx_conf_t *, ngx_command_t *, void *);

ngx_http_upstream_srv_conf_t  *ngx_postgres_find_upstream(ngx_http_request_t *,
                                   ngx_url_t *);

#endif /* _NGX_POSTGRES_MODULE_H_ */