KDb

KDbTableOrQuerySchema.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2004-2015 Jarosław Staniek <staniek@kde.org>
3 Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
4 Copyright (c) 1999 Preston Brown <pbrown@kde.org>
5 Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21*/
22
23#include "KDbTableOrQuerySchema.h"
24#include "KDbConnection.h"
25#include "KDbQuerySchema.h"
26#include "kdb_debug.h"
27
28class Q_DECL_HIDDEN KDbTableOrQuerySchema::Private
29{
30public:
31 Private() {}
32 //! The name is kept here because m_table and m_table can be 0
33 //! and we still want name() and acptionOrName() work.
35
36 KDbTableSchema* table;
37
39private:
40 Q_DISABLE_COPY(Private)
41};
42
44 : d(new Private)
45{
46 d->name = name;
47 d->table = conn->tableSchema(QLatin1String(name));
48 d->query = d->table ? nullptr : conn->querySchema(QLatin1String(name));
49 if (!d->table && !d->query) {
50 kdbWarning() << "tableOrQuery is neither table nor query!";
51 }
52}
53
55 : d(new Private)
56{
57 d->name = name;
58 d->table = type == Type::Table ? conn->tableSchema(QLatin1String(name)) : nullptr;
59 d->query = type == Type::Query ? conn->querySchema(QLatin1String(name)) : nullptr;
60 if (type == Type::Table && !d->table) {
61 kdbWarning() << "no table specified!";
62 }
63 if (type == Type::Query && !d->query) {
64 kdbWarning() << "no query specified!";
65 }
66}
67
69 : d(new Private)
70{
71 d->table = dynamic_cast<KDbTableSchema*>(tableOrQuery);
72 d->query = dynamic_cast<KDbQuerySchema*>(tableOrQuery);
73 if (!d->table && !d->query) {
74 kdbWarning() << "tableOrQuery is neither table nor query!";
75 }
76}
77
79 : d(new Private)
80{
81 d->table = conn->tableSchema(id);
82 d->query = d->table ? nullptr : conn->querySchema(id);
83 if (!d->table && !d->query) {
84 kdbWarning() << "no table or query found for id==" << id;
85 }
86}
87
89 : d(new Private)
90{
91 d->table = table;
92 d->query = nullptr;
93 if (!d->table) {
94 kdbWarning() << "no table specified!";
95 }
96}
97
99 : d(new Private)
100{
101 d->table = nullptr;
102 d->query = query;
103 if (!d->query) {
104 kdbWarning() << "no query specified!";
105 }
106}
107
108KDbTableOrQuerySchema::~KDbTableOrQuerySchema()
109{
110 delete d;
111}
112
114{
115 if (d->table)
116 return d->table->fieldCount();
117 if (d->query && conn)
118 return d->query->fieldsExpanded(conn).size();
119 return -1;
120}
121
123{
124 if (d->table) {
125 return d->table->query()->fieldsExpanded(conn, mode == ColumnsMode::Unique
128 }
129 if (d->query) {
130 return d->query->fieldsExpanded(conn, mode == ColumnsMode::Unique
133 }
134 kdbWarning() << "no query or table specified!";
136}
137
139{
140 if (d->table)
141 return d->table->name().toLatin1();
142 if (d->query)
143 return d->query->name().toLatin1();
144 return d->name;
145}
146
148{
149 KDbObject *object = d->table ? static_cast<KDbObject *>(d->table) : static_cast<KDbObject *>(d->query);
150 if (!object)
151 return QLatin1String(d->name);
152 return object->caption().isEmpty() ? object->name() : object->caption();
153}
154
156{
157 if (d->table)
158 return d->table->field(name);
159 if (d->query)
160 return d->query->field(name);
161
162 return nullptr;
163}
164
166{
167 if (d->table)
168 return d->table->query()->columnInfo(conn, name);
169
170 if (d->query)
171 return d->query->columnInfo(conn, name);
172
173 return nullptr;
174}
175
176//! Sends information about table or query schema @a schema to debug output @a dbg.
177QDebug operator<<(QDebug dbg, const KDbConnectionAndSchema &connectionAndSchema)
178{
179 if (std::get<1>(connectionAndSchema).table()) {
180 dbg.nospace() << *std::get<1>(connectionAndSchema).table();
181 } else if (std::get<1>(connectionAndSchema).query()) {
182 dbg.nospace() << KDbConnectionAndQuerySchema(std::get<0>(connectionAndSchema),
183 *std::get<1>(connectionAndSchema).query());
184 }
185 return dbg.space();
186}
187
189{
190 return d->query;
191}
192
194{
195 return d->table;
196}
Provides database connection, allowing queries and data modification.
KDbTableSchema * tableSchema(int tableId)
KDbQuerySchema * querySchema(int queryId)
virtual KDbField * field(int id)
int fieldCount() const
Meta-data for a field.
Definition KDbField.h:72
Helper class that assigns additional information for the column in a query.
KDbQuerySchema provides information about database query.
KDbQueryColumnInfo::Vector fieldsExpanded(KDbConnection *conn, FieldsExpandedMode mode=FieldsExpandedMode::Default) const
@ Default
All fields are returned even if duplicated.
@ Unique
Unique list of fields is returned.
KDbQueryColumnInfo * columnInfo(KDbConnection *conn, const QString &identifier, ExpandMode mode=ExpandMode::Expanded) const
const KDbField * field(KDbConnection *conn, const QString &identifier, ExpandMode mode=ExpandMode::Expanded) const
KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray &name)
const KDbQueryColumnInfo::Vector columns(KDbConnection *conn, ColumnsMode mode=ColumnsMode::NonUnique)
@ Unique
Unique columns are returned.
KDbTableSchema * table() const
KDbQuerySchema * query() const
KDbQueryColumnInfo * columnInfo(KDbConnection *conn, const QString &name)
KDbField * field(const QString &name)
Type
Type of object: table or query.
int fieldCount(KDbConnection *conn) const
Returns number of columns within record set returned from specified table or query.
KDbQuerySchema * query()
std::optional< QSqlQuery > query(const QString &queryStatement)
QString name(StandardAction id)
QDebug & nospace()
QDebug & space()
qsizetype size() const const
QByteArray toLatin1() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:42 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.