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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
0003    Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
0004    Copyright (C) 2003 Joseph Wenninger<jowenn@kde.org>
0005    Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
0006 
0007    This program is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This program is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this program; see the file COPYING.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #ifndef KDB_MYSQLDRIVER_H
0024 #define KDB_MYSQLDRIVER_H
0025 
0026 #include "KDbDriver.h"
0027 
0028 //! MySQL database driver.
0029 class MysqlDriver : public KDbDriver
0030 {
0031     Q_OBJECT
0032 
0033 public:
0034     /*!
0035      * Constructor sets database features and
0036      * maps the types in KDbField::Type to the MySQL types.
0037      *
0038      * See: https://dev.mysql.com/doc/mysql/en/Column_types.html
0039      */
0040     MysqlDriver(QObject *parent, const QVariantList &args);
0041 
0042     ~MysqlDriver() override;
0043 
0044     /*! @return false for this driver. */
0045     bool isSystemObjectName(const QString& name) const override;
0046 
0047     /*! @return true if @a is "mysql", "information_schema" or "performance_schema". */
0048     bool isSystemDatabaseName(const QString &name) const override;
0049 
0050     //! Escape a string for use as a value
0051     KDbEscapedString escapeString(const QString& str) const override;
0052     KDbEscapedString escapeString(const QByteArray& str) const override;
0053 
0054     //! Escape BLOB value @a array
0055     KDbEscapedString escapeBLOB(const QByteArray& array) const override;
0056 
0057     //! Overrides the default implementation
0058     QString sqlTypeName(KDbField::Type type, const KDbField &field) const override;
0059 
0060     //! Generates native (driver-specific) LENGTH() function call.
0061     //! char_length(val) is used because length(val) in mysql returns number of bytes,
0062     //! what is not right for multibyte (unicode) encodings. */
0063     KDbEscapedString lengthFunctionToString(const KDbNArgExpression &args,
0064                                             KDbQuerySchemaParameterValueListIterator *params,
0065                                             KDb::ExpressionCallStack *callStack) const override;
0066 
0067     //! Generates native (driver-specific) GREATEST() and LEAST() function call.
0068     //! Since MySQL's LEAST()/GREATEST() function ignores NULL values, it only returns NULL
0069     //! if all the expressions evaluate to NULL. So this is used for F(v0,..,vN):
0070     //! (CASE WHEN (v0) IS NULL OR .. OR (vN) IS NULL THEN NULL ELSE F(v0,..,vN) END)
0071     //! where F == GREATEST or LEAST.
0072     KDbEscapedString greatestOrLeastFunctionToString(const QString &name,
0073                                                      const KDbNArgExpression &args,
0074                                                      KDbQuerySchemaParameterValueListIterator* params,
0075                                                      KDb::ExpressionCallStack* callStack) const override;
0076 
0077     //! Generates native (driver-specific) UNICODE() function call.
0078     //! Uses ORD(CONVERT(X USING UTF16)).
0079     KDbEscapedString unicodeFunctionToString(const KDbNArgExpression &args,
0080                                              KDbQuerySchemaParameterValueListIterator* params,
0081                                              KDb::ExpressionCallStack* callStack) const override;
0082 
0083     //! Generates native (driver-specific) function call for concatenation of two strings.
0084     //! Uses CONCAT().
0085     KDbEscapedString concatenateFunctionToString(const KDbBinaryExpression &args,
0086                                                  KDbQuerySchemaParameterValueListIterator* params,
0087                                                  KDb::ExpressionCallStack* callStack) const;
0088 
0089 protected:
0090     QString drv_escapeIdentifier(const QString& str) const override;
0091     QByteArray drv_escapeIdentifier(const QByteArray &str) const override;
0092     KDbConnection *drv_createConnection(const KDbConnectionData &connData,
0093                                         const KDbConnectionOptions &options) override;
0094     bool drv_isSystemFieldName(const QString& name) const override;
0095     bool supportsDefaultValue(const KDbField &field) const override;
0096 
0097 private:
0098     static const char *keywords[];
0099     QString m_longTextPrimaryKeyType;
0100     Q_DISABLE_COPY(MysqlDriver)
0101 };
0102 
0103 #endif