67 for (
int i = 0; i < r.
count() && i < values.
count(); ++i)
88 for (
int i = 0; i < record.
count(); ++i) {
97 if (oldValue.
isNull() || oldValue != value)
159 for (
int i = rec.
count() - 1; i >= 0; i--)
175 Q_ASSERT_X(
false,
"QSqlTableModelPrivate::revertCachedRow()",
"Invalid entry in cache map");
180 emit q->dataChanged(q->createIndex(row, 0),
181 q->createIndex(row, q->columnCount() - 1));
190 int oldKey = it.
key();
214 const_cast<QSqlResult *>(
query.
result())->detachFromResultSet();
224 for (i = 0; i < rec.
count(); ++i) {
228 for (i = 0; i < whereValues.
count(); ++i) {
255 for (
int i = 0; i < record.
count(); ++i)
259 for (
int i = 0; i < record.
count(); ++i)
409 d->initRecordAndPrimaryIndex();
410 d->initColOffsets(
d->rec.count());
412 if (
d->rec.count() == 0)
448 d->initRecordAndPrimaryIndex();
469 switch (
d->strategy) {
472 if (index.
row() ==
d->insertIndex) {
475 return d->editBuffer.value(index.
column());
477 if (
d->editIndex == item.
row()) {
478 if (
d->editBuffer.isGenerated(item.
column()))
479 return d->editBuffer.value(item.
column());
483 if (
d->cache.contains(index.
row())) {
502 switch (
d->strategy) {
505 if (
d->insertIndex == section)
533 switch (
d->strategy) {
537 return index.
row() ==
d->editIndex &&
d->editBuffer.isGenerated(index.
column());
569 switch (
d->strategy) {
571 if (index.
row() ==
d->insertIndex) {
575 d->clearEditBuffer();
583 if (index.
row() ==
d->insertIndex) {
587 if (
d->editIndex != index.
row()) {
588 if (
d->editIndex != -1)
590 d->clearEditBuffer();
593 d->editIndex = index.
row();
650 whereValues, prepStatement);
659 return d->exec(stmt, prepStatement, rec, whereValues);
692 return d->exec(stmt, prepStatement, rec,
QSqlRecord() );
730 return d->exec(stmt, prepStatement,
QSqlRecord() , whereValues);
752 switch (
d->strategy) {
754 if (
d->insertIndex == -1)
758 if (
d->editBuffer.isEmpty())
760 if (
d->insertIndex != -1) {
763 d->bottom =
d->bottom.sibling(
d->bottom.row() + 1,
d->bottom.column());
768 d->clearEditBuffer();
774 it !=
d->cache.constEnd(); ++
it) {
775 switch (
it.value().op) {
779 d->bottom =
d->bottom.sibling(
d->bottom.row() + 1,
d->bottom.column());
790 Q_ASSERT_X(
false,
"QSqlTableModel::submitAll()",
"Invalid cache operation");
878 d->strategy = strategy;
900 switch (
d->strategy) {
904 if (
d->editIndex != -1)
906 else if (
d->insertIndex != -1)
910 while (!
d->cache.isEmpty())
927 switch (
d->strategy) {
931 if (
d->editIndex == row) {
932 d->editBuffer.clear();
933 int oldIndex =
d->editIndex;
936 }
else if (
d->insertIndex == row) {
937 d->revertInsertedRow();
941 d->revertCachedRow(row);
955 return d->primaryIndex;
970 d->primaryIndex =
key;
1005 d->sortColumn = column;
1006 d->sortOrder = order;
1040 return d->rec.indexOf(fieldName);
1054 if (
d->tableName.isEmpty()) {
1059 if (
d->rec.isEmpty()) {
1074 if (!
d->filter.isEmpty())
1095 if (parent.
isValid() || column < 0 || column + count >
d->rec.count())
1097 for (
int i = 0; i < count; ++i)
1098 d->rec.remove(column);
1099 if (
d->query.isActive())
1122 if (parent.
isValid() || row < 0 || count <= 0)
1126 switch (
d->strategy) {
1129 for (i = 0; i < count; ++i) {
1130 if (row + i ==
d->insertIndex)
1131 d->revertInsertedRow();
1138 for (i = 0; i < count; ++i) {
1181 switch (
d->strategy) {
1187 d->insertIndex = row;
1189 d->clearEditBuffer();
1194 if (!
d->cache.isEmpty()) {
1196 while (it !=
d->cache.begin() && (--
it).
key() >= row) {
1197 int oldKey = it.
key();
1200 it =
d->cache.insert(oldKey + count, oldValue);
1204 for (
int i = 0; i < count; ++i) {
1250 it !=
d->cache.constEnd(); ++
it) {
1254 }
else if (
d->insertIndex >= 0) {
1279 while (i !=
d->cache.constEnd() && i.key() <= it.
row()) {
1286 if (
d->insertIndex >= 0 && it.
row() >=
d->insertIndex)
1319 if (
d->query.isActive())
1340 if (
d->rec.field(index.
column()).isReadOnly())
1355 Q_ASSERT_X(row >= 0,
"QSqlTableModel::setRecord()",
"Cannot set a record to a row less than 0");
1360 switch (
d->strategy) {
1363 return d->setRecord(row, record);
1373 for (
int i = 0; i < record.
count(); ++i) {
static void clearGenerated(QSqlRecord &rec)
The QVariant class acts like a union for the most common Qt data types.
The QSqlError class provides SQL database error information.
The QSqlIndex class provides functions to manipulate and describe database indexes.
QModelIndex indexInQuery(const QModelIndex &item) const
Returns the index of the value in the database result set for the given item in the model...
void * internalPointer() const
Returns a void * pointer used by the model to associate the index with the internal data structure...
const Key & key() const
Returns the current item's key as a const reference.
EditStrategy
This enum type describes which strategy to choose when editing values in the database.
const QSqlDriver * driver() const
Returns the database driver associated with the query.
virtual QString selectStatement() const
Returns the SQL SELECT statement used internally to populate the model.
virtual void setEditStrategy(EditStrategy strategy)
Sets the strategy for editing values in the database to strategy.
bool isValid() const
Returns true if the field's variant type is valid; otherwise returns false.
static void setGeneratedValue(QSqlRecord &rec, int c, QVariant v)
virtual int nameToIndex(const QString &name) const
bool insertRows(int row, int count, const QModelIndex &parent=QModelIndex())
Inserts count empty rows at position row.
int rowCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
void beforeInsert(QSqlRecord &record)
This signal is emitted by insertRowIntoTable() before a new row is inserted into the currently active...
QSqlError lastError() const
Returns information about the last error that occurred on the database.
#define QT_END_NAMESPACE
This macro expands to.
int rowCount(const QModelIndex &parent=QModelIndex()) const
If the database supports returning the size of a query (see QSqlDriver::hasFeature()), the number of rows of the current query is returned.
bool prepare(const QString &query)
Prepares the SQL query query for execution.
QModelIndex sibling(int row, int column) const
Returns the sibling at row and column.
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
virtual bool hasFeature(DriverFeature f) const =0
Returns true if the driver supports feature feature; otherwise returns false.
#define it(className, varName)
int count(const T &t) const
Returns the number of occurrences of value in the vector.
QString filter() const
Returns the currently set filter.
void initRecordAndPrimaryIndex()
bool setRecord(int row, const QSqlRecord &record)
Set a record for OnFieldChange and OnRowChange.
virtual void setFilter(const QString &filter)
Sets the current filter to filter.
QSqlDatabase database() const
Returns a pointer to the used QSqlDatabase or 0 if no database was set.
bool isValid() const
Returns true if the QSqlDatabase has a valid driver.
The QSqlQuery class provides a means of executing and manipulating SQL statements.
EditStrategy editStrategy() const
Returns the current edit strategy.
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const
Returns the index of the data in row and column with parent.
virtual void clearCache()
The QSqlDatabase class represents a connection to a database.
virtual bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the role data for the item at index to value.
QModelIndex createIndex(int row, int column, void *data=0) const
Creates a model index for the given row and column with the internal pointer ptr. ...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
bool isEmpty() const
Returns true if there are no fields in the record; otherwise returns false.
iterator find(const Key &key)
Returns an iterator pointing to the item with key key in the map.
bool isNull(int i) const
Returns true if the field index is null or if there is no field at position index; otherwise returns ...
The QSqlRecord class encapsulates a database record.
void endInsertRows()
Ends a row insertion operation.
The QString class provides a Unicode character string.
bool isActive() const
Returns true if the query is active.
QSqlIndex primaryKey() const
Returns the primary key for the current table, or an empty QSqlIndex if the table is not set or has n...
QVariant data(const QModelIndex &item, int role=Qt::DisplayRole) const
Returns the value for the specified item and role.
The QObject class is the base class of all Qt objects.
const QSqlResult * result() const
Returns the result associated with the query.
virtual ~QSqlTableModel()
Destroys the object and frees any allocated resources.
bool setRecord(int row, const QSqlRecord &record)
Sets the values at the specified row to the values of record.
QSqlDriver * driver() const
Returns the database driver used to access the database connection.
void setPrimaryKey(const QSqlIndex &key)
Protected method that allows subclasses to set the primary key to key.
QString lastQuery() const
Returns the text of the current query being used, or an empty string if there is no current query tex...
QSqlIndex primaryIndex(const QString &tablename) const
Returns the primary index for table tablename.
bool isDirty(const QModelIndex &index) const
Returns true if the value at the index index is dirty, otherwise false.
QString name() const
Returns the name of the field.
bool isIdentifierEscaped(const QString &identifier, IdentifierType type) const
Returns whether identifier is escaped according to the database rules.
void primeInsert(int row, QSqlRecord &record)
This signal is emitted by insertRows(), when an insertion is initiated in the given row of the curren...
T & value() const
Returns a modifiable reference to the current item's value.
bool isGenerated(int i) const
Returns true if the record has a field at position index and this field is to be generated (the defau...
void clear()
Reimplemented Function
void clear()
Clears the result set and releases any resources held by the query.
QSqlRecord primaryValues(int index)
int columnCount(const QModelIndex &parent=QModelIndex()) const
Reimplemented Function
The QSqlQueryModel class provides a read-only data model for SQL result sets.
#define QT_BEGIN_NAMESPACE
This macro expands to.
QSqlRecord record(const QString &tablename) const
Returns a QSqlRecord populated with the names of all the fields in the table (or view) called tablena...
virtual void setTable(const QString &tableName)
Sets the database table on which the model operates to tableName.
const_iterator ConstIterator
Qt-style synonym for QMap::const_iterator.
virtual bool insertRowIntoTable(const QSqlRecord &values)
Inserts the values values into the currently active database table.
QSqlQuery query() const
Returns the QSqlQuery associated with this model.
void revert()
This reimplemented slot is called by the item delegates when the user canceled editing the current ro...
bool insertRow(int row, const QModelIndex &parent=QModelIndex())
Inserts a single row before the given row in the child items of the parent specified.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
virtual void clearEditBuffer()
QSqlTableModel::EditStrategy strategy
int row() const
Returns the row this model index refers to.
static QSqlDatabase database(const QString &connectionName=QLatin1String(defaultConnection), bool open=true)
Returns the database connection called connectionName.
const T value(const Key &key) const
Returns the value associated with the key key.
void setQuery(const QSqlQuery &query)
This function simply calls QSqlQueryModel::setQuery(query).
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
virtual void revertCachedRow(int row)
bool insertRecord(int row, const QSqlRecord &record)
Inserts the record after row.
virtual bool updateRowInTable(int row, const QSqlRecord &values)
Updates the given row in the currently active database table with the specified values.
virtual QString orderByClause() const
Returns an SQL ORDER BY clause based on the currently set sort order.
bool isValid() const
Returns true if this model index is valid; otherwise returns false.
const T & at(int i) const
Returns the item at index position i in the vector.
Qt::ItemFlags flags(const QModelIndex &index) const
Reimplemented Function
void sort(int column, Qt::SortOrder order)
Sorts the data by column with the sort order order.
bool removeColumns(int column, int count, const QModelIndex &parent=QModelIndex())
Removes count columns from the parent model, starting at index column.
void setQuery(const QSqlQuery &query)
Resets the model and sets the data provider to be the given query.
bool submit()
This reimplemented slot is called by the item delegates when the user stopped editing the current row...
int remove(const Key &key)
Removes all the items that have the key key from the map.
#define Q_ASSERT_X(cond, where, what)
int count() const
Returns the number of fields in the record.
void addBindValue(const QVariant &val, QSql::ParamType type=QSql::In)
Adds the value val to the list of values when using positional value binding.
QSqlError lastError() const
Returns error information about the last error (if any) that occurred with this query.
QString & append(QChar c)
The QMap::iterator class provides an STL-style non-const iterator for QMap and QMultiMap.
virtual void clear()
Clears the model and releases any acquired resource.
void headerDataChanged(Qt::Orientation orientation, int first, int last)
This signal is emitted whenever a header is changed.
void clear()
Clears the contents of the string and makes it empty.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the map...
virtual bool deleteRowFromTable(int row)
Deletes the given row from the currently active database table.
bool setData(const QModelIndex &index, const QVariant &value, int role=Qt::EditRole)
Sets the data for the item index for the role role to value.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key key and a value of value.
QString tableName() const
Returns the name of the currently selected table.
QObject * parent() const
Returns a pointer to the parent object.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Returns the header data for the given role in the section of the header with the specified orientatio...
QSqlRecord record(const QVector< QVariant > &values) const
Populates our record with values.
The QModelIndex class is used to locate data in a data model.
QVariant headerData(int section, Qt::Orientation orientation, int role=Qt::DisplayRole) const
Reimplemented Function
QModelIndex indexInQuery(const QModelIndex &item) const
Returns the index of the value in the database result set for the given item in the model...
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight)
This signal is emitted whenever the data in an existing item changes.
QSqlRecord record() const
Returns an empty record containing information about the fields of the current query.
void beforeUpdate(int row, QSqlRecord &record)
This signal is emitted by updateRowInTable() before the row is updated in the currently active databa...
void clear()
Removes all the record's fields.
QSqlTableModel(QObject *parent=0, QSqlDatabase db=QSqlDatabase())
Creates an empty QSqlTableModel and sets the parent to parent and the database connection to db...
The QSqlTableModel class provides an editable data model for a single database table.
iterator erase(iterator it)
Removes the (key, value) pair pointed to by the iterator pos from the map, and returns an iterator to...
bool isValid() const
Returns true if an error is set, otherwise false.
virtual bool select()
Populates the model with data from the table that was set via setTable(), using the specified filter ...
bool seek(int i, bool relative=false)
Retrieves the record at position index, if available, and positions the query on the retrieved record...
bool submitAll()
Submits all pending changes and returns true on success.
virtual void revertRow(int row)
Reverts all changes for the specified row.
void setValue(int i, const QVariant &val)
Sets the value of the field at position index to val.
The QSqlField class manipulates the fields in SQL database tables and views.
bool exec(const QString &query)
Executes the SQL in query.
QString stripDelimiters(const QString &identifier, IdentifierType type) const
Returns the identifier with the leading and trailing delimiters removed, identifier can either be a t...
void revertAll()
Reverts all pending changes.
int fieldIndex(const QString &fieldName) const
Returns the index of the field fieldName, or -1 if no corresponding field exists in the model...
QString fieldName(int i) const
Returns the name of the field at position index.
QVariant value(int i) const
Returns the value of field index in the current record.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
bool exec(const QString &stmt, bool prepStatement, const QSqlRecord &rec, const QSqlRecord &whereValues)
bool removeRows(int row, int count, const QModelIndex &parent=QModelIndex())
Removes count rows starting at row.
QVariant data(const QModelIndex &idx, int role=Qt::DisplayRole) const
Reimplemented Function
void clear()
Removes all items from the map.
int indexOf(const QString &name) const
Returns the position of the field called name within the record, or -1 if it cannot be found...
QVariant value(int i) const
Returns the value of the field located at position index in the record.
void beginInsertRows(const QModelIndex &parent, int first, int last)
Begins a row insertion operation.
int column() const
Returns the column this model index refers to.
void setGenerated(const QString &name, bool generated)
Sets the generated flag for the field called name to generated.
void beforeDelete(int row)
This signal is emitted by deleteRowFromTable() before the row is deleted from the currently active da...
virtual void setSort(int column, Qt::SortOrder order)
Sets the sort order for column to order.