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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2004-2015 Jarosław Staniek <staniek@kde.org>
0003    Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com>
0004    Copyright (c) 1999 Preston Brown <pbrown@kde.org>
0005    Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org>
0006 
0007    This library 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 library 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 library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include "KDbTableOrQuerySchema.h"
0024 #include "KDbConnection.h"
0025 #include "KDbQuerySchema.h"
0026 #include "kdb_debug.h"
0027 
0028 class Q_DECL_HIDDEN KDbTableOrQuerySchema::Private
0029 {
0030 public:
0031     Private() {}
0032     //! The name is kept here because m_table and m_table can be 0
0033     //! and we still want name() and acptionOrName() work.
0034     QByteArray name;
0035 
0036     KDbTableSchema* table;
0037 
0038     KDbQuerySchema* query;
0039 private:
0040     Q_DISABLE_COPY(Private)
0041 };
0042 
0043 KDbTableOrQuerySchema::KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray& name)
0044         : d(new Private)
0045 {
0046     d->name = name;
0047     d->table = conn->tableSchema(QLatin1String(name));
0048     d->query = d->table ? nullptr : conn->querySchema(QLatin1String(name));
0049     if (!d->table && !d->query) {
0050         kdbWarning() << "tableOrQuery is neither table nor query!";
0051     }
0052 }
0053 
0054 KDbTableOrQuerySchema::KDbTableOrQuerySchema(KDbConnection *conn, const QByteArray& name, Type type)
0055         : d(new Private)
0056 {
0057     d->name = name;
0058     d->table = type == Type::Table ? conn->tableSchema(QLatin1String(name)) : nullptr;
0059     d->query = type == Type::Query ? conn->querySchema(QLatin1String(name)) : nullptr;
0060     if (type == Type::Table && !d->table) {
0061         kdbWarning() << "no table specified!";
0062     }
0063     if (type == Type::Query && !d->query) {
0064         kdbWarning() << "no query specified!";
0065     }
0066 }
0067 
0068 KDbTableOrQuerySchema::KDbTableOrQuerySchema(KDbFieldList *tableOrQuery)
0069     : d(new Private)
0070 {
0071     d->table = dynamic_cast<KDbTableSchema*>(tableOrQuery);
0072     d->query = dynamic_cast<KDbQuerySchema*>(tableOrQuery);
0073     if (!d->table && !d->query) {
0074         kdbWarning() << "tableOrQuery is neither table nor query!";
0075     }
0076 }
0077 
0078 KDbTableOrQuerySchema::KDbTableOrQuerySchema(KDbConnection *conn, int id)
0079     : d(new Private)
0080 {
0081     d->table = conn->tableSchema(id);
0082     d->query = d->table ? nullptr : conn->querySchema(id);
0083     if (!d->table && !d->query) {
0084         kdbWarning() << "no table or query found for id==" << id;
0085     }
0086 }
0087 
0088 KDbTableOrQuerySchema::KDbTableOrQuerySchema(KDbTableSchema* table)
0089     : d(new Private)
0090 {
0091     d->table = table;
0092     d->query = nullptr;
0093     if (!d->table) {
0094         kdbWarning() << "no table specified!";
0095     }
0096 }
0097 
0098 KDbTableOrQuerySchema::KDbTableOrQuerySchema(KDbQuerySchema* query)
0099     : d(new Private)
0100 {
0101     d->table = nullptr;
0102     d->query = query;
0103     if (!d->query) {
0104         kdbWarning() << "no query specified!";
0105     }
0106 }
0107 
0108 KDbTableOrQuerySchema::~KDbTableOrQuerySchema()
0109 {
0110     delete d;
0111 }
0112 
0113 int KDbTableOrQuerySchema::fieldCount(KDbConnection *conn) const
0114 {
0115     if (d->table)
0116         return d->table->fieldCount();
0117     if (d->query && conn)
0118         return d->query->fieldsExpanded(conn).size();
0119     return -1;
0120 }
0121 
0122 const KDbQueryColumnInfo::Vector KDbTableOrQuerySchema::columns(KDbConnection * conn, ColumnsMode mode)
0123 {
0124     if (d->table) {
0125         return d->table->query()->fieldsExpanded(conn, mode == ColumnsMode::Unique
0126                                                      ? KDbQuerySchema::FieldsExpandedMode::Unique
0127                                                      : KDbQuerySchema::FieldsExpandedMode::Default);
0128     }
0129     if (d->query) {
0130         return d->query->fieldsExpanded(conn, mode == ColumnsMode::Unique
0131                                             ? KDbQuerySchema::FieldsExpandedMode::Unique
0132                                             : KDbQuerySchema::FieldsExpandedMode::Default);
0133     }
0134     kdbWarning() << "no query or table specified!";
0135     return KDbQueryColumnInfo::Vector();
0136 }
0137 
0138 QByteArray KDbTableOrQuerySchema::name() const
0139 {
0140     if (d->table)
0141         return d->table->name().toLatin1();
0142     if (d->query)
0143         return d->query->name().toLatin1();
0144     return d->name;
0145 }
0146 
0147 QString KDbTableOrQuerySchema::captionOrName() const
0148 {
0149     KDbObject *object = d->table ? static_cast<KDbObject *>(d->table) : static_cast<KDbObject *>(d->query);
0150     if (!object)
0151         return QLatin1String(d->name);
0152     return object->caption().isEmpty() ? object->name() : object->caption();
0153 }
0154 
0155 KDbField* KDbTableOrQuerySchema::field(const QString& name)
0156 {
0157     if (d->table)
0158         return d->table->field(name);
0159     if (d->query)
0160         return d->query->field(name);
0161 
0162     return nullptr;
0163 }
0164 
0165 KDbQueryColumnInfo* KDbTableOrQuerySchema::columnInfo(KDbConnection *conn, const QString& name)
0166 {
0167     if (d->table)
0168         return d->table->query()->columnInfo(conn, name);
0169 
0170     if (d->query)
0171         return d->query->columnInfo(conn, name);
0172 
0173     return nullptr;
0174 }
0175 
0176 //! Sends information about table or query schema @a schema to debug output @a dbg.
0177 QDebug operator<<(QDebug dbg, const KDbConnectionAndSchema &connectionAndSchema)
0178 {
0179     if (std::get<1>(connectionAndSchema).table()) {
0180         dbg.nospace() << *std::get<1>(connectionAndSchema).table();
0181     } else if (std::get<1>(connectionAndSchema).query()) {
0182         dbg.nospace() << KDbConnectionAndQuerySchema(std::get<0>(connectionAndSchema),
0183                                                      *std::get<1>(connectionAndSchema).query());
0184     }
0185     return dbg.space();
0186 }
0187 
0188 KDbQuerySchema* KDbTableOrQuerySchema::query() const
0189 {
0190     return d->query;
0191 }
0192 
0193 KDbTableSchema* KDbTableOrQuerySchema::table() const
0194 {
0195     return d->table;
0196 }