PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/qt-everywhere-opensource-src-4.7.1/src/sql/models/qsqlrelationaltablemodel.cpp

https://bitbucket.org/boroondas/3p-qt-open-83
C++ | 749 lines | 380 code | 88 blank | 281 comment | 75 complexity | d9c477cbcd41a747b3b734f27567c061 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause, GPL-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-3.0, CC-BY-SA-4.0, Apache-2.0
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2010 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 QtSql module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** Commercial Usage
  11. ** Licensees holding valid Qt Commercial licenses may use this file in
  12. ** accordance with the Qt Commercial License Agreement provided with the
  13. ** Software or, alternatively, in accordance with the terms contained in
  14. ** a written agreement between you and Nokia.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.LGPL included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. ** In addition, as a special exception, Nokia gives you certain additional
  25. ** rights. These rights are described in the Nokia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ** GNU General Public License Usage
  29. ** Alternatively, this file may be used under the terms of the GNU
  30. ** General Public License version 3.0 as published by the Free Software
  31. ** Foundation and appearing in the file LICENSE.GPL included in the
  32. ** packaging of this file. Please review the following information to
  33. ** ensure the GNU General Public License version 3.0 requirements will be
  34. ** met: http://www.gnu.org/copyleft/gpl.html.
  35. **
  36. ** If you have questions regarding the use of this file, please contact
  37. ** Nokia at qt-info@nokia.com.
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qsqlrelationaltablemodel.h"
  42. #include "qhash.h"
  43. #include "qstringlist.h"
  44. #include "qsqldatabase.h"
  45. #include "qsqldriver.h"
  46. #include "qsqlerror.h"
  47. #include "qsqlfield.h"
  48. #include "qsqlindex.h"
  49. #include "qsqlquery.h"
  50. #include "qsqlrecord.h"
  51. #include "qsqltablemodel_p.h"
  52. #include "qdebug.h"
  53. QT_BEGIN_NAMESPACE
  54. /*!
  55. \class QSqlRelation
  56. \brief The QSqlRelation class stores information about an SQL foreign key.
  57. QSqlRelation is a helper class for QSqlRelationalTableModel. See
  58. QSqlRelationalTableModel::setRelation() and
  59. QSqlRelationalTableModel::relation() for details.
  60. \sa QSqlRelationalTableModel, QSqlRelationalDelegate,
  61. {Relational Table Model Example}
  62. */
  63. /*!
  64. \fn QSqlRelation::QSqlRelation()
  65. Constructs an invalid QSqlRelation object.
  66. For such an object, the tableName(), indexColumn(), and
  67. displayColumn() functions return an empty string.
  68. \sa isValid()
  69. */
  70. /*!
  71. \fn QSqlRelation::QSqlRelation(const QString &tableName, const QString &indexColumn,
  72. const QString &displayColumn)
  73. Constructs a QSqlRelation object, where \a tableName is the SQL
  74. table name to which a foreign key refers, \a indexColumn is the
  75. foreign key, and \a displayColumn is the field that should be
  76. presented to the user.
  77. \sa tableName(), indexColumn(), displayColumn()
  78. */
  79. /*!
  80. \fn QString QSqlRelation::tableName() const
  81. Returns the name of the table to which a foreign key refers.
  82. */
  83. /*!
  84. \fn QString QSqlRelation::indexColumn() const
  85. Returns the index column from table tableName() to which a
  86. foreign key refers.
  87. */
  88. /*!
  89. \fn QString QSqlRelation::displayColumn() const
  90. Returns the column from table tableName() that should be
  91. presented to the user instead of a foreign key.
  92. */
  93. /*!
  94. \fn bool QSqlRelation::isValid() const
  95. Returns true if the QSqlRelation object is valid; otherwise
  96. returns false.
  97. */
  98. struct QRelation
  99. {
  100. public:
  101. QRelation(): model(0),m_parent(0),m_dictInitialized(false){}
  102. void init(QSqlRelationalTableModel *parent, const QSqlRelation &relation);
  103. void populateModel();
  104. bool isDictionaryInitialized();
  105. void populateDictionary();
  106. void clearDictionary();
  107. void clear();
  108. bool isValid();
  109. QSqlRelation rel;
  110. QSqlTableModel *model;
  111. QHash<QString, QVariant> dictionary;//maps keys to display values
  112. private:
  113. QSqlRelationalTableModel *m_parent;
  114. bool m_dictInitialized;
  115. };
  116. /*
  117. A QRelation must be initialized before it is considered valid.
  118. Note: population of the model and dictionary are kept separate
  119. from initialization, and are populated on an as needed basis.
  120. */
  121. void QRelation::init(QSqlRelationalTableModel *parent, const QSqlRelation &relation)
  122. {
  123. Q_ASSERT(parent != NULL);
  124. m_parent = parent;
  125. rel = relation;
  126. }
  127. void QRelation::populateModel()
  128. {
  129. if (!isValid())
  130. return;
  131. Q_ASSERT(m_parent != NULL);
  132. if (!model) {
  133. model = new QSqlTableModel(m_parent, m_parent->database());
  134. model->setTable(rel.tableName());
  135. model->select();
  136. }
  137. }
  138. bool QRelation::isDictionaryInitialized()
  139. {
  140. return m_dictInitialized;
  141. }
  142. void QRelation::populateDictionary()
  143. {
  144. if (!isValid())
  145. return;
  146. if (model == NULL)
  147. populateModel();
  148. QSqlRecord record;
  149. QString indexColumn;
  150. QString displayColumn;
  151. for (int i=0; i < model->rowCount(); ++i) {
  152. record = model->record(i);
  153. indexColumn = rel.indexColumn();
  154. if (m_parent->database().driver()->isIdentifierEscaped(indexColumn, QSqlDriver::FieldName))
  155. indexColumn = m_parent->database().driver()->stripDelimiters(indexColumn, QSqlDriver::FieldName);
  156. displayColumn = rel.displayColumn();
  157. if (m_parent->database().driver()->isIdentifierEscaped(displayColumn, QSqlDriver::FieldName))
  158. displayColumn = m_parent->database().driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName);
  159. dictionary[record.field(indexColumn).value().toString()] =
  160. record.field(displayColumn).value();
  161. }
  162. m_dictInitialized = true;
  163. }
  164. void QRelation::clearDictionary()
  165. {
  166. dictionary.clear();
  167. m_dictInitialized = false;
  168. }
  169. void QRelation::clear()
  170. {
  171. delete model;
  172. model = 0;
  173. clearDictionary();
  174. }
  175. bool QRelation::isValid()
  176. {
  177. return (rel.isValid() && m_parent != NULL);
  178. }
  179. class QSqlRelationalTableModelPrivate: public QSqlTableModelPrivate
  180. {
  181. Q_DECLARE_PUBLIC(QSqlRelationalTableModel)
  182. public:
  183. QSqlRelationalTableModelPrivate()
  184. : QSqlTableModelPrivate()
  185. {}
  186. QString relationField(const QString &tableName, const QString &fieldName) const;
  187. int nameToIndex(const QString &name) const;
  188. mutable QVector<QRelation> relations;
  189. QSqlRecord baseRec; // the record without relations
  190. void clearChanges();
  191. void clearEditBuffer();
  192. void clearCache();
  193. void revertCachedRow(int row);
  194. void translateFieldNames(int row, QSqlRecord &values) const;
  195. };
  196. static void qAppendWhereClause(QString &query, const QString &clause1, const QString &clause2)
  197. {
  198. if (clause1.isEmpty() && clause2.isEmpty())
  199. return;
  200. if (clause1.isEmpty() || clause2.isEmpty())
  201. query.append(QLatin1String(" WHERE (")).append(clause1).append(clause2);
  202. else
  203. query.append(QLatin1String(" WHERE (")).append(clause1).append(
  204. QLatin1String(") AND (")).append(clause2);
  205. query.append(QLatin1String(") "));
  206. }
  207. void QSqlRelationalTableModelPrivate::clearChanges()
  208. {
  209. for (int i = 0; i < relations.count(); ++i) {
  210. QRelation &rel = relations[i];
  211. rel.clear();
  212. }
  213. }
  214. void QSqlRelationalTableModelPrivate::revertCachedRow(int row)
  215. {
  216. QSqlTableModelPrivate::revertCachedRow(row);
  217. }
  218. int QSqlRelationalTableModelPrivate::nameToIndex(const QString &name) const
  219. {
  220. QString fieldname = name;
  221. if (db.driver()->isIdentifierEscaped(fieldname, QSqlDriver::FieldName))
  222. fieldname = db.driver()->stripDelimiters(fieldname, QSqlDriver::FieldName);
  223. return baseRec.indexOf(fieldname);
  224. }
  225. void QSqlRelationalTableModelPrivate::clearEditBuffer()
  226. {
  227. editBuffer = baseRec;
  228. }
  229. /*!
  230. \reimp
  231. */
  232. void QSqlRelationalTableModelPrivate::clearCache()
  233. {
  234. for (int i = 0; i < relations.count(); ++i)
  235. relations[i].clearDictionary();
  236. QSqlTableModelPrivate::clearCache();
  237. }
  238. /*!
  239. \class QSqlRelationalTableModel
  240. \brief The QSqlRelationalTableModel class provides an editable
  241. data model for a single database table, with foreign key support.
  242. \ingroup database
  243. \inmodule QtSql
  244. QSqlRelationalTableModel acts like QSqlTableModel, but allows
  245. columns to be set as foreign keys into other database tables.
  246. \table
  247. \row \o \inlineimage noforeignkeys.png
  248. \o \inlineimage foreignkeys.png
  249. \endtable
  250. The screenshot on the left shows a plain QSqlTableModel in a
  251. QTableView. Foreign keys (\c city and \c country) aren't resolved
  252. to human-readable values. The screenshot on the right shows a
  253. QSqlRelationalTableModel, with foreign keys resolved into
  254. human-readable text strings.
  255. The following code snippet shows how the QSqlRelationalTableModel
  256. was set up:
  257. \snippet examples/sql/relationaltablemodel/relationaltablemodel.cpp 0
  258. \codeline
  259. \snippet examples/sql/relationaltablemodel/relationaltablemodel.cpp 1
  260. \snippet examples/sql/relationaltablemodel/relationaltablemodel.cpp 2
  261. The setRelation() function calls establish a relationship between
  262. two tables. The first call specifies that column 2 in table \c
  263. employee is a foreign key that maps with field \c id of table \c
  264. city, and that the view should present the \c{city}'s \c name
  265. field to the user. The second call does something similar with
  266. column 3.
  267. If you use a read-write QSqlRelationalTableModel, you probably
  268. want to use QSqlRelationalDelegate on the view. Unlike the default
  269. delegate, QSqlRelationalDelegate provides a combobox for fields
  270. that are foreign keys into other tables. To use the class, simply
  271. call QAbstractItemView::setItemDelegate() on the view with an
  272. instance of QSqlRelationalDelegate:
  273. \snippet examples/sql/relationaltablemodel/relationaltablemodel.cpp 4
  274. The \l{sql/relationaltablemodel} example illustrates how to use
  275. QSqlRelationalTableModel in conjunction with
  276. QSqlRelationalDelegate to provide tables with foreigh key
  277. support.
  278. \image relationaltable.png
  279. Notes:
  280. \list
  281. \o The table must have a primary key declared.
  282. \o The table's primary key may not contain a relation to
  283. another table.
  284. \o If a relational table contains keys that refer to non-existent
  285. rows in the referenced table, the rows containing the invalid
  286. keys will not be exposed through the model. The user or the
  287. database is responsible for keeping referential integrity.
  288. \o If a relation's display column name is also used as a column
  289. name in the main table, or if it is used as display column
  290. name in more than one relation it will be aliased. The alias is
  291. is the relation's table name and display column name joined
  292. by an underscore (e.g. tablename_columnname). All occurrences
  293. of the duplicate display column name are aliased when
  294. duplication is detected, but no aliasing is done to the column
  295. names in the main table. The aliasing doesn't affect
  296. QSqlRelation, so QSqlRelation::displayColumn() will return the
  297. original display column name, but QSqlRecord::fieldName() will
  298. return aliases.
  299. \o When using setData() the role should always be Qt::EditRole,
  300. and when using data() the role should always be Qt::DisplayRole.
  301. \endlist
  302. \sa QSqlRelation, QSqlRelationalDelegate,
  303. {Relational Table Model Example}
  304. */
  305. /*!
  306. Creates an empty QSqlRelationalTableModel and sets the parent to \a parent
  307. and the database connection to \a db. If \a db is not valid, the
  308. default database connection will be used.
  309. */
  310. QSqlRelationalTableModel::QSqlRelationalTableModel(QObject *parent, QSqlDatabase db)
  311. : QSqlTableModel(*new QSqlRelationalTableModelPrivate, parent, db)
  312. {
  313. }
  314. /*!
  315. Destroys the object and frees any allocated resources.
  316. */
  317. QSqlRelationalTableModel::~QSqlRelationalTableModel()
  318. {
  319. }
  320. /*!
  321. \reimp
  322. */
  323. QVariant QSqlRelationalTableModel::data(const QModelIndex &index, int role) const
  324. {
  325. Q_D(const QSqlRelationalTableModel);
  326. if (role == Qt::DisplayRole && index.column() > 0 && index.column() < d->relations.count() &&
  327. d->relations.value(index.column()).isValid()) {
  328. QRelation &relation = d->relations[index.column()];
  329. if (!relation.isDictionaryInitialized())
  330. relation.populateDictionary();
  331. //only perform a dictionary lookup for the display value
  332. //when the value at index has been changed or added.
  333. //At an unmodified index, the underlying model will
  334. //already have the correct display value.
  335. QVariant v;
  336. switch (d->strategy) {
  337. case OnFieldChange:
  338. break;
  339. case OnRowChange:
  340. if (index.row() == d->editIndex || index.row() == d->insertIndex) {
  341. v = d->editBuffer.value(index.column());
  342. }
  343. break;
  344. case OnManualSubmit:
  345. const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
  346. v = row.rec.value(index.column());
  347. break;
  348. }
  349. if (v.isValid())
  350. return relation.dictionary[v.toString()];
  351. }
  352. return QSqlTableModel::data(index, role);
  353. }
  354. /*!
  355. Sets the data for the \a role in the item with the specified \a
  356. index to the \a value given. Depending on the edit strategy, the
  357. value might be applied to the database at once, or it may be
  358. cached in the model.
  359. Returns true if the value could be set, or false on error (for
  360. example, if \a index is out of bounds).
  361. For relational columns, \a value must be the index, not the
  362. display value. The index must also exist in the referenced
  363. table, otherwise the function returns false.
  364. \sa editStrategy(), data(), submit(), revertRow()
  365. */
  366. bool QSqlRelationalTableModel::setData(const QModelIndex &index, const QVariant &value,
  367. int role)
  368. {
  369. Q_D(QSqlRelationalTableModel);
  370. if ( role == Qt::EditRole && index.column() > 0 && index.column() < d->relations.count()
  371. && d->relations.value(index.column()).isValid()) {
  372. QRelation &relation = d->relations[index.column()];
  373. if (!relation.isDictionaryInitialized())
  374. relation.populateDictionary();
  375. if (!relation.dictionary.contains(value.toString()))
  376. return false;
  377. }
  378. return QSqlTableModel::setData(index, value, role);
  379. }
  380. /*!
  381. Lets the specified \a column be a foreign index specified by \a relation.
  382. Example:
  383. \snippet examples/sql/relationaltablemodel/relationaltablemodel.cpp 0
  384. \codeline
  385. \snippet examples/sql/relationaltablemodel/relationaltablemodel.cpp 1
  386. The setRelation() call specifies that column 2 in table \c
  387. employee is a foreign key that maps with field \c id of table \c
  388. city, and that the view should present the \c{city}'s \c name
  389. field to the user.
  390. Note: The table's primary key may not contain a relation to another table.
  391. \sa relation()
  392. */
  393. void QSqlRelationalTableModel::setRelation(int column, const QSqlRelation &relation)
  394. {
  395. Q_D(QSqlRelationalTableModel);
  396. if (column < 0)
  397. return;
  398. if (d->relations.size() <= column)
  399. d->relations.resize(column + 1);
  400. d->relations[column].init(this, relation);
  401. }
  402. /*!
  403. Returns the relation for the column \a column, or an invalid
  404. relation if no relation is set.
  405. \sa setRelation(), QSqlRelation::isValid()
  406. */
  407. QSqlRelation QSqlRelationalTableModel::relation(int column) const
  408. {
  409. Q_D(const QSqlRelationalTableModel);
  410. return d->relations.value(column).rel;
  411. }
  412. QString QSqlRelationalTableModelPrivate::relationField(const QString &tableName,
  413. const QString &fieldName) const
  414. {
  415. QString ret;
  416. ret.reserve(tableName.size() + fieldName.size() + 1);
  417. ret.append(tableName).append(QLatin1Char('.')).append(fieldName);
  418. return ret;
  419. }
  420. /*!
  421. \reimp
  422. */
  423. QString QSqlRelationalTableModel::selectStatement() const
  424. {
  425. Q_D(const QSqlRelationalTableModel);
  426. QString query;
  427. if (tableName().isEmpty())
  428. return query;
  429. if (d->relations.isEmpty())
  430. return QSqlTableModel::selectStatement();
  431. QString tList;
  432. QString fList;
  433. QString where;
  434. QSqlRecord rec = d->baseRec;
  435. QStringList tables;
  436. const QRelation nullRelation;
  437. // Count how many times each field name occurs in the record
  438. QHash<QString, int> fieldNames;
  439. QStringList fieldList;
  440. for (int i = 0; i < rec.count(); ++i) {
  441. QSqlRelation relation = d->relations.value(i, nullRelation).rel;
  442. QString name;
  443. if (relation.isValid())
  444. {
  445. // Count the display column name, not the original foreign key
  446. name = relation.displayColumn();
  447. if (d->db.driver()->isIdentifierEscaped(name, QSqlDriver::FieldName))
  448. name = d->db.driver()->stripDelimiters(name, QSqlDriver::FieldName);
  449. QSqlRecord rec = database().record(relation.tableName());
  450. for (int i = 0; i < rec.count(); ++i) {
  451. if (name.compare(rec.fieldName(i), Qt::CaseInsensitive) == 0) {
  452. name = rec.fieldName(i);
  453. break;
  454. }
  455. }
  456. }
  457. else
  458. name = rec.fieldName(i);
  459. fieldNames.insert(name, fieldNames.value(name, 0) + 1);
  460. fieldList.append(name);
  461. }
  462. for (int i = 0; i < rec.count(); ++i) {
  463. QSqlRelation relation = d->relations.value(i, nullRelation).rel;
  464. if (relation.isValid()) {
  465. QString relTableAlias = QString::fromLatin1("relTblAl_%1").arg(i);
  466. if (!fList.isEmpty())
  467. fList.append(QLatin1String(", "));
  468. fList.append(d->relationField(relTableAlias,relation.displayColumn()));
  469. // If there are duplicate field names they must be aliased
  470. if (fieldNames.value(fieldList[i]) > 1) {
  471. QString relTableName = relation.tableName().section(QChar::fromLatin1('.'), -1, -1);
  472. if (d->db.driver()->isIdentifierEscaped(relTableName, QSqlDriver::TableName))
  473. relTableName = d->db.driver()->stripDelimiters(relTableName, QSqlDriver::TableName);
  474. QString displayColumn = relation.displayColumn();
  475. if (d->db.driver()->isIdentifierEscaped(displayColumn, QSqlDriver::FieldName))
  476. displayColumn = d->db.driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName);
  477. fList.append(QString::fromLatin1(" AS %1_%2_%3").arg(relTableName).arg(displayColumn).arg(fieldNames.value(fieldList[i])));
  478. fieldNames.insert(fieldList[i], fieldNames.value(fieldList[i])-1);
  479. }
  480. // this needs fixing!! the below if is borken.
  481. tables.append(relation.tableName().append(QLatin1Char(' ')).append(relTableAlias));
  482. if(!where.isEmpty())
  483. where.append(QLatin1String(" AND "));
  484. where.append(d->relationField(tableName(), d->db.driver()->escapeIdentifier(rec.fieldName(i), QSqlDriver::FieldName)));
  485. where.append(QLatin1String(" = "));
  486. where.append(d->relationField(relTableAlias, relation.indexColumn()));
  487. } else {
  488. if (!fList.isEmpty())
  489. fList.append(QLatin1String(", "));
  490. fList.append(d->relationField(tableName(), d->db.driver()->escapeIdentifier(rec.fieldName(i), QSqlDriver::FieldName)));
  491. }
  492. }
  493. if (!tables.isEmpty())
  494. tList.append(tables.join(QLatin1String(", ")));
  495. if (fList.isEmpty())
  496. return query;
  497. if(!tList.isEmpty())
  498. tList.prepend(QLatin1String(", "));
  499. tList.prepend(tableName());
  500. query.append(QLatin1String("SELECT "));
  501. query.append(fList).append(QLatin1String(" FROM ")).append(tList);
  502. qAppendWhereClause(query, where, filter());
  503. QString orderBy = orderByClause();
  504. if (!orderBy.isEmpty())
  505. query.append(QLatin1Char(' ')).append(orderBy);
  506. return query;
  507. }
  508. /*!
  509. Returns a QSqlTableModel object for accessing the table for which
  510. \a column is a foreign key, or 0 if there is no relation for the
  511. given \a column.
  512. The returned object is owned by the QSqlRelationalTableModel.
  513. \sa setRelation(), relation()
  514. */
  515. QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const
  516. {
  517. Q_D(const QSqlRelationalTableModel);
  518. if ( column < 0 || column >= d->relations.count())
  519. return 0;
  520. QRelation &relation = const_cast<QSqlRelationalTableModelPrivate *>(d)->relations[column];
  521. if (!relation.isValid())
  522. return 0;
  523. if (!relation.model)
  524. relation.populateModel();
  525. return relation.model;
  526. }
  527. /*!
  528. \reimp
  529. */
  530. void QSqlRelationalTableModel::revertRow(int row)
  531. {
  532. QSqlTableModel::revertRow(row);
  533. }
  534. /*!
  535. \reimp
  536. */
  537. void QSqlRelationalTableModel::clear()
  538. {
  539. Q_D(QSqlRelationalTableModel);
  540. d->clearChanges();
  541. d->relations.clear();
  542. QSqlTableModel::clear();
  543. }
  544. /*!
  545. \reimp
  546. */
  547. bool QSqlRelationalTableModel::select()
  548. {
  549. return QSqlTableModel::select();
  550. }
  551. /*!
  552. \reimp
  553. */
  554. void QSqlRelationalTableModel::setTable(const QString &table)
  555. {
  556. Q_D(QSqlRelationalTableModel);
  557. // memorize the table before applying the relations
  558. d->baseRec = d->db.record(table);
  559. QSqlTableModel::setTable(table);
  560. }
  561. /*! \internal
  562. */
  563. void QSqlRelationalTableModelPrivate::translateFieldNames(int row, QSqlRecord &values) const
  564. {
  565. Q_Q(const QSqlRelationalTableModel);
  566. for (int i = 0; i < values.count(); ++i) {
  567. int realCol = q->indexInQuery(q->createIndex(row, i)).column();
  568. if (realCol != -1 && relations.value(realCol).isValid()) {
  569. QVariant v = values.value(i);
  570. values.replace(i, baseRec.field(realCol));
  571. values.setValue(i, v);
  572. }
  573. }
  574. }
  575. /*!
  576. \reimp
  577. */
  578. bool QSqlRelationalTableModel::updateRowInTable(int row, const QSqlRecord &values)
  579. {
  580. Q_D(QSqlRelationalTableModel);
  581. QSqlRecord rec = values;
  582. d->translateFieldNames(row, rec);
  583. return QSqlTableModel::updateRowInTable(row, rec);
  584. }
  585. /*!
  586. \reimp
  587. */
  588. bool QSqlRelationalTableModel::insertRowIntoTable(const QSqlRecord &values)
  589. {
  590. Q_D(QSqlRelationalTableModel);
  591. QSqlRecord rec = values;
  592. d->translateFieldNames(0, rec);
  593. return QSqlTableModel::insertRowIntoTable(rec);
  594. }
  595. /*!
  596. \reimp
  597. */
  598. QString QSqlRelationalTableModel::orderByClause() const
  599. {
  600. Q_D(const QSqlRelationalTableModel);
  601. const QSqlRelation rel = d->relations.value(d->sortColumn).rel;
  602. if (!rel.isValid())
  603. return QSqlTableModel::orderByClause();
  604. QString s = QLatin1String("ORDER BY ");
  605. s.append(d->relationField(QLatin1String("relTblAl_") + QString::number(d->sortColumn),
  606. rel.displayColumn()));
  607. s += d->sortOrder == Qt::AscendingOrder ? QLatin1String(" ASC") : QLatin1String(" DESC");
  608. return s;
  609. }
  610. /*!
  611. \reimp
  612. */
  613. bool QSqlRelationalTableModel::removeColumns(int column, int count, const QModelIndex &parent)
  614. {
  615. Q_D(QSqlRelationalTableModel);
  616. if (parent.isValid() || column < 0 || column + count > d->rec.count())
  617. return false;
  618. for (int i = 0; i < count; ++i) {
  619. d->baseRec.remove(column);
  620. if (d->relations.count() > column)
  621. d->relations.remove(column);
  622. }
  623. return QSqlTableModel::removeColumns(column, count, parent);
  624. }
  625. QT_END_NAMESPACE