SQL/IBASE: misc cleanup

Misc cleanup of the IBASE driver - use qsizetype and range-based for
loops.

Change-Id: I69fe8ed3303fb352b59989c625de5a51239cf604
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
This commit is contained in:
Christian Ehrlicher 2023-03-12 18:41:06 +01:00
parent b04f9388ac
commit dfe317f552

View File

@ -36,7 +36,7 @@ using namespace Qt::StringLiterals;
#define blr_boolean_dtype blr_bool #define blr_boolean_dtype blr_bool
#endif #endif
enum { QIBaseChunkSize = SHRT_MAX / 2 }; constexpr qsizetype QIBaseChunkSize = SHRT_MAX / 2;
static bool getIBaseError(QString& msg, const ISC_STATUS* status, ISC_LONG &sqlcode) static bool getIBaseError(QString& msg, const ISC_STATUS* status, ISC_LONG &sqlcode)
{ {
@ -122,7 +122,7 @@ static void delDA(XSQLDA *&sqlda)
delete [] sqlda->sqlvar[i].sqldata; delete [] sqlda->sqlvar[i].sqldata;
} }
free(sqlda); free(sqlda);
sqlda = 0; sqlda = nullptr;
} }
static QMetaType::Type qIBaseTypeName(int iType, bool hasScale) static QMetaType::Type qIBaseTypeName(int iType, bool hasScale)
@ -352,9 +352,9 @@ public:
bool isSelect(); bool isSelect();
QVariant fetchBlob(ISC_QUAD *bId); QVariant fetchBlob(ISC_QUAD *bId);
bool writeBlob(int i, const QByteArray &ba); bool writeBlob(qsizetype iPos, const QByteArray &ba);
QVariant fetchArray(int pos, ISC_QUAD *arr); QVariant fetchArray(int pos, ISC_QUAD *arr);
bool writeArray(int i, const QList<QVariant> &list); bool writeArray(qsizetype i, const QList<QVariant> &list);
public: public:
ISC_STATUS status[20]; ISC_STATUS status[20];
isc_tr_handle trans; isc_tr_handle trans;
@ -374,8 +374,8 @@ QIBaseResultPrivate::QIBaseResultPrivate(QIBaseResult *q, const QIBaseDriver *dr
localTransaction(!drv_d_func()->ibase), localTransaction(!drv_d_func()->ibase),
stmt(0), stmt(0),
ibase(drv_d_func()->ibase), ibase(drv_d_func()->ibase),
sqlda(0), sqlda(nullptr),
inda(0), inda(nullptr),
queryType(-1) queryType(-1)
{ {
} }
@ -399,20 +399,20 @@ void QIBaseResultPrivate::cleanup()
q->cleanup(); q->cleanup();
} }
bool QIBaseResultPrivate::writeBlob(int i, const QByteArray &ba) bool QIBaseResultPrivate::writeBlob(qsizetype iPos, const QByteArray &ba)
{ {
isc_blob_handle handle = 0; isc_blob_handle handle = 0;
ISC_QUAD *bId = (ISC_QUAD*)inda->sqlvar[i].sqldata; ISC_QUAD *bId = (ISC_QUAD*)inda->sqlvar[iPos].sqldata;
isc_create_blob2(status, &ibase, &trans, &handle, bId, 0, 0); isc_create_blob2(status, &ibase, &trans, &handle, bId, 0, 0);
if (!isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to create BLOB"), if (!isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to create BLOB"),
QSqlError::StatementError)) { QSqlError::StatementError)) {
int i = 0; qsizetype i = 0;
while (i < ba.size()) { while (i < ba.size()) {
isc_put_segment(status, &handle, qMin(ba.size() - i, int(QIBaseChunkSize)), isc_put_segment(status, &handle, qMin(ba.size() - i, QIBaseChunkSize),
const_cast<char*>(ba.data()) + i); ba.data() + i);
if (isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to write BLOB"))) if (isError(QT_TRANSLATE_NOOP("QIBaseResult", "Unable to write BLOB")))
return false; return false;
i += qMin(ba.size() - i, int(QIBaseChunkSize)); i += qMin(ba.size() - i, QIBaseChunkSize);
} }
} }
isc_close_blob(status, &handle); isc_close_blob(status, &handle);
@ -432,7 +432,7 @@ QVariant QIBaseResultPrivate::fetchBlob(ISC_QUAD *bId)
QByteArray ba; QByteArray ba;
int chunkSize = QIBaseChunkSize; int chunkSize = QIBaseChunkSize;
ba.resize(chunkSize); ba.resize(chunkSize);
int read = 0; qsizetype read = 0;
while (isc_get_segment(status, &handle, &len, chunkSize, ba.data() + read) == 0 || status[1] == isc_segment) { while (isc_get_segment(status, &handle, &len, chunkSize, ba.data() + read) == 0 || status[1] == isc_segment) {
read += len; read += len;
ba.resize(read + chunkSize); ba.resize(read + chunkSize);
@ -454,7 +454,7 @@ QVariant QIBaseResultPrivate::fetchBlob(ISC_QUAD *bId)
} }
template<typename T> template<typename T>
static QList<QVariant> toList(char** buf, int count, T* = nullptr) static QList<QVariant> toList(char** buf, int count)
{ {
QList<QVariant> res; QList<QVariant> res;
for (int i = 0; i < count; ++i) { for (int i = 0; i < count; ++i) {
@ -493,7 +493,7 @@ static char* readArrayBuffer(QList<QVariant>& list, char *buffer, short curDim,
} }
break; } break; }
case blr_long: case blr_long:
valList = toList<int>(&buffer, numElements[dim], static_cast<int *>(0)); valList = toList<int>(&buffer, numElements[dim]);
break; break;
case blr_short: case blr_short:
valList = toList<short>(&buffer, numElements[dim]); valList = toList<short>(&buffer, numElements[dim]);
@ -593,9 +593,8 @@ QVariant QIBaseResultPrivate::fetchArray(int pos, ISC_QUAD *arr)
template<typename T> template<typename T>
static char* fillList(char *buffer, const QList<QVariant> &list, T* = nullptr) static char* fillList(char *buffer, const QList<QVariant> &list, T* = nullptr)
{ {
for (int i = 0; i < list.size(); ++i) { for (const auto &elem : list) {
T val; T val = qvariant_cast<T>(elem);
val = qvariant_cast<T>(list.at(i));
memcpy(buffer, &val, sizeof(T)); memcpy(buffer, &val, sizeof(T));
buffer += sizeof(T); buffer += sizeof(T);
} }
@ -605,11 +604,9 @@ static char* fillList(char *buffer, const QList<QVariant> &list, T* = nullptr)
template<> template<>
char* fillList<float>(char *buffer, const QList<QVariant> &list, float*) char* fillList<float>(char *buffer, const QList<QVariant> &list, float*)
{ {
for (int i = 0; i < list.size(); ++i) { for (const auto &elem : list) {
double val; double val = qvariant_cast<double>(elem);
float val2 = 0; float val2 = (float)val;
val = qvariant_cast<double>(list.at(i));
val2 = (float)val;
memcpy(buffer, &val2, sizeof(float)); memcpy(buffer, &val2, sizeof(float));
buffer += sizeof(float); buffer += sizeof(float);
} }
@ -644,7 +641,6 @@ static char* createArrayBuffer(char *buffer, const QList<QVariant> &list,
QMetaType::Type type, short curDim, ISC_ARRAY_DESC *arrayDesc, QMetaType::Type type, short curDim, ISC_ARRAY_DESC *arrayDesc,
QString& error) QString& error)
{ {
int i;
ISC_ARRAY_BOUND *bounds = arrayDesc->array_desc_bounds; ISC_ARRAY_BOUND *bounds = arrayDesc->array_desc_bounds;
short dim = arrayDesc->array_desc_dimensions - 1; short dim = arrayDesc->array_desc_dimensions - 1;
@ -659,14 +655,14 @@ static char* createArrayBuffer(char *buffer, const QList<QVariant> &list,
} }
if (curDim != dim) { if (curDim != dim) {
for(i = 0; i < list.size(); ++i) { for (const auto &elem : list) {
if (list.at(i).typeId() != QMetaType::QVariantList) { // dimensions mismatch if (elem.typeId() != QMetaType::QVariantList) { // dimensions mismatch
error = "Array dimensons mismatch. Fieldname: %1"_L1; error = "Array dimensons mismatch. Fieldname: %1"_L1;
return 0; return 0;
} }
buffer = createArrayBuffer(buffer, list.at(i).toList(), type, curDim + 1, buffer = createArrayBuffer(buffer, elem.toList(), type, curDim + 1,
arrayDesc, error); arrayDesc, error);
if (!buffer) if (!buffer)
return 0; return 0;
@ -693,28 +689,27 @@ static char* createArrayBuffer(char *buffer, const QList<QVariant> &list,
buffer = fillList<quint64>(buffer, list); buffer = fillList<quint64>(buffer, list);
break; break;
case QMetaType::QString: case QMetaType::QString:
for (i = 0; i < list.size(); ++i) for (const auto &elem : list)
buffer = qFillBufferWithString(buffer, list.at(i).toString(), buffer = qFillBufferWithString(buffer, elem.toString(),
arrayDesc->array_desc_length, arrayDesc->array_desc_length,
arrayDesc->array_desc_dtype == blr_varying, arrayDesc->array_desc_dtype == blr_varying,
true); true);
break; break;
case QMetaType::QDate: case QMetaType::QDate:
for (i = 0; i < list.size(); ++i) { for (const auto &elem : list) {
*((ISC_DATE*)buffer) = toDate(list.at(i).toDate()); *((ISC_DATE*)buffer) = toDate(elem.toDate());
buffer += sizeof(ISC_DATE); buffer += sizeof(ISC_DATE);
} }
break; break;
case QMetaType::QTime: case QMetaType::QTime:
for (i = 0; i < list.size(); ++i) { for (const auto &elem : list) {
*((ISC_TIME*)buffer) = toTime(list.at(i).toTime()); *((ISC_TIME*)buffer) = toTime(elem.toTime());
buffer += sizeof(ISC_TIME); buffer += sizeof(ISC_TIME);
} }
break; break;
case QMetaType::QDateTime: case QMetaType::QDateTime:
for (i = 0; i < list.size(); ++i) { for (const auto &elem : list) {
*((ISC_TIMESTAMP*)buffer) = toTimeStamp(list.at(i).toDateTime()); *((ISC_TIMESTAMP*)buffer) = toTimeStamp(elem.toDateTime());
buffer += sizeof(ISC_TIMESTAMP); buffer += sizeof(ISC_TIMESTAMP);
} }
break; break;
@ -728,7 +723,7 @@ static char* createArrayBuffer(char *buffer, const QList<QVariant> &list,
return buffer; return buffer;
} }
bool QIBaseResultPrivate::writeArray(int column, const QList<QVariant> &list) bool QIBaseResultPrivate::writeArray(qsizetype column, const QList<QVariant> &list)
{ {
Q_Q(QIBaseResult); Q_Q(QIBaseResult);
QString error; QString error;
@ -745,7 +740,6 @@ bool QIBaseResultPrivate::writeArray(int column, const QList<QVariant> &list)
short arraySize = 1; short arraySize = 1;
ISC_LONG bufLen; ISC_LONG bufLen;
QList<QVariant> subList = list;
short dimensions = desc.array_desc_dimensions; short dimensions = desc.array_desc_dimensions;
for(int i = 0; i < dimensions; ++i) { for(int i = 0; i < dimensions; ++i) {
@ -936,20 +930,17 @@ bool QIBaseResult::exec()
if (d->inda) { if (d->inda) {
const QList<QVariant> &values = boundValues(); const QList<QVariant> &values = boundValues();
int i;
if (values.count() > d->inda->sqld) { if (values.count() > d->inda->sqld) {
qWarning() << "QIBaseResult::exec: Parameter mismatch, expected"_L1 << qWarning() << "QIBaseResult::exec: Parameter mismatch, expected"_L1 <<
d->inda->sqld << ", got"_L1 << values.count() << d->inda->sqld << ", got"_L1 << values.count() <<
"parameters"_L1; "parameters"_L1;
return false; return false;
} }
int para = 0; for (qsizetype para = 0; para < values.count(); ++para) {
for (i = 0; i < values.count(); ++i) {
para = i;
if (!d->inda->sqlvar[para].sqldata) if (!d->inda->sqlvar[para].sqldata)
// skip unknown datatypes // skip unknown datatypes
continue; continue;
const QVariant val(values[i]); const QVariant &val = values[para];
if (d->inda->sqlvar[para].sqltype & 1) { if (d->inda->sqlvar[para].sqltype & 1) {
if (QSqlResultPrivate::isVariantNull(val)) { if (QSqlResultPrivate::isVariantNull(val)) {
// set null indicator // set null indicator
@ -1408,26 +1399,26 @@ bool QIBaseDriver::hasFeature(DriverFeature f) const
return false; return false;
} }
bool QIBaseDriver::open(const QString & db, bool QIBaseDriver::open(const QString &db,
const QString & user, const QString &user,
const QString & password, const QString &password,
const QString & host, const QString &host,
int port, int port,
const QString & connOpts) const QString &connOpts)
{ {
Q_D(QIBaseDriver); Q_D(QIBaseDriver);
if (isOpen()) if (isOpen())
close(); close();
const QStringList opts(connOpts.split(u';', Qt::SkipEmptyParts)); const auto opts(QStringView(connOpts).split(u';', Qt::SkipEmptyParts));
QByteArray role; QByteArray role;
for (int i = 0; i < opts.count(); ++i) { for (const auto &opt : opts) {
QString tmp(opts.at(i).simplified()); const auto tmp(opt.trimmed());
qsizetype idx; qsizetype idx;
if ((idx = tmp.indexOf(u'=')) != -1) { if ((idx = tmp.indexOf(u'=')) != -1) {
QString val = tmp.mid(idx + 1).simplified(); const auto val = tmp.mid(idx + 1).trimmed();
QString opt = tmp.left(idx).simplified(); const auto opt = tmp.left(idx).trimmed().toString();
if (opt.toUpper() == "ISC_DPB_SQL_ROLE_NAME"_L1) { if (opt.toUpper() == "ISC_DPB_SQL_ROLE_NAME"_L1) {
role = val.toLocal8Bit(); role = val.toLocal8Bit();
role.truncate(255); role.truncate(255);