Warning, /pim/akonadi/src/server/storage/entities-source.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 
0010 <!-- table class source template -->
0011 <xsl:template name="table-source">
0012 <xsl:variable name="className"><xsl:value-of select="@name"/></xsl:variable>
0013 <xsl:variable name="tableName"><xsl:value-of select="@name"/>Table</xsl:variable>
0014 <xsl:variable name="entityName"><xsl:value-of select="@name"/></xsl:variable>
0015 
0016 // private class
0017 class <xsl:value-of select="$className"/>::Private : public QSharedData
0018 {
0019 public:
0020     Private() : QSharedData() // NOLINT(readability-redundant-member-init)
0021     <!-- BEGIN Variable Initializers - order as Variable Declarations below -->
0022     <xsl:for-each select="column[@type = 'bool']">
0023       , <xsl:value-of select="@name"/>(<xsl:choose><xsl:when test="@default"><xsl:value-of select="@default"/></xsl:when><xsl:otherwise>false</xsl:otherwise></xsl:choose>)
0024     </xsl:for-each>
0025     <xsl:for-each select="column[@name != 'id']">
0026       , <xsl:value-of select="@name"/>_changed(false)
0027     </xsl:for-each>
0028     <!-- END Variable Initializers - order as Variable Declarations below -->
0029     {}
0030 
0031     <!-- BEGIN Variable Declarations - order by decreasing sizeof() -->
0032     <xsl:for-each select="column[@type = 'qint64' and @name != 'id']">
0033     qint64 <xsl:value-of select="@name"/> = <xsl:choose><xsl:when test="@default"><xsl:value-of select="@default"/></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose>;
0034     </xsl:for-each>
0035     <xsl:if test="column[@type = 'QDateTime']">
0036     </xsl:if>
0037     <xsl:for-each select="column[@type = 'QString']">
0038     QString <xsl:value-of select="@name"/>;
0039     </xsl:for-each>
0040     <xsl:for-each select="column[@type = 'QByteArray']">
0041     QByteArray <xsl:value-of select="@name"/>;
0042     </xsl:for-each>
0043     <xsl:if test="column[@type = 'QDateTime']">
0044     // on non-wince, QDateTime is one int
0045     <xsl:for-each select="column[@type = 'QDateTime']">
0046     QDateTime <xsl:value-of select="@name"/>;
0047     </xsl:for-each>
0048     </xsl:if>
0049     <xsl:for-each select="column[@type = 'int']">
0050     int <xsl:value-of select="@name"/> = <xsl:choose><xsl:when test="@default"><xsl:value-of select="@default"/></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose>;
0051     </xsl:for-each>
0052     <xsl:for-each select="column[@type = 'bool']">
0053     bool <xsl:value-of select="@name"/> : 1;
0054     </xsl:for-each>
0055     <xsl:for-each select="column[@type = 'Tristate']">
0056     Tristate <xsl:value-of select="@name"/>;
0057     </xsl:for-each>
0058     <xsl:for-each select="column[@type = 'enum']">
0059     <xsl:value-of select="@enumType"/><xsl:text> </xsl:text><xsl:value-of select="@name"/> = <xsl:choose><xsl:when test="@default"><xsl:value-of select="@default"/></xsl:when><xsl:otherwise>0</xsl:otherwise></xsl:choose>;
0060     </xsl:for-each>
0061     <xsl:for-each select="column[@name != 'id']">
0062     bool <xsl:value-of select="@name"/>_changed : 1;
0063     </xsl:for-each>
0064     <!-- END Variable Declarations - order by decreasing sizeof() -->
0065 
0066     static void addToCache(const <xsl:value-of select="$className"/> &amp;entry);
0067 
0068     // cache
0069     static QAtomicInt cacheEnabled;
0070     static QMutex cacheMutex;
0071     <xsl:if test="column[@name = 'id']">
0072     static QHash&lt;qint64, <xsl:value-of select="$className"/>&gt; idCache;
0073     </xsl:if>
0074     <xsl:if test="column[@name = 'name']">
0075     static QHash&lt;<xsl:value-of select="column[@name = 'name']/@type"/>, <xsl:value-of select="$className"/>&gt; nameCache;
0076     </xsl:if>
0077 };
0078 
0079 
0080 // static members
0081 QAtomicInt <xsl:value-of select="$className"/>::Private::cacheEnabled(0);
0082 QMutex <xsl:value-of select="$className"/>::Private::cacheMutex;
0083 <xsl:if test="column[@name = 'id']">
0084 QHash&lt;qint64, <xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::Private::idCache;
0085 </xsl:if>
0086 <xsl:if test="column[@name = 'name']">
0087 QHash&lt;<xsl:value-of select="column[@name = 'name']/@type"/>, <xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::Private::nameCache;
0088 </xsl:if>
0089 
0090 
0091 void <xsl:value-of select="$className"/>::Private::addToCache(const <xsl:value-of select="$className"/> &amp;entry)
0092 {
0093     Q_ASSERT(cacheEnabled);
0094     Q_UNUSED(entry); <!-- in case the table has neither an id nor name column -->
0095     QMutexLocker lock(&amp;cacheMutex);
0096     <xsl:if test="column[@name = 'id']">
0097     idCache.insert(entry.id(), entry);
0098     </xsl:if>
0099     <xsl:if test="column[@name = 'name']">
0100       <xsl:choose>
0101         <xsl:when test="$className = 'PartType'">
0102         <!-- special case for PartType, which is identified as "NS:NAME" -->
0103     nameCache.insert(entry.ns() + QLatin1Char(':') + entry.name(), entry);
0104         </xsl:when>
0105         <xsl:otherwise>
0106     nameCache.insert(entry.name(), entry);
0107         </xsl:otherwise>
0108       </xsl:choose>
0109     </xsl:if>
0110 }
0111 
0112 
0113 // constructor
0114 <xsl:value-of select="$className"/>::<xsl:value-of select="$className"/>()
0115     : d(new Private)
0116 {
0117 }
0118 
0119 <xsl:value-of select="$className"/>::<xsl:value-of select="$className"/>(
0120   <xsl:for-each select="column[@name != 'id']">
0121     <xsl:call-template name="argument"/><xsl:if test="position() != last()">, </xsl:if>
0122   </xsl:for-each>
0123 ) : d(new Private)
0124 {
0125 <xsl:for-each select="column[@name != 'id']">
0126     d-&gt;<xsl:value-of select="@name"/> = <xsl:value-of select="@name"/>;
0127     d-&gt;<xsl:value-of select="@name"/>_changed = true;
0128 </xsl:for-each>
0129 }
0130 
0131 <xsl:if test="column[@name = 'id']">
0132 <xsl:value-of select="$className"/>::<xsl:value-of select="$className"/>(
0133   <xsl:for-each select="column">
0134     <xsl:call-template name="argument"/><xsl:if test="position() != last()">, </xsl:if>
0135   </xsl:for-each>
0136 ) :
0137     Entity(id),
0138     d(new Private)
0139 {
0140 <xsl:for-each select="column[@name != 'id']">
0141     d-&gt;<xsl:value-of select="@name"/> = <xsl:value-of select="@name"/>;
0142     d-&gt;<xsl:value-of select="@name"/>_changed = true;
0143 </xsl:for-each>
0144 }
0145 </xsl:if>
0146 
0147 <xsl:value-of select="$className"/>::<xsl:value-of select="$className"/>(const <xsl:value-of select="$className"/> &amp;other)
0148     : Entity(other), d(other.d)
0149 {
0150 }
0151 
0152 // destructor
0153 <xsl:value-of select="$className"/>::~<xsl:value-of select="$className"/>() {}
0154 
0155 // assignment operator
0156 <xsl:value-of select="$className"/>&amp; <xsl:value-of select="$className"/>::operator=(const <xsl:value-of select="$className"/> &amp;other)
0157 {
0158     if (this != &amp;other) {
0159         d = other.d;
0160         setId(other.id());
0161     }
0162     return *this;
0163 }
0164 
0165 // comparison operator
0166 bool <xsl:value-of select="$className"/>::operator==(const <xsl:value-of select="$className"/> &amp;other) const
0167 {
0168     return id() == other.id();
0169 }
0170 
0171 // accessor methods
0172 <xsl:for-each select="column[@name != 'id']">
0173 <xsl:call-template name="data-type"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::<xsl:value-of select="@name"/>() const
0174 {
0175     <xsl:text>return d-&gt;</xsl:text><xsl:value-of select="@name"/>;
0176 }
0177 
0178 void <xsl:value-of select="$className"/>::<xsl:call-template name="setter-signature"/>
0179 {
0180     d-&gt;<xsl:value-of select="@name"/> = <xsl:value-of select="@name"/>;
0181     d-&gt;<xsl:value-of select="@name"/>_changed = true;
0182 }
0183 
0184 </xsl:for-each>
0185 
0186 // SQL table information
0187 <xsl:text>QString </xsl:text><xsl:value-of select="$className"/>::tableName()
0188 {
0189     static const QString tableName = QStringLiteral("<xsl:value-of select="$tableName"/>");
0190     return tableName;
0191 }
0192 
0193 QStringList <xsl:value-of select="$className"/>::columnNames()
0194 {
0195     static const QStringList columns = {
0196         <xsl:for-each select="column">
0197         <xsl:value-of select="@name"/>Column()
0198         <xsl:if test="position() != last()">,</xsl:if>
0199         </xsl:for-each>
0200     };
0201     return columns;
0202 }
0203 
0204 QStringList <xsl:value-of select="$className"/>::fullColumnNames()
0205 {
0206     static const QStringList columns = {
0207         <xsl:for-each select="column">
0208         <xsl:value-of select="@name"/>FullColumnName()
0209         <xsl:if test="position() != last()">,</xsl:if>
0210         </xsl:for-each>
0211     };
0212     return columns;
0213 }
0214 
0215 <xsl:for-each select="column">
0216 QString <xsl:value-of select="$className"/>::<xsl:value-of select="@name"/>Column()
0217 {
0218     static const QString column = QStringLiteral("<xsl:value-of select="@name"/>");
0219     return column;
0220 }
0221 
0222 QString <xsl:value-of select="$className"/>::<xsl:value-of select="@name"/>FullColumnName()
0223 {
0224     static const QString column = QStringLiteral("<xsl:value-of select="$tableName"/>.<xsl:value-of select="@name"/>");
0225     return column;
0226 }
0227 </xsl:for-each>
0228 
0229 
0230 // count records
0231 int <xsl:value-of select="$className"/>::count(const QString &amp;column, const QVariant &amp;value)
0232 {
0233     return count(DataStore::self(), column, value);
0234 }
0235 
0236 int <xsl:value-of select="$className"/>::count(DataStore *store, const QString &amp;column, const QVariant &amp;value)
0237 {
0238     return Entity::count&lt;<xsl:value-of select="$className"/>&gt;(store, column, value);
0239 }
0240 
0241 
0242 // check existence
0243 <xsl:if test="column[@name = 'id']">
0244 bool <xsl:value-of select="$className"/>::exists(qint64 id)
0245 {
0246     if (Private::cacheEnabled) {
0247         QMutexLocker lock(&amp;Private::cacheMutex);
0248         if (Private::idCache.contains(id)) {
0249             return true;
0250         }
0251     }
0252     return count(idColumn(), id) > 0;
0253 }
0254 </xsl:if>
0255 <xsl:if test="column[@name = 'name']">
0256 bool <xsl:value-of select="$className"/>::exists(const <xsl:value-of select="column[@name = 'name']/@type" /> &amp;name)
0257 {
0258     return exists(DataStore::self(), name);
0259 }
0260 
0261 bool <xsl:value-of select="$className"/>::exists(DataStore *store, const <xsl:value-of select="column[@name = 'name']/@type"/> &amp;name)
0262 {
0263     if (Private::cacheEnabled) {
0264         QMutexLocker lock(&amp;Private::cacheMutex);
0265         if (Private::nameCache.contains(name)) {
0266             return true;
0267         }
0268     }
0269     return count(store, nameColumn(), name) > 0;
0270 }
0271 </xsl:if>
0272 
0273 
0274 // result extraction
0275 QList&lt;<xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::extractResult(QSqlQuery &amp;query)
0276 {
0277     return extractResult(DataStore::self(), query);
0278 }
0279 
0280 QList&lt;<xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::extractResult(DataStore *store, QSqlQuery &amp;query)
0281 {
0282     Q_UNUSED(store); // only used in specfic case when the entity has a QDateTime column (e.g. PimItem). This will suppress
0283                      // "unused parameter" warning for other entities that do not have a QDateTime column.
0284     QList&lt;<xsl:value-of select="$className"/>&gt; rv;
0285     if (query.driver()->hasFeature(QSqlDriver::QuerySize)) {
0286         rv.reserve(query.size());
0287     }
0288     while (query.next()) {
0289         rv.append(<xsl:value-of select="$className"/>(
0290         <xsl:for-each select="column">
0291               (query.isNull(<xsl:value-of select="position() - 1"/>)
0292                 ? <xsl:call-template name="data-type"/>()
0293                 <xsl:text>: </xsl:text>
0294                 <xsl:choose>
0295                 <xsl:when test="starts-with(@type,'QString')">
0296                   <xsl:text>Utils::variantToString(query.value(</xsl:text><xsl:value-of select="position() - 1"/><xsl:text>))</xsl:text>
0297                 </xsl:when>
0298                 <xsl:when test="starts-with(@type, 'enum')">
0299                   <xsl:text>static_cast&lt;</xsl:text><xsl:value-of select="@enumType"/>&gt;(query.value(<xsl:value-of select="position() - 1"/><xsl:text>).value&lt;int&gt;())</xsl:text>
0300                 </xsl:when>
0301                 <xsl:when test="starts-with(@type, 'QDateTime')">
0302                   <xsl:text>Utils::variantToDateTime(query.value(</xsl:text><xsl:value-of select="position() - 1" /><xsl:text>), store)</xsl:text>
0303                 </xsl:when>
0304                 <xsl:otherwise>
0305                   <xsl:text>query.value(</xsl:text><xsl:value-of select="position() - 1"/>).value&lt;<xsl:value-of select="@type"/>&gt;<xsl:text>()</xsl:text>
0306                 </xsl:otherwise>
0307               </xsl:choose>
0308               <xsl:text>)</xsl:text><xsl:if test="position() != last()"><xsl:text>,</xsl:text></xsl:if>
0309             </xsl:for-each>
0310             ));
0311     }
0312     query.finish();
0313     return rv;
0314 }
0315 
0316 // data retrieval
0317 <xsl:if test="column[@name='id']">
0318 <xsl:value-of select="$className"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::retrieveById(qint64 id)
0319 {
0320     return retrieveById(DataStore::self(), id);
0321 }
0322 <xsl:value-of select="$className"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::retrieveById(DataStore *store, qint64 id)
0323 {
0324     <xsl:call-template name="data-retrieval">
0325       <xsl:with-param name="dataStore">store</xsl:with-param>
0326       <xsl:with-param name="key">id</xsl:with-param>
0327       <xsl:with-param name="cache">idCache</xsl:with-param>
0328     </xsl:call-template>
0329 }
0330 
0331 </xsl:if>
0332 <xsl:if test="column[@name = 'name'] and $className != 'PartType'">
0333 <xsl:value-of select="$className"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::retrieveByName(const <xsl:value-of select="column[@name = 'name']/@type"/> &amp;name)
0334 {
0335     return retrieveByName(DataStore::self(), name);
0336 }
0337 
0338 <xsl:value-of select="$className"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::retrieveByName(DataStore *store, const <xsl:value-of select="column[@name = 'name']/@type"/> &amp;name)
0339 {
0340     <xsl:call-template name="data-retrieval">
0341       <xsl:with-param name="dataStore">store</xsl:with-param>
0342       <xsl:with-param name="key">name</xsl:with-param>
0343       <xsl:with-param name="cache">nameCache</xsl:with-param>
0344     </xsl:call-template>
0345 }
0346 
0347 <xsl:value-of select="$className"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::retrieveByNameOrCreate(const <xsl:value-of select="column[@name = 'name']/@type"/> &amp;name)
0348 {
0349   return retrieveByNameOrCreate(DataStore::self(), name);
0350 }
0351 
0352 <xsl:value-of select="$className"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::retrieveByNameOrCreate(DataStore *store, const <xsl:value-of select="column[@name = 'name']/@type"/> &amp;name)
0353 {
0354   static QMutex lock;
0355   QMutexLocker locker(&amp;lock);
0356   auto rv = retrieveByName(store, name);
0357   if (rv.isValid()) {
0358     return rv;
0359   }
0360 
0361   rv.setName(name);
0362   if (!rv.insert(store)) {
0363     return <xsl:value-of select="$className"/>();
0364   }
0365 
0366   if (Private::cacheEnabled) {
0367     Private::addToCache(rv);
0368   }
0369   return rv;
0370 }
0371 </xsl:if>
0372 
0373 <xsl:if test="column[@name = 'name'] and $className = 'PartType'">
0374 <xsl:text>PartType PartType::retrieveByFQName(const QString &amp;ns, const QString &amp;name)</xsl:text>
0375 {
0376     return retrieveByFQName(DataStore::self(), ns, name);
0377 }
0378 
0379 <xsl:text>PartType PartType::retrieveByFQName(DataStore *store, const QString &amp;ns, const QString &amp;name)</xsl:text>
0380 {
0381     const QString fqname = ns + QLatin1Char(':') + name;
0382     <xsl:call-template name="data-retrieval">
0383       <xsl:with-param name="dataStore">store</xsl:with-param>
0384       <xsl:with-param name="key">ns</xsl:with-param>
0385       <xsl:with-param name="key2">name</xsl:with-param>
0386       <xsl:with-param name="lookupKey">fqname</xsl:with-param>
0387       <xsl:with-param name="cache">nameCache</xsl:with-param>
0388     </xsl:call-template>
0389 }
0390 
0391 <xsl:text>PartType PartType::retrieveByFQNameOrCreate(const QString &amp;ns, const QString &amp;name)</xsl:text>
0392 {
0393     return retrieveByFQNameOrCreate(DataStore::self(), ns, name);
0394 }
0395 
0396 <xsl:text>PartType PartType::retrieveByFQNameOrCreate(DataStore *store, const QString &amp;ns, const QString &amp;name)</xsl:text>
0397 {
0398   static QMutex lock;
0399   QMutexLocker locker(&amp;lock);
0400   PartType rv = retrieveByFQName(store, ns, name);
0401   if (rv.isValid()) {
0402     return rv;
0403   }
0404 
0405   rv.setNs(ns);
0406   rv.setName(name);
0407   if (!rv.insert(store)) {
0408     return PartType();
0409   }
0410 
0411   if (Private::cacheEnabled) {
0412     Private::addToCache(rv);
0413   }
0414   return rv;
0415 }
0416 </xsl:if>
0417 
0418 QList&lt;<xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::retrieveAll()
0419 {
0420     return retrieveAll(DataStore::self());
0421 }
0422 
0423 QList&lt;<xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::retrieveAll(DataStore *store)
0424 {
0425     QSqlDatabase db = store->database();
0426     if (!db.isOpen()) {
0427         return {};
0428     }
0429 
0430     QueryBuilder qb(store, tableName(), QueryBuilder::Select);
0431     qb.addColumns(columnNames());
0432     if (!qb.exec()) {
0433         qCWarning(AKONADISERVER_LOG) &lt;&lt; "Error during selection of all records from table" &lt;&lt; tableName()
0434                                      &lt;&lt; qb.query().lastError().text() &lt;&lt; qb.query().lastQuery();
0435         return {};
0436     }
0437     return extractResult(store, qb.query());
0438 }
0439 
0440 QList&lt;<xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::retrieveFiltered(const QString &amp;key, const QVariant &amp;value)
0441 {
0442     return retrieveFiltered(DataStore::self(), key, value);
0443 }
0444 
0445 QList&lt;<xsl:value-of select="$className"/>&gt; <xsl:value-of select="$className"/>::retrieveFiltered(DataStore *store, const QString &amp;key, const QVariant &amp;value)
0446 {
0447     QSqlDatabase db = store->database();
0448     if (!db.isOpen()) {
0449         return {};
0450     }
0451 
0452     SelectQueryBuilder&lt;<xsl:value-of select="$className"/>&gt; qb(store);
0453     if (value.isNull()) {
0454         qb.addValueCondition(key, Query::Is, QVariant());
0455     } else {
0456         qb.addValueCondition(key, Query::Equals, value);
0457     }
0458     if (!qb.exec()) {
0459         qCWarning(AKONADISERVER_LOG) &lt;&lt; "Error during selection of records from table" &lt;&lt; tableName()
0460                                      &lt;&lt; "filtered by" &lt;&lt; key &lt;&lt; "=" &lt;&lt; value
0461                                      &lt;&lt; qb.query().lastError().text();
0462         return {};
0463     }
0464     return qb.result();
0465 }
0466 
0467 // data retrieval for referenced tables
0468 <xsl:for-each select="column[@refTable != '']">
0469 <xsl:variable name="method-name"><xsl:call-template name="method-name-n1"><xsl:with-param name="table" select="@refTable"/></xsl:call-template></xsl:variable>
0470 
0471 <xsl:value-of select="@refTable"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::<xsl:value-of select="$method-name"/>() const
0472 {
0473     return <xsl:value-of select="$method-name"/>(DataStore::self());
0474 }
0475 
0476 <xsl:value-of select="@refTable"/><xsl:text> </xsl:text><xsl:value-of select="$className"/>::<xsl:value-of select="$method-name"/>(DataStore *store) const
0477 {
0478     return <xsl:value-of select="@refTable"/>::retrieveById(store, <xsl:value-of select="@name"/>());
0479 }
0480 
0481 void <xsl:value-of select="$className"/>::
0482     set<xsl:call-template name="uppercase-first"><xsl:with-param name="argument"><xsl:value-of select="$method-name"/></xsl:with-param></xsl:call-template>
0483     (const <xsl:value-of select="@refTable"/> &amp;value)
0484 {
0485     d-&gt;<xsl:value-of select="@name"/> = value.id();
0486     d-&gt;<xsl:value-of select="@name"/>_changed = true;
0487 }
0488 </xsl:for-each>
0489 
0490 // data retrieval for inverse referenced tables
0491 <xsl:for-each select="reference">
0492 QList&lt;<xsl:value-of select="@table"/>&gt; <xsl:value-of select="$className"/>::<xsl:value-of select="@name"/>() const
0493 {
0494     return <xsl:value-of select="@name"/>(DataStore::self());
0495 }
0496 
0497 QList&lt;<xsl:value-of select="@table"/>&gt; <xsl:value-of select="$className"/>::<xsl:value-of select="@name"/>(DataStore *store) const
0498 {
0499     return <xsl:value-of select="@table"/>::retrieveFiltered(store, <xsl:value-of select="@table"/>::<xsl:value-of select="@key"/>Column(), id());
0500 }
0501 </xsl:for-each>
0502 
0503 <!-- methods for n:m relations -->
0504 <xsl:for-each select="../relation[@table1 = $entityName]">
0505 <xsl:variable name="relationName"><xsl:value-of select="@table1"/><xsl:value-of select="@table2"/>Relation</xsl:variable>
0506 <xsl:variable name="rightSideClass"><xsl:value-of select="@table2"/></xsl:variable>
0507 <xsl:variable name="rightSideEntity"><xsl:value-of select="@table2"/></xsl:variable>
0508 <xsl:variable name="methodName"><xsl:call-template name="method-name-n1"><xsl:with-param name="table" select="@table2"/></xsl:call-template>s</xsl:variable>
0509 
0510 // data retrieval for n:m relations
0511 QList&lt;<xsl:value-of select="$rightSideClass"/>&gt; <xsl:value-of select="$className"/>::<xsl:value-of select="$methodName"/>() const
0512 {
0513     return <xsl:value-of select="$methodName"/>(DataStore::self());
0514 }
0515 
0516 QList&lt;<xsl:value-of select="$rightSideClass"/>&gt; <xsl:value-of select="$className"/>::<xsl:value-of select="$methodName"/>(DataStore *store) const
0517 {
0518     QSqlDatabase db = store->database();
0519     if (!db.isOpen()) {
0520         return {};
0521     }
0522 
0523     QueryBuilder qb(store, <xsl:value-of select="$rightSideClass"/>::tableName(), QueryBuilder::Select);
0524     static const QStringList columns = {
0525     <xsl:for-each select="/database/table[@name = $rightSideEntity]/column">
0526         <xsl:value-of select="$rightSideClass"/>::<xsl:value-of select="@name"/>FullColumnName()
0527         <xsl:if test="position() != last()">,</xsl:if>
0528     </xsl:for-each>
0529     };
0530     qb.addColumns(columns);
0531     qb.addJoin(QueryBuilder::InnerJoin, <xsl:value-of select="$relationName"/>::tableName(),
0532             <xsl:value-of select="$relationName"/>::rightFullColumnName(),
0533             <xsl:value-of select="$rightSideClass"/>::<xsl:value-of select="@column2"/>FullColumnName());
0534     qb.addValueCondition(<xsl:value-of select="$relationName"/>::leftFullColumnName(), Query::Equals, id());
0535 
0536     if (!qb.exec()) {
0537         qCWarning(AKONADISERVER_LOG) &lt;&lt; "Error during selection of records from table <xsl:value-of select="@table1"/><xsl:value-of select="@table2"/>Relation"
0538                                      &lt;&lt; qb.query().lastError().text();
0539         return {};
0540     }
0541 
0542     return <xsl:value-of select="$rightSideClass"/>::extractResult(store, qb.query());
0543 }
0544 
0545 // manipulate n:m relations
0546 bool <xsl:value-of select="$className"/>::relatesTo<xsl:value-of select="@table2"/>(const <xsl:value-of select="$rightSideClass"/> &amp;value) const
0547 {
0548     return relatesTo<xsl:value-of select="@table2"/>(DataStore::self(), value);
0549 }
0550 
0551 bool <xsl:value-of select="$className"/>::relatesTo<xsl:value-of select="@table2"/>(DataStore *store, const <xsl:value-of select="$rightSideClass"/> &amp;value) const
0552 {
0553     return Entity::relatesTo&lt;<xsl:value-of select="$relationName"/>&gt;(store, id(), value.id());
0554 }
0555 
0556 bool <xsl:value-of select="$className"/>::relatesTo<xsl:value-of select="@table2"/>(qint64 leftId, qint64 rightId)
0557 {
0558     return relatesTo<xsl:value-of select="@table2"/>(DataStore::self(), leftId, rightId);
0559 }
0560 
0561 bool <xsl:value-of select="$className"/>::relatesTo<xsl:value-of select="@table2"/>(DataStore *store, qint64 leftId, qint64 rightId)
0562 {
0563     return Entity::relatesTo&lt;<xsl:value-of select="$relationName"/>&gt;(store, leftId, rightId);
0564 }
0565 
0566 bool <xsl:value-of select="$className"/>::add<xsl:value-of select="@table2"/>(const <xsl:value-of select="$rightSideClass"/> &amp;value) const
0567 {
0568     return add<xsl:value-of select="@table2"/>(DataStore::self(), value);
0569 }
0570 
0571 bool <xsl:value-of select="$className"/>::add<xsl:value-of select="@table2"/>(DataStore *store, const <xsl:value-of select="$rightSideClass"/> &amp;value) const
0572 {
0573     return Entity::addToRelation&lt;<xsl:value-of select="$relationName"/>&gt;(store, id(), value.id());
0574 }
0575 
0576 bool <xsl:value-of select="$className"/>::add<xsl:value-of select="@table2"/>(qint64 leftId, qint64 rightId)
0577 {
0578     return add<xsl:value-of select="@table2"/>(DataStore::self(), leftId, rightId);
0579 }
0580 
0581 bool <xsl:value-of select="$className"/>::add<xsl:value-of select="@table2"/>(DataStore *store, qint64 leftId, qint64 rightId)
0582 {
0583     return Entity::addToRelation&lt;<xsl:value-of select="$relationName"/>&gt;(store, leftId, rightId);
0584 }
0585 
0586 bool <xsl:value-of select="$className"/>::remove<xsl:value-of select="@table2"/>(const <xsl:value-of select="$rightSideClass"/> &amp;value) const
0587 {
0588     return remove<xsl:value-of select="@table2"/>(DataStore::self(), value);
0589 }
0590 
0591 bool <xsl:value-of select="$className"/>::remove<xsl:value-of select="@table2"/>(DataStore *store, const <xsl:value-of select="$rightSideClass"/> &amp;value) const
0592 {
0593     return Entity::removeFromRelation&lt;<xsl:value-of select="$relationName"/>&gt;(store, id(), value.id());
0594 }
0595 
0596 bool <xsl:value-of select="$className"/>::remove<xsl:value-of select="@table2"/>(qint64 leftId, qint64 rightId)
0597 {
0598     return remove<xsl:value-of select="@table2"/>(DataStore::self(), leftId, rightId);
0599 }
0600 
0601 bool <xsl:value-of select="$className"/>::remove<xsl:value-of select="@table2"/>(DataStore *store, qint64 leftId, qint64 rightId)
0602 {
0603     return Entity::removeFromRelation&lt;<xsl:value-of select="$relationName"/>&gt;(store, leftId, rightId);
0604 }
0605 
0606 bool <xsl:value-of select="$className"/>::clear<xsl:value-of select="@table2"/>s() const
0607 {
0608     return clear<xsl:value-of select="@table2"/>s(DataStore::self());
0609 }
0610 
0611 bool <xsl:value-of select="$className"/>::clear<xsl:value-of select="@table2"/>s(DataStore *store) const
0612 {
0613     return Entity::clearRelation&lt;<xsl:value-of select="$relationName"/>&gt;(store, id());
0614 }
0615 
0616 bool <xsl:value-of select="$className"/>::clear<xsl:value-of select="@table2"/>s(qint64 id)
0617 {
0618     return clear<xsl:value-of select="@table2"/>s(DataStore::self(), id);
0619 }
0620 
0621 bool <xsl:value-of select="$className"/>::clear<xsl:value-of select="@table2"/>s(DataStore *store, qint64 id)
0622 {
0623     return Entity::clearRelation&lt;<xsl:value-of select="$relationName"/>&gt;(store, id);
0624 }
0625 
0626 </xsl:for-each>
0627 
0628 #ifndef QT_NO_DEBUG_STREAM
0629 // debug stream operator
0630 QDebug &amp;operator&lt;&lt;(QDebug &amp;d, const <xsl:value-of select="$className"/> &amp;entity)
0631 {
0632     d &lt;&lt; "[<xsl:value-of select="$className"/>: "
0633     <xsl:for-each select="column">
0634         &lt;&lt; "<xsl:value-of select="@name"/> = " &lt;&lt;
0635         <xsl:choose>
0636         <xsl:when test="starts-with(@type, 'enum')">
0637         static_cast&lt;int&gt;(entity.<xsl:value-of select="@name"/>())
0638         </xsl:when>
0639         <xsl:otherwise>
0640         entity.<xsl:value-of select="@name"/>()
0641         </xsl:otherwise>
0642       </xsl:choose>
0643       <xsl:if test="position() != last()">&lt;&lt; ", "</xsl:if>
0644     </xsl:for-each>
0645         &lt;&lt; "]";
0646     return d;
0647 }
0648 #endif
0649 
0650 // inserting new data
0651 bool <xsl:value-of select="$className"/>::insert(qint64* insertId)
0652 {
0653     return insert(DataStore::self(), insertId);
0654 }
0655 
0656 bool <xsl:value-of select="$className"/>::insert(DataStore *store, qint64* insertId)
0657 {
0658     QSqlDatabase db = store->database();
0659     if (!db.isOpen()) {
0660         return false;
0661     }
0662 
0663     QueryBuilder qb(store, tableName(), QueryBuilder::Insert);
0664     <xsl:if test="@identificationColumn">
0665     qb.setIdentificationColumn(QLatin1StringView("<xsl:value-of select="@identificationColumn"/>"));
0666     </xsl:if>
0667     <xsl:for-each select="column[@name != 'id']">
0668       <xsl:variable name="refColumn"><xsl:value-of select="@refColumn"/></xsl:variable>
0669       <xsl:if test="$refColumn = 'id'">
0670     if (d-&gt;<xsl:value-of select="@name"/>_changed  &amp;&amp; d-&gt;<xsl:value-of select="@name"/> &gt; 0) {
0671         qb.setColumnValue( <xsl:value-of select="@name"/>Column(), this-&gt;<xsl:value-of select="@name"/>() );
0672     }
0673       </xsl:if>
0674       <xsl:if test="$refColumn != 'id'">
0675     if (d-&gt;<xsl:value-of select="@name"/>_changed) {
0676         <xsl:choose>
0677           <xsl:when test="starts-with(@type, 'enum')">
0678         qb.setColumnValue(<xsl:value-of select="@name"/>Column(), static_cast&lt;int&gt;(this-&gt;<xsl:value-of select="@name"/>()));
0679           </xsl:when>
0680           <xsl:otherwise>
0681         qb.setColumnValue(<xsl:value-of select="@name"/>Column(), this-&gt;<xsl:value-of select="@name"/>());
0682           </xsl:otherwise>
0683         </xsl:choose>
0684     }
0685       </xsl:if>
0686     </xsl:for-each>
0687 
0688     if (!qb.exec()) {
0689         qCWarning(AKONADISERVER_LOG) &lt;&lt; "Error during insertion into table" &lt;&lt; tableName()
0690                                      &lt;&lt; qb.query().lastError().text();
0691         return false;
0692     }
0693 
0694     setId(qb.insertId());
0695     if (insertId) {
0696         *insertId = id();
0697     }
0698     return true;
0699 }
0700 
0701 bool <xsl:value-of select="$className"/>::hasPendingChanges() const
0702 {
0703     return false // NOLINT(readability-simplify-boolean-expr)
0704       <xsl:for-each select="column[@name != 'id']">
0705         || d-&gt;<xsl:value-of select="@name"/>_changed
0706       </xsl:for-each>;
0707 }
0708 
0709 // update existing data
0710 bool <xsl:value-of select="$className"/>::update()
0711 {
0712     return update(DataStore::self());
0713 }
0714 
0715 bool <xsl:value-of select="$className"/>::update(DataStore *store)
0716 {
0717     invalidateCache();
0718     QSqlDatabase db = store->database();
0719     if (!db.isOpen()) {
0720         return false;
0721     }
0722 
0723     QueryBuilder qb(store, tableName(), QueryBuilder::Update);
0724 
0725     <xsl:for-each select="column[@name != 'id']">
0726     <xsl:variable name="refColumn"><xsl:value-of select="@refColumn"/></xsl:variable>
0727     if (d-&gt;<xsl:value-of select="@name"/>_changed) {
0728         <xsl:if test="$refColumn = 'id'">
0729         if (d-&gt;<xsl:value-of select="@name"/> &lt;= 0) {
0730             qb.setColumnValue(<xsl:value-of select="@name"/>Column(), QVariant());
0731         } else {
0732         </xsl:if>
0733         <xsl:choose>
0734             <xsl:when test="starts-with(@type, 'enum')">
0735             qb.setColumnValue(<xsl:value-of select="@name"/>Column(), static_cast&lt;int&gt;(this-&gt;<xsl:value-of select="@name"/>()));
0736             </xsl:when>
0737             <xsl:otherwise>
0738             qb.setColumnValue(<xsl:value-of select="@name"/>Column(), this-&gt;<xsl:value-of select="@name"/>());
0739             </xsl:otherwise>
0740         </xsl:choose>
0741         <xsl:if test="$refColumn = 'id'">
0742         }
0743         </xsl:if>
0744     }
0745     </xsl:for-each>
0746 
0747     <xsl:if test="column[@name = 'id']">
0748     qb.addValueCondition(idColumn(), Query::Equals, id());
0749     </xsl:if>
0750 
0751     if (!qb.exec()) {
0752         qCWarning(AKONADISERVER_LOG) &lt;&lt; "Error during updating record with id" &lt;&lt; id()
0753                                      &lt;&lt; " in table" &lt;&lt; tableName() &lt;&lt; qb.query().lastError().text();
0754         return false;
0755     }
0756     return true;
0757 }
0758 
0759 // delete records
0760 bool <xsl:value-of select="$className"/>::remove(const QString &amp;column, const QVariant &amp;value)
0761 {
0762     return remove(DataStore::self(), column, value);
0763 }
0764 
0765 bool <xsl:value-of select="$className"/>::remove(DataStore *store, const QString &amp;column, const QVariant &amp;value)
0766 {
0767     invalidateCompleteCache();
0768     return Entity::remove&lt;<xsl:value-of select="$className"/>&gt;(store, column, value);
0769 }
0770 
0771 <xsl:if test="column[@name = 'id']">
0772 bool <xsl:value-of select="$className"/>::remove()
0773 {
0774     return remove(DataStore::self());
0775 }
0776 
0777 bool <xsl:value-of select="$className"/>::remove(DataStore *store)
0778 {
0779     invalidateCache();
0780     return Entity::remove&lt;<xsl:value-of select="$className"/>&gt;(store, idColumn(), id());
0781 }
0782 
0783 bool <xsl:value-of select="$className"/>::remove(qint64 id)
0784 {
0785     return remove(DataStore::self(), id);
0786 }
0787 
0788 bool <xsl:value-of select="$className"/>::remove(DataStore *store, qint64 id)
0789 {
0790     return remove(store, idColumn(), id);
0791 }
0792 </xsl:if>
0793 
0794 // cache stuff
0795 void <xsl:value-of select="$className"/>::invalidateCache() const
0796 {
0797     if (Private::cacheEnabled) {
0798         QMutexLocker lock(&amp;Private::cacheMutex);
0799         <xsl:if test="column[@name = 'id']">
0800         Private::idCache.remove(id());
0801         </xsl:if>
0802         <xsl:if test="column[@name = 'name']">
0803           <xsl:choose>
0804             <xsl:when test="$className = 'PartType'">
0805             <!-- Special handling for PartType, which is identified as "NS:NAME" -->
0806         Private::nameCache.remove(ns() + QLatin1Char(':') + name());
0807             </xsl:when>
0808             <xsl:otherwise>
0809         Private::nameCache.remove(name());
0810             </xsl:otherwise>
0811           </xsl:choose>
0812         </xsl:if>
0813     }
0814 }
0815 
0816 void <xsl:value-of select="$className"/>::invalidateCompleteCache()
0817 {
0818     if (Private::cacheEnabled) {
0819         QMutexLocker lock(&amp;Private::cacheMutex);
0820         <xsl:if test="column[@name = 'id']">
0821         Private::idCache.clear();
0822         </xsl:if>
0823         <xsl:if test="column[@name = 'name']">
0824         Private::nameCache.clear();
0825         </xsl:if>
0826     }
0827 }
0828 
0829 void <xsl:value-of select="$className"/>::enableCache(bool enable)
0830 {
0831     Private::cacheEnabled = enable;
0832 }
0833 
0834 </xsl:template>
0835 
0836 
0837 <!-- relation class source template -->
0838 <xsl:template name="relation-source">
0839 <xsl:variable name="className"><xsl:value-of select="@table1"/><xsl:value-of select="@table2"/>Relation</xsl:variable>
0840 <xsl:variable name="tableName"><xsl:value-of select="@table1"/><xsl:value-of select="@table2"/>Relation</xsl:variable>
0841 
0842 // SQL table information
0843 QString <xsl:value-of select="$className"/>::tableName()
0844 {
0845     static const QString table = QStringLiteral("<xsl:value-of select="$tableName"/>" );
0846     return table;
0847 }
0848 
0849 QString <xsl:value-of select="$className"/>::leftColumn()
0850 {
0851     static const QString column = QStringLiteral("<xsl:value-of select="@table1"/>_<xsl:value-of select="@column1"/>");
0852     return column;
0853 }
0854 
0855 QString <xsl:value-of select="$className"/>::leftFullColumnName()
0856 {
0857     static const QString column = QStringLiteral("<xsl:value-of select="$tableName"/>.<xsl:value-of select="@table1"/>_<xsl:value-of select="@column1"/>");
0858     return column;
0859 }
0860 
0861 QString <xsl:value-of select="$className"/>::rightColumn()
0862 {
0863     static const QString column = QStringLiteral("<xsl:value-of select="@table2"/>_<xsl:value-of select="@column2"/>");
0864     return column;
0865 }
0866 
0867 QString <xsl:value-of select="$className"/>::rightFullColumnName()
0868 {
0869     static const QString column = QStringLiteral("<xsl:value-of select="$tableName"/>.<xsl:value-of select="@table2"/>_<xsl:value-of select="@column2"/>");
0870     return column;
0871 }
0872 </xsl:template>
0873 
0874 </xsl:stylesheet>