Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add "$postgres_affected". Rename "$postgres_column_count" to "$postgr…

…es_columns" and "$postgres_row_count" to "$postgres_rows".
  • Loading branch information...
commit 12c2710f2ae12cad3dcb8275d9e5dce069c409aa 1 parent 1274e68
@PiotrSikora PiotrSikora authored
View
13 README
@@ -90,16 +90,21 @@ CONFIGURATION DIRECTIVES:
CONFIGURATION VARIABLES:
------------------------
- $postgres_column_count
- ----------------------
+ $postgres_columns
+ -----------------
Number of columns in received result-set.
- $postgres_row_count
- -------------------
+ $postgres_rows
+ --------------
Number of rows in received result-set.
+ $postgres_affected
+ ------------------
+ Number of rows affected by INSERT, UPDATE, DELETE, MOVE, FETCH or COPY SQL query.
+
+
$postgres_query
---------------
SQL query, as seen by PostgreSQL database.
View
1  src/ngx_postgres_handler.c
@@ -159,6 +159,7 @@ ngx_postgres_handler(ngx_http_request_t *r)
pgctx->var_cols = NGX_ERROR;
pgctx->var_rows = NGX_ERROR;
+ pgctx->var_affected = NGX_ERROR;
if (pglcf->variables != NULL) {
pgctx->variables = ngx_array_create(r->pool, pglcf->variables->nelts,
View
12 src/ngx_postgres_module.c
@@ -101,12 +101,16 @@ static ngx_command_t ngx_postgres_module_commands[] = {
static ngx_http_variable_t ngx_postgres_module_variables[] = {
- { ngx_string("postgres_column_count"), NULL,
- ngx_postgres_variable_column_count, 0,
+ { ngx_string("postgres_columns"), NULL,
+ ngx_postgres_variable_columns, 0,
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
- { ngx_string("postgres_row_count"), NULL,
- ngx_postgres_variable_row_count, 0,
+ { ngx_string("postgres_rows"), NULL,
+ ngx_postgres_variable_rows, 0,
+ NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
+
+ { ngx_string("postgres_affected"), NULL,
+ ngx_postgres_variable_affected, 0,
NGX_HTTP_VAR_NOCACHEABLE|NGX_HTTP_VAR_NOHASH, 0 },
{ ngx_string("postgres_query"), NULL,
View
1  src/ngx_postgres_module.h
@@ -129,6 +129,7 @@ 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_postgres_ctx_t;
View
9 src/ngx_postgres_processor.c
@@ -324,6 +324,8 @@ ngx_postgres_process_response(ngx_http_request_t *r, PGresult *res)
ngx_postgres_ctx_t *pgctx;
ngx_postgres_variable_t *pgvar;
ngx_str_t *store;
+ char *affected;
+ size_t affected_len;
ngx_uint_t i;
dd("entering");
@@ -337,6 +339,13 @@ ngx_postgres_process_response(ngx_http_request_t *r, PGresult *res)
/* set $postgres_row_count */
pgctx->var_rows = PQntuples(res);
+ /* set $postgres_affected */
+ affected = PQcmdTuples(res);
+ affected_len = ngx_strlen(affected);
+ if (affected_len) {
+ pgctx->var_affected = ngx_atoi((u_char *) affected, affected_len);
+ }
+
if (pglcf->variables != NULL) {
/* set custom variables */
pgvar = pglcf->variables->elts;
View
39 src/ngx_postgres_variable.c
@@ -31,12 +31,12 @@
ngx_int_t
-ngx_postgres_variable_column_count(ngx_http_request_t *r,
+ngx_postgres_variable_columns(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_postgres_ctx_t *pgctx;
- dd("entering: \"$postgres_column_count\"");
+ dd("entering: \"$postgres_columns\"");
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
@@ -62,12 +62,12 @@ ngx_postgres_variable_column_count(ngx_http_request_t *r,
}
ngx_int_t
-ngx_postgres_variable_row_count(ngx_http_request_t *r,
+ngx_postgres_variable_rows(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
ngx_postgres_ctx_t *pgctx;
- dd("entering: \"$postgres_row_count\"");
+ dd("entering: \"$postgres_rows\"");
pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
@@ -93,6 +93,37 @@ ngx_postgres_variable_row_count(ngx_http_request_t *r,
}
ngx_int_t
+ngx_postgres_variable_affected(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_postgres_ctx_t *pgctx;
+
+ dd("entering: \"$postgres_affected\"");
+
+ pgctx = ngx_http_get_module_ctx(r, ngx_postgres_module);
+
+ if ((pgctx == NULL) || (pgctx->var_affected == NGX_ERROR)) {
+ v->not_found = 1;
+ dd("returning NGX_OK");
+ return NGX_OK;
+ }
+
+ v->data = ngx_pnalloc(r->pool, NGX_INT32_LEN);
+ if (v->data == NULL) {
+ dd("returning NGX_ERROR");
+ return NGX_ERROR;
+ }
+
+ v->len = ngx_sprintf(v->data, "%i", pgctx->var_affected) - v->data;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+
+ dd("returning NGX_OK");
+ return NGX_OK;
+}
+
+ngx_int_t
ngx_postgres_variable_query(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
View
6 src/ngx_postgres_variable.h
@@ -34,9 +34,11 @@
#include "ngx_postgres_module.h"
-ngx_int_t ngx_postgres_variable_column_count(ngx_http_request_t *,
+ngx_int_t ngx_postgres_variable_columns(ngx_http_request_t *,
ngx_http_variable_value_t *, uintptr_t);
-ngx_int_t ngx_postgres_variable_row_count(ngx_http_request_t *,
+ngx_int_t ngx_postgres_variable_rows(ngx_http_request_t *,
+ ngx_http_variable_value_t *, uintptr_t);
+ngx_int_t ngx_postgres_variable_affected(ngx_http_request_t *,
ngx_http_variable_value_t *, uintptr_t);
ngx_int_t ngx_postgres_variable_query(ngx_http_request_t *,
ngx_http_variable_value_t *, uintptr_t);
View
4 test/t/output.t
@@ -29,7 +29,7 @@ __DATA__
GET /postgres
--- error_code: 200
--- response_headers
-Content-Type:
+! Content-Type
--- response_body eval
""
--- timeout: 10
@@ -292,7 +292,7 @@ Content-Type: text/plain
GET /postgres
--- error_code: 200
--- response_headers
-Content-Type:
+! Content-Type
--- response_body eval
""
--- timeout: 10
View
127 test/t/variables.t
@@ -5,7 +5,7 @@ use Test::Nginx::Socket;
repeat_each(2);
-plan tests => repeat_each() * (blocks() * 3 + 1 * 3 + 1 * 1 - 3 * 2);
+plan tests => repeat_each() * (blocks() * 3 + 1 * 4 + 1 * 1 - 3 * 2);
worker_connections(128);
run_tests();
@@ -15,8 +15,6 @@ no_diff();
__DATA__
=== TEST 1: sanity
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -58,8 +56,6 @@ X-Test: test
=== TEST 2: out-of-range value (optional)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -76,14 +72,12 @@ GET /postgres
--- error_code: 200
--- response_headers
Content-Type: application/x-resty-dbd-stream
-X-Test:
+! X-Test
--- timeout: 10
=== TEST 3: NULL value (optional)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -100,14 +94,12 @@ GET /postgres
--- error_code: 200
--- response_headers
Content-Type: application/x-resty-dbd-stream
-X-Test:
+! X-Test
--- timeout: 10
=== TEST 4: zero-length value (optional)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -124,14 +116,12 @@ GET /postgres
--- error_code: 200
--- response_headers
Content-Type: application/x-resty-dbd-stream
-X-Test:
+! X-Test
--- timeout: 10
=== TEST 5: out-of-range value (required)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -151,8 +141,6 @@ GET /postgres
=== TEST 6: NULL value (required)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -172,8 +160,6 @@ GET /postgres
=== TEST 7: zero-length value (required)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -192,9 +178,7 @@ GET /postgres
-=== TEST 8: $postgres_column_count
-little-endian systems only
-
+=== TEST 8: $postgres_columns
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -203,7 +187,7 @@ little-endian systems only
location /postgres {
postgres_pass database;
postgres_query "select 'a', 'b', 'c'";
- add_header "X-Columns" $postgres_column_count;
+ add_header "X-Columns" $postgres_columns;
}
--- request
GET /postgres
@@ -215,9 +199,7 @@ X-Columns: 3
-=== TEST 9: $postgres_row_count
-little-endian systems only
-
+=== TEST 9: $postgres_rows
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -226,7 +208,7 @@ little-endian systems only
location /postgres {
postgres_pass database;
postgres_query "select 'a', 'b', 'c'";
- add_header "X-Rows" $postgres_row_count;
+ add_header "X-Rows" $postgres_rows;
}
--- request
GET /postgres
@@ -239,8 +221,6 @@ X-Rows: 1
=== TEST 10: $postgres_query (simple value)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -262,8 +242,6 @@ X-Query: select 'test' as echo
=== TEST 11: $postgres_query (simple value)
-little-endian systems only
-
--- http_config
upstream database {
postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
@@ -285,14 +263,13 @@ X-Query: select 'GET' as echo
=== TEST 12: variables used in non-ngx_postgres location
-little-endian systems only
-
--- http_config
--- config
location /etc {
root /;
- add_header "X-Columns" $postgres_column_count;
- add_header "X-Rows" $postgres_row_count;
+ add_header "X-Columns" $postgres_columns;
+ add_header "X-Rows" $postgres_rows;
+ add_header "X-Affected" $postgres_affected;
add_header "X-Query" $postgres_query;
postgres_set $pg 0 0 required;
add_header "X-Custom" $pg;
@@ -302,8 +279,82 @@ GET /etc/passwd
--- error_code: 200
--- response_headers
Content-Type: text/plain
-X-Columns:
-X-Rows:
-X-Query:
-X-Custom:
+! X-Columns
+! X-Rows
+! X-Affected
+! X-Query
+! X-Custom
+--- timeout: 10
+
+
+
+=== TEST 13: $postgres_affected (SELECT)
+--- http_config
+ upstream database {
+ postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
+ }
+--- config
+ location /postgres {
+ postgres_pass database;
+ postgres_query "select '$request_method' as echo";
+ add_header "X-Affected" $postgres_affected;
+ }
+--- request
+GET /postgres
+--- error_code: 200
+--- response_headers
+Content-Type: application/x-resty-dbd-stream
+! X-Affected
+--- timeout: 10
+
+
+
+=== TEST 14: $postgres_affected (UPDATE, no changes)
+db init:
+
+create table cats (id integer, name text);
+insert into cats (id) values (2);
+insert into cats (id, name) values (3, 'bob');
+--- http_config
+ upstream database {
+ postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
+ }
+--- config
+ location /postgres {
+ postgres_pass database;
+ postgres_query "update cats set id=3 where name='noone'";
+ add_header "X-Affected" $postgres_affected;
+ }
+--- request
+GET /postgres
+--- error_code: 200
+--- response_headers
+Content-Type: application/x-resty-dbd-stream
+X-Affected: 0
+--- timeout: 10
+
+
+
+=== TEST 15: $postgres_affected (UPDATE, one change)
+db init:
+
+create table cats (id integer, name text);
+insert into cats (id) values (2);
+insert into cats (id, name) values (3, 'bob');
+--- http_config
+ upstream database {
+ postgres_server 127.0.0.1 dbname=test user=monty password=some_pass;
+ }
+--- config
+ location /postgres {
+ postgres_pass database;
+ postgres_query "update cats set id=3 where name='bob'";
+ add_header "X-Affected" $postgres_affected;
+ }
+--- request
+GET /postgres
+--- error_code: 200
+--- response_headers
+Content-Type: application/x-resty-dbd-stream
+X-Affected: 1
--- timeout: 10
Please sign in to comment.
Something went wrong with that request. Please try again.