diff options
author | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-11-01 18:54:07 +0200 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org.ua> | 2014-11-01 18:54:07 +0200 |
commit | 46b13215f079139220d6886c9919a9a228d8d486 (patch) | |
tree | 970916a8cb8f97588833f7d7cd7c5650450f30fa | |
parent | b9f2a6f03404cc5d85240250f4f961775dc44648 (diff) | |
download | vmod-sql-46b13215f079139220d6886c9919a9a228d8d486.tar.gz vmod-sql-46b13215f079139220d6886c9919a9a228d8d486.tar.bz2 |
Support for VCL 4.0
* NEWS: Update.
* configure.ac: Version 1.0.90.
Detect Varnish version.
* doc/vmod-sql.3: Update.
* src/.gitignore: Update.
* src/Makefile.am: Always build vcc_if.c and vcc_if.h
* src/vmod-sql.c: Use Varnish datatypes; define them for v.3
(vmod_ntuples, vmod_nfields): New functions.
* src/vmod.vcc: Rewrite in VCL 4.0 format.
-rw-r--r-- | NEWS | 5 | ||||
-rw-r--r-- | configure.ac | 17 | ||||
-rw-r--r-- | doc/vmod-sql.3 | 6 | ||||
-rw-r--r-- | src/.gitignore | 2 | ||||
-rw-r--r-- | src/Makefile.am | 32 | ||||
-rw-r--r-- | src/vmod-sql.c | 71 | ||||
-rw-r--r-- | src/vmod.vcc | 87 |
7 files changed, 180 insertions, 40 deletions
@@ -1,10 +1,13 @@ -vmod-sql -- history of user-visible changes. 2013-10-19 +vmod-sql -- history of user-visible changes. 2014-11-01 Copyright (C) 2013-2014 Sergey Poznyakoff See the end of file for copying conditions. Please send vmod-sql bug reports to <gray@gnu.org> +Version 1.0.90 (git) + + Version 1.0, 2013-10-19 Initial release diff --git a/configure.ac b/configure.ac index 805cf5e..af14d73 100644 --- a/configure.ac +++ b/configure.ac @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with vmod-sql. If not, see <http://www.gnu.org/licenses/>. AC_PREREQ(2.69) -AC_INIT([vmod-sql], 1.0, [gray@gnu.org]) +AC_INIT([vmod-sql], 1.0.90, [gray@gnu.org]) AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR(src/vmod-sql.c) @@ -64,10 +64,17 @@ case "$VARNISHSRC" in esac VARNISHSRC=`cd $VARNISHSRC && pwd` +VARNISHVERSION= AC_CHECK_FILE([$VARNISHSRC/include/varnishapi.h], - [], - [AC_MSG_FAILURE(["$VARNISHSRC" is not a Varnish source directory])]) - + [VARNISHVERSION=3], + [AC_CHECK_FILE([$VARNISHSRC/include/vapi/vsm.h], + [VARNISHVERSION=4], + [AC_MSG_FAILURE(["$VARNISHSRC" is not a Varnish source directory])]) ]) + +AC_DEFINE_UNQUOTED([VARNISHVERSION],$VARNISHVERSION,[Varnish major version number]) +AM_CONDITIONAL([VARNISH3],[test $VARNISHVERSION -eq 3]) +AM_CONDITIONAL([VARNISH4],[test $VARNISHVERSION -eq 4]) + # Check that varnishtest is built in the varnish source directory AC_CHECK_FILE([$VARNISHSRC/bin/varnishtest/varnishtest], [], @@ -157,6 +164,7 @@ AC_CONFIG_COMMANDS([status],[ echo "" delim="-------------------------------------------------------------------" echo $delim | tr '-' '*' +echo "Building for Varnish version $VARNISHVERSION" echo "Backends to build:" res= for module in mysql pgsql @@ -174,6 +182,7 @@ case "$res" in AC_MSG_ERROR([Nothing to build?]) esac ],[ +VARNISHVERSION=$VARNISHVERSION build_pgsql=$build_pgsql build_mysql=$build_mysql]) diff --git a/doc/vmod-sql.3 b/doc/vmod-sql.3 index 864c6e1..446cfbc 100644 --- a/doc/vmod-sql.3 +++ b/doc/vmod-sql.3 @@ -13,7 +13,7 @@ .\" .\" You should have received a copy of the GNU General Public License .\" along with vmod-sql. If not, see <http://www.gnu.org/licenses/>. -.TH VMOD-SQL 1 "October 19, 2013" "VMOD-SQL" "User Reference" +.TH VMOD-SQL 1 "November 1, 2014" "VMOD-SQL" "User Reference" .SH NAME vmod-sql \- SQL access for Varnish Cache .SH SYNOPSIS @@ -132,7 +132,7 @@ descriptor will be the lowest-numbered descriptor not currently open for any connection. .PP The function -.B sql.connect0 +.B sql.connect_init is equivalent to .BR sql.connect , but succeeds only if the connection is assigned the descriptor @@ -146,7 +146,7 @@ closes an existing database connection identified by descriptor .PP The function .B sql.query -performs a database query given as its first argument. The second +performs a database query given in argument \fIquery\fR. \fIParams\fR argument is a list of variable assignments separated with semicolons, similarly to the \fIparams\fR argument described above. Each assignment has the form \fINAME\fB=\fIVALUE\fR. diff --git a/src/.gitignore b/src/.gitignore index 7f6e438..35d672c 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,2 +1,4 @@ vcc_if.c vcc_if.h +*.rst +*.vcc3 diff --git a/src/Makefile.am b/src/Makefile.am index 639ed51..fa9929d 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -14,7 +14,10 @@ # You should have received a copy of the GNU General Public License # along with vmod-sql. If not, see <http://www.gnu.org/licenses/>. -AM_CPPFLAGS = -I$(VARNISHSRC)/include -I$(VARNISHSRC) +AM_CPPFLAGS = \ + -I$(VARNISHSRC)/include\ + -I$(VARNISHSRC)/bin/varnishd\ + -I$(VARNISHSRC) vmoddir = $(VMODDIR) vmod_LTLIBRARIES = libvmod_sql.la @@ -26,7 +29,6 @@ libvmod_sql_la_SOURCES = \ sql.c\ vmod-sql.c\ vmod-sql.h\ - vcc_if.c vcc_if.h\ wordsplit.h\ wordsplit.c @@ -38,12 +40,28 @@ if USE_PGSQL libvmod_sql_la_SOURCES += pgsql.c endif -BUILT_SOURCES = vcc_if.c vcc_if.h +vmod-sql.lo: vcc_if.c vcc_if.h -vcc_if.c vcc_if.h: $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc - @PYTHON@ $(VARNISHSRC)/lib/libvmod_std/vmod.py $(top_srcdir)/src/vmod.vcc +CLEANFILES = vcc_if.c vcc_if.h *.rst -EXTRA_DIST = vmod.vcc +if VARNISH4 + vmodtool = $(VARNISHSRC)/lib/libvcc/vmodtool.py + vmodtoolargs = --strict + vccfile = $(top_srcdir)/src/vmod.vcc +else + vmodtool = $(VARNISHSRC)/lib/libvmod_std/vmod.py + vmodtoolargs = + vccfile = vmod.vcc3 + +vmod.vcc3: $(top_srcdir)/src/vmod.vcc + CLEANFILES += vmod.vcc3 +endif -CLEANFILES = $(builddir)/vcc_if.c $(builddir)/vcc_if.h +.vcc.vcc3: + sed -n '/^\$$/{s///;s/\(Module *[^ ][^ ]*\).*/\1/;p}' $< > $@ + +vcc_if.c vcc_if.h: $(vmodtool) $(vccfile) + @PYTHON@ $(vmodtool) $(vmodtoolargs) $(vccfile) + +EXTRA_DIST = vmod.vcc diff --git a/src/vmod-sql.c b/src/vmod-sql.c index 2857f1f..c16e11c 100644 --- a/src/vmod-sql.c +++ b/src/vmod-sql.c @@ -19,7 +19,21 @@ #include "wordsplit.h" #include "vrt.h" #include "vcc_if.h" -#include "bin/varnishd/cache.h" +#include "pthread.h" +#if VARNISHVERSION == 3 +# include "bin/varnishd/cache.h" +# define VCL_VOID void +# define VCL_INT int +# define VCL_BOOL unsigned +# define VCL_STRING const char * +# define WS_Copy(w,s,l) WS_Dup(w,l) +# define MOD_CTX struct sess * +# define WSPTR(s) ((s)->wrk->ws) +#else +# include "bin/varnishd/cache/cache.h" +# define MOD_CTX const struct vrt_ctx * +# define WSPTR(s) ((s)->ws) +#endif static pthread_once_t thread_once = PTHREAD_ONCE_INIT; static pthread_key_t thread_key; @@ -155,9 +169,9 @@ find_backend(const char *name) return NULL; } -int -vmod_connect(struct sess *sp, struct vmod_priv *priv, const char *bkname, - const char *param) +VCL_INT +vmod_connect(MOD_CTX ctx, struct vmod_priv *priv, + VCL_STRING bkname, VCL_STRING param) { struct wordsplit ws; struct vmod_sql_backend *be; @@ -191,15 +205,15 @@ vmod_connect(struct sess *sp, struct vmod_priv *priv, const char *bkname, return n; } -void -vmod_connect_init(struct sess *sp, struct vmod_priv *priv, const char *bkname, - const char *param) +VCL_VOID +vmod_connect_init(MOD_CTX ctx, struct vmod_priv *priv, + VCL_STRING bkname, VCL_STRING param) { - AZ(vmod_connect(sp, priv, bkname, param)); + AZ(vmod_connect(ctx, priv, bkname, param)); } static struct vmod_sql_connection * -runquery(struct sess *sp, int n, const char *query, const char *arg) +runquery(MOD_CTX ctx, int n, const char *query, const char *arg) { struct vmod_sql_connection *cp; struct wordsplit ws, wsenv; @@ -256,15 +270,16 @@ runquery(struct sess *sp, int n, const char *query, const char *arg) return cp; } -unsigned -vmod_query(struct sess *sp, struct vmod_priv *priv, int cd, const char *query, - const char *arg) +VCL_BOOL +vmod_query(MOD_CTX ctx, struct vmod_priv *priv, + VCL_INT cd, VCL_STRING query, VCL_STRING arg) { - return !runquery(sp, cd, query, arg); + return !runquery(ctx, cd, query, arg); } -const char * -vmod_result(struct sess *sp, struct vmod_priv *priv, int cd, int row, int col) +VCL_STRING +vmod_result(MOD_CTX ctx, struct vmod_priv *priv, + VCL_INT cd, VCL_INT row, VCL_INT col) { struct vmod_sql_connection *cp; const char *s; @@ -277,11 +292,11 @@ vmod_result(struct sess *sp, struct vmod_priv *priv, int cd, int row, int col) if (!s) return NULL; - return WS_Dup(sp->ws, s); + return WS_Copy(WSPTR(ctx), s, -1); } -int -vmod_affected(struct sess *sp, struct vmod_priv *priv, int cd) +VCL_INT +vmod_affected(MOD_CTX ctx, struct vmod_priv *priv, VCL_INT cd) { struct vmod_sql_connection *cp; @@ -291,4 +306,24 @@ vmod_affected(struct sess *sp, struct vmod_priv *priv, int cd) return i_modsql_affected_rows(cp); } +VCL_INT +vmod_ntuples(MOD_CTX ctx, struct vmod_priv *priv, VCL_INT cd) +{ + struct vmod_sql_connection *cp; + + cp = i_modsql_conntab_get(cd); + if (!cp) + return 0; + return i_modsql_num_tuples(cp); +} +VCL_INT +vmod_nfields(MOD_CTX ctx, struct vmod_priv *priv, VCL_INT cd) +{ + struct vmod_sql_connection *cp; + + cp = i_modsql_conntab_get(cd); + if (!cp) + return 0; + return i_modsql_num_fields(cp); +} diff --git a/src/vmod.vcc b/src/vmod.vcc index 46d4bdf..57869e0 100644 --- a/src/vmod.vcc +++ b/src/vmod.vcc @@ -1,7 +1,80 @@ -Module sql -Init module_init -Function INT connect(PRIV_VCL, STRING, STRING) -Function VOID connect_init(PRIV_VCL, STRING, STRING) -Function BOOL query(PRIV_VCL, INT, STRING, STRING) -Function STRING result(PRIV_VCL, INT, INT, INT) -Function INT affected(PRIV_VCL, INT) +# This file is part of vmod-sql +# Copyright (C) 2013-2014 Sergey Poznyakoff +# +# Vmod-sql 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-sql 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-sql. If not, see <http://www.gnu.org/licenses/>. + +$Module sql 3 SQL access functions for Varnish Cache + +COLOPHON +======== +This document provides a short description of the **sql** module. +For a detailed documentation, please see the vmod-sql(3) manual page. + +You will find documentation sources in subdirectory **doc** of the +**vmod-sql** source tree. + +DESCRIPTION +=========== +Vmod-sql provides functions for accessing SQL databases from Varnish +configuration files. It supports MySQL and PostgreSQL. + +$Init module_init +$Function INT connect(PRIV_VCL, STRING, STRING) + +Description + Initiates a connection to the database. Returns integer value, + which should be passed as first argument to another module's + functions to identify the connection to operate upon. + +$Function VOID connect_init(PRIV_VCL, STRING, STRING) + +Description + Same as **sql.connect**, but succeeds only if the connection is + assigned the descriptor 0. This function is provided as a shortcut + to use when only one database connection is needed. + +$Function BOOL query(PRIV_VCL, INT, STRING, STRING) + +Description + Performs a database query given as its second argument. Third + argument is a list of variable assignments separated with semicolons. + Each assignment has the form *NAME*=*VALUE*. + + Before being executed, the query is expanded by replacing each + occurrence of **$NAME** construct (a variable reference) with the + corresponding *VALUE* from the third argument. Similarly to the + shell syntax, the variable reference can be written as **${NAME}**. + +$Function STRING result(PRIV_VCL, INT, INT, INT) + +Description + Returns the value of the column identified by arguments 2 and 3 + (row and column numbers). + +$Function INT affected(PRIV_VCL, INT) + +Description + If the most recent query updated the database, returns the number + of affected rows. + +$Function INT ntuples(PRIV_VCL, INT) + +Description + Returns the number of tuples (rows) returned by the query. + +$Function INT nfields(PRIV_VCL, INT) + +Description + Returns the number of fields in each tuple returned by the + query. |