File indexing completed on 2024-04-21 04:40:34

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_KDBNATIVESTATEMENTBUILDER_H
0021 #define KDB_KDBNATIVESTATEMENTBUILDER_H
0022 
0023 #include "KDb.h"
0024 #include "KDbSelectStatementOptions.h"
0025 
0026 //! A builder for generating various types of native SQL statements
0027 /*! The statement strings can be specific for the used connection and database driver,
0028     and thus generally not portable across connections. */
0029 class KDB_EXPORT KDbNativeStatementBuilder
0030 {
0031 public:
0032     /**
0033      * Creates a new native builder object. @a connection is required.
0034      *
0035      * If @a dialect is KDbEscaping generated statement strings will be of KDbSQL dialect,
0036      * else they will be specific to database connection or connection's database driver.
0037      */
0038     KDbNativeStatementBuilder(KDbConnection *connection, KDb::IdentifierEscapingType dialect);
0039 
0040     ~KDbNativeStatementBuilder();
0041 
0042     /*! Generates a native "SELECT ..." statement string that can be used for executing
0043      query defined by @a querySchema, @a params and @a options.
0044 
0045      @a target and @a querySchema must not be 0. The statement is written to @ref *target on success.
0046      @return true on success. */
0047     bool generateSelectStatement(KDbEscapedString *target, KDbQuerySchema* querySchema,
0048                                  const KDbSelectStatementOptions& options,
0049                                  const QList<QVariant>& parameters = QList<QVariant>()) const;
0050 
0051     /*! @overload generateSelectStatement(KDbEscapedString *target, KDbQuerySchema* querySchema,
0052                                          const KDbSelectStatementOptions& options,
0053                                          const QList<QVariant>& parameters) const. */
0054     bool generateSelectStatement(KDbEscapedString *target,
0055                                  KDbQuerySchema* querySchema,
0056                                  const QList<QVariant>& parameters = QList<QVariant>()) const;
0057 
0058     /*! Generates a native "SELECT ..." statement string that can be used for executing
0059      query defined by an functional equivalent of a "SELECT * FROM table_name" statement
0060      where <i>table_name</i> is @a tableSchema's name. @a params and @a options are used
0061      like in the
0062      @ref toSelectStatement(KDbEscapedString*, KDbQuerySchema*, const KDbSelectStatementOptions&, const QList<QVariant>&)
0063      variant.
0064      @a target and @a querySchema must not be 0.
0065      @return true on success. */
0066     bool generateSelectStatement(KDbEscapedString *target, KDbTableSchema* tableSchema,
0067                                  const KDbSelectStatementOptions& options = KDbSelectStatementOptions()) const;
0068 
0069     /*! Generates a native "CREATE TABLE ..." statement string that can be used for creation
0070      of @a tableSchema in the database. The statement is written to @ref *target on success.
0071      @return true on success.
0072      If @a target is @c nullptr, @c false is returned.
0073     */
0074     bool generateCreateTableStatement(KDbEscapedString *target,
0075                                       const KDbTableSchema& tableSchema) const;
0076 
0077 private:
0078     Q_DISABLE_COPY(KDbNativeStatementBuilder)
0079     class Private;
0080     Private * const d;
0081 };
0082 
0083 #endif