From 9b643bc6c9f1b97d8753848bcdc6d22f1272fabe Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Thu, 16 Feb 2023 20:13:46 +0100 Subject: [PATCH] SQL: small optimization for SQLDriver::escapeIdentifier() Avoid a memmove (and replace it with a memcpy) by not using QString::prepend() but create a completely new string object instead. Change-Id: Ibdb4a9c6b15b96f1743d47e158ff0fb9b2048221 Reviewed-by: Andy Shaw --- src/plugins/sqldrivers/db2/qsql_db2.cpp | 2 +- src/plugins/sqldrivers/ibase/qsql_ibase.cpp | 2 +- src/plugins/sqldrivers/oci/qsql_oci.cpp | 2 +- src/plugins/sqldrivers/odbc/qsql_odbc.cpp | 7 ++++--- src/plugins/sqldrivers/psql/qsql_psql.cpp | 2 +- src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp | 2 +- 6 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/plugins/sqldrivers/db2/qsql_db2.cpp b/src/plugins/sqldrivers/db2/qsql_db2.cpp index c2ca844d9d..a18d7a84fa 100644 --- a/src/plugins/sqldrivers/db2/qsql_db2.cpp +++ b/src/plugins/sqldrivers/db2/qsql_db2.cpp @@ -1699,8 +1699,8 @@ QString QDB2Driver::escapeIdentifier(const QString &identifier, IdentifierType) QString res = identifier; if (!identifier.isEmpty() && !identifier.startsWith(u'"') && !identifier.endsWith(u'"') ) { res.replace(u'"', "\"\""_L1); - res.prepend(u'"').append(u'"'); res.replace(u'.', "\".\""_L1); + res = u'"' + res + u'"'; } return res; } diff --git a/src/plugins/sqldrivers/ibase/qsql_ibase.cpp b/src/plugins/sqldrivers/ibase/qsql_ibase.cpp index d228628c08..b92b91c5cb 100644 --- a/src/plugins/sqldrivers/ibase/qsql_ibase.cpp +++ b/src/plugins/sqldrivers/ibase/qsql_ibase.cpp @@ -1860,8 +1860,8 @@ QString QIBaseDriver::escapeIdentifier(const QString &identifier, IdentifierType QString res = identifier; if (!identifier.isEmpty() && !identifier.startsWith(u'"') && !identifier.endsWith(u'"') ) { res.replace(u'"', "\"\""_L1); - res.prepend(u'"').append(u'"'); res.replace(u'.', "\".\""_L1); + res = u'"' + res + u'"'; } return res; } diff --git a/src/plugins/sqldrivers/oci/qsql_oci.cpp b/src/plugins/sqldrivers/oci/qsql_oci.cpp index c7d68e73f1..3dfb4f38d8 100644 --- a/src/plugins/sqldrivers/oci/qsql_oci.cpp +++ b/src/plugins/sqldrivers/oci/qsql_oci.cpp @@ -2753,8 +2753,8 @@ QString QOCIDriver::escapeIdentifier(const QString &identifier, IdentifierType t QString res = identifier; if (!identifier.isEmpty() && !isIdentifierEscaped(identifier, type)) { res.replace(u'"', "\"\""_L1); - res.prepend(u'"').append(u'"'); res.replace(u'.', "\".\""_L1); + res = u'"' + res + u'"'; } return res; } diff --git a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp index b0b5edbcc8..2d367afa7e 100644 --- a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp +++ b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp @@ -2598,9 +2598,10 @@ QString QODBCDriver::escapeIdentifier(const QString &identifier, IdentifierType) QChar quote = const_cast(d)->quoteChar(); QString res = identifier; if (!identifier.isEmpty() && !identifier.startsWith(quote) && !identifier.endsWith(quote) ) { - res.replace(quote, QString(quote)+QString(quote)); - res.prepend(quote).append(quote); - res.replace(u'.', QString(quote) + u'.' +QString(quote)); + const QString quoteStr(quote); + res.replace(quote, quoteStr + quoteStr); + res.replace(u'.', quoteStr + u'.' + quoteStr); + res = quote + res + quote; } return res; } diff --git a/src/plugins/sqldrivers/psql/qsql_psql.cpp b/src/plugins/sqldrivers/psql/qsql_psql.cpp index 06a15bb14a..81d7e14444 100644 --- a/src/plugins/sqldrivers/psql/qsql_psql.cpp +++ b/src/plugins/sqldrivers/psql/qsql_psql.cpp @@ -1516,8 +1516,8 @@ QString QPSQLDriver::escapeIdentifier(const QString &identifier, IdentifierType) QString res = identifier; if (!identifier.isEmpty() && !identifier.startsWith(u'"') && !identifier.endsWith(u'"') ) { res.replace(u'"', "\"\""_L1); - res.prepend(u'"').append(u'"'); res.replace(u'.', "\".\""_L1); + res = u'"' + res + u'"'; } return res; } diff --git a/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp b/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp index 6812addae1..2933c68a9d 100644 --- a/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp +++ b/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp @@ -49,9 +49,9 @@ static QString _q_escapeIdentifier(const QString &identifier, QSqlDriver::Identi return res; if (!identifier.isEmpty() && !identifier.startsWith(u'"') && !identifier.endsWith(u'"')) { res.replace(u'"', "\"\""_L1); - res.prepend(u'"').append(u'"'); if (type == QSqlDriver::TableName) res.replace(u'.', "\".\""_L1); + res = u'"' + res + u'"'; } return res; }