summaryrefslogtreecommitdiffstats
path: root/src/sql
diff options
context:
space:
mode:
authorTobias Hunger <tobias.hunger@qt.io>2019-04-16 16:32:08 +0200
committerTobias Hunger <tobias.hunger@qt.io>2019-04-16 16:32:08 +0200
commit6630937e63ae5797487b86743a7733c8ae5cc42c (patch)
tree3d53dacf6430f9099e1fb20835881205de674961 /src/sql
parent37ed6dae00640f9cc980ffda05347c12a7eb5d7e (diff)
parentc7af193d2e49e9f10b86262e63d8d13abf72b5cf (diff)
Merge commit 'dev' into 'wip/cmake-merge'
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/doc/snippets/code/doc_src_sql-driver.qdoc13
-rw-r--r--src/sql/doc/src/sql-driver.qdoc17
-rw-r--r--src/sql/kernel/qsqldatabase.cpp44
-rw-r--r--src/sql/kernel/qsqldatabase.h1
-rw-r--r--src/sql/kernel/qsqldriver.cpp16
-rw-r--r--src/sql/kernel/qsqlerror.cpp2
-rw-r--r--src/sql/kernel/qsqlerror.h6
-rw-r--r--src/sql/kernel/qsqlresult.cpp2
-rw-r--r--src/sql/models/qsqlquerymodel_p.h2
-rw-r--r--src/sql/models/qsqlrelationaldelegate.h8
-rw-r--r--src/sql/models/qsqlrelationaltablemodel.cpp10
-rw-r--r--src/sql/models/qsqlrelationaltablemodel.h4
-rw-r--r--src/sql/models/qsqltablemodel_p.h2
13 files changed, 101 insertions, 26 deletions
diff --git a/src/sql/doc/snippets/code/doc_src_sql-driver.qdoc b/src/sql/doc/snippets/code/doc_src_sql-driver.qdoc
index d127bdf8a5..9709deeccb 100644
--- a/src/sql/doc/snippets/code/doc_src_sql-driver.qdoc
+++ b/src/sql/doc/snippets/code/doc_src_sql-driver.qdoc
@@ -237,3 +237,16 @@ Could not create database object
//! [38]
QPSQLDriver::getResult: Query results lost - probably discarded on executing another SQL query.
//! [38]
+
+//! [39]
+CREATE TABLE "testTable" ("id" INTEGER);
+//! [39]
+
+//! [40]
+QString tableString("testTable");
+QSqlQuery q;
+// Create table query is not quoted, therefore it is mapped to lower case
+q.exec(QString("CREATE TABLE %1 (id INTEGER)").arg(tableString));
+// Call toLower() on the string so that it can be matched
+QSqlRecord rec = database.record(tableString.toLower());
+//! [40]
diff --git a/src/sql/doc/src/sql-driver.qdoc b/src/sql/doc/src/sql-driver.qdoc
index fd95e89812..cccce48bb3 100644
--- a/src/sql/doc/src/sql-driver.qdoc
+++ b/src/sql/doc/src/sql-driver.qdoc
@@ -381,6 +381,23 @@
multibyte enabled PostgreSQL server can be found in the PostgreSQL
Administrator Guide, Chapter 5.
+ \section3 QPSQL Case Sensitivity
+
+ PostgreSQL databases will only respect case sensitivity if the table or field
+ name is quoted when the table is created. So for example, a SQL query such
+ as:
+
+ \snippet code/doc_src_sql-driver.qdoc 39
+
+ will ensure that it can be accessed with the same case that was used. If the
+ table or field name is not quoted when created, the actual table name
+ or field name will be lower-case. When QSqlDatabase::record() or
+ QSqlDatabase::primaryIndex() access a table or field that was unquoted
+ when created, the name passed to the function must be lower-case to
+ ensure it is found. For example:
+
+ \snippet code/doc_src_sql-driver.qdoc 40
+
\section3 QPSQL BLOB Support
Binary Large Objects are supported through the \c BYTEA field type in
diff --git a/src/sql/kernel/qsqldatabase.cpp b/src/sql/kernel/qsqldatabase.cpp
index 2c7b4b83db..d63a9e59a8 100644
--- a/src/sql/kernel/qsqldatabase.cpp
+++ b/src/sql/kernel/qsqldatabase.cpp
@@ -1088,6 +1088,11 @@ QStringList QSqlDatabase::tables(QSql::TableType type) const
Returns the primary index for table \a tablename. If no primary
index exists, an empty QSqlIndex is returned.
+ \note Some drivers, such as the \l {QPSQL Case Sensitivity}{QPSQL}
+ driver, may may require you to pass \a tablename in lower case if
+ the table was not quoted when created. See the
+ \l{sql-driver.html}{Qt SQL driver} documentation for more information.
+
\sa tables(), record()
*/
@@ -1102,6 +1107,11 @@ QSqlIndex QSqlDatabase::primaryIndex(const QString& tablename) const
the table (or view) called \a tablename. The order in which the
fields appear in the record is undefined. If no such table (or
view) exists, an empty record is returned.
+
+ \note Some drivers, such as the \l {QPSQL Case Sensitivity}{QPSQL}
+ driver, may may require you to pass \a tablename in lower case if
+ the table was not quoted when created. See the
+ \l{sql-driver.html}{Qt SQL driver} documentation for more information.
*/
QSqlRecord QSqlDatabase::record(const QString& tablename) const
@@ -1380,6 +1390,40 @@ QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QStrin
}
/*!
+ \since 5.13
+ \overload
+
+ Clones the database connection \a other and stores it as \a
+ connectionName. All the settings from the original database, e.g.
+ databaseName(), hostName(), etc., are copied across. Does nothing
+ if \a other is an invalid database. Returns the newly created
+ database connection.
+
+ \note The new connection has not been opened. Before using the new
+ connection, you must call open().
+
+ This overload is useful when cloning the database in another thread to the
+ one that is used by the database represented by \a other.
+*/
+
+QSqlDatabase QSqlDatabase::cloneDatabase(const QString &other, const QString &connectionName)
+{
+ const QConnectionDict *dict = dbDict();
+ Q_ASSERT(dict);
+
+ dict->lock.lockForRead();
+ QSqlDatabase otherDb = dict->value(other);
+ dict->lock.unlock();
+ if (!otherDb.isValid())
+ return QSqlDatabase();
+
+ QSqlDatabase db(otherDb.driverName());
+ db.d->copy(otherDb.d);
+ QSqlDatabasePrivate::addDatabase(db, connectionName);
+ return db;
+}
+
+/*!
\since 4.4
Returns the connection name, which may be empty. \note The
diff --git a/src/sql/kernel/qsqldatabase.h b/src/sql/kernel/qsqldatabase.h
index 3aadab9b2f..f233c72c19 100644
--- a/src/sql/kernel/qsqldatabase.h
+++ b/src/sql/kernel/qsqldatabase.h
@@ -118,6 +118,7 @@ public:
static QSqlDatabase addDatabase(QSqlDriver* driver,
const QString& connectionName = QLatin1String(defaultConnection));
static QSqlDatabase cloneDatabase(const QSqlDatabase &other, const QString& connectionName);
+ static QSqlDatabase cloneDatabase(const QString &other, const QString& connectionName);
static QSqlDatabase database(const QString& connectionName = QLatin1String(defaultConnection),
bool open = true);
static void removeDatabase(const QString& connectionName);
diff --git a/src/sql/kernel/qsqldriver.cpp b/src/sql/kernel/qsqldriver.cpp
index 8c6ae382f6..7f7b81b05b 100644
--- a/src/sql/kernel/qsqldriver.cpp
+++ b/src/sql/kernel/qsqldriver.cpp
@@ -488,6 +488,8 @@ QString QSqlDriver::stripDelimiters(const QString &identifier, IdentifierType ty
QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName,
const QSqlRecord &rec, bool preparedStatement) const
{
+ const auto tableNameString = tableName.isEmpty() ? QString()
+ : prepareIdentifier(tableName, QSqlDriver::TableName, this);
int i;
QString s;
s.reserve(128);
@@ -500,13 +502,12 @@ QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName,
if (s.isEmpty())
return s;
s.chop(2);
- s.prepend(QLatin1String("SELECT ")).append(QLatin1String(" FROM ")).append(tableName);
+ s = QLatin1String("SELECT ") + s + QLatin1String(" FROM ") + tableNameString;
break;
case WhereStatement:
{
- const QString tableNamePrefix = tableName.isEmpty()
- ? QString()
- : prepareIdentifier(tableName, QSqlDriver::TableName, this) + QLatin1Char('.');
+ const QString tableNamePrefix = tableNameString.isEmpty()
+ ? QString() : tableNameString + QLatin1Char('.');
for (int i = 0; i < rec.count(); ++i) {
if (!rec.isGenerated(i))
continue;
@@ -523,8 +524,7 @@ QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName,
break;
}
case UpdateStatement:
- s.append(QLatin1String("UPDATE ")).append(tableName).append(
- QLatin1String(" SET "));
+ s = s + QLatin1String("UPDATE ") + tableNameString + QLatin1String(" SET ");
for (i = 0; i < rec.count(); ++i) {
if (!rec.isGenerated(i))
continue;
@@ -541,10 +541,10 @@ QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName,
s.clear();
break;
case DeleteStatement:
- s.append(QLatin1String("DELETE FROM ")).append(tableName);
+ s = s + QLatin1String("DELETE FROM ") + tableNameString;
break;
case InsertStatement: {
- s.append(QLatin1String("INSERT INTO ")).append(tableName).append(QLatin1String(" ("));
+ s = s + QLatin1String("INSERT INTO ") + tableNameString + QLatin1String(" (");
QString vals;
for (i = 0; i < rec.count(); ++i) {
if (!rec.isGenerated(i))
diff --git a/src/sql/kernel/qsqlerror.cpp b/src/sql/kernel/qsqlerror.cpp
index 41ea497ad7..7a1a91948c 100644
--- a/src/sql/kernel/qsqlerror.cpp
+++ b/src/sql/kernel/qsqlerror.cpp
@@ -365,7 +365,7 @@ QString QSqlError::nativeErrorCode() const
QString QSqlError::text() const
{
QString result = d->databaseError;
- if (!d->databaseError.endsWith(QLatin1String("\n")))
+ if (!d->databaseError.isEmpty() && !d->driverError.isEmpty() && !d->databaseError.endsWith(QLatin1String("\n")))
result += QLatin1Char(' ');
result += d->driverError;
return result;
diff --git a/src/sql/kernel/qsqlerror.h b/src/sql/kernel/qsqlerror.h
index 6dac47a7fe..7dfb6eb9ec 100644
--- a/src/sql/kernel/qsqlerror.h
+++ b/src/sql/kernel/qsqlerror.h
@@ -66,15 +66,15 @@ public:
ErrorType type = NoError,
const QString &errorCode = QString());
QSqlError(const QSqlError& other);
- QSqlError(QSqlError &&other) Q_DECL_NOTHROW : d(other.d) { other.d = nullptr; }
+ QSqlError(QSqlError &&other) noexcept : d(other.d) { other.d = nullptr; }
QSqlError& operator=(const QSqlError& other);
- QSqlError &operator=(QSqlError &&other) Q_DECL_NOTHROW { swap(other); return *this; }
+ QSqlError &operator=(QSqlError &&other) noexcept { swap(other); return *this; }
bool operator==(const QSqlError& other) const;
bool operator!=(const QSqlError& other) const;
~QSqlError();
- void swap(QSqlError &other) Q_DECL_NOTHROW { qSwap(d, other.d); }
+ void swap(QSqlError &other) noexcept { qSwap(d, other.d); }
QString driverText() const;
QString databaseText() const;
diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp
index 915a980bf0..589088238b 100644
--- a/src/sql/kernel/qsqlresult.cpp
+++ b/src/sql/kernel/qsqlresult.cpp
@@ -386,7 +386,7 @@ const QSqlDriver *QSqlResult::driver() const
void QSqlResult::setActive(bool active)
{
Q_D(QSqlResult);
- if (active && d->executedQuery.isEmpty())
+ if (active)
d->executedQuery = d->sql;
d->active = active;
diff --git a/src/sql/models/qsqlquerymodel_p.h b/src/sql/models/qsqlquerymodel_p.h
index d5ca2f89cb..64e9aeb3db 100644
--- a/src/sql/models/qsqlquerymodel_p.h
+++ b/src/sql/models/qsqlquerymodel_p.h
@@ -75,7 +75,7 @@ public:
void initColOffsets(int size);
int columnInQuery(int modelColumn) const;
- mutable QSqlQuery query = { QSqlQuery(0) };
+ mutable QSqlQuery query = { QSqlQuery(nullptr) };
mutable QSqlError error;
QModelIndex bottom;
QSqlRecord rec;
diff --git a/src/sql/models/qsqlrelationaldelegate.h b/src/sql/models/qsqlrelationaldelegate.h
index e8ae5a229d..2f16a4a54b 100644
--- a/src/sql/models/qsqlrelationaldelegate.h
+++ b/src/sql/models/qsqlrelationaldelegate.h
@@ -58,7 +58,7 @@ QT_REQUIRE_CONFIG(sqlmodel);
#include <QtCore/qmetaobject.h>
QT_BEGIN_NAMESPACE
-
+// ### Qt6: QStyledItemDelegate
class QSqlRelationalDelegate: public QItemDelegate
{
static int fieldIndex(const QSqlTableModel *const model,
@@ -109,11 +109,11 @@ QWidget *createEditor(QWidget *aParent,
// to present the DisplayRole and not the EditRole which
// is the id reference to the related model
QVariant v = index.data(Qt::DisplayRole);
- QByteArray n = editor->metaObject()->userProperty().name();
+ const QByteArray n = editor->metaObject()->userProperty().name();
if (!n.isEmpty()) {
if (!v.isValid())
- v = QVariant(editor->property(n).userType(), nullptr);
- editor->setProperty(n, v);
+ v = QVariant(editor->property(n.data()).userType(), nullptr);
+ editor->setProperty(n.data(), v);
return;
}
}
diff --git a/src/sql/models/qsqlrelationaltablemodel.cpp b/src/sql/models/qsqlrelationaltablemodel.cpp
index 1f590c4ab2..34be010474 100644
--- a/src/sql/models/qsqlrelationaltablemodel.cpp
+++ b/src/sql/models/qsqlrelationaltablemodel.cpp
@@ -626,8 +626,8 @@ QString QSqlRelationalTableModel::selectStatement() const
/*!
Returns a QSqlTableModel object for accessing the table for which
- \a column is a foreign key, or 0 if there is no relation for the
- given \a column.
+ \a column is a foreign key, or \nullptr if there is no relation for
+ the given \a column.
The returned object is owned by the QSqlRelationalTableModel.
@@ -636,12 +636,12 @@ QString QSqlRelationalTableModel::selectStatement() const
QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const
{
Q_D(const QSqlRelationalTableModel);
- if ( column < 0 || column >= d->relations.count())
- return 0;
+ if (column < 0 || column >= d->relations.count())
+ return nullptr;
QRelation &relation = const_cast<QSqlRelationalTableModelPrivate *>(d)->relations[column];
if (!relation.isValid())
- return 0;
+ return nullptr;
if (!relation.model)
relation.populateModel();
diff --git a/src/sql/models/qsqlrelationaltablemodel.h b/src/sql/models/qsqlrelationaltablemodel.h
index 555755009c..1929bd320a 100644
--- a/src/sql/models/qsqlrelationaltablemodel.h
+++ b/src/sql/models/qsqlrelationaltablemodel.h
@@ -58,7 +58,7 @@ public:
const QString &displayCol)
: tName(aTableName), iColumn(indexCol), dColumn(displayCol) {}
- void swap(QSqlRelation &other) Q_DECL_NOTHROW
+ void swap(QSqlRelation &other) noexcept
{
qSwap(tName, other.tName);
qSwap(iColumn, other.iColumn);
@@ -71,7 +71,7 @@ public:
{ return iColumn; }
inline QString displayColumn() const
{ return dColumn; }
- bool isValid() const Q_DECL_NOTHROW
+ bool isValid() const noexcept
{ return !(tName.isEmpty() || iColumn.isEmpty() || dColumn.isEmpty()); }
private:
QString tName, iColumn, dColumn;
diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h
index bb568ab444..9ac34e7259 100644
--- a/src/sql/models/qsqltablemodel_p.h
+++ b/src/sql/models/qsqltablemodel_p.h
@@ -93,7 +93,7 @@ public:
QSqlTableModel::EditStrategy strategy;
bool busyInsertingRows;
- QSqlQuery editQuery = { QSqlQuery(0) };
+ QSqlQuery editQuery = { QSqlQuery(nullptr) };
QSqlIndex primaryIndex;
QString tableName;
QString filter;