7#include <QtCore/qtimezone.h>
8#include <QtCore/private/qflatmap_p.h>
10#include <firebird/impl/blr.h>
20using namespace Firebird;
24using namespace Qt::StringLiterals;
30 IUtil *util = master->getUtilInterface();
32 util->formatStatus(buf,
sizeof(buf), status);
33 return QString::fromUtf8(buf);
37
38
39
40
41
42
43
46 const QString msg = fbErrorString(master, status);
47 const ISC_LONG sqlcode = isc_sqlcode(status->getErrors());
48 const QString code = (sqlcode != -1 && sqlcode != 0)
49 ? QString::number(sqlcode) : QString();
51 return QSqlError({}, msg, type, code);
56 static const char *typeNames[] = {
57 "NoError",
"ConnectionError",
"StatementError",
"TransactionError",
"UnknownError"
59 const int ti =
std::clamp(
static_cast<
int>(err.type()), 0,
60 static_cast<
int>(
std::size(typeNames)) - 1);
61 const QString code = err.nativeErrorCode();
62 const QString text = err.databaseText().isEmpty() ? err.driverText() : err.databaseText();
63 return QString::fromLatin1(
"[%1, %2] %3").arg(QLatin1StringView(typeNames[ti]), code, text);
67
68
69
70
71
72
73
74
76 Firebird::ITransaction *&tr,
TxnEnd end)
92
93
94
95
96
100 const unsigned char tpb[] = {
101 isc_tpb_version3, isc_tpb_write, isc_tpb_read_committed, isc_tpb_rec_version,
102 static_cast<
unsigned char>(wait == TxnWait::Wait ? isc_tpb_wait : isc_tpb_nowait)
104 return att->startTransaction(&st,
sizeof(tpb), tpb);
113 case blr_cstring:
return QMetaType::QString;
115 case blr_long:
return hasScale ? QMetaType::Double : QMetaType::Int;
117 case blr_int64:
return hasScale ? QMetaType::Double : QMetaType::LongLong;
122 case blr_dec128:
return QMetaType::Double;
123 case blr_int128:
return QMetaType::QString;
124 case blr_sql_date:
return QMetaType::QDate;
125 case blr_sql_time:
return QMetaType::QTime;
126 case blr_sql_time_tz:
return QMetaType::QDateTime;
128 case blr_timestamp_tz:
return QMetaType::QDateTime;
129 case blr_bool:
return QMetaType::Bool;
131
132 default:
return QMetaType::QString;
142
143 if (fbBaseType(fbType) == SQL_INT128)
144 return QMetaType::QString;
145 return QMetaType::Double;
149 case SQL_LONG:
return QMetaType::Int;
150 case SQL_INT64:
return QMetaType::LongLong;
151 case SQL_INT128:
return QMetaType::QString;
152 case SQL_FLOAT:
return QMetaType::Double;
153 case SQL_DOUBLE:
return QMetaType::Double;
155 case SQL_DEC34:
return QMetaType::QString;
156 case SQL_BOOLEAN:
return QMetaType::Bool;
157 case SQL_TYPE_DATE:
return QMetaType::QDate;
158 case SQL_TYPE_TIME:
return QMetaType::QTime;
160 case SQL_TIMESTAMP_TZ:
161 case SQL_TIME_TZ:
return QMetaType::QDateTime;
162 case SQL_BLOB:
return QMetaType::QByteArray;
163 case SQL_ARRAY:
return QMetaType::QVariantList;
165 case SQL_VARYING:
return QMetaType::QString;
167 qCWarning(lcFirebird,
"fbTypeToQt: unknown Firebird type %d", fbType);
168 return QMetaType::QString;
174#ifndef QSQL_HAS_INT128
179 ThrowStatusWrapper st(status);
181 master->getUtilInterface()->getInt128(&st)->toString(
182 &st,
reinterpret_cast<
const FB_I128 *>(slot), scale,
sizeof(buf), buf);
183 return QString::fromLatin1(buf);
189 IMaster *master, IStatus *status, QString &errorText)
191#ifdef QSQL_HAS_INT128
194 qinternalint128 v = 0;
195 if (!QtPrivate::sqlScaledFromVariant(val, scale, &v, &errorText))
198
199
200 static_assert(std::is_same_v<ISC_UINT64, quint64>,
201 "the FB_I128 words are written through quint64 pointers");
203 QtPrivate::sqlInt128ToWords(v, &raw.fb_data[0], &raw.fb_data[1]);
204 std::memcpy(slot, &raw,
sizeof(raw));
208
209
210
211
212
213 const QByteArray strVal = QtPrivate::sqlNumericInputString(val);
214 if (!QtPrivate::sqlIsPlainDecimal(strVal)) {
215 errorText = QtPrivate::sqlCannotEncodeError(strVal, scale);
219 ThrowStatusWrapper st(status);
220 master->getUtilInterface()->getInt128(&st)->fromString(
221 &st, scale, strVal.constData(),
reinterpret_cast<FB_I128 *>(slot));
223 }
catch (
const FbException &) {
225 errorText = QtPrivate::sqlIsIntegralVariant(val.typeId())
226 ? QtPrivate::sqlOutOfRangeError(val, scale)
227 : QtPrivate::sqlCannotEncodeError(strVal, scale);
234 IMaster *master, IStatus *status, QString *overflowText)
236#ifdef QSQL_HAS_INT128
241 std::memcpy(&raw, slot,
sizeof(raw));
242 const qinternalint128 value = QtPrivate::sqlInt128FromWords(raw.fb_data[0], raw.fb_data[1]);
243 bool overflow =
false;
244 const QVariant result = QtPrivate::sqlApplyScale(value, scale, policy, &overflow);
246 if (overflow && overflowText)
247 *overflowText = QtPrivate::sqlScaledToString(value, scale);
250 const QString str = int128SlotToString(slot, scale, master, status);
252 case QSql::HighPrecision:
254 case QSql::LowPrecisionDouble:
255 return QVariant(str.toDouble());
256 case QSql::LowPrecisionInt32:
257 case QSql::LowPrecisionInt64: {
259
260
261 const qsizetype dot = str.indexOf(u'.');
262 const QStringView intPart = dot < 0 ? QStringView(str) : QStringView(str).first(dot);
264 if (policy == QSql::LowPrecisionInt32) {
265 const int v = intPart.toInt(&ok);
269 const qint64 v = intPart.toLongLong(&ok);
276 return QVariant(str.toDouble());
279 return QVariant(str.toDouble());
288
289
290
291
292
293
296 return util->encodeDate(
static_cast<
unsigned>(d.year()),
297 static_cast<
unsigned>(d.month()),
298 static_cast<
unsigned>(d.day()));
305 return util->encodeTime(
static_cast<
unsigned>(t.hour()),
306 static_cast<
unsigned>(t.minute()),
307 static_cast<
unsigned>(t.second()),
308 static_cast<
unsigned>(t.msec()) * 10);
314
315
316
317
319 ts.timestamp_date = encodeQDate(util, dt.date());
320 ts.timestamp_time = encodeQTime(util, dt.time());
325
326
327
328
331 QByteArray id = zone.id();
332 if (id.size() > 3 && id.startsWith(
"UTC") && (id.at(3) ==
'+' || id.at(3) ==
'-'))
340 return QTimeZone::utc();
341 if (id.at(0) ==
'+' || id.at(0) ==
'-')
342 return QTimeZone(
"UTC" + id);
343 return QTimeZone(id);
347
348
349
350
351
352
353
354
357 static thread_local QVarLengthFlatMap<ISC_USHORT, QTimeZone, 8> zones;
358 const auto it = zones.find(zoneId);
359 if (it != zones.end())
361 const QTimeZone zone = fromFirebirdZoneId(QByteArray(tzName));
362 zones.insert(zoneId, zone);
368 ThrowStatusWrapper st(iStatus);
369 ISC_TIMESTAMP_TZ tstz;
371 const bool valid = dt.isValid();
372 const QDate d = valid ? dt.date() : QDate(1858, 11, 17);
373 const QTime t = valid ? dt.time() : QTime(0, 0, 0);
374 const QByteArray ianaId =
375 (valid && dt.timeZone().isValid()) ? toFirebirdZoneId(dt.timeZone()) :
"UTC"_ba;
376 util->encodeTimeStampTz(&st, &tstz,
377 static_cast<
unsigned>(d.year()),
378 static_cast<
unsigned>(d.month()),
379 static_cast<
unsigned>(d.day()),
380 static_cast<
unsigned>(t.hour()),
381 static_cast<
unsigned>(t.minute()),
382 static_cast<
unsigned>(t.second()),
383 static_cast<
unsigned>(t.msec()) * 10,
390 ThrowStatusWrapper st(iStatus);
392 const bool valid = dt.isValid();
393 const QTime t = valid ? dt.time() : QTime(0, 0, 0);
394 const QByteArray ianaId =
395 (valid && dt.timeZone().isValid()) ? toFirebirdZoneId(dt.timeZone()) :
"UTC"_ba;
396 util->encodeTimeTz(&st, &ttz,
397 static_cast<
unsigned>(t.hour()),
398 static_cast<
unsigned>(t.minute()),
399 static_cast<
unsigned>(t.second()),
400 static_cast<
unsigned>(t.msec()) * 10,
412 util->decodeDate(date, &year, &month, &day);
413 return QDate(
static_cast<
int>(year),
static_cast<
int>(month),
static_cast<
int>(day));
422 util->decodeTime(time, &hour, &minute, &sec, &frac);
423 return QTime(
static_cast<
int>(hour),
static_cast<
int>(minute),
424 static_cast<
int>(sec),
static_cast<
int>(frac / 10));
430
431
432
433 return QDateTime(decodeFirebirdDate(util, ts.timestamp_date),
434 decodeFirebirdTime(util, ts.timestamp_time));
438 const ISC_TIMESTAMP_TZ &tstz)
440 CheckStatusWrapper st(iStatus);
448 char tzName[64] = {};
449 util->decodeTimeStampTz(&st, &tstz, &year, &month, &day, &hour, &minute, &sec, &frac,
450 sizeof(tzName), tzName);
451 const QTimeZone tz = zoneForFirebirdId(tstz.time_zone, tzName);
452 return QDateTime(QDate(
static_cast<
int>(year),
static_cast<
int>(month),
static_cast<
int>(day)),
453 QTime(
static_cast<
int>(hour),
static_cast<
int>(minute),
454 static_cast<
int>(sec),
static_cast<
int>(frac / 10)),
459 const ISC_TIME_TZ &ttz)
461 CheckStatusWrapper st(iStatus);
466 char tzName[64] = {};
467 util->decodeTimeTz(&st, &ttz, &hour, &minute, &sec, &frac,
sizeof(tzName), tzName);
468 const QTimeZone tz = zoneForFirebirdId(ttz.time_zone, tzName);
469 return QDateTime(QDate(1970, 1, 1),
470 QTime(
static_cast<
int>(hour),
static_cast<
int>(minute),
471 static_cast<
int>(sec),
static_cast<
int>(frac / 10)),
476
477
478
479
480
481void encodeTextValue(
char *data,
int fbType, qsizetype length,
const QByteArray &bytes)
483 if (fbBaseType(fbType) == SQL_VARYING) {
485 const qsizetype len =
std::min(length, bytes.size());
486 *
reinterpret_cast<
unsigned short *>(data) =
static_cast<
unsigned short>(len);
487 std::memcpy(data + 2, bytes.constData(), len);
489 const qsizetype len =
std::min(bytes.size(), length);
490 std::memcpy(data, bytes.constData(), len);
492 std::memset(data + len,
' ', length - len);
Q_LOGGING_CATEGORY(lcEventDispatcher, "qt.eventdispatcher")
static QString int128SlotToString(const char *slot, int scale, IMaster *master, IStatus *status)
QSqlError fbError(IMaster *master, IStatus *status, QSqlError::ErrorType type)
static QTimeZone zoneForFirebirdId(ISC_USHORT zoneId, const char *tzName)
static QByteArray toFirebirdZoneId(const QTimeZone &zone)
static QTimeZone fromFirebirdZoneId(const QByteArray &id)
QDateTime decodeFirebirdTimestamp(Firebird::IUtil *util, const ISC_TIMESTAMP &ts)
bool encodeInt128Slot(const QVariant &val, int scale, char *slot, Firebird::IMaster *master, Firebird::IStatus *status, QString &errorText)
QString fbErrorLog(const QSqlError &err)
Firebird::ITransaction * startDefaultTransaction(Firebird::IAttachment *att, Firebird::ThrowStatusWrapper &st, TxnWait wait)
QString fbErrorString(Firebird::IMaster *master, Firebird::IStatus *status)
ISC_TIMESTAMP encodeQDateTime(Firebird::IUtil *util, const QDateTime &dt)
ISC_TIME encodeQTime(Firebird::IUtil *util, const QTime &t)
QDate decodeFirebirdDate(Firebird::IUtil *util, ISC_DATE date)
QDateTime decodeFirebirdTimeTz(Firebird::IStatus *iStatus, Firebird::IUtil *util, const ISC_TIME_TZ &ttz)
void encodeTextValue(char *data, int fbType, qsizetype length, const QByteArray &bytes)
void finishAndClear(Firebird::ThrowStatusWrapper &st, Firebird::ITransaction *&tr, TxnEnd end)
QDateTime decodeFirebirdTimestampTz(Firebird::IStatus *iStatus, Firebird::IUtil *util, const ISC_TIMESTAMP_TZ &tstz)
QMetaType::Type blrTypeToQt(unsigned char blrType, bool hasScale)
QTime decodeFirebirdTime(Firebird::IUtil *util, ISC_TIME time)
ISC_TIMESTAMP_TZ encodeQDateTimeTz(Firebird::IStatus *iStatus, Firebird::IUtil *util, const QDateTime &dt)
ISC_TIME_TZ encodeQTimeTz(Firebird::IStatus *iStatus, Firebird::IUtil *util, const QDateTime &dt)
ISC_DATE encodeQDate(Firebird::IUtil *util, const QDate &d)
QMetaType::Type fbTypeToQt(int fbType, int scale)
QVariant decodeInt128Slot(const char *slot, int scale, QSql::NumericalPrecisionPolicy policy, Firebird::IMaster *master, Firebird::IStatus *status, QString *overflowText=nullptr)