20#include "KDbRelationship.h"
21#include "KDbIndexSchema.h"
22#include "KDbTableSchema.h"
23#include "KDbQuerySchema.h"
27class Q_DECL_HIDDEN KDbRelationship::Private
30 Private(KDbRelationship *r)
37 if (!field1 || !field2 || !query) {
38 kdbWarning() <<
"!masterField || !detailsField || !query";
42 kdbWarning() <<
"relationship's fields cannot be asterisks";
46 kdbWarning() <<
"fields cannot belong to the same table";
50 kdbWarning() <<
"fields do not belong to this query";
63 detailsField = field2;
65 }
else if (!p1 && p2) {
78 detailsField = field2;
83 detailsIndexOwned =
true;
84 const bool ok = detailsIndex->addField(detailsField);
86 detailsIndex->setForeignKey(
true);
87 }
else if (!p1 && !p2) {
91 masterIndexOwned =
true;
92 bool ok = masterIndex->addField(masterField);
94 masterIndex->setForeignKey(
true);
96 detailsField = field2;
99 detailsIndexOwned =
true;
100 ok = detailsIndex->addField(detailsField);
102 detailsIndex->setForeignKey(
true);
105 if (!masterIndex || !detailsIndex) {
109 (void)setIndices(masterIndex, detailsIndex,
false);
117 masterIndex =
nullptr;
118 detailsIndex =
nullptr;
120 if (!newMasterIndex || !newDetailsIndex || !newMasterIndex->
table()
121 || !newDetailsIndex->
table() || newMasterIndex->
table() == newDetailsIndex->
table()
129 for (; masterIt != masterIndexFields->
constEnd()
130 && detailsIt != detailsIndexFields->
constEnd();
131 ++masterIt, ++detailsIt) {
133 KDbField *detailsField = *detailsIt;
135 = masterField->
type();
137 if (masterType != detailsType
140 kdbWarning() <<
"INDEX on" << newMasterIndex->
table()->
name() <<
", INDEX on"
143 << masterField->
name() <<
","
151 kdbWarning() <<
"KDbRelationship::setIndices(INDEX on '" << masterIndex->table()->name()
152 <<
"',INDEX on " << detailsIndex->table()->name() <<
"): !equal signedness of field types: "
163 masterIndex->detachRelationship(q);
166 detailsIndex->detachRelationship(q);
168 masterIndex = newMasterIndex;
169 detailsIndex = newDetailsIndex;
171 detailsIndex->attachRelationship(q, ownedByMaster);
178 bool masterIndexOwned =
false;
179 bool detailsIndexOwned =
false;
182 KDbRelationship *
const q;
185KDbRelationship::KDbRelationship()
186 : d(new Private(this))
193 (void)setIndices(masterIndex, detailsIndex);
199 d->createIndices(query, field1, field2);
202KDbRelationship::~KDbRelationship()
204 if (d->masterIndexOwned) {
205 delete d->masterIndex;
207 if (d->detailsIndexOwned) {
208 delete d->detailsIndex;
213KDbRelationship& KDbRelationship::operator=(KDbRelationship &other)
215 (void)setIndices(other.masterIndex(), other.detailsIndex());
219bool KDbRelationship::operator==(
const KDbRelationship& other)
const
221 return d->masterIndex == other.masterIndex() && d->detailsIndex == other.detailsIndex();
226 return d->masterIndex;
231 return d->masterIndex;
236 return d->detailsIndex;
241 return d->detailsIndex;
254bool KDbRelationship::isEmpty()
const
261 return d->masterIndex ? d->masterIndex->
table() :
nullptr;
266 return d->masterIndex ? d->masterIndex->
table() :
nullptr;
271 return d->detailsIndex ? d->detailsIndex->
table() :
nullptr;
276 return d->detailsIndex ? d->detailsIndex->
table() :
nullptr;
281 return d->setIndices(masterIndex, detailsIndex,
true);
static QString defaultSqlTypeName(KDbField::Type type)
KDbField::List * fields()
bool isQueryAsterisk() const
QList< KDbField * >::ConstIterator ListIterator
iterator for list of fields
QPair< KDbField *, KDbField * > Pair
fields pair
bool isIntegerType() const
bool isUnsigned() const
if the type has the unsigned attribute
bool isPrimaryKey() const
Provides information about database index that can be created for a database table.
void attachRelationship(KDbRelationship *rel)
KDbQuerySchema provides information about database query.
KDbIndexSchema * primaryKey()
bool addIndex(KDbIndexSchema *index)
Adds index index to this table schema Ownership of the index is transferred to the table schema.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
const_iterator constBegin() const const
const_iterator constEnd() const const
bool contains(const AT &value) const const
bool isEmpty() const const