/src/qt3support/sql/q3sqlcursor.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 1519 lines · 695 code · 140 blank · 684 comment · 125 complexity · 5a68709307fdc926baca8fb5e7081fa9 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the Qt3Support module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <qplatformdefs.h>
  42. #include "q3sqlcursor.h"
  43. #ifndef QT_NO_SQL
  44. #include "qsqldriver.h"
  45. #include "qsqlresult.h"
  46. #include "qdatetime.h"
  47. #include "qsqldatabase.h"
  48. #include "qsql.h"
  49. #include "q3sqlrecordinfo.h"
  50. #include "q3sqlfieldinfo.h"
  51. QT_BEGIN_NAMESPACE
  52. class Q3SqlCursorPrivate
  53. {
  54. public:
  55. Q3SqlCursorPrivate(const QString& name, QSqlDatabase sdb)
  56. : lastAt(QSql::BeforeFirst), nm(name), srt(name), md(0), db(sdb), q(0)
  57. {}
  58. ~Q3SqlCursorPrivate()
  59. {
  60. delete q;
  61. }
  62. QSqlQuery* query()
  63. {
  64. if (!q)
  65. q = new QSqlQuery(QString(), db);
  66. return q;
  67. }
  68. int lastAt;
  69. QString nm; //name
  70. QSqlIndex srt; //sort
  71. QString ftr; //filter
  72. int md; //mode
  73. QSqlIndex priIndx; //primary index
  74. QSqlRecord editBuffer;
  75. // the primary index as it was before the user changed the values in editBuffer
  76. QString editIndex;
  77. Q3SqlRecordInfo infoBuffer;
  78. QSqlDatabase db;
  79. QSqlQuery *q;
  80. };
  81. QString qOrderByClause(const QSqlIndex & i, const QString& prefix = QString())
  82. {
  83. QString str;
  84. int k = i.count();
  85. if(k == 0)
  86. return QString();
  87. str = QLatin1String(" order by ") + i.toString(prefix);
  88. return str;
  89. }
  90. QString qWhereClause(const QString& prefix, QSqlField* field, const QSqlDriver* driver)
  91. {
  92. QString f;
  93. if (field && driver) {
  94. if (!prefix.isEmpty())
  95. f += prefix + QLatin1Char('.');
  96. f += field->name();
  97. if (field->isNull()) {
  98. f += QLatin1String(" IS NULL");
  99. } else {
  100. f += QLatin1String(" = ") + driver->formatValue(field);
  101. }
  102. }
  103. return f;
  104. }
  105. QString qWhereClause(QSqlRecord* rec, const QString& prefix, const QString& sep,
  106. const QSqlDriver* driver)
  107. {
  108. static QString blank(QLatin1Char(' '));
  109. QString filter;
  110. bool separator = false;
  111. for (int j = 0; j < rec->count(); ++j) {
  112. QSqlField f = rec->field(j);
  113. if (rec->isGenerated(j)) {
  114. if (separator)
  115. filter += sep + blank;
  116. filter += qWhereClause(prefix, &f, driver);
  117. filter += blank;
  118. separator = true;
  119. }
  120. }
  121. return filter;
  122. }
  123. /*!
  124. \class Q3SqlCursor
  125. \brief The Q3SqlCursor class provides browsing and editing of SQL
  126. tables and views.
  127. \compat
  128. A Q3SqlCursor is a database record (see \l QSqlRecord) that
  129. corresponds to a table or view within an SQL database (see \l
  130. QSqlDatabase). There are two buffers in a cursor, one used for
  131. browsing and one used for editing records. Each buffer contains a
  132. list of fields which correspond to the fields in the table or
  133. view.
  134. When positioned on a valid record, the browse buffer contains the
  135. values of the current record's fields from the database. The edit
  136. buffer is separate, and is used for editing existing records and
  137. inserting new records.
  138. For browsing data, a cursor must first select() data from the
  139. database. After a successful select() the cursor is active
  140. (isActive() returns true), but is initially not positioned on a
  141. valid record (isValid() returns false). To position the cursor on
  142. a valid record, use one of the navigation functions, next(),
  143. previous(), first(), last(), or seek(). Once positioned on a valid
  144. record, data can be retrieved from the browse buffer using
  145. value(). If a navigation function is not successful, it returns
  146. false, the cursor will no longer be positioned on a valid record
  147. and the values returned by value() are undefined.
  148. For example:
  149. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 0
  150. In the above example, a cursor is created specifying a table or
  151. view name in the database. Then, select() is called, which can be
  152. optionally parameterised to filter and order the records
  153. retrieved. Each record in the cursor is retrieved using next().
  154. When next() returns false, there are no more records to process,
  155. and the loop terminates.
  156. For editing records (rows of data), a cursor contains a separate
  157. edit buffer which is independent of the fields used when browsing.
  158. The functions insert(), update() and del() operate on the edit
  159. buffer. This allows the cursor to be repositioned to other
  160. records while simultaneously maintaining a separate buffer for
  161. edits. You can get a pointer to the edit buffer using
  162. editBuffer(). The primeInsert(), primeUpdate() and primeDelete()
  163. functions also return a pointer to the edit buffer and prepare it
  164. for insert, update and delete respectively. Edit operations only
  165. affect a single row at a time. Note that update() and del()
  166. require that the table or view contain a primaryIndex() to ensure
  167. that edit operations affect a unique record within the database.
  168. For example:
  169. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 1
  170. To edit an existing database record, first move to the record you
  171. wish to update. Call primeUpdate() to get the pointer to the
  172. cursor's edit buffer. Then use this pointer to modify the values
  173. in the edit buffer. Finally, call update() to save the changes to
  174. the database. The values in the edit buffer will be used to
  175. locate the appropriate record when updating the database (see
  176. primaryIndex()).
  177. Similarly, when deleting an existing database record, first move
  178. to the record you wish to delete. Then, call primeDelete() to get
  179. the pointer to the edit buffer. Finally, call del() to delete the
  180. record from the database. Again, the values in the edit buffer
  181. will be used to locate and delete the appropriate record.
  182. To insert a new record, call primeInsert() to get the pointer to
  183. the edit buffer. Use this pointer to populate the edit buffer
  184. with new values and then insert() the record into the database.
  185. After calling insert(), update() or del(), the cursor is no longer
  186. positioned on a valid record and can no longer be navigated
  187. (isValid() return false). The reason for this is that any changes
  188. made to the database will not be visible until select() is called
  189. to refresh the cursor. You can change this behavior by passing
  190. false to insert(), update() or del() which will prevent the cursor
  191. from becoming invalid. The edits will still not be visible when
  192. navigating the cursor until select() is called.
  193. Q3SqlCursor contains virtual methods which allow editing behavior
  194. to be customized by subclasses. This allows custom cursors to be
  195. created that encapsulate the editing behavior of a database table
  196. for an entire application. For example, a cursor can be customized
  197. to always auto-number primary index fields, or provide fields with
  198. suitable default values, when inserting new records. Q3SqlCursor
  199. generates SQL statements which are sent to the database engine;
  200. you can control which fields are included in these statements
  201. using setGenerated().
  202. Note that Q3SqlCursor does not inherit from QObject. This means
  203. that you are responsible for destroying instances of this class
  204. yourself. However if you create a Q3SqlCursor and use it in a
  205. \l Q3DataTable, \l Q3DataBrowser or a \l Q3DataView these classes will
  206. usually take ownership of the cursor and destroy it when they
  207. don't need it anymore. The documentation for Q3DataTable,
  208. Q3DataBrowser and Q3DataView explicitly states which calls take
  209. ownership of the cursor.
  210. */
  211. /*!
  212. \enum Q3SqlCursor::Mode
  213. This enum type describes how Q3SqlCursor operates on records in the
  214. database.
  215. \value ReadOnly the cursor can only SELECT records from the
  216. database.
  217. \value Insert the cursor can INSERT records into the database.
  218. \value Update the cursor can UPDATE records in the database.
  219. \value Delete the cursor can DELETE records from the database.
  220. \value Writable the cursor can INSERT, UPDATE and DELETE records
  221. in the database.
  222. */
  223. /*!
  224. \fn QVariant Q3SqlCursor::value(const QString &name) const
  225. \overload
  226. Returns the value of the field named \a name.
  227. */
  228. /*!
  229. \fn void Q3SqlCursor::setValue(const QString &name, const QVariant &val)
  230. \overload
  231. Sets the value for the field named \a name to \a val.
  232. */
  233. /*!
  234. Constructs a cursor on database \a db using table or view \a name.
  235. If \a autopopulate is true (the default), the \a name of the
  236. cursor must correspond to an existing table or view name in the
  237. database so that field information can be automatically created.
  238. If the table or view does not exist, the cursor will not be
  239. functional.
  240. The cursor is created with an initial mode of Q3SqlCursor::Writable
  241. (meaning that records can be inserted, updated or deleted using
  242. the cursor). If the cursor does not have a unique primary index,
  243. update and deletes cannot be performed.
  244. Note that \a autopopulate refers to populating the cursor with
  245. meta-data, e.g. the names of the table's fields, not with
  246. retrieving data. The select() function is used to populate the
  247. cursor with data.
  248. \sa setName() setMode()
  249. */
  250. Q3SqlCursor::Q3SqlCursor(const QString & name, bool autopopulate, QSqlDatabase db)
  251. : QSqlRecord(), QSqlQuery(QString(), db)
  252. {
  253. d = new Q3SqlCursorPrivate(name, db);
  254. setMode(Writable);
  255. if (!d->nm.isEmpty())
  256. setName(d->nm, autopopulate);
  257. }
  258. /*!
  259. Constructs a copy of \a other.
  260. */
  261. Q3SqlCursor::Q3SqlCursor(const Q3SqlCursor & other)
  262. : QSqlRecord(other), QSqlQuery(other)
  263. {
  264. d = new Q3SqlCursorPrivate(other.d->nm, other.d->db);
  265. d->lastAt = other.d->lastAt;
  266. d->nm = other.d->nm;
  267. d->srt = other.d->srt;
  268. d->ftr = other.d->ftr;
  269. d->priIndx = other.d->priIndx;
  270. d->editBuffer = other.d->editBuffer;
  271. d->infoBuffer = other.d->infoBuffer;
  272. d->q = 0; // do not share queries
  273. setMode(other.mode());
  274. }
  275. /*!
  276. Destroys the object and frees any allocated resources.
  277. */
  278. Q3SqlCursor::~Q3SqlCursor()
  279. {
  280. delete d;
  281. }
  282. /*!
  283. Sets the cursor equal to \a other.
  284. */
  285. Q3SqlCursor& Q3SqlCursor::operator=(const Q3SqlCursor& other)
  286. {
  287. QSqlRecord::operator=(other);
  288. QSqlQuery::operator=(other);
  289. delete d;
  290. d = new Q3SqlCursorPrivate(other.d->nm, other.d->db);
  291. d->lastAt = other.d->lastAt;
  292. d->nm = other.d->nm;
  293. d->srt = other.d->srt;
  294. d->ftr = other.d->ftr;
  295. d->priIndx = other.d->priIndx;
  296. d->editBuffer = other.d->editBuffer;
  297. d->infoBuffer = other.d->infoBuffer;
  298. d->q = 0; // do not share queries
  299. setMode(other.mode());
  300. return *this;
  301. }
  302. /*!
  303. Sets the current sort to \a sort. Note that no new records are
  304. selected. To select new records, use select(). The \a sort will
  305. apply to any subsequent select() calls that do not explicitly
  306. specify a sort.
  307. */
  308. void Q3SqlCursor::setSort(const QSqlIndex& sort)
  309. {
  310. d->srt = sort;
  311. }
  312. /*!
  313. Returns the current sort, or an empty index if there is no current
  314. sort.
  315. */
  316. QSqlIndex Q3SqlCursor::sort() const
  317. {
  318. return d->srt;
  319. }
  320. /*!
  321. Sets the current filter to \a filter. Note that no new records are
  322. selected. To select new records, use select(). The \a filter will
  323. apply to any subsequent select() calls that do not explicitly
  324. specify a filter.
  325. The filter is a SQL \c WHERE clause without the keyword 'WHERE',
  326. e.g. \c{name='Dave'} which will be processed by the DBMS.
  327. */
  328. void Q3SqlCursor::setFilter(const QString& filter)
  329. {
  330. d->ftr = filter;
  331. }
  332. /*!
  333. Returns the current filter, or an empty string if there is no
  334. current filter.
  335. */
  336. QString Q3SqlCursor::filter() const
  337. {
  338. return d->ftr;
  339. }
  340. /*!
  341. Sets the name of the cursor to \a name. If \a autopopulate is true
  342. (the default), the \a name must correspond to a valid table or
  343. view name in the database. Also, note that all references to the
  344. cursor edit buffer become invalidated when fields are
  345. auto-populated. See the Q3SqlCursor constructor documentation for
  346. more information.
  347. */
  348. void Q3SqlCursor::setName(const QString& name, bool autopopulate)
  349. {
  350. d->nm = name;
  351. if (autopopulate) {
  352. if (driver()) {
  353. d->infoBuffer = driver()->record(name);
  354. *this = d->infoBuffer.toRecord();
  355. d->editBuffer = *this;
  356. d->priIndx = driver()->primaryIndex(name);
  357. }
  358. if (isEmpty())
  359. qWarning("Q3SqlCursor::setName: unable to build record, does '%s' exist?", name.latin1());
  360. }
  361. }
  362. /*!
  363. Returns the name of the cursor.
  364. */
  365. QString Q3SqlCursor::name() const
  366. {
  367. return d->nm;
  368. }
  369. /*! \internal
  370. */
  371. QString Q3SqlCursor::toString(const QString& prefix, const QString& sep) const
  372. {
  373. QString pflist;
  374. QString pfix = prefix.isEmpty() ? prefix : prefix + QLatin1Char('.');
  375. bool comma = false;
  376. for (int i = 0; i < count(); ++i) {
  377. const QString fname = fieldName(i);
  378. if (isGenerated(i)) {
  379. if(comma)
  380. pflist += sep + QLatin1Char(' ');
  381. pflist += pfix + driver()->escapeIdentifier(fname, QSqlDriver::FieldName);
  382. comma = true;
  383. }
  384. }
  385. return pflist;
  386. }
  387. /*!
  388. \internal
  389. Assigns the record \a list.
  390. */
  391. QSqlRecord & Q3SqlCursor::operator=(const QSqlRecord & list)
  392. {
  393. return QSqlRecord::operator=(list);
  394. }
  395. /*!
  396. Append a copy of field \a fieldInfo to the end of the cursor. Note
  397. that all references to the cursor edit buffer become invalidated.
  398. */
  399. void Q3SqlCursor::append(const Q3SqlFieldInfo& fieldInfo)
  400. {
  401. d->editBuffer.append(fieldInfo.toField());
  402. d->infoBuffer.append(fieldInfo);
  403. QSqlRecord::append(fieldInfo.toField());
  404. }
  405. /*!
  406. Removes all fields from the cursor. Note that all references to
  407. the cursor edit buffer become invalidated.
  408. */
  409. void Q3SqlCursor::clear()
  410. {
  411. d->editBuffer.clear();
  412. d->infoBuffer.clear();
  413. QSqlRecord::clear();
  414. }
  415. /*!
  416. Insert a copy of \a fieldInfo at position \a pos. If a field
  417. already exists at \a pos, it is removed. Note that all references
  418. to the cursor edit buffer become invalidated.
  419. */
  420. void Q3SqlCursor::insert(int pos, const Q3SqlFieldInfo& fieldInfo)
  421. {
  422. d->editBuffer.replace(pos, fieldInfo.toField());
  423. d->infoBuffer[pos] = fieldInfo;
  424. QSqlRecord::replace(pos, fieldInfo.toField());
  425. }
  426. /*!
  427. Removes the field at \a pos. If \a pos does not exist, nothing
  428. happens. Note that all references to the cursor edit buffer become
  429. invalidated.
  430. */
  431. void Q3SqlCursor::remove(int pos)
  432. {
  433. d->editBuffer.remove(pos);
  434. d->infoBuffer[pos] = Q3SqlFieldInfo();
  435. QSqlRecord::remove(pos);
  436. }
  437. /*!
  438. Sets the generated flag for the field \a name to \a generated. If
  439. the field does not exist, nothing happens. Only fields that have
  440. \a generated set to true are included in the SQL that is
  441. generated by insert(), update() or del().
  442. */
  443. void Q3SqlCursor::setGenerated(const QString& name, bool generated)
  444. {
  445. int pos = indexOf(name);
  446. if (pos == -1)
  447. return;
  448. QSqlRecord::setGenerated(name, generated);
  449. d->editBuffer.setGenerated(name, generated);
  450. d->infoBuffer[pos].setGenerated(generated);
  451. }
  452. /*!
  453. \overload
  454. Sets the generated flag for the field \a i to \a generated.
  455. */
  456. void Q3SqlCursor::setGenerated(int i, bool generated)
  457. {
  458. if (i < 0 || i >= (int)d->infoBuffer.count())
  459. return;
  460. QSqlRecord::setGenerated(i, generated);
  461. d->editBuffer.setGenerated(i, generated);
  462. d->infoBuffer[i].setGenerated(generated);
  463. }
  464. /*!
  465. Returns the primary index associated with the cursor as defined in
  466. the database, or an empty index if there is no primary index. If
  467. \a setFromCursor is true (the default), the index fields are
  468. populated with the corresponding values in the cursor's current
  469. record.
  470. */
  471. QSqlIndex Q3SqlCursor::primaryIndex(bool setFromCursor) const
  472. {
  473. if (setFromCursor) {
  474. for (int i = 0; i < d->priIndx.count(); ++i) {
  475. const QString fn = d->priIndx.fieldName(i);
  476. if (contains(fn))
  477. d->priIndx.setValue(i, QSqlRecord::value(fn));
  478. }
  479. }
  480. return d->priIndx;
  481. }
  482. /*!
  483. Sets the primary index associated with the cursor to the index \a
  484. idx. Note that this index must contain a field or set of fields
  485. which identify a unique record within the underlying database
  486. table or view so that update() and del() will execute as expected.
  487. \sa update() del()
  488. */
  489. void Q3SqlCursor::setPrimaryIndex(const QSqlIndex& idx)
  490. {
  491. d->priIndx = idx;
  492. }
  493. /*!
  494. Returns an index composed of \a fieldNames, all in ASCending
  495. order. Note that all field names must exist in the cursor,
  496. otherwise an empty index is returned.
  497. \sa QSqlIndex
  498. */
  499. QSqlIndex Q3SqlCursor::index(const QStringList& fieldNames) const
  500. {
  501. QSqlIndex idx;
  502. for (QStringList::ConstIterator it = fieldNames.begin(); it != fieldNames.end(); ++it) {
  503. QSqlField f = field((*it));
  504. if (!f.isValid()) { /* all fields must exist */
  505. idx.clear();
  506. break;
  507. }
  508. idx.append(f);
  509. }
  510. return idx;
  511. }
  512. /*!
  513. \overload
  514. Returns an index based on \a fieldName.
  515. */
  516. QSqlIndex Q3SqlCursor::index(const QString& fieldName) const
  517. {
  518. QStringList fl(fieldName);
  519. return index(fl);
  520. }
  521. /*!
  522. Selects all fields in the cursor from the database matching the
  523. filter criteria \a filter. The data is returned in the order
  524. specified by the index \a sort. Returns true if the data was
  525. successfully selected; otherwise returns false.
  526. The \a filter is a string containing a SQL \c WHERE clause but
  527. without the 'WHERE' keyword. The cursor is initially positioned at
  528. an invalid row after this function is called. To move to a valid
  529. row, use seek(), first(), last(), previous() or next().
  530. Example:
  531. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 2
  532. The filter will apply to any subsequent select() calls that do not
  533. explicitly specify another filter. Similarly the sort will apply
  534. to any subsequent select() calls that do not explicitly specify
  535. another sort.
  536. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 3
  537. */
  538. bool Q3SqlCursor::select(const QString & filter, const QSqlIndex & sort)
  539. {
  540. QString fieldList(toString(d->nm));
  541. if (fieldList.isEmpty())
  542. return false;
  543. QString str(QLatin1String("select ") + fieldList);
  544. str += QLatin1String(" from ") + d->nm;
  545. if (!filter.isEmpty()) {
  546. d->ftr = filter;
  547. str += QLatin1String(" where ") + filter;
  548. } else
  549. d->ftr.clear();
  550. if (sort.count() > 0)
  551. str += QLatin1String(" order by ") + sort.toString(d->nm);
  552. d->srt = sort;
  553. return exec(str);
  554. }
  555. /*!
  556. \overload
  557. Selects all fields in the cursor from the database. The rows are
  558. returned in the order specified by the last call to setSort() or
  559. the last call to select() that specified a sort, whichever is the
  560. most recent. If there is no current sort, the order in which the
  561. rows are returned is undefined. The records are filtered according
  562. to the filter specified by the last call to setFilter() or the
  563. last call to select() that specified a filter, whichever is the
  564. most recent. If there is no current filter, all records are
  565. returned. The cursor is initially positioned at an invalid row. To
  566. move to a valid row, use seek(), first(), last(), previous() or
  567. next().
  568. \sa setSort() setFilter()
  569. */
  570. bool Q3SqlCursor::select()
  571. {
  572. return select(filter(), sort());
  573. }
  574. /*!
  575. \overload
  576. Selects all fields in the cursor from the database. The data is
  577. returned in the order specified by the index \a sort. The records
  578. are filtered according to the filter specified by the last call to
  579. setFilter() or the last call to select() that specified a filter,
  580. whichever is the most recent. The cursor is initially positioned
  581. at an invalid row. To move to a valid row, use seek(), first(),
  582. last(), previous() or next().
  583. */
  584. bool Q3SqlCursor::select(const QSqlIndex& sort)
  585. {
  586. return select(filter(), sort);
  587. }
  588. /*!
  589. \overload
  590. Selects all fields in the cursor matching the filter index \a
  591. filter. The data is returned in the order specified by the index
  592. \a sort. The \a filter index works by constructing a WHERE clause
  593. using the names of the fields from the \a filter and their values
  594. from the current cursor record. The cursor is initially positioned
  595. at an invalid row. To move to a valid row, use seek(), first(),
  596. last(), previous() or next(). This function is useful, for example,
  597. for retrieving data based upon a table's primary index:
  598. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 4
  599. In this example the QSqlIndex, pk, is used for two different
  600. purposes. When used as the filter (first) argument, the field
  601. names it contains are used to construct the WHERE clause, each set
  602. to the current cursor value, \c{WHERE id=10}, in this case. When
  603. used as the sort (second) argument the field names it contains are
  604. used for the ORDER BY clause, \c{ORDER BY id} in this example.
  605. */
  606. bool Q3SqlCursor::select(const QSqlIndex & filter, const QSqlIndex & sort)
  607. {
  608. return select(toString(filter, this, d->nm, QString(QLatin1Char('=')), QLatin1String("and")), sort);
  609. }
  610. /*!
  611. Sets the cursor mode to \a mode. This value can be an OR'ed
  612. combination of \l Q3SqlCursor::Mode values. The default mode for a
  613. cursor is Q3SqlCursor::Writable.
  614. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 5
  615. */
  616. void Q3SqlCursor::setMode(int mode)
  617. {
  618. d->md = mode;
  619. }
  620. /*!
  621. Returns the current cursor mode.
  622. \sa setMode()
  623. */
  624. int Q3SqlCursor::mode() const
  625. {
  626. return d->md;
  627. }
  628. /*!
  629. Sets field \a name to \a calculated. If the field \a name does not
  630. exist, nothing happens. The value of a calculated field is set by
  631. the calculateField() virtual function which you must reimplement
  632. (or the field value will be an invalid QVariant). Calculated
  633. fields do not appear in generated SQL statements sent to the
  634. database.
  635. \sa calculateField()
  636. */
  637. void Q3SqlCursor::setCalculated(const QString& name, bool calculated)
  638. {
  639. int pos = indexOf(name);
  640. if (pos < 0)
  641. return;
  642. d->infoBuffer[pos].setCalculated(calculated);
  643. if (calculated)
  644. setGenerated(pos, false);
  645. }
  646. /*!
  647. Returns true if the field \a name exists and is calculated;
  648. otherwise returns false.
  649. \sa setCalculated()
  650. */
  651. bool Q3SqlCursor::isCalculated(const QString& name) const
  652. {
  653. int pos = indexOf(name);
  654. if (pos < 0)
  655. return false;
  656. return d->infoBuffer[pos].isCalculated();
  657. }
  658. /*!
  659. Sets field \a{name}'s trimmed status to \a trim. If the field \a
  660. name does not exist, nothing happens.
  661. When a trimmed field of type string is read from the
  662. database any trailing (right-most) spaces are removed.
  663. \sa isTrimmed() QVariant
  664. */
  665. void Q3SqlCursor::setTrimmed(const QString& name, bool trim)
  666. {
  667. int pos = indexOf(name);
  668. if (pos < 0)
  669. return;
  670. d->infoBuffer[pos].setTrim(trim);
  671. }
  672. /*!
  673. Returns true if the field \a name exists and is trimmed; otherwise
  674. returns false.
  675. When a trimmed field of type string or cstring is read from the
  676. database any trailing (right-most) spaces are removed.
  677. \sa setTrimmed()
  678. */
  679. bool Q3SqlCursor::isTrimmed(const QString& name) const
  680. {
  681. int pos = indexOf(name);
  682. if (pos < 0)
  683. return false;
  684. return d->infoBuffer[pos].isTrim();
  685. }
  686. /*!
  687. Returns true if the cursor is read-only; otherwise returns false.
  688. The default is false. Read-only cursors cannot be edited using
  689. insert(), update() or del().
  690. \sa setMode()
  691. */
  692. bool Q3SqlCursor::isReadOnly() const
  693. {
  694. return d->md == 0;
  695. }
  696. /*!
  697. Returns true if the cursor will perform inserts; otherwise returns
  698. false.
  699. \sa setMode()
  700. */
  701. bool Q3SqlCursor::canInsert() const
  702. {
  703. return ((d->md & Insert) == Insert) ;
  704. }
  705. /*!
  706. Returns true if the cursor will perform updates; otherwise returns
  707. false.
  708. \sa setMode()
  709. */
  710. bool Q3SqlCursor::canUpdate() const
  711. {
  712. return ((d->md & Update) == Update) ;
  713. }
  714. /*!
  715. Returns true if the cursor will perform deletes; otherwise returns
  716. false.
  717. \sa setMode()
  718. */
  719. bool Q3SqlCursor::canDelete() const
  720. {
  721. return ((d->md & Delete) == Delete) ;
  722. }
  723. /*!
  724. \overload
  725. Returns a formatted string composed of the \a prefix (e.g. table
  726. or view name), ".", the \a field name, the \a fieldSep and the
  727. field value. If the \a prefix is empty then the string will begin
  728. with the \a field name. This function is useful for generating SQL
  729. statements.
  730. */
  731. QString Q3SqlCursor::toString(const QString& prefix, QSqlField* field, const QString& fieldSep) const
  732. {
  733. QString f;
  734. if (field && driver()) {
  735. f = (prefix.length() > 0 ? prefix + QLatin1Char('.') : QString()) + driver()->escapeIdentifier(field->name(), QSqlDriver::FieldName);
  736. f += QLatin1Char(' ') + fieldSep + QLatin1Char(' ');
  737. if (field->isNull()) {
  738. f += QLatin1String("NULL");
  739. } else {
  740. f += driver()->formatValue(field);
  741. }
  742. }
  743. return f;
  744. }
  745. /*!
  746. Returns a formatted string composed of all the fields in \a rec.
  747. Each field is composed of the \a prefix (e.g. table or view name),
  748. ".", the field name, the \a fieldSep and the field value. If the
  749. \a prefix is empty then each field will begin with the field name.
  750. The fields are then joined together separated by \a sep. Fields
  751. where isGenerated() returns false are not included. This function
  752. is useful for generating SQL statements.
  753. */
  754. QString Q3SqlCursor::toString(QSqlRecord* rec, const QString& prefix, const QString& fieldSep,
  755. const QString& sep) const
  756. {
  757. static QString blank(QLatin1Char(' '));
  758. QString filter;
  759. bool separator = false;
  760. for (int j = 0; j < count(); ++j) {
  761. QSqlField f = rec->field(j);
  762. if (rec->isGenerated(j)) {
  763. if (separator)
  764. filter += sep + blank;
  765. filter += toString(prefix, &f, fieldSep);
  766. filter += blank;
  767. separator = true;
  768. }
  769. }
  770. return filter;
  771. }
  772. /*!
  773. \overload
  774. Returns a formatted string composed of all the fields in the index
  775. \a i. Each field is composed of the \a prefix (e.g. table or view
  776. name), ".", the field name, the \a fieldSep and the field value.
  777. If the \a prefix is empty then each field will begin with the field
  778. name. The field values are taken from \a rec. The fields are then
  779. joined together separated by \a sep. Fields where isGenerated()
  780. returns false are ignored. This function is useful for generating
  781. SQL statements.
  782. */
  783. QString Q3SqlCursor::toString(const QSqlIndex& i, QSqlRecord* rec, const QString& prefix,
  784. const QString& fieldSep, const QString& sep) const
  785. {
  786. QString filter;
  787. bool separator = false;
  788. for(int j = 0; j < i.count(); ++j){
  789. if (rec->isGenerated(j)) {
  790. if(separator) {
  791. filter += QLatin1Char(' ') + sep + QLatin1Char(' ') ;
  792. }
  793. QString fn = i.fieldName(j);
  794. QSqlField f = rec->field(fn);
  795. filter += toString(prefix, &f, fieldSep);
  796. separator = true;
  797. }
  798. }
  799. return filter;
  800. }
  801. /*!
  802. Inserts the current contents of the cursor's edit record buffer
  803. into the database, if the cursor allows inserts. Returns the
  804. number of rows affected by the insert. For error information, use
  805. lastError().
  806. If \a invalidate is true (the default), the cursor will no longer
  807. be positioned on a valid record and can no longer be navigated. A
  808. new select() call must be made before navigating to a valid
  809. record.
  810. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 6
  811. In the above example, a cursor is created on the 'prices' table
  812. and a pointer to the insert buffer is acquired using primeInsert().
  813. Each field's value is set to the desired value and then insert()
  814. is called to insert the data into the database. Remember: all edit
  815. operations (insert(), update() and delete()) operate on the
  816. contents of the cursor edit buffer and not on the contents of the
  817. cursor itself.
  818. \sa setMode() lastError()
  819. */
  820. int Q3SqlCursor::insert(bool invalidate)
  821. {
  822. if ((d->md & Insert) != Insert || !driver())
  823. return false;
  824. int k = d->editBuffer.count();
  825. if (k == 0)
  826. return 0;
  827. QString fList;
  828. QString vList;
  829. bool comma = false;
  830. // use a prepared query if the driver supports it
  831. if (driver()->hasFeature(QSqlDriver::PreparedQueries)) {
  832. int cnt = 0;
  833. bool oraStyle = driver()->hasFeature(QSqlDriver::NamedPlaceholders);
  834. for(int j = 0; j < k; ++j) {
  835. QSqlField f = d->editBuffer.field(j);
  836. if (d->editBuffer.isGenerated(j)) {
  837. if (comma) {
  838. fList += QLatin1Char(',');
  839. vList += QLatin1Char(',');
  840. }
  841. fList += driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName);
  842. vList += (oraStyle == true) ? QLatin1String(":f") + QString::number(cnt) : QString(QLatin1Char('?'));
  843. cnt++;
  844. comma = true;
  845. }
  846. }
  847. if (!comma) {
  848. return 0;
  849. }
  850. QString str;
  851. str.append(QLatin1String("insert into ")).append(name())
  852. .append(QLatin1String(" (")).append(fList)
  853. .append(QLatin1String(") values (")).append(vList). append(QLatin1Char(')'));
  854. return applyPrepared(str, invalidate);
  855. } else {
  856. for(int j = 0; j < k; ++j) {
  857. QSqlField f = d->editBuffer.field(j);
  858. if (d->editBuffer.isGenerated(j)) {
  859. if (comma) {
  860. fList += QLatin1Char(',');
  861. vList += QLatin1Char(',');
  862. }
  863. fList += driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName);
  864. vList += driver()->formatValue(&f);
  865. comma = true;
  866. }
  867. }
  868. if (!comma) {
  869. // no valid fields found
  870. return 0;
  871. }
  872. QString str;
  873. str.append(QLatin1String("insert into ")).append(name()).append(QLatin1String(" ("))
  874. .append(fList).append(QLatin1String(") values (")).append(vList). append (QLatin1String(")"));
  875. return apply(str, invalidate);
  876. }
  877. }
  878. /*!
  879. Returns the current internal edit buffer. If \a copy is true (the
  880. default is false), the current cursor field values are first
  881. copied into the edit buffer. The edit buffer is valid as long as
  882. the cursor remains valid. The cursor retains ownership of the
  883. returned pointer, so it must not be deleted or modified.
  884. \sa primeInsert(), primeUpdate() primeDelete()
  885. */
  886. QSqlRecord* Q3SqlCursor::editBuffer(bool copy)
  887. {
  888. sync();
  889. if (copy) {
  890. for(int i = 0; i < d->editBuffer.count(); i++) {
  891. if (QSqlRecord::isNull(i)) {
  892. d->editBuffer.setNull(i);
  893. } else {
  894. d->editBuffer.setValue(i, value(i));
  895. }
  896. }
  897. }
  898. return &d->editBuffer;
  899. }
  900. /*!
  901. This function primes the edit buffer's field values for update and
  902. returns the edit buffer. The default implementation copies the
  903. field values from the current cursor record into the edit buffer
  904. (therefore, this function is equivalent to calling editBuffer(
  905. true)). The cursor retains ownership of the returned pointer, so
  906. it must not be deleted or modified.
  907. \sa editBuffer() update()
  908. */
  909. QSqlRecord* Q3SqlCursor::primeUpdate()
  910. {
  911. // memorize the primary keys as they were before the user changed the values in editBuffer
  912. QSqlRecord* buf = editBuffer(true);
  913. QSqlIndex idx = primaryIndex(false);
  914. if (!idx.isEmpty())
  915. d->editIndex = toString(idx, buf, d->nm, QString(QLatin1Char('=')), QLatin1String("and"));
  916. else
  917. d->editIndex = qWhereClause(buf, d->nm, QLatin1String("and"), driver());
  918. return buf;
  919. }
  920. /*!
  921. This function primes the edit buffer's field values for delete and
  922. returns the edit buffer. The default implementation copies the
  923. field values from the current cursor record into the edit buffer
  924. (therefore, this function is equivalent to calling editBuffer(
  925. true)). The cursor retains ownership of the returned pointer, so
  926. it must not be deleted or modified.
  927. \sa editBuffer() del()
  928. */
  929. QSqlRecord* Q3SqlCursor::primeDelete()
  930. {
  931. return editBuffer(true);
  932. }
  933. /*!
  934. This function primes the edit buffer's field values for insert and
  935. returns the edit buffer. The default implementation clears all
  936. field values in the edit buffer. The cursor retains ownership of
  937. the returned pointer, so it must not be deleted or modified.
  938. \sa editBuffer() insert()
  939. */
  940. QSqlRecord* Q3SqlCursor::primeInsert()
  941. {
  942. d->editBuffer.clearValues();
  943. return &d->editBuffer;
  944. }
  945. /*!
  946. Updates the database with the current contents of the edit buffer.
  947. Returns the number of records which were updated.
  948. For error information, use lastError().
  949. Only records which meet the filter criteria specified by the
  950. cursor's primary index are updated. If the cursor does not contain
  951. a primary index, no update is performed and 0 is returned.
  952. If \a invalidate is true (the default), the current cursor can no
  953. longer be navigated. A new select() call must be made before you
  954. can move to a valid record. For example:
  955. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 7
  956. In the above example, a cursor is created on the 'prices' table
  957. and is positioned on the record to be updated. Then a pointer to
  958. the cursor's edit buffer is acquired using primeUpdate(). A new
  959. value is calculated and placed into the edit buffer with the
  960. setValue() call. Finally, an update() call is made on the cursor
  961. which uses the tables's primary index to update the record in the
  962. database with the contents of the cursor's edit buffer. Remember:
  963. all edit operations (insert(), update() and delete()) operate on
  964. the contents of the cursor edit buffer and not on the contents of
  965. the cursor itself.
  966. Note that if the primary index does not uniquely distinguish
  967. records the database may be changed into an inconsistent state.
  968. \sa setMode() lastError()
  969. */
  970. int Q3SqlCursor::update(bool invalidate)
  971. {
  972. if (d->editIndex.isEmpty())
  973. return 0;
  974. return update(d->editIndex, invalidate);
  975. }
  976. /*!
  977. \overload
  978. Updates the database with the current contents of the cursor edit
  979. buffer using the specified \a filter. Returns the number of
  980. records which were updated.
  981. For error information, use lastError().
  982. Only records which meet the filter criteria are updated, otherwise
  983. all records in the table are updated.
  984. If \a invalidate is true (the default), the cursor can no longer
  985. be navigated. A new select() call must be made before you can move
  986. to a valid record.
  987. \sa primeUpdate() setMode() lastError()
  988. */
  989. int Q3SqlCursor::update(const QString & filter, bool invalidate)
  990. {
  991. if ((d->md & Update) != Update) {
  992. return false;
  993. }
  994. int k = count();
  995. if (k == 0) {
  996. return 0;
  997. }
  998. // use a prepared query if the driver supports it
  999. if (driver()->hasFeature(QSqlDriver::PreparedQueries)) {
  1000. QString fList;
  1001. bool comma = false;
  1002. int cnt = 0;
  1003. bool oraStyle = driver()->hasFeature(QSqlDriver::NamedPlaceholders);
  1004. for(int j = 0; j < k; ++j) {
  1005. QSqlField f = d->editBuffer.field(j);
  1006. if (d->editBuffer.isGenerated(j)) {
  1007. if (comma) {
  1008. fList += QLatin1Char(',');
  1009. }
  1010. fList += f.name() + QLatin1String(" = ") + (oraStyle == true ? QLatin1String(":f") + QString::number(cnt) : QString(QLatin1Char('?')));
  1011. cnt++;
  1012. comma = true;
  1013. }
  1014. }
  1015. if (!comma) {
  1016. return 0;
  1017. }
  1018. QString str(QLatin1String("update ") + name() + QLatin1String(" set ") + fList);
  1019. if (filter.length()) {
  1020. str+= QLatin1String(" where ") + filter;
  1021. }
  1022. return applyPrepared(str, invalidate);
  1023. } else {
  1024. QString str = QLatin1String("update ") + name();
  1025. str += QLatin1String(" set ") + toString(&d->editBuffer, QString(), QString(QLatin1Char('=')), QString(QLatin1Char(',')));
  1026. if (filter.length()) {
  1027. str+= QLatin1String(" where ") + filter;
  1028. }
  1029. return apply(str, invalidate);
  1030. }
  1031. }
  1032. /*!
  1033. Deletes a record from the database using the cursor's primary
  1034. index and the contents of the cursor edit buffer. Returns the
  1035. number of records which were deleted.
  1036. For error information, use lastError().
  1037. Only records which meet the filter criteria specified by the
  1038. cursor's primary index are deleted. If the cursor does not contain
  1039. a primary index, no delete is performed and 0 is returned. If \a
  1040. invalidate is true (the default), the current cursor can no longer
  1041. be navigated. A new select() call must be made before you can move
  1042. to a valid record. For example:
  1043. \snippet doc/src/snippets/code/src_qt3support_sql_q3sqlcursor.cpp 8
  1044. In the above example, a cursor is created on the 'prices' table
  1045. and positioned to the record to be deleted. First primeDelete() is
  1046. called to populate the edit buffer with the current cursor values,
  1047. e.g. with an id of 999, and then del() is called to actually
  1048. delete the record from the database. Remember: all edit operations
  1049. (insert(), update() and delete()) operate on the contents of the
  1050. cursor edit buffer and not on the contents of the cursor itself.
  1051. \sa primeDelete() setMode() lastError()
  1052. */
  1053. int Q3SqlCursor::del(bool invalidate)
  1054. {
  1055. QSqlIndex idx = primaryIndex(false);
  1056. if (idx.isEmpty())
  1057. return del(qWhereClause(&d->editBuffer, d->nm, QLatin1String("and"), driver()), invalidate);
  1058. return del(toString(primaryIndex(), &d->editBuffer, d->nm, QString(QLatin1Char('=')), QLatin1String("and")), invalidate);
  1059. }
  1060. /*!
  1061. \overload
  1062. Deletes the current cursor record from the database using the
  1063. filter \a filter. Only records which meet the filter criteria are
  1064. deleted. Returns the number of records which were deleted. If \a
  1065. invalidate is true (the default), the current cursor can no longer
  1066. be navigated. A new select() call must be made before you can move
  1067. to a valid record. For error information, use lastError().
  1068. The \a filter is an SQL \c WHERE clause, e.g. \c{id=500}.
  1069. \sa setMode() lastError()
  1070. */
  1071. int Q3SqlCursor::del(const QString & filter, bool invalidate)
  1072. {
  1073. if ((d->md & Delete) != Delete)
  1074. return 0;
  1075. int k = count();
  1076. if(k == 0) return 0;
  1077. QString str = QLatin1String("delete from ") + name();
  1078. if (filter.length())
  1079. str+= QLatin1String(" where ") + filter;
  1080. return apply(str, invalidate);
  1081. }
  1082. /*
  1083. \internal
  1084. */
  1085. int Q3SqlCursor::apply(const QString& q, bool invalidate)
  1086. {
  1087. int ar = 0;
  1088. if (invalidate) {
  1089. if (exec(q))
  1090. ar = numRowsAffected();
  1091. } else if (driver()) {
  1092. QSqlQuery* sql = d->query();
  1093. if (sql && sql->exec(q))
  1094. ar = sql->numRowsAffected();
  1095. }
  1096. return ar;
  1097. }
  1098. /*
  1099. \internal
  1100. */
  1101. int Q3SqlCursor::applyPrepared(const QString& q, bool invalidate)
  1102. {
  1103. int ar = 0;
  1104. QSqlQuery* sql = 0;
  1105. if (invalidate) {
  1106. sql = (QSqlQuery*)this;
  1107. d->lastAt = QSql::BeforeFirst;
  1108. } else {
  1109. sql = d->query();
  1110. }
  1111. if (!sql)
  1112. return 0;
  1113. if (invalidate || sql->lastQuery() != q) {
  1114. if (!sql->prepare(q))
  1115. return 0;
  1116. }
  1117. int cnt = 0;
  1118. int fieldCount = (int)count();
  1119. for (int j = 0; j < fieldCount; ++j) {
  1120. const QSqlField f = d->editBuffer.field(j);
  1121. if (d->editBuffer.isGenerated(j)) {
  1122. if (f.type() == QVariant::ByteArray)
  1123. sql->bindValue(cnt, f.value(), QSql::In | QSql::Binary);
  1124. else
  1125. sql->bindValue(cnt, f.value());
  1126. cnt++;
  1127. }
  1128. }
  1129. if (sql->exec()) {
  1130. ar = sql->numRowsAffected();
  1131. }
  1132. return ar;
  1133. }
  1134. /*!
  1135. Executes the SQL query \a sql. Returns true of the cursor is
  1136. active, otherwise returns false.
  1137. */
  1138. bool Q3SqlCursor::exec(const QString & sql)
  1139. {
  1140. d->lastAt = QSql::BeforeFirst;
  1141. QSqlQuery::exec(sql);
  1142. return isActive();
  1143. }
  1144. /*!
  1145. Protected virtual function which is called whenever a field needs
  1146. to be calculated. If calculated fields are being used, derived
  1147. classes must reimplement this function and return the appropriate
  1148. value for field \a name. The default implementation returns an
  1149. invalid QVariant.
  1150. \sa setCalculated()
  1151. */
  1152. QVariant Q3SqlCursor::calculateField(const QString&)
  1153. {
  1154. return QVariant();
  1155. }
  1156. /*! \internal
  1157. Ensure fieldlist is synced with query.
  1158. */
  1159. static QString qTrim(const QString& s)
  1160. {
  1161. QString result = s;
  1162. int end = result.length() - 1;
  1163. while (end >= 0 && result[end].isSpace()) // skip white space from end
  1164. end--;
  1165. result.truncate(end + 1);
  1166. return result;
  1167. }
  1168. /*! \internal
  1169. */
  1170. void Q3SqlCursor::sync()
  1171. {
  1172. if (isActive() && isValid() && d->lastAt != at()) {
  1173. d->lastAt = at();
  1174. int i = 0;
  1175. int j = 0;
  1176. bool haveCalculatedFields = false;
  1177. for (; i < count(); ++i) {
  1178. if (!haveCalculatedFields && d->infoBuffer[i].isCalculated()) {
  1179. haveCalculatedFields = true;
  1180. }
  1181. if (QSqlRecord::isGenerated(i)) {
  1182. QVariant v = QSqlQuery::value(j);
  1183. if ((v.type() == QVariant::String) &&
  1184. d->infoBuffer[i].isTrim()) {
  1185. v = qTrim(v.toString());
  1186. }
  1187. QSqlRecord::setValue(i, v);
  1188. if (QSqlQuery::isNull(j))
  1189. QSqlRecord::field(i).clear();
  1190. j++;
  1191. }
  1192. }
  1193. if (haveCalculatedFields) {
  1194. for (i = 0; i < count(); ++i) {
  1195. if (d->infoBuffer[i].isCalculated())
  1196. QSqlRecord::setValue(i, calculateField(fieldName(i)));
  1197. }
  1198. }
  1199. }
  1200. }
  1201. /*!
  1202. Returns the value of field number \a i.
  1203. */
  1204. QVariant Q3SqlCursor::value(int i) const
  1205. {
  1206. const_cast<Q3SqlCursor *>(this)->sync();
  1207. return QSqlRecord::value(i);
  1208. }
  1209. /*! \internal
  1210. cursors should be filled with Q3SqlFieldInfos...
  1211. */
  1212. void Q3SqlCursor::append(const QSqlField& field)
  1213. {
  1214. append(Q3SqlFieldInfo(field));
  1215. }
  1216. /*!
  1217. Returns true if the field \a i is NULL or if there is no field at
  1218. position \a i; otherwise returns false.
  1219. This is the same as calling QSqlRecord::isNull(\a i)
  1220. */
  1221. bool Q3SqlCursor::isNull(int i) const
  1222. {
  1223. const_cast<Q3SqlCursor *>(this)->sync();
  1224. return QSqlRecord::isNull(i);
  1225. }
  1226. /*!
  1227. \overload
  1228. Returns true if the field called \a name is NULL or if there is no
  1229. field called \a name; otherwise returns false.
  1230. This is the same as calling QSqlRecord::isNull(\a name)
  1231. */
  1232. bool Q3SqlCursor::isNull(const QString& name) const
  1233. {
  1234. const_cast<Q3SqlCursor *>(this)->sync();
  1235. return QSqlRecord::isNull(name);
  1236. }
  1237. /*! \internal */
  1238. void Q3SqlCursor::setValue(int i, const QVariant& val)
  1239. {
  1240. sync();
  1241. #ifdef QT_DEBUG
  1242. qDebug("Q3SqlCursor::setValue(): This will not affect actual database values. Use primeInsert(), primeUpdate() or primeDelete().");
  1243. #endif
  1244. QSqlRecord::setValue(i, val);
  1245. }
  1246. /*! \internal */
  1247. bool Q3SqlCursor::seek(int i, bool relative)
  1248. {
  1249. bool res = QSqlQuery::seek(i, relative);
  1250. sync();
  1251. return res;
  1252. }
  1253. /*! \internal */
  1254. bool Q3SqlCursor::next()
  1255. {
  1256. bool res = QSqlQuery::next();
  1257. sync();
  1258. return res;
  1259. }
  1260. /*!
  1261. \fn Q3SqlCursor::previous()
  1262. \internal
  1263. */
  1264. /*! \internal */
  1265. bool Q3SqlCursor::prev()
  1266. {
  1267. bool res = QSqlQuery::previous();
  1268. sync();
  1269. return res;
  1270. }
  1271. /*! \internal */
  1272. bool Q3SqlCursor::first()
  1273. {
  1274. bool res = QSqlQuery::first();
  1275. sync();
  1276. return res;
  1277. }
  1278. /*! \internal */
  1279. bool Q3SqlCursor::last()
  1280. {
  1281. bool res = QSqlQuery::last();
  1282. sync();
  1283. return res;
  1284. }
  1285. QT_END_NAMESPACE
  1286. #endif