[sr-dev] git:tteras/db_sqlite: db_sqlite: add support for autogeneration of db scheme (make dbschema)

Timo Teras timo.teras at iki.fi
Thu May 26 16:08:00 CEST 2011


Module: sip-router
Branch: tteras/db_sqlite
Commit: 9b4f2e9912d70b75f02dede15d1e9a833bf66d8d
URL:    http://git.sip-router.org/cgi-bin/gitweb.cgi/sip-router/?a=commit;h=9b4f2e9912d70b75f02dede15d1e9a833bf66d8d

Author: Henning Westerholt <henning.westerholt at 1und1.de>
Committer: Timo Teräs <timo.teras at iki.fi>
Date:   Thu May 26 12:18:16 2011 +0200

db_sqlite: add support for autogeneration of db scheme (make dbschema)

---

 doc/stylesheets/dbschema_k/xsl/db_sqlite.xsl |  133 ++++++++++++++++++++++++++
 lib/srdb1/schema/Makefile                    |   20 ++++-
 2 files changed, 151 insertions(+), 2 deletions(-)

diff --git a/doc/stylesheets/dbschema_k/xsl/db_sqlite.xsl b/doc/stylesheets/dbschema_k/xsl/db_sqlite.xsl
new file mode 100644
index 0000000..1a42bc2
--- /dev/null
+++ b/doc/stylesheets/dbschema_k/xsl/db_sqlite.xsl
@@ -0,0 +1,133 @@
+<?xml version='1.0'?>
+<!--
+ * XSL converter script for sqlite databases
+ *
+ * Copyright (C) 2001-2007 FhG Fokus
+ *
+ * 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
+ *
+ */
+-->
+
+
+<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="sql.xsl"/>
+
+<!-- specify the table type -->
+    <xsl:template name="table.close">
+	<xsl:text>)</xsl:text>
+	<xsl:if test="type[@db=$db]">
+	    <xsl:text> Type=</xsl:text>
+	    <xsl:value-of select="normalize-space(type[@db=$db])"/>
+	</xsl:if>
+	<xsl:text>;&#x0A;&#x0A;</xsl:text>
+    </xsl:template>
+
+    <xsl:template name="column.type">
+	<xsl:variable name="type">
+	    <xsl:call-template name="get-type"/>
+	</xsl:variable>
+
+	<xsl:choose>
+	    <xsl:when test="type[@db=$db]">
+		<xsl:value-of select="normalize-space(type[@db=$db])"/>
+	    </xsl:when>
+	    <xsl:when test="$type='char'">
+		<xsl:text>SMALLINT</xsl:text>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='short'">
+		<xsl:text>SMALLINT</xsl:text>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='int'">
+			<xsl:if test="not(autoincrement)">
+				<xsl:text>INTEGER</xsl:text>
+			</xsl:if>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='long'">
+		<xsl:text>BIGINT</xsl:text>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='datetime'">
+		<xsl:text>TIMESTAMP</xsl:text>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='double'">
+		<xsl:text>DOUBLE PRECISION</xsl:text>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='float'">
+		<xsl:text>REAL</xsl:text>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='string'">
+		<xsl:text>VARCHAR</xsl:text>
+		<xsl:call-template name="column.size"/>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='binary'">
+		<xsl:text>BYTEA</xsl:text>
+		<xsl:call-template name="column.size"/>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:when test="$type='text'">
+		<xsl:text>TEXT</xsl:text>
+		<xsl:call-template name="column.size"/>
+		<xsl:call-template name="column.trailing"/>
+	    </xsl:when>
+	    <xsl:otherwise>
+		<xsl:call-template name="type-error"/>
+	    </xsl:otherwise>
+	</xsl:choose>
+	</xsl:template>
+
+	<xsl:template name="column.trailing">
+	<xsl:variable name="column.type">
+	    <xsl:call-template name="get-type"/>
+	</xsl:variable>
+	<xsl:if test="$column.type='datetime'">
+	    <xsl:text> WITHOUT TIME ZONE</xsl:text>
+	</xsl:if>
+	<xsl:if test="autoincrement">
+		<xsl:text>INTEGER</xsl:text>
+	</xsl:if>
+	<!-- PRIMARY KEY column definition -->
+	<xsl:if test="primary">
+		<xsl:text> PRIMARY KEY</xsl:text>
+	</xsl:if>
+	</xsl:template>
+
+	<xsl:template name="get-index-name">
+	<xsl:variable name="index.name">
+	    <xsl:call-template name="get-name"/>
+	</xsl:variable>
+	<xsl:variable name="table.name">
+	    <xsl:call-template name="get-name">
+		<xsl:with-param name="select" select="parent::table"/>
+	    </xsl:call-template>
+	</xsl:variable>
+	<!-- because postgres don't like identical index names, even on table level -->
+	<xsl:value-of select="concat($table.name, '_', $index.name)"/>
+	</xsl:template>
+
+</xsl:stylesheet>
diff --git a/lib/srdb1/schema/Makefile b/lib/srdb1/schema/Makefile
index 276ba16..5fbeecd 100644
--- a/lib/srdb1/schema/Makefile
+++ b/lib/srdb1/schema/Makefile
@@ -21,6 +21,9 @@ DBTEXT_XSL = $(STYLESHEETS)/dbtext.xsl
 # Stylesheet used to generate berkeley database schema
 DB_BERKELEY_XSL = $(STYLESHEETS)/db_berkeley.xsl
 
+# Stylesheet used to generate sqlite database schema
+DB_SQLITE_XSL = $(STYLESHEETS)/db_sqlite.xsl
+
 # Stylesheet used to generate oracle database schema
 ORACLE_XSL = $(STYLESHEETS)/oracle.xsl
 
@@ -56,7 +59,7 @@ ifeq ($(VERBOSE), 1)
 	override XSLTPROC := $(XSLTPROC) --verbose
 endif
 
-all: mysql postgres dbtext db_berkeley docbook oracle #modules dbdoc
+all: mysql postgres dbtext db_berkeley db_sqlite docbook oracle #modules dbdoc
 
 .PHONY: mysql mysql_clean
 mysql:
@@ -148,6 +151,19 @@ db_berkeley_clean:
 	- at rm -f $(SCHEME)/db_berkeley/kamailio/*
 
 
+.PHONY: db_sqlite db_sqlite_clean
+db_sqlite:
+	for FILE in $(TABLES); do \
+		XML_CATALOG_FILES=$(CATALOG) $(XSLTPROC) $(XSLTPROC_FLAGS) \
+		--stringparam dir "$(SCHEME)/db_sqlite" \
+		--stringparam prefix "$$FILE-" \
+		--stringparam db "db_sqlite" \
+		$(DB_SQLITE_XSL) kamailio-"$$FILE".xml ; \
+	done
+
+db_sqlite_clean:
+	- at rm -f $(SCHEME)/db_sqlite/*
+
 .PHONY: docbook docbook_clean
 docbook:
 	for FILE in $(TABLES); do \
@@ -219,4 +235,4 @@ dbdoc_clean:
 	done
 
 .PHONY: clean
-clean: mysql_clean postgres_clean oracle_clean dbtext_clean db_berkeley_clean docbook_clean # modules_clean dbdoc_clean
+clean: mysql_clean postgres_clean oracle_clean dbtext_clean db_berkeley_clean db_sqlite_clean docbook_clean # modules_clean dbdoc_clean




More information about the sr-dev mailing list