/usr/include/qdjango/db/QDjangoQuerySet.h is in libqdjango-dev 0.6.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 | /*
* Copyright (C) 2010-2015 Jeremy Lainé
* Copyright (C) 2011 Mathias Hasselmann
* Contact: https://github.com/jlaine/qdjango
*
* This file is part of the QDjango Library.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#ifndef QDJANGO_QUERYSET_H
#define QDJANGO_QUERYSET_H
#include "QDjango.h"
#include "QDjangoWhere.h"
#include "QDjangoQuerySet_p.h"
/** \brief The QDjangoQuerySet class is a template class for performing
* database queries.
*
* The QDjangoQuerySet template class allows you to define and manipulate
* sets of QDjangoModel objects stored in the database.
*
* You can chain filter expressions using the filter() and exclude() methods
* or apply limits on the number of rows using the limit() method.
*
* You can retrieve database values using the values() and valuesList()
* methods or retrieve model instances using the get() and at() methods.
*
* You can also delete sets of objects using the remove() method.
*
* Behinds the scenes, the QDjangoQuerySet class uses implicit sharing to
* reduce memory usage and avoid needless copying of data.
*
* \ingroup Database
*/
template <class T>
class QDjangoQuerySet
{
public:
/** \cond declarations for STL-style container algorithms */
typedef int size_type;
typedef T value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type &reference;
typedef const value_type &const_reference;
typedef qptrdiff difference_type;
/** \endcond */
/** The QDjangoQuerySet::const_iterator class provides an STL-style const iterator
* for QDjangoQuerySet.
*
* QDjangoQuerySet::const_iterator allows you to iterate over a QDjangoQuerySet.
* As a const iterator it doesn't permit you to modify the QDjangoQuerySet.
*
* The default QDjangoQuerySet::const_iterator constructor creates an uninitialized iterator. You must
* initialize it using a QDjangoQuerySet function like QDjangoQuerySet::constBegin(), or
* QDjangoQuerySet::constEnd() before you can start iterating. Here's a typical loop that
* prints all the objects stored in a set:
*
* \code
* QDjangoQuerySet<Weblog::Post> posts;
*
* foreach(const Weblog::Post &p, posts) {
* cout << p << endl;
* }
* \endcode
*/
class const_iterator
{
friend class QDjangoQuerySet;
public:
/** A synonym for std::bidirectional_iterator_tag indicating this iterator
* permits bidirectional access.
*/
typedef std::bidirectional_iterator_tag iterator_category;
/** \cond declarations for STL-style container algorithms */
typedef qptrdiff difference_type;
typedef T value_type;
typedef T *pointer;
typedef T &reference;
/** \endcond */
/** Constructs an uninitialized iterator.
*
* Functions like operator*() and operator++() should not be called on an uninitialized
* iterator. Use const_iterator::operator=() to assign a value to it before using it.
*
* \sa See also QDjangoQuerySet::constBegin() and QDjangoQuerySet::constEnd().
*/
const_iterator()
: m_querySet(0)
, m_fetched(-1)
, m_offset(0)
{
}
/** Constructs a copy of \p other.
*/
const_iterator(const const_iterator &other)
: m_querySet(other.m_querySet)
, m_fetched(-1)
, m_offset(other.m_offset)
{
}
private:
const_iterator(const QDjangoQuerySet<T> *querySet, int offset = 0)
: m_querySet(querySet)
, m_fetched(-1)
, m_offset(offset)
{
}
public:
/** Returns the current item.
*
* \sa operator->()
*/
const T &operator*() const { return *t(); }
/** Returns a pointer to the current item.
*
* \sa operator*()
*/
const T *operator->() const { return t(); }
/** Returns \c true if \p other points to the same item as this iterator;
* otherwise returns \c false.
*
* \sa operator!=()
*/
bool operator==(const const_iterator &other) const
{
return m_querySet == other.m_querySet && m_offset == other.m_offset;
}
/** Returns \c true if \p other points to a different item than this iterator;
* otherwise returns \c false.
*
* \sa operator==()
*/
bool operator!=(const const_iterator &other) const
{
return m_querySet != other.m_querySet || m_offset != other.m_offset;
}
/** Returns \c true if other \p points to a position behind this iterator;
* otherwise returns \c false.
*/
bool operator<(const const_iterator& other) const
{
return (m_querySet == other.m_querySet && m_offset < other.m_offset)
|| m_querySet < other.m_querySet;
}
/** Returns \c true if other \p points to a position behind or equal this iterator;
* otherwise returns \c false.
*/
bool operator<=(const const_iterator& other) const
{
return (m_querySet == other.m_querySet && m_offset <= other.m_offset)
|| m_querySet < other.m_querySet;
}
/** Returns \c true if other \p points to a position before this iterator;
* otherwise returns \c false.
*/
bool operator>(const const_iterator& other) const
{
return (m_querySet == other.m_querySet && m_offset > other.m_offset)
|| m_querySet > other.m_querySet;
}
/** Returns \c true if other \p points to a position before or equal this iterator;
* otherwise returns \c false.
*/
bool operator>=(const const_iterator& other) const
{
return (m_querySet == other.m_querySet && m_offset >= other.m_offset)
|| m_querySet > other.m_querySet;
}
/** The prefix ++ operator (\c ++it) advances the iterator to the next item in the set
* and returns an iterator to the new current item.
*
* Calling this function on QDjangoQuerySet::end() leads to undefined results.
*
* \sa operator--()
*/
const_iterator &operator++() { ++m_offset; return *this; }
/** The postfix ++ operator (\c it++) advances the iterator to the next item in the set and
* returns an iterator to the previously current item.
*
* Calling this function on QDjangoQuerySet::end() leads to undefined results.
*
* \sa operator--(int)
*/
const_iterator operator++(int) { const_iterator n(*this); ++m_offset; return n; }
/** Advances the iterator by \p i items.
* (If \p i is negative, the iterator goes backward.)
*
* \sa operator-=() and operator+().
*/
const_iterator &operator+=(int i) { m_offset += i; return *this; }
/** Returns an iterator to the item at \p i positions forward from this iterator.
* (If \p i is negative, the iterator goes backward.)
*
* \sa operator-() and operator+=()
*/
const_iterator operator+(int i) const { return const_iterator(m_querySet, m_offset + i); }
/** Makes the iterator go back by \p i items.
* (If \p i is negative, the iterator goes forward.)
*
* \sa operator+=() and operator-()
*/
const_iterator &operator-=(int i) { m_offset -= i; return *this; }
/** Returns an iterator to the item at \p i positions backward from this iterator.
* (If \p i is negative, the iterator goes forward.)
*
* \sa operator+() and operator-=()
*/
const_iterator operator-(int i) const { return const_iterator(m_querySet, m_offset - i); }
/** The prefix -- operator (\c --it) makes the preceding item current
* and returns an iterator to the new current item.
*
* Calling this function on QDjangoQuerySet::begin() leads to undefined results.
*
* \sa operator++().
*/
const_iterator &operator--() { --m_offset; return *this; }
/** The postfix -- operator (\c it--) makes the preceding item current
* and returns an iterator to the previously current item.
*
* Calling this function on QDjangoQuerySet::begin() leads to undefined results.
*
* \sa operator++(int).
*/
const_iterator operator--(int) { const_iterator n(*this); --m_offset; return n; }
/** Returns the number of items between the item pointed to by \p other
* and the item pointed to by this iterator.
*/
difference_type operator-(const const_iterator &other) const { return m_offset - other.m_offset; }
private:
const T *t() const
{
if (m_fetched != m_offset && m_querySet) {
if (const_cast<QDjangoQuerySet<T> *>(m_querySet)->at(m_offset, &m_object)) {
m_fetched = m_offset;
}
}
return m_fetched == m_offset ? &m_object : 0;
}
private:
const QDjangoQuerySet<T> *m_querySet;
mutable int m_fetched;
mutable T m_object;
int m_offset;
};
/** Qt-style synonym for QDjangoQuerySet::const_iterator. */
typedef const_iterator ConstIterator;
QDjangoQuerySet();
QDjangoQuerySet(const QDjangoQuerySet<T> &other);
~QDjangoQuerySet();
QDjangoQuerySet all() const;
QDjangoQuerySet exclude(const QDjangoWhere &where) const;
QDjangoQuerySet filter(const QDjangoWhere &where) const;
QDjangoQuerySet limit(int pos, int length = -1) const;
QDjangoQuerySet none() const;
QDjangoQuerySet orderBy(const QStringList &keys) const;
QDjangoQuerySet selectRelated() const;
int count() const;
QDjangoWhere where() const;
bool remove();
int size();
int update(const QVariantMap &fields);
QList<QVariantMap> values(const QStringList &fields = QStringList());
QList<QVariantList> valuesList(const QStringList &fields = QStringList());
T *get(const QDjangoWhere &where, T *target = 0) const;
T *at(int index, T *target = 0);
const_iterator constBegin() const;
const_iterator begin() const;
const_iterator constEnd() const;
const_iterator end() const;
QDjangoQuerySet<T> &operator=(const QDjangoQuerySet<T> &other);
private:
QDjangoQuerySetPrivate *d;
};
/** Constructs a new queryset.
*/
template <class T>
QDjangoQuerySet<T>::QDjangoQuerySet()
{
d = new QDjangoQuerySetPrivate(T::staticMetaObject.className());
}
/** Constructs a copy of \a other.
*
* \param other
*/
template <class T>
QDjangoQuerySet<T>::QDjangoQuerySet(const QDjangoQuerySet<T> &other)
{
other.d->counter.ref();
d = other.d;
}
/** Destroys the queryset.
*/
template <class T>
QDjangoQuerySet<T>::~QDjangoQuerySet()
{
if (!d->counter.deref())
delete d;
}
/** Returns the object in the QDjangoQuerySet at the given index.
*
* Returns 0 if the index is out of bounds.
*
* If target is 0, a new object instance will be allocated which
* you must free yourself.
*
* \param index
* \param target optional existing model instance.
*/
template <class T>
T *QDjangoQuerySet<T>::at(int index, T *target)
{
T *entry = target ? target : new T;
if (!d->sqlLoad(entry, index))
{
if (!target)
delete entry;
return 0;
}
return entry;
}
/** Returns a const STL-style iterator pointing to the first object in the QDjangoQuerySet.
*
* \sa begin() and constEnd().
*/
template <class T>
typename QDjangoQuerySet<T>::const_iterator QDjangoQuerySet<T>::constBegin() const
{
return const_iterator(this);
}
/** Returns a const STL-style iterator pointing to the first object in the QDjangoQuerySet.
*
* \sa constBegin() and end().
*/
template <class T>
typename QDjangoQuerySet<T>::const_iterator QDjangoQuerySet<T>::begin() const
{
return const_iterator(this);
}
/** Returns a const STL-style iterator pointing to the imaginary object after the last
* object in the QDjangoQuerySet.
*
* \sa constBegin() and end().
*/
template <class T>
typename QDjangoQuerySet<T>::const_iterator QDjangoQuerySet<T>::constEnd() const
{
return const_iterator(this, QDjangoQuerySet<T>::count());
}
/** Returns a const STL-style iterator pointing to the imaginary object after the last
* object in the QDjangoQuerySet.
*
* \sa begin() and constEnd().
*/
template <class T>
typename QDjangoQuerySet<T>::const_iterator QDjangoQuerySet<T>::end() const
{
return const_iterator(this, QDjangoQuerySet<T>::count());
}
/** Returns a copy of the current QDjangoQuerySet.
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::all() const
{
QDjangoQuerySet<T> other;
other.d->lowMark = d->lowMark;
other.d->highMark = d->highMark;
other.d->orderBy = d->orderBy;
other.d->selectRelated = d->selectRelated;
other.d->whereClause = d->whereClause;
return other;
}
/** Counts the number of objects in the queryset using an SQL COUNT query,
* or -1 if the query failed.
*
* If you intend to iterate over the results, you should consider using
* size() instead.
*
* \note If the QDjangoQuerySet is already fully fetched, this simply returns
* the number of objects.
*/
template <class T>
int QDjangoQuerySet<T>::count() const
{
if (d->hasResults)
return d->properties.size();
// execute COUNT query
QDjangoQuery query(d->countQuery());
if (!query.exec() || !query.next())
return -1;
return query.value(0).toInt();
}
/** Returns a new QDjangoQuerySet containing objects for which the given key
* where condition is false.
*
* You can chain calls to filter() and exclude() to further refine the
* filtering conditions.
*
* \param where QDjangoWhere expressing the exclude condition
*
* \sa filter()
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::exclude(const QDjangoWhere &where) const
{
QDjangoQuerySet<T> other = all();
other.d->addFilter(!where);
return other;
}
/** Returns a new QDjangoQuerySet containing objects for which the given
* where condition is true.
*
* You can chain calls to filter() and exclude() to progressively refine
* your filtering conditions.
*
* \param where QDjangoWhere expressing the filter condition
*
* \sa exclude()
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::filter(const QDjangoWhere &where) const
{
QDjangoQuerySet<T> other = all();
other.d->addFilter(where);
return other;
}
/** Returns the object in the QDjangoQuerySet for which the given
* where condition is true.
*
* Returns 0 if the number of matching object is not exactly one.
*
* If target is 0, a new object instance will be allocated which
* you must free yourself.
*
* \param where QDjangoWhere expressing the lookup condition
* \param target optional existing model instance.
*/
template <class T>
T *QDjangoQuerySet<T>::get(const QDjangoWhere &where, T *target) const
{
QDjangoQuerySet<T> qs = filter(where);
return qs.size() == 1 ? qs.at(0, target) : 0;
}
/** Returns a new QDjangoQuerySet containing limiting the number of
* records to manipulate.
*
* You can chain calls to limit() to further restrict the number
* of returned records.
*
* However, you cannot apply additional restrictions using filter(),
* exclude(), get(), orderBy() or remove() on the returned QDjangoQuerySet.
*
* \param pos offset of the records
* \param length maximum number of records
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::limit(int pos, int length) const
{
Q_ASSERT(pos >= 0);
Q_ASSERT(length >= -1);
QDjangoQuerySet<T> other = all();
other.d->lowMark += pos;
if (length > 0)
{
// calculate new high mark
other.d->highMark = other.d->lowMark + length;
// never exceed the current high mark
if (d->highMark > 0 && other.d->highMark > d->highMark)
other.d->highMark = d->highMark;
}
return other;
}
/** Returns an empty QDjangoQuerySet.
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::none() const
{
QDjangoQuerySet<T> other;
other.d->whereClause = !QDjangoWhere();
return other;
}
/** Returns a QDjangoQuerySet whose elements are ordered using the given keys.
*
* By default the elements will by in ascending order. You can prefix the key
* names with a "-" (minus sign) to use descending order.
*
* \param keys
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::orderBy(const QStringList &keys) const
{
// it is not possible to change ordering once a limit has been set
Q_ASSERT(!d->lowMark && !d->highMark);
QDjangoQuerySet<T> other = all();
other.d->orderBy << keys;
return other;
}
/** Deletes all objects in the QDjangoQuerySet.
*
* \return true if deletion succeeded, false otherwise
*/
template <class T>
bool QDjangoQuerySet<T>::remove()
{
return d->sqlDelete();
}
/** Returns a QDjangoQuerySet that will automatically "follow" foreign-key
* relationships, selecting that additional related-object data when it
* executes its query.
*/
template <class T>
QDjangoQuerySet<T> QDjangoQuerySet<T>::selectRelated() const
{
QDjangoQuerySet<T> other = all();
other.d->selectRelated = true;
return other;
}
/** Returns the number of objects in the QDjangoQuerySet, or -1
* if the query failed.
*
* If you do not plan to access the objects, you should consider using
* count() instead.
*/
template <class T>
int QDjangoQuerySet<T>::size()
{
if (!d->sqlFetch())
return -1;
return d->properties.size();
}
/** Performs an SQL update query for the specified \a fields and returns the
* number of rows affected, or -1 if the update failed.
*/
template <class T>
int QDjangoQuerySet<T>::update(const QVariantMap &fields)
{
return d->sqlUpdate(fields);
}
/** Returns a list of property hashes for the current QDjangoQuerySet.
* If no \a fields are specified, all the model's declared fields are returned.
*
* \param fields
*/
template <class T>
QList<QVariantMap> QDjangoQuerySet<T>::values(const QStringList &fields)
{
return d->sqlValues(fields);
}
/** Returns a list of property lists for the current QDjangoQuerySet.
* If no \a fields are specified, all the model's fields are returned in the
* order they where declared.
*
* \param fields
*/
template <class T>
QList<QVariantList> QDjangoQuerySet<T>::valuesList(const QStringList &fields)
{
return d->sqlValuesList(fields);
}
/** Returns the QDjangoWhere expressing the WHERE clause of the
* QDjangoQuerySet.
*/
template <class T>
QDjangoWhere QDjangoQuerySet<T>::where() const
{
return d->resolvedWhere(QDjango::database());
}
/** Assigns the specified queryset to this object.
*
* \param other
*/
template <class T>
QDjangoQuerySet<T> &QDjangoQuerySet<T>::operator=(const QDjangoQuerySet<T> &other)
{
other.d->counter.ref();
if (!d->counter.deref())
delete d;
d = other.d;
return *this;
}
#endif
|