File indexing completed on 2024-04-28 15:58:54

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KDB_DRIVER_SQLITE_H
0021 #define KDB_DRIVER_SQLITE_H
0022 
0023 #include "KDbDriver.h"
0024 
0025 class KDbConnection;
0026 class SqliteDriverPrivate;
0027 
0028 //! SQLite database driver.
0029 class SqliteDriver : public KDbDriver
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     SqliteDriver(QObject *parent, const QVariantList &args);
0035 
0036     ~SqliteDriver() override;
0037 
0038     /*! @return true if @a n is a system object name;
0039       for this driver any object with name prefixed with "sqlite_"
0040       is considered as system object.
0041     */
0042     bool isSystemObjectName(const QString& n) const override;
0043 
0044     /*! @return false for this driver. */
0045     bool isSystemDatabaseName(const QString&) const override;
0046 
0047     //! Escape a string for use as a value
0048     KDbEscapedString escapeString(const QString& str) const override;
0049     KDbEscapedString escapeString(const QByteArray& str) const override;
0050 
0051     //! Escape BLOB value @a array
0052     KDbEscapedString escapeBLOB(const QByteArray& array) const override;
0053 
0054     /*! Implemented for KDbDriver class.
0055      @return SQL clause to add for unicode text collation sequence
0056      used in ORDER BY clauses of SQL statements generated by KDb.
0057      Later other clauses may use this statement.
0058      One space character should be be prepended.
0059      Can be reimplemented for other drivers, e.g. the SQLite3 driver returns " COLLATE ''".
0060      Default implementation returns empty string. */
0061     KDbEscapedString collationSql() const override;
0062 
0063     //! Generates native (driver-specific) GREATEST() and LEAST() function calls.
0064     //! Uses MAX() and MIN(), respectively.
0065     //! If arguments are of text type, to each argument default (unicode) collation
0066     //! is assigned that is configured for SQLite by KDb.
0067     //! Example: SELECT MAX('ą' COLLATE '', 'z' COLLATE '').
0068     KDbEscapedString greatestOrLeastFunctionToString(const QString &name, const KDbNArgExpression &args,
0069                                     KDbQuerySchemaParameterValueListIterator *params,
0070                                     KDb::ExpressionCallStack *callStack) const override;
0071 
0072     //! Generates native (driver-specific) RANDOM() and RANDOM(X,Y) function calls.
0073     //! Accepted @a args can contain zero or two positive integer arguments X, Y; X < Y.
0074     //! In case of numeric arguments, RANDOM(X, Y) returns a random integer that is equal
0075     //! or greater than X and less than Y.
0076     //! Because SQLite returns integer between -9223372036854775808 and +9223372036854775807,
0077     //! RANDOM() for SQLite is equal to (RANDOM()+9223372036854775807)/18446744073709551615.
0078     //! Similarly, RANDOM(X,Y) for SQLite is equal to
0079     //! (X + CAST((Y-X) * (RANDOM()+9223372036854775807)/18446744073709551615 AS INT)).
0080     KDbEscapedString randomFunctionToString(const KDbNArgExpression &args,
0081                                             KDbQuerySchemaParameterValueListIterator *params,
0082                                             KDb::ExpressionCallStack *callStack) const override;
0083 
0084     //! Generates native (driver-specific) CEILING() and FLOOR() function calls.
0085     //! Default implementation USES CEILING() and FLOOR(), respectively.
0086     //! For CEILING() uses:
0087     //! (CASE WHEN X = CAST(X AS INT) THEN CAST(X AS INT) WHEN X >= 0 THEN CAST(X AS INT) + 1 ELSE CAST(X AS INT) END).
0088     //! For FLOOR() uses:
0089     //! (CASE WHEN X >= 0 OR X = CAST(X AS INT) THEN CAST(X AS INT) ELSE CAST(X AS INT) - 1 END).
0090     KDbEscapedString ceilingOrFloorFunctionToString(const QString &name, const KDbNArgExpression &args,
0091                                    KDbQuerySchemaParameterValueListIterator *params,
0092                                    KDb::ExpressionCallStack *callStack) const override;
0093 
0094 protected:
0095     QString drv_escapeIdentifier(const QString& str) const override;
0096     QByteArray drv_escapeIdentifier(const QByteArray& str) const override;
0097     KDbConnection *drv_createConnection(const KDbConnectionData& connData,
0098                                                 const KDbConnectionOptions &options) override;
0099     KDbAdminTools* drv_createAdminTools() const override;
0100 
0101     /*! @return true if @a n is a system field name;
0102       for this driver fields with name equal "_ROWID_"
0103       is considered as system field.
0104     */
0105     bool drv_isSystemFieldName(const QString &n) const override;
0106 
0107     SqliteDriverPrivate * const dp;
0108 
0109 private:
0110     static const char * const keywords[];
0111     Q_DISABLE_COPY(SqliteDriver)
0112 };
0113 
0114 #endif