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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2003-2017 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 #include "KDbQueryAsterisk.h"
0021 #include "KDbQuerySchema.h"
0022 #include "KDbTableSchema.h"
0023 
0024 class KDbQueryAsterisk::Private
0025 {
0026 public:
0027     Private(const KDbTableSchema *t) : table(t) {}
0028 
0029     /*! Table schema for this asterisk */
0030     const KDbTableSchema* table;
0031 };
0032 
0033 // ----
0034 
0035 KDbQueryAsterisk::KDbQueryAsterisk(KDbQuerySchema *query)
0036     : KDbQueryAsterisk(query, nullptr)
0037 {
0038 }
0039 
0040 KDbQueryAsterisk::KDbQueryAsterisk(KDbQuerySchema *query, const KDbTableSchema &table)
0041     : KDbQueryAsterisk(query, &table)
0042 {
0043 }
0044 
0045 KDbQueryAsterisk::KDbQueryAsterisk(KDbQuerySchema *query, const KDbTableSchema *table)
0046     : KDbField(query, -1)
0047     , d(new Private(table))
0048 {
0049     setType(KDbField::Asterisk);
0050 }
0051 
0052 KDbQueryAsterisk::KDbQueryAsterisk(const KDbQueryAsterisk &asterisk)
0053         : KDbField(asterisk)
0054         , d(new Private(asterisk.table()))
0055 {
0056 }
0057 
0058 KDbQueryAsterisk::~KDbQueryAsterisk()
0059 {
0060     delete d;
0061 }
0062 
0063 bool KDbQueryAsterisk::operator==(const KDbQueryAsterisk& other) const
0064 {
0065     return d->table == other.d->table && parent() == other.parent();
0066 }
0067 
0068 KDbQuerySchema *KDbQueryAsterisk::query()
0069 {
0070     return static_cast<KDbQuerySchema*>(parent());
0071 }
0072 
0073 const KDbQuerySchema *KDbQueryAsterisk::query() const
0074 {
0075     return static_cast<const KDbQuerySchema*>(parent());
0076 }
0077 
0078 const KDbTableSchema* KDbQueryAsterisk::table() const
0079 {
0080     return d->table;
0081 }
0082 
0083 KDbField* KDbQueryAsterisk::copy()
0084 {
0085     return new KDbQueryAsterisk(*this);
0086 }
0087 
0088 void KDbQueryAsterisk::setTable(const KDbTableSchema *table)
0089 {
0090     d->table = table;
0091 }
0092 
0093 bool KDbQueryAsterisk::isSingleTableAsterisk() const
0094 {
0095     return d->table;
0096 }
0097 
0098 bool KDbQueryAsterisk::isAllTableAsterisk() const
0099 {
0100     return !d->table;
0101 }
0102 
0103 QDebug operator<<(QDebug dbg, const KDbQueryAsterisk& asterisk)
0104 {
0105     if (asterisk.isAllTableAsterisk()) {
0106         dbg.nospace() << "ALL-TABLES ASTERISK (*) ON TABLES(";
0107         bool first = true;
0108         foreach(KDbTableSchema *table, *asterisk.query()->tables()) {
0109             if (first)
0110                 first = false;
0111             else
0112                 dbg.nospace() << ',';
0113             dbg.space() << table->name();
0114         }
0115         dbg.space() << ')';
0116     } else {
0117         dbg.nospace() << "SINGLE-TABLE ASTERISK (" << asterisk.table()->name() << ".*)";
0118     }
0119     return dbg.space();
0120 }