1 /* This file is part of the KDE project
2    Copyright (C) 2004-2015 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 KDBTABLEORQUERYSCHEMA_H
21 #define KDBTABLEORQUERYSCHEMA_H
22 
23 #include <QByteArray>
24 
25 #include "KDbQueryColumnInfo.h"
26 
27 class KDbConnection;
28 class KDbFieldList;
29 class KDbTableSchema;
30 class KDbQuerySchema;
31 
32 /*! Variant class providing a pointer to table or query. */
33 class KDB_EXPORT KDbTableOrQuerySchema
34 {
35 public:
36     //! Type of object: table or query
37     //! @since 3.1
38     enum class Type {
39         Table,
40         Query
41     };
42 
43     /*! Creates a new KDbTableOrQuerySchema variant object, retrieving table or query schema
44      using @a conn connection and @a name. If both table and query exists for @a name,
45      table has priority over query.
46      Check whether a query or table has been found by testing (query() || table()) expression. */
47     KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray& name);
48 
49     /*! Creates a new KDbTableOrQuerySchema variant object, retrieving table or query schema
50      using @a conn connection and @a name. If @a type is Table, @a name is assumed
51      to be a table name, otherwise @a name is assumed to be a query name.
52      Check whether a query or table has been found by testing (query() || table()) expression.
53      @since 3.1 */
54     KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray &name, Type type);
55 
56     /*! Creates a new KDbTableOrQuerySchema variant object. @a tableOrQuery should be of
57      class KDbTableSchema or KDbQuerySchema.
58      Check whether a query or table has been found by testing (query() || table()) expression. */
59     explicit KDbTableOrQuerySchema(KDbFieldList *tableOrQuery);
60 
61     /*! Creates a new KDbTableOrQuerySchema variant object, retrieving table or query schema
62      using @a conn connection and @a id.
63      Check whether a query or table has been found by testing (query() || table()) expression. */
64     KDbTableOrQuerySchema(KDbConnection *conn, int id);
65 
66     /*! Creates a new KDbTableOrQuerySchema variant object, keeping a pointer to @a table
67      object. */
68     explicit KDbTableOrQuerySchema(KDbTableSchema* table);
69 
70     /*! Creates a new KDbTableOrQuerySchema variant object, keeping a pointer to @a query
71      object. */
72     explicit KDbTableOrQuerySchema(KDbQuerySchema* query);
73 
74     ~KDbTableOrQuerySchema();
75 
76     //! @return a pointer to the query if it's provided
77     KDbQuerySchema* query() const;
78 
79     //! @return a pointer to the table if it's provided
80     KDbTableSchema* table() const;
81 
82     //! @return name of a query or table
83     QByteArray name() const;
84 
85     //! @return caption (if present) or name of the table/query
86     QString captionOrName() const;
87 
88     /**
89      * @brief Returns number of columns within record set returned from specified table or query
90      *
91      * In case of query expanded fields list is counted.
92      * For tables @a conn is not required.
93      * Returns -1 if the object has neither table or query assigned.
94      * Returns -1 if the object has query assigned but @a conn is @c nullptr.
95      */
96     int fieldCount(KDbConnection *conn) const;
97 
98     /*! Mode for columns(). */
99     enum class ColumnsMode {
100         NonUnique, //!< Non-unique columns are returned
101         Unique     //!< Unique columns are returned
102     };
103 
104     //! @return all columns for the table or the query
105     const KDbQueryColumnInfo::Vector columns(KDbConnection *conn, ColumnsMode mode = ColumnsMode::NonUnique);
106 
107     /*! @return a field of the table or the query schema for name @a name
108      or 0 if there is no such field. */
109     KDbField* field(const QString& name);
110 
111     /*! Like KDbField* field(const QString& name);
112      but returns all information associated with field/column @a name. */
113     KDbQueryColumnInfo* columnInfo(KDbConnection *conn, const QString& name);
114 
115 private:
116     class Private;
117     Private * const d;
118 
119     Q_DISABLE_COPY(KDbTableOrQuerySchema)
120 };
121 
122 //! A pair (connection, table-or-schema) for QDebug operator<<
123 //! @since 3.1
124 typedef std::tuple<KDbConnection*, const KDbTableOrQuerySchema&> KDbConnectionAndSchema;
125 
126 //! Sends information about table or query schema and connection @a connectionAndSchema to debug output @a dbg.
127 //! @since 3.1
128 KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbConnectionAndSchema &connectionAndSchema);
129 
130 #endif
131