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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2016 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_CONN_SQLITE_H
0021 #define KDB_CONN_SQLITE_H
0022 
0023 #include <QStringList>
0024 
0025 #include "KDbConnection.h"
0026 
0027 class SqliteConnectionInternal;
0028 class KDbDriver;
0029 
0030 /*! @brief SQLite-specific connection
0031     Following connection options are supported (see KDbConnectionOptions):
0032     - extraSqliteExtensionPaths (read/write, QStringList): adds extra seach paths for SQLite
0033                                 extensions. Set them before KDbConnection::useDatabase()
0034                                 is called. Absolute paths are recommended.
0035 */
0036 class SqliteConnection : public KDbConnection
0037 {
0038     Q_DECLARE_TR_FUNCTIONS(SqliteConnection)
0039 public:
0040     ~SqliteConnection() override;
0041 
0042     Q_REQUIRED_RESULT KDbCursor *prepareQuery(const KDbEscapedString &sql,
0043                                               KDbCursor::Options options
0044                                               = KDbCursor::Option::None) override;
0045     Q_REQUIRED_RESULT KDbCursor *prepareQuery(KDbQuerySchema *query,
0046                                               KDbCursor::Options options
0047                                               = KDbCursor::Option::None) override;
0048 
0049     Q_REQUIRED_RESULT KDbPreparedStatementInterface *prepareStatementInternal() override;
0050 
0051 protected:
0052     /*! Used by driver */
0053     SqliteConnection(KDbDriver *driver, const KDbConnectionData& connData,
0054                      const KDbConnectionOptions &options);
0055 
0056     bool drv_connect() override;
0057     bool drv_getServerVersion(KDbServerVersionInfo* version) override;
0058     bool drv_disconnect() override;
0059     bool drv_getDatabasesList(QStringList* list) override;
0060 
0061 #if 0 // TODO
0062 //! @todo move this somewhere to low level class (MIGRATION?)
0063     virtual bool drv_getTablesList(QStringList* list);
0064 #endif
0065 
0066 //! @todo move this somewhere to low level class (MIGRATION?)
0067     tristate drv_containsTable(const QString &tableName) override;
0068 
0069     /*! Creates new database using connection. Note: Do not pass @a dbName
0070       arg because for file-based engine (that has one database per connection)
0071       it is defined during connection. */
0072     bool drv_createDatabase(const QString &dbName = QString()) override;
0073 
0074     /*! Opens existing database using connection. Do not pass @a dbName
0075       arg because for file-based engine (that has one database per connection)
0076       it is defined during connection. If you pass it,
0077       database file name will be changed. */
0078     bool drv_useDatabase(const QString &dbName = QString(), bool *cancelled = nullptr,
0079                          KDbMessageHandler* msgHandler = nullptr) override;
0080 
0081     bool drv_closeDatabase() override;
0082 
0083     /*! Drops database from the server using connection.
0084       After drop, database shouldn't be accessible
0085       anymore, so database file is just removed. See note from drv_useDatabase(). */
0086     bool drv_dropDatabase(const QString &dbName = QString()) override;
0087 
0088     KDbSqlResult* drv_prepareSql(const KDbEscapedString& sql) override;
0089 
0090     bool drv_executeSql(const KDbEscapedString& sql) override;
0091 
0092     //! Implemented for KDbResultable
0093     QString serverResultName() const override;
0094 
0095     void storeResult();
0096 
0097     tristate drv_changeFieldProperty(KDbTableSchema* table, KDbField* field,
0098             const QString& propertyName, const QVariant& value) override;
0099 
0100     //! for drv_changeFieldProperty()
0101     tristate changeFieldType(KDbTableSchema *table, KDbField *field, KDbField::Type type);
0102 
0103     SqliteConnectionInternal* d;
0104 
0105 private:
0106     bool drv_useDatabaseInternal(bool *cancelled, KDbMessageHandler* msgHandler, bool createIfMissing);
0107 
0108     //! Closes database without altering stored result number and message
0109     void drv_closeDatabaseSilently();
0110 
0111     //! Finds a native SQLite extension @a name in the search path and loads it.
0112     //! Path and filename extension should not be provided.
0113     //! @return true on success
0114     bool findAndLoadExtension(const QString & name);
0115 
0116     //! Loads extension from plugin at @a path (absolute path is recommended)
0117     //! @return true on success
0118     bool loadExtension(const QString& path);
0119 
0120     friend class SqliteDriver;
0121     friend class SqliteCursor;
0122     friend class SqliteSqlResult;
0123     Q_DISABLE_COPY(SqliteConnection)
0124 };
0125 
0126 #endif