Changeset View
Changeset View
Standalone View
Standalone View
src/KDb.cpp
1 | /* This file is part of the KDE project | 1 | /* This file is part of the KDE project | ||
---|---|---|---|---|---|
2 | Copyright (C) 2004-2017 Jarosław Staniek <staniek@kde.org> | 2 | Copyright (C) 2004-2018 Jarosław Staniek <staniek@kde.org> | ||
3 | Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com> | 3 | Copyright (c) 2006, 2007 Thomas Braxton <kde.braxton@gmail.com> | ||
4 | Copyright (c) 1999 Preston Brown <pbrown@kde.org> | 4 | Copyright (c) 1999 Preston Brown <pbrown@kde.org> | ||
5 | Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org> | 5 | Copyright (c) 1997 Matthias Kalle Dalheimer <kalle@kde.org> | ||
6 | 6 | | |||
7 | This library is free software; you can redistribute it and/or | 7 | This library is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU Library General Public | 8 | modify it under the terms of the GNU Library General Public | ||
9 | License as published by the Free Software Foundation; either | 9 | License as published by the Free Software Foundation; either | ||
10 | version 2 of the License, or (at your option) any later version. | 10 | version 2 of the License, or (at your option) any later version. | ||
11 | 11 | | |||
12 | This library is distributed in the hope that it will be useful, | 12 | This library is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | Library General Public License for more details. | 15 | Library General Public License for more details. | ||
16 | 16 | | |||
17 | You should have received a copy of the GNU Library General Public License | 17 | You should have received a copy of the GNU Library General Public License | ||
18 | along with this library; see the file COPYING.LIB. If not, write to | 18 | along with this library; see the file COPYING.LIB. If not, write to | ||
19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 19 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
20 | * Boston, MA 02110-1301, USA. | 20 | * Boston, MA 02110-1301, USA. | ||
21 | */ | 21 | */ | ||
22 | 22 | | |||
23 | #include "KDb.h" | 23 | #include "KDb.h" | ||
24 | #include "KDbConnectionData.h" | | |||
25 | #include "KDbConnection.h" | 24 | #include "KDbConnection.h" | ||
25 | #include "KDbConnectionData.h" | ||||
26 | #include "KDbCursor.h" | 26 | #include "KDbCursor.h" | ||
27 | #include "kdb_debug.h" | 27 | #include "KDbDateTime.h" | ||
28 | #include "KDbDriverBehavior.h" | 28 | #include "KDbDriverBehavior.h" | ||
29 | #include "KDbDriverManager.h" | 29 | #include "KDbDriverManager.h" | ||
30 | #include "KDbDriver_p.h" | 30 | #include "KDbDriver_p.h" | ||
31 | #include "KDbLookupFieldSchema.h" | 31 | #include "KDbLookupFieldSchema.h" | ||
32 | #include "KDbMessageHandler.h" | 32 | #include "KDbMessageHandler.h" | ||
33 | #include "KDbNativeStatementBuilder.h" | 33 | #include "KDbNativeStatementBuilder.h" | ||
34 | #include "KDb_p.h" | | |||
35 | #include "KDbQuerySchema.h" | 34 | #include "KDbQuerySchema.h" | ||
36 | #include "KDbRecordData.h" | 35 | #include "KDbRecordData.h" | ||
37 | #include "KDbSqlResult.h" | 36 | #include "KDbSqlResult.h" | ||
38 | #include "KDbTableOrQuerySchema.h" | 37 | #include "KDbTableOrQuerySchema.h" | ||
39 | #include "KDbVersionInfo.h" | 38 | #include "KDbVersionInfo.h" | ||
39 | #include "KDb_p.h" | ||||
40 | #include "kdb_debug.h" | ||||
40 | #include "transliteration/transliteration_table.h" | 41 | #include "transliteration/transliteration_table.h" | ||
41 | 42 | | |||
42 | #include <QMap> | 43 | #include <QMap> | ||
43 | #include <QHash> | 44 | #include <QHash> | ||
44 | #include <QBuffer> | 45 | #include <QBuffer> | ||
45 | #include <QPixmap> | 46 | #include <QPixmap> | ||
46 | #include <QSet> | 47 | #include <QSet> | ||
47 | #include <QTimer> | 48 | #include <QTimer> | ||
▲ Show 20 Lines • Show All 2150 Lines • ▼ Show 20 Line(s) | |||||
2198 | QString KDb::identifierExpectedMessage(const QString &valueName, const QVariant& v) | 2199 | QString KDb::identifierExpectedMessage(const QString &valueName, const QVariant& v) | ||
2199 | { | 2200 | { | ||
2200 | return QLatin1String("<p>") + kdb::tr("Value of \"%1\" field must be an identifier.") | 2201 | return QLatin1String("<p>") + kdb::tr("Value of \"%1\" field must be an identifier.") | ||
2201 | .arg(valueName) | 2202 | .arg(valueName) | ||
2202 | + QLatin1String("</p><p>") | 2203 | + QLatin1String("</p><p>") | ||
2203 | + kdb::tr("\"%1\" is not a valid identifier.").arg(v.toString()) + QLatin1String("</p>"); | 2204 | + kdb::tr("\"%1\" is not a valid identifier.").arg(v.toString()) + QLatin1String("</p>"); | ||
2204 | } | 2205 | } | ||
2205 | 2206 | | |||
2207 | //--------- | ||||
2208 | | ||||
2209 | KDbEscapedString KDb::valueToSql(KDbField::Type ftype, const QVariant& v) | ||||
2210 | { | ||||
2211 | return valueToSqlInternal(nullptr, ftype, v); | ||||
2212 | } | ||||
2213 | | ||||
2214 | static QByteArray dateToSqlInternal(const QVariant& v, bool allowInvalidKDbDate) | ||||
2215 | { | ||||
2216 | QByteArray result(QByteArrayLiteral("<INVALID_DATE>")); | ||||
2217 | if (v.canConvert<KDbDate>()) { | ||||
2218 | const KDbDate date(v.value<KDbDate>()); | ||||
2219 | if (date.isValid() || allowInvalidKDbDate) { | ||||
2220 | result = date.toString(); // OK even if invalid or null | ||||
2221 | } | ||||
2222 | } else if (v.canConvert<QDate>()) { | ||||
2223 | const QDate date(v.toDate()); | ||||
2224 | if (date.isValid()) { | ||||
2225 | result = date.toString(Qt::ISODate).toLatin1(); | ||||
2226 | } | ||||
2227 | } | ||||
2228 | return result; | ||||
2229 | } | ||||
2230 | | ||||
2231 | KDbEscapedString KDb::dateToSql(const QVariant& v) | ||||
2232 | { | ||||
2233 | return KDbEscapedString('#') + dateToSqlInternal(v, true) + '#'; | ||||
2234 | } | ||||
2235 | | ||||
2236 | static QByteArray timeToSqlInternal(const QVariant& v, bool allowInvalidKDbTime) | ||||
2237 | { | ||||
2238 | QByteArray result(QByteArrayLiteral("<INVALID_TIME>")); | ||||
2239 | if (v.canConvert<KDbTime>()) { | ||||
2240 | const KDbTime time(v.value<KDbTime>()); | ||||
2241 | if (time.isValid() || allowInvalidKDbTime) { | ||||
2242 | result = time.toString(); // OK even if invalid or null | ||||
2243 | } | ||||
2244 | } else if (v.canConvert<QTime>()) { | ||||
2245 | const QTime time(v.toTime()); | ||||
2246 | if (time.isValid()) { | ||||
2247 | if (time.msec() == 0) { | ||||
2248 | result = time.toString(Qt::ISODate).toLatin1(); | ||||
2249 | } else { | ||||
2250 | result = time.toString(Qt::ISODateWithMs).toLatin1(); | ||||
2251 | } | ||||
2252 | } | ||||
2253 | } | ||||
2254 | return result; | ||||
2255 | } | ||||
2256 | | ||||
2257 | KDbEscapedString KDb::timeToSql(const QVariant& v) | ||||
2258 | { | ||||
2259 | return KDbEscapedString('#') + timeToSqlInternal(v, true) + '#'; | ||||
2260 | } | ||||
2261 | | ||||
2262 | static QByteArray dateTimeToSqlInternal(const QVariant& v, char separator, bool allowInvalidKDbDateTime) | ||||
2263 | { | ||||
2264 | QByteArray result(QByteArrayLiteral("<INVALID_DATETIME>")); | ||||
2265 | if (v.canConvert<KDbDateTime>()) { | ||||
2266 | const KDbDateTime dateTime(v.value<KDbDateTime>()); | ||||
2267 | if (dateTime.isValid() || allowInvalidKDbDateTime) { | ||||
2268 | result = dateTime.toString(); // OK even if invalid or null | ||||
2269 | } | ||||
2270 | } else if (v.canConvert<QDateTime>()) { | ||||
2271 | const QDateTime dateTime(v.toDateTime()); | ||||
2272 | if (dateTime.isValid()) { | ||||
2273 | result = dateTime.date().toString(Qt::ISODate).toLatin1() + separator; | ||||
2274 | const QTime time(dateTime.time()); | ||||
2275 | if (time.msec() == 0) { | ||||
2276 | result += time.toString(Qt::ISODate).toLatin1(); | ||||
2277 | } else { | ||||
2278 | result += time.toString(Qt::ISODateWithMs).toLatin1(); | ||||
2279 | } | ||||
2280 | } | ||||
2281 | } | ||||
2282 | return result; | ||||
2283 | } | ||||
2284 | | ||||
2285 | KDbEscapedString KDb::dateTimeToSql(const QVariant& v) | ||||
2286 | { | ||||
2287 | return KDbEscapedString('#') + dateTimeToSqlInternal(v, ' ', true) + '#'; | ||||
2288 | } | ||||
2289 | | ||||
2290 | KDbEscapedString KDb::dateTimeToSql(const QDateTime& v) | ||||
2291 | { | ||||
2292 | return KDb::dateTimeToIsoString(v); | ||||
2293 | } | ||||
2294 | | ||||
2295 | KDbEscapedString KDb::dateToIsoString(const QVariant& v) | ||||
2296 | { | ||||
2297 | return KDbEscapedString('\'') + dateToSqlInternal(v, false) + KDbEscapedString('\''); | ||||
2298 | } | ||||
2299 | | ||||
2300 | KDbEscapedString KDb::timeToIsoString(const QVariant& v) | ||||
2301 | { | ||||
2302 | return KDbEscapedString('\'') + timeToSqlInternal(v, false) + KDbEscapedString('\''); | ||||
2303 | } | ||||
2304 | | ||||
2305 | KDbEscapedString KDb::dateTimeToIsoString(const QVariant& v) | ||||
2306 | { | ||||
2307 | return KDbEscapedString('\'') + dateTimeToSqlInternal(v, 'T', false) + KDbEscapedString('\''); | ||||
2308 | } | ||||
2309 | | ||||
2206 | //-------------------------------------------------------------------------------- | 2310 | //-------------------------------------------------------------------------------- | ||
2207 | 2311 | | |||
2208 | #ifdef KDB_DEBUG_GUI | 2312 | #ifdef KDB_DEBUG_GUI | ||
2209 | 2313 | | |||
2210 | static KDb::DebugGUIHandler s_debugGUIHandler = nullptr; | 2314 | static KDb::DebugGUIHandler s_debugGUIHandler = nullptr; | ||
2211 | 2315 | | |||
2212 | void KDb::setDebugGUIHandler(KDb::DebugGUIHandler handler) | 2316 | void KDb::setDebugGUIHandler(KDb::DebugGUIHandler handler) | ||
2213 | { | 2317 | { | ||
Show All 25 Lines |