File indexing completed on 2022-11-23 11:08:57

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004-2015 Jarosław Staniek <staniek@kde.org>
0003 
0004    This library 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 library 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 library; see the file COPYING.LIB.  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 KDBTABLEORQUERYSCHEMA_H
0021 #define KDBTABLEORQUERYSCHEMA_H
0022 
0023 #include <QByteArray>
0024 
0025 #include "KDbQueryColumnInfo.h"
0026 
0027 class KDbConnection;
0028 class KDbFieldList;
0029 class KDbTableSchema;
0030 class KDbQuerySchema;
0031 
0032 /*! Variant class providing a pointer to table or query. */
0033 class KDB_EXPORT KDbTableOrQuerySchema
0034 {
0035 public:
0036     //! Type of object: table or query
0037     //! @since 3.1
0038     enum class Type {
0039         Table,
0040         Query
0041     };
0042 
0043     /*! Creates a new KDbTableOrQuerySchema variant object, retrieving table or query schema
0044      using @a conn connection and @a name. If both table and query exists for @a name,
0045      table has priority over query.
0046      Check whether a query or table has been found by testing (query() || table()) expression. */
0047     KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray& name);
0048 
0049     /*! Creates a new KDbTableOrQuerySchema variant object, retrieving table or query schema
0050      using @a conn connection and @a name. If @a type is Table, @a name is assumed
0051      to be a table name, otherwise @a name is assumed to be a query name.
0052      Check whether a query or table has been found by testing (query() || table()) expression.
0053      @since 3.1 */
0054     KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray &name, Type type);
0055 
0056     /*! Creates a new KDbTableOrQuerySchema variant object. @a tableOrQuery should be of
0057      class KDbTableSchema or KDbQuerySchema.
0058      Check whether a query or table has been found by testing (query() || table()) expression. */
0059     explicit KDbTableOrQuerySchema(KDbFieldList *tableOrQuery);
0060 
0061     /*! Creates a new KDbTableOrQuerySchema variant object, retrieving table or query schema
0062      using @a conn connection and @a id.
0063      Check whether a query or table has been found by testing (query() || table()) expression. */
0064     KDbTableOrQuerySchema(KDbConnection *conn, int id);
0065 
0066     /*! Creates a new KDbTableOrQuerySchema variant object, keeping a pointer to @a table
0067      object. */
0068     explicit KDbTableOrQuerySchema(KDbTableSchema* table);
0069 
0070     /*! Creates a new KDbTableOrQuerySchema variant object, keeping a pointer to @a query
0071      object. */
0072     explicit KDbTableOrQuerySchema(KDbQuerySchema* query);
0073 
0074     ~KDbTableOrQuerySchema();
0075 
0076     //! @return a pointer to the query if it's provided
0077     KDbQuerySchema* query() const;
0078 
0079     //! @return a pointer to the table if it's provided
0080     KDbTableSchema* table() const;
0081 
0082     //! @return name of a query or table
0083     QByteArray name() const;
0084 
0085     //! @return caption (if present) or name of the table/query
0086     QString captionOrName() const;
0087 
0088     /**
0089      * @brief Returns number of columns within record set returned from specified table or query
0090      *
0091      * In case of query expanded fields list is counted.
0092      * For tables @a conn is not required.
0093      * Returns -1 if the object has neither table or query assigned.
0094      * Returns -1 if the object has query assigned but @a conn is @c nullptr.
0095      */
0096     int fieldCount(KDbConnection *conn) const;
0097 
0098     /*! Mode for columns(). */
0099     enum class ColumnsMode {
0100         NonUnique, //!< Non-unique columns are returned
0101         Unique     //!< Unique columns are returned
0102     };
0103 
0104     //! @return all columns for the table or the query
0105     const KDbQueryColumnInfo::Vector columns(KDbConnection *conn, ColumnsMode mode = ColumnsMode::NonUnique);
0106 
0107     /*! @return a field of the table or the query schema for name @a name
0108      or 0 if there is no such field. */
0109     KDbField* field(const QString& name);
0110 
0111     /*! Like KDbField* field(const QString& name);
0112      but returns all information associated with field/column @a name. */
0113     KDbQueryColumnInfo* columnInfo(KDbConnection *conn, const QString& name);
0114 
0115 private:
0116     class Private;
0117     Private * const d;
0118 
0119     Q_DISABLE_COPY(KDbTableOrQuerySchema)
0120 };
0121 
0122 //! A pair (connection, table-or-schema) for QDebug operator<<
0123 //! @since 3.1
0124 typedef std::tuple<KDbConnection*, const KDbTableOrQuerySchema&> KDbConnectionAndSchema;
0125 
0126 //! Sends information about table or query schema and connection @a connectionAndSchema to debug output @a dbg.
0127 //! @since 3.1
0128 KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbConnectionAndSchema &connectionAndSchema);
0129 
0130 #endif