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>
This commit is contained in:
parent
6e899f8e8c
commit
1df974590f
@ -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");
|
||||
|
Loading…
Reference in New Issue
Block a user