1 /* This file is part of the KDE project
2    Copyright (C) 2003-2018 Jarosław Staniek <staniek@kde.org>
3 
4    This library 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 library 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 library; see the file COPYING.LIB.  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_DRIVERBEHAVIOR_H
21 #define KDB_DRIVERBEHAVIOR_H
22 
23 #include "KDbDriver.h"
24 #include "KDbUtils.h"
25 
26 /**
27  * @brief Detailed definition of driver's default behavior
28  *
29  * This class is mostly interesting for KDb driver developers.
30  * Defaults can be changed by KDbDriver subclass in constructors.
31  */
32 class KDB_EXPORT KDbDriverBehavior
33 {
34 public:
35     explicit KDbDriverBehavior(KDbDriver *driver);
36 
37     ~KDbDriverBehavior();
38 
39     /*! Features (like transactions, etc.) supported by this driver
40      (sum of selected  Features enum items).
41      This member should be filled in driver implementation's constructor
42      (by default m_features==NoFeatures). */
43     int features;
44 
45     //! real type names for this engine
46     QVector<QString> typeNames;
47 
48     /*! Driver properties (indexed by name), useful for presenting properties to the user.
49      Contains i18n-ed captions.
50      In driver implementations available properties can be initialized, for example:
51      @code
52         beh->properties.insert("maximum_performance", 1000, SomeClass::tr("Maximum performance"));
53      @endcode
54      You do not need to set captions of properties predefined by the KDbDriver superclass,
55      they will be reused. Predefined properties are set in KDbDriver. */
56     KDbUtils::PropertySet properties;
57 
58     //! "UNSIGNED" by default
59     QString UNSIGNED_TYPE_KEYWORD;
60 
61     //! "AUTO_INCREMENT" by default, used as add-in word to field definition
62     //! May be also used as full definition if SPECIAL_AUTO_INCREMENT_DEF is true.
63     QString AUTO_INCREMENT_FIELD_OPTION;
64 
65     //! "AUTO_INCREMENT PRIMARY KEY" by default, used as add-in word to field definition
66     //! May be also used as full definition if SPECIAL_AUTO_INCREMENT_DEF is true.
67     QString AUTO_INCREMENT_PK_FIELD_OPTION;
68 
69     //! "" by default, used as type string for autoinc. field definition
70     //! pgsql defines it as "SERIAL", sqlite defines it as "INTEGER"
71     QString AUTO_INCREMENT_TYPE;
72 
73     /*! True if autoincrement field has special definition
74      e.g. like "INTEGER PRIMARY KEY" for SQLite.
75      Special definition string should be stored in AUTO_INCREMENT_FIELD_OPTION.
76      False by default. */
77     bool SPECIAL_AUTO_INCREMENT_DEF;
78 
79     /*! True if autoincrement requires field to be declared as primary key.
80      This is true for SQLite. False by default. */
81     bool AUTO_INCREMENT_REQUIRES_PK;
82 
83     /*! Name of a field (or built-in function) with autoincremented unique value,
84      typically returned by KDbSqlResult::lastInsertRecordId().
85 
86      Examples:
87      - PostgreSQL and SQLite engines use 'OID' field
88      - MySQL uses LAST_INSERT_ID() built-in function
89     */
90     QString ROW_ID_FIELD_NAME;
91 
92     /*! True if the value (fetched from field or function,
93      defined by ROW_ID_FIELD_NAME member) is EXACTLY the value of autoincremented field,
94      not an implicit (internal) row number. Default value is false.
95 
96      Examples:
97      - PostgreSQL and SQLite engines have this flag set to false ('OID' field has
98         it's own implicit value)
99      - MySQL engine has this flag set to true (LAST_INSERT_ID() returns real value
100      of last autoincremented field).
101 
102      Notes:
103      If it's false, we have a convenient way for identifying row even when there's
104      no primary key defined. So, as '_ROWID' column in MySQL is really
105      just a synonym for the primary key, this engine needs to have primary keys always
106      defined if we want to use interactive editing features like row updating and deleting.
107     */
108     bool ROW_ID_FIELD_RETURNS_LAST_AUTOINCREMENTED_VALUE;
109 
110     /*! Name of any (e.g. first found) database for this connection that
111      typically always exists. This can be not set if we want to do some magic checking
112      what database name is availabe by reimplementing
113      KDbConnection::anyAvailableDatabaseName().
114      Example: for PostgreSQL this is "template1".
115 
116      @see KDbConnection::SetAvailableDatabaseName()
117     */
118     QString ALWAYS_AVAILABLE_DATABASE_NAME;
119 
120     /*! Opening quotation mark used for escaping identifier (see KDbDriver::escapeIdentifier()).
121      Default value is '"'. Change it for your driver.
122     */
123     char OPENING_QUOTATION_MARK_BEGIN_FOR_IDENTIFIER;
124 
125     /*! Opening quotation mark used for escaping identifier (see KDbDriver::escapeIdentifier()).
126      Default value is '"'. Change it for your driver.
127     */
128     char CLOSING_QUOTATION_MARK_BEGIN_FOR_IDENTIFIER;
129 
130     /*! True if using database is required to perform real connection.
131      This is true for may engines, e.g. for PostgreSQL, where connection
132      strings should contain a database name.
133      If the flag is false, then developer has to call KDbConnection::useDatabase()
134      after creating the KDbConnection object.
135      This flag can be also used for file-based db drivers, e.g. SQLite sets it to true.
136      MySQL sets it to false.
137      By default this flag is true.
138     */
139     bool USING_DATABASE_REQUIRED_TO_CONNECT;
140 
141     /*! True if connection should be established (KDbConnection::connect()) in order
142      to check database existence (KDbConnection::databaseExists()).
143      This is true for most engines, but not for SQLite (and probably most
144      file-based databases) where existence of database is checked at filesystem level.
145      By default this flag is true.
146     */
147     bool CONNECTION_REQUIRED_TO_CHECK_DB_EXISTENCE;
148 
149     /*! True if connection should be established (KDbConnection::connect()) in order
150      to create new database (KDbConnection::createDatabase()).
151      This is true for most engines, but not for SQLite (and probably most
152      file-based databases) where opening physical connection for nonexisting
153      file creates new file.
154      By default this flag is true.
155     */
156     bool CONNECTION_REQUIRED_TO_CREATE_DB;
157 
158     /*! True if connection should be established (KDbConnection::connect()) in order
159      to drop database (KDbConnection::dropDatabase()).
160      This is true for most engines, but not for SQLite (and probably most
161      file-based databases) where dropping database is performed at filesystem level.
162      By default this flag is true.
163     */
164     bool CONNECTION_REQUIRED_TO_DROP_DB;
165 
166     /*! Because some engines need to have opened any database before
167      executing administrative sql statements like "create database" or "drop database",
168      using appropriate, existing temporary database for this connection.
169      This is the case for PostgreSQL.
170      For file-based db drivers this flag is ignored.
171      By default this flag is false.
172 
173      Note: This method has nothing to do with creating or using temporary databases
174      in such meaning that these database are not persistent.
175 
176      @see KDbConnection::useTemporaryDatabaseIfNeeded()
177     */
178     bool USE_TEMPORARY_DATABASE_FOR_CONNECTION_IF_NEEDED;
179 
180     /*! Set to @c true in a subclass if after successful drv_createDatabase() the database
181      is in opened state (as after useDatabase()).
182      @c false for most engines. */
183     bool IS_DB_OPEN_AFTER_CREATE;
184 
185     /*! True if before we know whether the fetched result of executed query
186      is empty or not, we need to fetch first record. Particularly, it's true for SQLite.
187      The flag is used in KDbCursor::open(). By default this flag is false. */
188     bool _1ST_ROW_READ_AHEAD_REQUIRED_TO_KNOW_IF_THE_RESULT_IS_EMPTY;
189 
190     /*! True if "SELECT 1 from (subquery)" is supported. False by default.
191      Used in KDbConnection::resultExists() for optimization. It's set to true for SQLite driver. */
192     bool SELECT_1_SUBQUERY_SUPPORTED;
193 
194     /*! Literal for boolean true. "1" by default
195         which is typically expected by backends even while the standard says "TRUE":
196         https://troels.arvin.dk/db/rdbms/#data_types-boolean
197     */
198     QString BOOLEAN_TRUE_LITERAL;
199 
200     /*! Literal for boolean false. "0" by default
201         which is typically expected by backends even while the standard says "TRUE":
202         https://troels.arvin.dk/db/rdbms/#data_types-boolean
203     */
204     QString BOOLEAN_FALSE_LITERAL;
205 
206     /*! Specifies maximum length for Text type. If 0, there is are limits and length argument is not needed,
207      e.g. VARCHAR works for the driver this is the case for SQLite and PostgreSQL.
208      If greater than 0, this value should be used to express maximum value, e.g. VARCHAR(...).
209      This is the case for MySQL.
210      The default is 0. */
211     int TEXT_TYPE_MAX_LENGTH;
212 
213     /*! "LIKE" by default, used to construct native expressions "x LIKE y" and "x NOT LIKE y".
214      LIKE is case-insensitive for MySQL, SQLite, and often on Sybase/MSSQL
215      while for PostgreSQL it's case-sensitive. So for PostgreSQL LIKE_OPERATOR == "ILIKE". */
216     QString LIKE_OPERATOR;
217 
218     /*! "RANDOM()" function name, used in Driver::randomFunctionToString() to construct native
219      expressions. */
220     QString RANDOM_FUNCTION;
221 
222     /**
223      * SQL statement used to obtain list of physical table names.
224      * Used by default implementation of KDbConnection::drv_getTableNames(). Empty by default.
225      * If empty, default implementation of KDbConnection::drv_getTableNames() fails.
226      *
227      * @since 3.2
228      */
229     KDbEscapedString GET_TABLE_NAMES_SQL;
230 
231 private:
232     void initInternalProperties();
233     friend class KDbDriver;
234 
235     Q_DISABLE_COPY(KDbDriverBehavior)
236     class Private;
237     Private * const d;
238 };
239 
240 #endif
241