|
@@ -0,0 +1,240 @@
|
|
|
+<?xml version='1.0'?>
|
|
|
+<!--
|
|
|
+ * $Id: db_berkeley.xsl 3142 2007-11-15 14:09:15Z henningw $
|
|
|
+ *
|
|
|
+ * XSL converter script for generating module parameter templates.
|
|
|
+ *
|
|
|
+ * Copyright (C) 2008 Henning Westerholt
|
|
|
+ *
|
|
|
+ * This file is part of Kamailio, a free SIP server.
|
|
|
+ *
|
|
|
+ * Kamailio 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 2 of the License, or
|
|
|
+ * (at your option) any later version
|
|
|
+ *
|
|
|
+ * Kamailio 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 this program; if not, write to the Free Software
|
|
|
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
+ *
|
|
|
+ */
|
|
|
+-->
|
|
|
+
|
|
|
+<!--
|
|
|
+ * Yes, this is probably not pretty, as XSL is not the perfect tool to generate
|
|
|
+ * C code. But we've now this infrastructure for the databases, and it works.
|
|
|
+-->
|
|
|
+
|
|
|
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
|
|
|
+ version='1.0'
|
|
|
+ xmlns:xi="http://www.w3.org/2001/XInclude">
|
|
|
+
|
|
|
+<xsl:import href="common.xsl"/>
|
|
|
+<xsl:output method="text" indent="no" omit-xml-declaration="yes"/>
|
|
|
+
|
|
|
+ <!-- Create the file for the tables in module subdirectory -->
|
|
|
+<xsl:template match="/">
|
|
|
+ <xsl:variable name="createfile" select="concat($dir, concat('/', concat('db_', $prefix, '.c')))"/>
|
|
|
+ <xsl:document href="{$createfile}" method="text" indent="no" omit-xml-declaration="yes">
|
|
|
+
|
|
|
+<!-- doxygen header -->
|
|
|
+<xsl:text>
|
|
|
+/*!
|
|
|
+ * \file
|
|
|
+ * \ingroup db
|
|
|
+ * \brief Database support for modules.
|
|
|
+ *
|
|
|
+ * Database support functions for modules.
|
|
|
+ *
|
|
|
+ * @cond
|
|
|
+ * WARNING:
|
|
|
+ * This file was autogenerated from the XML source file
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat(' * ', $dir, 'kamailio-', $prefix, '.xml.')"/>
|
|
|
+<xsl:text>
|
|
|
+ * It can be regenerated by running 'make modules' in the db/schema
|
|
|
+ * directory of the source code. You need to have xsltproc and
|
|
|
+ * docbook-xsl stylesheets installed.
|
|
|
+ * ALL CHANGES DONE HERE WILL BE LOST IF THE FILE IS REGENERATED
|
|
|
+ * @endcond
|
|
|
+ */
|
|
|
+
|
|
|
+</xsl:text>
|
|
|
+
|
|
|
+<xsl:value-of select="concat('#include "', 'db_', $prefix, '.h"

')"/>
|
|
|
+
|
|
|
+<xsl:text>/* database variables */
</xsl:text>
|
|
|
+<xsl:text>/* TODO assign read-write or read-only URI, introduce a parameter in XML */

</xsl:text>
|
|
|
+<xsl:value-of select="concat('//extern str ', $prefix, '_db_url;
')"/>
|
|
|
+<xsl:value-of select="concat('db_con_t * ', $prefix, '_dbh = NULL;
')"/>
|
|
|
+<xsl:value-of select="concat('db_func_t ', $prefix, '_dbf;

')"/>
|
|
|
+
|
|
|
+<xsl:apply-templates select="/database[1]"/>
|
|
|
+
|
|
|
+<xsl:text>
|
|
|
+/*
|
|
|
+ * Closes the DB connection.
|
|
|
+ */
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('void ', $prefix, '_db_close(void) {
')"/>
|
|
|
+<xsl:value-of select="concat('	if (', $prefix, '_dbh) {
')"/>
|
|
|
+<xsl:value-of select="concat('		', $prefix, '_dbf.close(', $prefix, '_dbh);
')"/>
|
|
|
+<xsl:value-of select="concat('		', $prefix, '_dbh = NULL;
	}
}

')"/>
|
|
|
+
|
|
|
+<xsl:text>
|
|
|
+/*!
|
|
|
+ * Initialises the DB API, check the table version and closes the connection.
|
|
|
+ * This should be called from the mod_init function.
|
|
|
+ *
|
|
|
+ * \return 0 means ok, -1 means an error occured.
|
|
|
+ */
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('int ', $prefix, '_db_init(void) {
')"/>
|
|
|
+<xsl:value-of select="concat('	if (!', $prefix, '_db_url.s || !', $prefix, '_db_url.len) {')"/>
|
|
|
+<xsl:text>
|
|
|
+ LM_ERR("you have to set the db_url module parameter.\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('	if (db_bind_mod(&', $prefix, '_db_url, &', $prefix, '_dbf) < 0) {')"/>
|
|
|
+<xsl:text>
|
|
|
+ LM_ERR("can't bind database module.\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('	if ((', $prefix, '_dbh = ', $prefix, '_dbf.init(&', $prefix, '_db_url)) == NULL) {')"/>
|
|
|
+<xsl:text>
|
|
|
+ LM_ERR("can't connect to database.\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+</xsl:text>
|
|
|
+<xsl:text> if (
|
|
|
+</xsl:text>
|
|
|
+
|
|
|
+<!-- create the table version check -->
|
|
|
+<xsl:for-each select="//database[1]/table">
|
|
|
+ <xsl:variable name="table">
|
|
|
+ <xsl:call-template name="get-name">
|
|
|
+ </xsl:call-template>
|
|
|
+ </xsl:variable>
|
|
|
+ <xsl:value-of select="concat('	(db_check_table_version(&', $prefix, '_dbf, ', $prefix, '_dbh, &', $table, '_table, ', $table, '_version) < 0)')"/>
|
|
|
+ <xsl:if test="not(position()=last())">
|
|
|
+ <xsl:text> ||
</xsl:text>
|
|
|
+ </xsl:if>
|
|
|
+</xsl:for-each>
|
|
|
+<xsl:text>
|
|
|
+ ) {
|
|
|
+ LM_ERR("during table version check.\n");
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('		', $prefix, '_db_close();')"/>
|
|
|
+<xsl:text>
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('	', $prefix, '_db_close();')"/>
|
|
|
+<xsl:text>
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+</xsl:text>
|
|
|
+
|
|
|
+<xsl:text>
|
|
|
+/*!
|
|
|
+ * Initialize the DB connection without checking the table version and DB URL.
|
|
|
+ * This should be called from child_init. An already existing database
|
|
|
+ * connection will be closed, and a new one created.
|
|
|
+ *
|
|
|
+ * \return 0 means ok, -1 means an error occured.
|
|
|
+ */
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('int ', $prefix, '_db_open(void) {
')"/>
|
|
|
+<xsl:value-of select="concat('	if (', $prefix, '_dbh) {
')"/>
|
|
|
+<xsl:value-of select="concat('		', $prefix, '_dbf.close(', $prefix, '_dbh);
	}
')"/>
|
|
|
+<xsl:value-of select="concat('	if ((', $prefix, '_dbh = ', $prefix, '_dbf.init(&', $prefix, '_db_url)) == NULL) {')"/>
|
|
|
+<xsl:text>
|
|
|
+ LM_ERR("can't connect to database.\n");
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+</xsl:text>
|
|
|
+
|
|
|
+<xsl:text>
|
|
|
+/*!
|
|
|
+ * Update the variable length after eventual assignments from the config script.
|
|
|
+ * This is necessary because we're using the 'str' type.
|
|
|
+ */
|
|
|
+</xsl:text>
|
|
|
+<xsl:value-of select="concat('void ', $prefix, '_db_vars(void) {
')"/>
|
|
|
+<!-- TODO remove if case, if db_url is also set from this code -->
|
|
|
+<xsl:value-of select="concat('	if (', $prefix, '_db_url.s) ', $prefix, '_db_url.len = strlen(', $prefix, '_db_url.s);
')"/>
|
|
|
+
|
|
|
+<xsl:for-each select="//database[1]/table">
|
|
|
+ <xsl:variable name="table.name">
|
|
|
+ <xsl:call-template name="get-name">
|
|
|
+ </xsl:call-template>
|
|
|
+ </xsl:variable>
|
|
|
+ <xsl:value-of select="concat('	', $table.name, '_table.len = strlen(', $table.name, '_table.s',');
')"/>
|
|
|
+ <xsl:for-each select="column">
|
|
|
+ <xsl:variable name="column.name">
|
|
|
+ <xsl:call-template name="get-name"/>
|
|
|
+ </xsl:variable>
|
|
|
+ <xsl:value-of select="concat('	', $table.name, '_', $column.name, '_col.len = strlen(', $table.name, '_', $column.name, '_col.s', ');
')"/>
|
|
|
+ </xsl:for-each>
|
|
|
+</xsl:for-each>
|
|
|
+<xsl:text>}
</xsl:text>
|
|
|
+ <xsl:text>
</xsl:text>
|
|
|
+
|
|
|
+</xsl:document>
|
|
|
+</xsl:template>
|
|
|
+
|
|
|
+ <!-- create table name parameter -->
|
|
|
+ <xsl:template match="table">
|
|
|
+ <xsl:variable name="name">
|
|
|
+ <xsl:call-template name="get-name"/>
|
|
|
+ </xsl:variable>
|
|
|
+
|
|
|
+ <xsl:value-of select="concat('str ', $name, '_table = str_init("', $name, '");
')"/>
|
|
|
+ <xsl:text>
</xsl:text>
|
|
|
+
|
|
|
+ <xsl:text>/* column names */
</xsl:text>
|
|
|
+ <xsl:apply-imports/>
|
|
|
+
|
|
|
+ <xsl:text>
</xsl:text>
|
|
|
+ <xsl:text>/* table version */
</xsl:text>
|
|
|
+ <xsl:apply-templates select="version"/>
|
|
|
+ </xsl:template>
|
|
|
+
|
|
|
+ <!-- create version parameter -->
|
|
|
+ <xsl:template match="version">
|
|
|
+ <xsl:variable name="table.name">
|
|
|
+ <xsl:call-template name="get-name">
|
|
|
+ <xsl:with-param name="select" select="parent::table"/>
|
|
|
+ </xsl:call-template>
|
|
|
+ </xsl:variable>
|
|
|
+ <xsl:value-of select="concat('const unsigned int ', $table.name, '_version', ' = ')"/>
|
|
|
+ <xsl:value-of select="text()"/>
|
|
|
+ <xsl:text>;
</xsl:text>
|
|
|
+ <xsl:text>
</xsl:text>
|
|
|
+ </xsl:template>
|
|
|
+
|
|
|
+ <!-- Create column parameter -->
|
|
|
+ <xsl:template match="column">
|
|
|
+ <xsl:variable name="table.name">
|
|
|
+ <xsl:call-template name="get-name">
|
|
|
+ <xsl:with-param name="select" select="parent::table"/>
|
|
|
+ </xsl:call-template>
|
|
|
+ </xsl:variable>
|
|
|
+ <xsl:variable name="column.name">
|
|
|
+ <xsl:call-template name="get-name"/>
|
|
|
+ </xsl:variable>
|
|
|
+ <xsl:value-of select="concat('str ', $table.name, '_', $column.name, '_col = str_init("', $column.name, '");
')"/>
|
|
|
+ </xsl:template>
|
|
|
+
|
|
|
+</xsl:stylesheet>
|