Permalink
Browse files

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

…es_columns" and "$postgres_row_count" to "$postgres_rows".
  • Loading branch information...
1 parent 1274e68 commit 12c2710f2ae12cad3dcb8275d9e5dce069c409aa @PiotrSikora PiotrSikora committed Jun 23, 2010
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.
@@ -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
@@ -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,
@@ -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;
@@ -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;
@@ -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);
@@ -92,6 +92,37 @@ ngx_postgres_variable_row_count(ngx_http_request_t *r,
return NGX_OK;
}
+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)
@@ -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
@@ -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
Oops, something went wrong.

0 comments on commit 12c2710

Please sign in to comment.