1 /* This file is part of the KDE project
2    Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
3 
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Library General Public
6    License as published by the Free Software Foundation; either
7    version 2 of the License, or (at your option) any later version.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Library General Public License for more details.
13 
14    You should have received a copy of the GNU Library General Public License
15    along with this program; see the file COPYING.  If not, write to
16    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18 */
19 
20 #ifndef KDB_CONN_SQLITE_H
21 #define KDB_CONN_SQLITE_H
22 
23 #include <QStringList>
24 
25 #include "KDbConnection.h"
26 
27 class SqliteConnectionInternal;
28 class KDbDriver;
29 
30 /*! @brief SQLite-specific connection
31     Following connection options are supported (see KDbConnectionOptions):
32     - extraSqliteExtensionPaths (read/write, QStringList): adds extra seach paths for SQLite
33                                 extensions. Set them before KDbConnection::useDatabase()
34                                 is called. Absolute paths are recommended.
Private()35 */
36 class SqliteConnection : public KDbConnection
37 {
38     Q_DECLARE_TR_FUNCTIONS(SqliteConnection)
39 public:
40     ~SqliteConnection() override;
41 
42     KDbCursor *prepareQuery(const KDbEscapedString &sql, KDbCursor::Options options
43                             = KDbCursor::Option::None) override Q_REQUIRED_RESULT;
44     KDbCursor *prepareQuery(KDbQuerySchema *query, KDbCursor::Options options
45                             = KDbCursor::Option::None) override Q_REQUIRED_RESULT;
46 
47     KDbPreparedStatementInterface* prepareStatementInternal() override Q_REQUIRED_RESULT;
48 
49 protected:
50     /*! Used by driver */
51     SqliteConnection(KDbDriver *driver, const KDbConnectionData& connData,
52                      const KDbConnectionOptions &options);
53 
54     bool drv_connect() override;
55     bool drv_getServerVersion(KDbServerVersionInfo* version) override;
56     bool drv_disconnect() override;
57     bool drv_getDatabasesList(QStringList* list) override;
58 
59 #if 0 // TODO
60 //! @todo move this somewhere to low level class (MIGRATION?)
61     virtual bool drv_getTablesList(QStringList* list);
62 #endif
63 
64 //! @todo move this somewhere to low level class (MIGRATION?)
65     tristate drv_containsTable(const QString &tableName) override;
66 
67     /*! Creates new database using connection. Note: Do not pass @a dbName
68       arg because for file-based engine (that has one database per connection)
69       it is defined during connection. */
70     bool drv_createDatabase(const QString &dbName = QString()) override;
71 
72     /*! Opens existing database using connection. Do not pass @a dbName
73       arg because for file-based engine (that has one database per connection)
74       it is defined during connection. If you pass it,
75       database file name will be changed. */
76     bool drv_useDatabase(const QString &dbName = QString(), bool *cancelled = nullptr,
77                          KDbMessageHandler* msgHandler = nullptr) override;
78 
79     bool drv_closeDatabase() override;
80 
81     /*! Drops database from the server using connection.
82       After drop, database shouldn't be accessible
83       anymore, so database file is just removed. See note from drv_useDatabase(). */
84     bool drv_dropDatabase(const QString &dbName = QString()) override;
85 
86     KDbSqlResult* drv_prepareSql(const KDbEscapedString& sql) override;
87 
88     bool drv_executeSql(const KDbEscapedString& sql) override;
89 
90     //! Implemented for KDbResultable
91     QString serverResultName() const override;
92 
93     void storeResult();
94 
95     tristate drv_changeFieldProperty(KDbTableSchema* table, KDbField* field,
96             const QString& propertyName, const QVariant& value) override;
97 
98     //! for drv_changeFieldProperty()
99     tristate changeFieldType(KDbTableSchema *table, KDbField *field, KDbField::Type type);
100 
101     SqliteConnectionInternal* d;
102 
103 private:
104     bool drv_useDatabaseInternal(bool *cancelled, KDbMessageHandler* msgHandler, bool createIfMissing);
105 
106     //! Closes database without altering stored result number and message
107     void drv_closeDatabaseSilently();
108 
109     //! Finds a native SQLite extension @a name in the search path and loads it.
110     //! Path and filename extension should not be provided.
111     //! @return true on success
112     bool findAndLoadExtension(const QString & name);
113 
114     //! Loads extension from plugin at @a path (absolute path is recommended)
115     //! @return true on success
116     bool loadExtension(const QString& path);
117 
118     friend class SqliteDriver;
119     friend class SqliteCursor;
120     friend class SqliteSqlResult;
121     Q_DISABLE_COPY(SqliteConnection)
122 };
123 
KDbLookupFieldSchemaRecordSource(const KDbLookupFieldSchemaRecordSource & other)124 #endif
125