summaryrefslogtreecommitdiffstats
path: root/src/plugins/sqldrivers
diff options
context:
space:
mode:
authorChristian Ehrlicher <ch.ehrlicher@gmx.de>2021-01-16 21:50:52 +0100
committerChristian Ehrlicher <ch.ehrlicher@gmx.de>2021-03-08 12:44:40 +0100
commit1df974590f19dec7b632d000ab9408a4de6f117a (patch)
tree4c101b64ad212d240aed97908a2a7f368f9039ee /src/plugins/sqldrivers
parent6e899f8e8c58e4ff980a8106c72ad522b5a7dc10 (diff)
QSql/MariaDB/MySQL: misc cleanup
Cleanup the mariadb driver a little bit: - use nullptr instead NULL - use unsigned long instead int for blob handling now that QByteArray can handle more than 2^31 bytes Change-Id: I79639a42a739aabdf776cc688cb5d664248aa610 Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/plugins/sqldrivers')
-rw-r--r--src/plugins/sqldrivers/mysql/qsql_mysql.cpp40
1 files changed, 16 insertions, 24 deletions
diff --git a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
index 3092a7f772..770e7b16d8 100644
--- a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
+++ b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp
@@ -299,14 +299,10 @@ static bool qIsInteger(int t)
void QMYSQLResultPrivate::bindBlobs()
{
- int i;
- MYSQL_FIELD *fieldInfo;
- MYSQL_BIND *bind;
-
- for(i = 0; i < fields.count(); ++i) {
- fieldInfo = fields.at(i).myField;
+ for (int i = 0; i < fields.count(); ++i) {
+ MYSQL_FIELD *fieldInfo = fields.at(i).myField;
if (qIsBlob(inBinds[i].buffer_type) && meta && fieldInfo) {
- bind = &inBinds[i];
+ MYSQL_BIND *bind = &inBinds[i];
bind->buffer_length = fieldInfo->max_length;
delete[] static_cast<char*>(bind->buffer);
bind->buffer = new char[fieldInfo->max_length];
@@ -322,9 +318,6 @@ bool QMYSQLResultPrivate::bindInValues()
if (!meta)
return false;
- MYSQL_BIND *bind;
- char *field;
- int i = 0;
fields.resize(mysql_num_fields(meta));
inBinds = new MYSQL_BIND[fields.size()];
@@ -332,6 +325,7 @@ bool QMYSQLResultPrivate::bindInValues()
MYSQL_FIELD *fieldInfo;
+ int i = 0;
while((fieldInfo = mysql_fetch_field(meta))) {
QMyField &f = fields[i];
f.myField = fieldInfo;
@@ -348,8 +342,8 @@ bool QMYSQLResultPrivate::bindInValues()
} else {
fieldInfo->type = MYSQL_TYPE_STRING;
}
- bind = &inBinds[i];
- field = new char[fieldInfo->length + 1];
+ MYSQL_BIND *bind = &inBinds[i];
+ char *field = new char[fieldInfo->length + 1];
memset(field, 0, fieldInfo->length + 1);
bind->buffer_type = fieldInfo->type;
@@ -425,8 +419,8 @@ void QMYSQLResult::cleanup()
d->hasBlobs = false;
d->fields.clear();
- d->result = NULL;
- d->row = NULL;
+ d->result = nullptr;
+ d->row = nullptr;
setAt(-1);
setActive(false);
}
@@ -535,7 +529,7 @@ QVariant QMYSQLResult::data(int field)
if (!driver())
return QVariant();
- int fieldLength = 0;
+ my_ulonglong fieldLength = 0;
const QMYSQLResultPrivate::QMyField &f = d->fields.at(field);
QString val;
if (d->preparedQuery) {
@@ -555,7 +549,7 @@ QVariant QMYSQLResult::data(int field)
if (f.type.id() != QMetaType::QByteArray)
val = QString::fromUtf8(f.outField, f.bufLength);
} else {
- if (d->row[field] == NULL) {
+ if (d->row[field] == nullptr) {
// NULL value
return QVariant(f.type);
}
@@ -634,7 +628,7 @@ bool QMYSQLResult::isNull(int field)
if (d->preparedQuery)
return d->fields.at(field).nullIndicator;
else
- return d->row[field] == NULL;
+ return d->row[field] == nullptr;
}
bool QMYSQLResult::reset (const QString& query)
@@ -1076,7 +1070,7 @@ QMYSQLDriver::QMYSQLDriver(MYSQL * con, QObject * parent)
Q_D(QMYSQLDriver);
init();
if (con) {
- d->mysql = (MYSQL *) con;
+ d->mysql = con;
setOpen(true);
setOpenError(false);
if (qMySqlConnectionCount == 1)
@@ -1311,7 +1305,7 @@ void QMYSQLDriver::close()
mysql_thread_end();
#endif
mysql_close(d->mysql);
- d->mysql = NULL;
+ d->mysql = nullptr;
setOpen(false);
setOpenError(false);
}
@@ -1458,12 +1452,10 @@ QString QMYSQLDriver::formatValue(const QSqlField &field, bool trimStrings) cons
if (isOpen()) {
const QByteArray ba = field.value().toByteArray();
// buffer has to be at least length*2+1 bytes
- char* buffer = new char[ba.size() * 2 + 1];
- int escapedSize = int(mysql_real_escape_string(d->mysql, buffer,
- ba.data(), ba.size()));
+ QVarLengthArray<char, 512> buffer(ba.size() * 2 + 1);
+ auto escapedSize = mysql_real_escape_string(d->mysql, buffer.data(), ba.data(), ba.size());
r.reserve(escapedSize + 3);
- r.append(QLatin1Char('\'')).append(QString::fromUtf8(buffer)).append(QLatin1Char('\''));
- delete[] buffer;
+ r = QLatin1Char('\'') + QString::fromUtf8(buffer) + QLatin1Char('\'');
break;
} else {
qWarning("QMYSQLDriver::formatValue: Database not open");