Warning, /pim/akonadi/src/server/storage/entities.xsl is written in an unsupported language. File is not indexed.

0001 <!--
0002     SPDX-FileCopyrightText: 2006-2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 -->
0006 
0007 <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
0008                 version="1.0">
0009 <xsl:output method="text" encoding="utf-8"/>
0010 
0011 <xsl:include href="entities-header.xsl"/>
0012 <xsl:include href="entities-source.xsl"/>
0013 
0014 <!-- select whether to generate header or implementation code. -->
0015 <xsl:param name="code">header</xsl:param>
0016 
0017 <xsl:template match="/">
0018 /*
0019  * This is an auto-generated file.
0020  * Do not edit! All changes made to it will be lost.
0021  */
0022 
0023 <!-- header generation -->
0024 <xsl:if test="$code='header'">
0025 #ifndef AKONADI_ENTITIES_H
0026 #define AKONADI_ENTITIES_H
0027 #include "storage/entity.h"
0028 
0029 #include &lt;private/tristate_p.h&gt;
0030 
0031 #include &lt;QtCore/QDebug&gt;
0032 #include &lt;QtCore/QSharedDataPointer&gt;
0033 #include &lt;QtCore/QString&gt;
0034 #include &lt;QtCore/QVariant&gt;
0035 #include &lt;QtCore/QStringList&gt;
0036 template &lt;typename T&gt; class QList;
0037 
0038 class QSqlQuery;
0039 
0040 namespace Akonadi {
0041 namespace Server {
0042 
0043 // forward declaration for table classes
0044 <xsl:for-each select="database/table">
0045 class <xsl:value-of select="@name"/>;
0046 </xsl:for-each>
0047 
0048 // forward declaration for relation classes
0049 <xsl:for-each select="database/relation">
0050 class <xsl:value-of select="@table1"/><xsl:value-of select="@table2"/>Relation;
0051 </xsl:for-each>
0052 
0053 <xsl:for-each select="database/table">
0054 <xsl:call-template name="table-header"/>
0055 </xsl:for-each>
0056 
0057 <xsl:for-each select="database/relation">
0058 <xsl:call-template name="relation-header"/>
0059 </xsl:for-each>
0060 
0061 /** Returns a list of all table names. */
0062 QList&lt;QString&gt; allDatabaseTables();
0063 
0064 } // namespace Server
0065 } // namespace Akonadi
0066 
0067 <xsl:for-each select="database/table">
0068 <xsl:call-template name="table-debug-header"/>
0069 </xsl:for-each>
0070 
0071 <xsl:for-each select="database/table">
0072 Q_DECLARE_TYPEINFO(Akonadi::Server::<xsl:value-of select="@name"/>, Q_RELOCATABLE_TYPE);
0073 </xsl:for-each>
0074 #endif
0075 
0076 </xsl:if>
0077 
0078 <!-- cpp generation -->
0079 <xsl:if test="$code='source'">
0080 #include &lt;entities.h&gt;
0081 #include &lt;storage/datastore.h&gt;
0082 #include &lt;storage/selectquerybuilder.h&gt;
0083 #include &lt;utils.h&gt;
0084 #include &lt;akonadiserver_debug.h&gt;
0085 
0086 #include &lt;QSqlDatabase&gt;
0087 #include &lt;QSqlQuery&gt;
0088 #include &lt;QSqlError&gt;
0089 #include &lt;QSqlDriver&gt;
0090 #include &lt;QVariant&gt;
0091 #include &lt;QHash&gt;
0092 #include &lt;QMutex&gt;
0093 #include &lt;QThread&gt;
0094 
0095 using namespace Akonadi;
0096 using namespace Akonadi::Server;
0097 
0098 static QStringList removeEntry(QStringList list, const QString&amp; entry)
0099 {
0100     list.removeOne(entry);
0101     return list;
0102 }
0103 
0104 <xsl:for-each select="database/table">
0105 <xsl:call-template name="table-source"/>
0106 </xsl:for-each>
0107 
0108 <xsl:for-each select="database/relation">
0109 <xsl:call-template name="relation-source"/>
0110 </xsl:for-each>
0111 
0112 QList&lt;QString&gt; Akonadi::Server::allDatabaseTables()
0113 {
0114     static const QList&lt;QString&gt; allTables = {
0115     <xsl:for-each select="database/table">
0116         QStringLiteral("<xsl:value-of select="@name"/>Table"),
0117     </xsl:for-each>
0118     <xsl:for-each select="database/relation">
0119         QStringLiteral("<xsl:value-of select="@table1"/><xsl:value-of select="@table2"/>Relation")
0120         <xsl:if test="position() != last()">,</xsl:if>
0121     </xsl:for-each>
0122     };
0123 
0124     return allTables;
0125 }
0126 
0127 </xsl:if>
0128 
0129 </xsl:template>
0130 
0131 
0132 <!-- Helper templates -->
0133 
0134 <!-- uppercase first letter -->
0135 <xsl:template name="uppercase-first">
0136 <xsl:param name="argument"/>
0137 <xsl:value-of select="concat(translate(substring($argument,1,1),'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring($argument,2))"/>
0138 </xsl:template>
0139 
0140 <xsl:template name="data-type">
0141     <xsl:choose>
0142     <xsl:when test="@type = 'enum'"><xsl:value-of select="../@name"/>::<xsl:value-of select="@enumType"/></xsl:when>
0143     <xsl:otherwise><xsl:value-of select="@type"/></xsl:otherwise>
0144     </xsl:choose>
0145 </xsl:template>
0146 
0147 <!-- generates function argument code for the current column -->
0148 <xsl:template name="argument">
0149     <xsl:if test="starts-with(@type,'Q')">const </xsl:if><xsl:call-template name="data-type"/><xsl:text> </xsl:text>
0150     <xsl:if test="starts-with(@type,'Q')">&amp;</xsl:if><xsl:value-of select="@name"/>
0151 </xsl:template>
0152 
0153 <!-- signature of setter method -->
0154 <xsl:template name="setter-signature">
0155 <xsl:variable name="methodName">
0156   <xsl:value-of select="concat(translate(substring(@name,1,1),'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), substring(@name,2))"/>
0157 </xsl:variable>
0158 set<xsl:value-of select="$methodName"/>(<xsl:call-template name="argument"/>)
0159 </xsl:template>
0160 
0161 <!-- field name list -->
0162 <xsl:template name="column-list">
0163   <xsl:for-each select="column">
0164     <xsl:value-of select="@name"/>
0165     <xsl:if test="position() != last()"><xsl:text>, </xsl:text></xsl:if>
0166   </xsl:for-each>
0167 </xsl:template>
0168 
0169 <!-- data retrieval for a given key field -->
0170 <xsl:template name="data-retrieval">
0171 <xsl:param name="dataStore"/>
0172 <xsl:param name="key"/>
0173 <xsl:param name="key2"/>
0174 <xsl:param name="lookupKey" select="$key"/>
0175 <xsl:param name="cache"/>
0176 <xsl:variable name="className"><xsl:value-of select="@name"/></xsl:variable>
0177     <xsl:if test="$cache != ''">
0178     if (Private::cacheEnabled) {
0179         QMutexLocker lock(&amp;Private::cacheMutex);
0180         auto it = Private::<xsl:value-of select="$cache"/>.constFind(<xsl:value-of select="$lookupKey"/>);
0181         if (it != Private::<xsl:value-of select="$cache"/>.constEnd()) {
0182             return it.value();
0183         }
0184     }
0185     </xsl:if>
0186     QSqlDatabase db = <xsl:value-of select="$dataStore"/>->database();
0187     if (!db.isOpen()) {
0188         return <xsl:value-of select="$className"/>();
0189     }
0190 
0191     QueryBuilder qb(<xsl:value-of select="$dataStore"/>, tableName(), QueryBuilder::Select);
0192     static const QStringList columns = removeEntry(columnNames(), <xsl:value-of select="$key"/>Column());
0193     qb.addColumns(columns);
0194     qb.addValueCondition(<xsl:value-of select="$key"/>Column(), Query::Equals, <xsl:value-of select="$key"/>);
0195     <xsl:if test="$key2 != ''">
0196     qb.addValueCondition(<xsl:value-of select="$key2"/>Column(), Query::Equals, <xsl:value-of select="$key2"/>);
0197     </xsl:if>
0198     if (!qb.exec()) {
0199         qCWarning(AKONADISERVER_LOG) &lt;&lt; "Error during selection of record with <xsl:value-of select="$key"/>"
0200                                      &lt;&lt; <xsl:value-of select="$key"/> &lt;&lt; "from table" &lt;&lt; tableName()
0201                                      &lt;&lt; qb.query().lastError().text();
0202         return <xsl:value-of select="$className"/>();
0203     }
0204     if (!qb.query().next()) {
0205         return <xsl:value-of select="$className"/>();
0206     }
0207 
0208     <!-- this indirection is required to prevent off-by-one access now that we skip the key column -->
0209     int valueIndex = 0;
0210     <xsl:for-each select="column">
0211     const <xsl:call-template name="data-type"/> &amp;value<xsl:value-of select="position()"/> =
0212     <xsl:choose>
0213       <xsl:when test="@name=$key">
0214         <xsl:value-of select="$key"/>;
0215       </xsl:when>
0216       <xsl:otherwise>
0217         (qb.query().isNull(valueIndex)) ?
0218         <xsl:call-template name="data-type"/>(<xsl:value-of select="@default"/>) :
0219         <xsl:choose>
0220           <xsl:when test="starts-with(@type,'QString')">
0221             Utils::variantToString(qb.query().value( valueIndex))
0222           </xsl:when>
0223           <xsl:when test="starts-with(@type, 'enum')">
0224             static_cast&lt;<xsl:value-of select="@enumType"/>&gt;(qb.query().value( valueIndex ).value&lt;int&gt;())
0225           </xsl:when>
0226           <xsl:when test="starts-with(@type, 'QDateTime')">
0227             Utils::variantToDateTime(qb.query().value(valueIndex))
0228           </xsl:when>
0229           <xsl:otherwise>
0230             qb.query().value( valueIndex ).value&lt;<xsl:value-of select="@type"/>&gt;()
0231           </xsl:otherwise>
0232         </xsl:choose>
0233            ; ++valueIndex;
0234       </xsl:otherwise>
0235     </xsl:choose>
0236   </xsl:for-each>
0237 
0238     <xsl:value-of select="$className"/> rv(
0239     <xsl:for-each select="column">
0240         value<xsl:value-of select="position()"/>
0241       <xsl:if test="position() != last()">,</xsl:if>
0242     </xsl:for-each>
0243     );
0244     if (Private::cacheEnabled) {
0245         Private::addToCache(rv);
0246     }
0247     return rv;
0248 </xsl:template>
0249 
0250 <!-- method name for n:1 referred records -->
0251 <xsl:template name="method-name-n1">
0252 <xsl:param name="table" />
0253 <xsl:choose>
0254 <xsl:when test="@methodName != ''">
0255   <xsl:value-of select="@methodName"/>
0256 </xsl:when>
0257 <xsl:otherwise>
0258   <xsl:value-of select="concat(translate(substring($table,1,1),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), substring($table,2))"/>
0259 </xsl:otherwise>
0260 </xsl:choose>
0261 </xsl:template>
0262 
0263 </xsl:stylesheet>
0264