| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/kernel/qsqldriver.cpp |
| Switch to Source code | Preprocessed file |
| Line | Source | Count | ||||||
|---|---|---|---|---|---|---|---|---|
| 1 | - | |||||||
| 2 | - | |||||||
| 3 | - | |||||||
| 4 | - | |||||||
| 5 | static QString prepareIdentifier(const QString &identifier, | - | ||||||
| 6 | QSqlDriver::IdentifierType type, const QSqlDriver *driver) | - | ||||||
| 7 | { | - | ||||||
| 8 | ((!(driver != __null)) ? qt_assert("driver != NULL",__FILE__,4854) : qt_noop()); | - | ||||||
| 9 | QString ret = identifier; | - | ||||||
| 10 | if (!driver->isIdentifierEscaped(identifier, type)) { | - | ||||||
| 11 | ret = driver->escapeIdentifier(identifier, type); | - | ||||||
| 12 | } | - | ||||||
| 13 | return ret; | - | ||||||
| 14 | } | - | ||||||
| 15 | QSqlDriver::QSqlDriver(QObject *parent) | - | ||||||
| 16 | : QObject(*new QSqlDriverPrivate, parent) | - | ||||||
| 17 | { | - | ||||||
| 18 | } | - | ||||||
| 19 | - | |||||||
| 20 | - | |||||||
| 21 | - | |||||||
| 22 | QSqlDriver::QSqlDriver(QSqlDriverPrivate &dd, QObject *parent) | - | ||||||
| 23 | : QObject(dd, parent) | - | ||||||
| 24 | { | - | ||||||
| 25 | } | - | ||||||
| 26 | - | |||||||
| 27 | - | |||||||
| 28 | - | |||||||
| 29 | - | |||||||
| 30 | - | |||||||
| 31 | QSqlDriver::~QSqlDriver() | - | ||||||
| 32 | { | - | ||||||
| 33 | } | - | ||||||
| 34 | bool QSqlDriver::isOpen() const | - | ||||||
| 35 | { | - | ||||||
| 36 | returnconst QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 37 | return executed 8308 times by 12 tests: d->return d->isOpen;Executed by:
executed 8308 times by 12 tests: return d->isOpen;Executed by:
executed 8308 times by 12 tests: isOpen;return d->isOpen;Executed by:
executed 8308 times by 12 tests: return d->isOpen;Executed by:
| 8308 | ||||||
| 38 | } | - | ||||||
| 39 | - | |||||||
| 40 | - | |||||||
| 41 | - | |||||||
| 42 | - | |||||||
| 43 | - | |||||||
| 44 | - | |||||||
| 45 | bool QSqlDriver::isOpenError() const | - | ||||||
| 46 | { | - | ||||||
| 47 | returnconst QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 48 | return executed 5596 times by 9 tests: d->return d->isOpenError;Executed by:
executed 5596 times by 9 tests: return d->isOpenError;Executed by:
executed 5596 times by 9 tests: isOpenError;return d->isOpenError;Executed by:
executed 5596 times by 9 tests: return d->isOpenError;Executed by:
| 5596 | ||||||
| 49 | } | - | ||||||
| 50 | void QSqlDriver::setOpen(bool open) | - | ||||||
| 51 | { | - | ||||||
| 52 | QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 53 | d->isOpen = open; | - | ||||||
| 54 | } executed 89 times by 10 tests: end of blockExecuted by:
| 89 | ||||||
| 55 | void QSqlDriver::setOpenError(bool error) | - | ||||||
| 56 | { | - | ||||||
| 57 | QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 58 | d->isOpenError = error; | - | ||||||
| 59 | if (error
| 3-89 | ||||||
| 60 | d_func()->d-> executed 3 times by 1 test: isOpen = false;d->isOpen = false;Executed by:
executed 3 times by 1 test: d->isOpen = false;Executed by:
| 3 | ||||||
| 61 | } executed 92 times by 10 tests: end of blockExecuted by:
| 92 | ||||||
| 62 | bool QSqlDriver::beginTransaction() | - | ||||||
| 63 | { | - | ||||||
| 64 | return false; | - | ||||||
| 65 | } | - | ||||||
| 66 | bool QSqlDriver::commitTransaction() | - | ||||||
| 67 | { | - | ||||||
| 68 | return false; | - | ||||||
| 69 | } | - | ||||||
| 70 | bool QSqlDriver::rollbackTransaction() | - | ||||||
| 71 | { | - | ||||||
| 72 | return false; | - | ||||||
| 73 | } | - | ||||||
| 74 | void QSqlDriver::setLastError(const QSqlError &error) | - | ||||||
| 75 | { | - | ||||||
| 76 | QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 77 | d->error = error; | - | ||||||
| 78 | } executed 37 times by 9 tests: end of blockExecuted by:
| 37 | ||||||
| 79 | - | |||||||
| 80 | - | |||||||
| 81 | - | |||||||
| 82 | - | |||||||
| 83 | - | |||||||
| 84 | - | |||||||
| 85 | QSqlError QSqlDriver::lastError() const | - | ||||||
| 86 | { | - | ||||||
| 87 | returnconst QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 88 | return executed 29 times by 6 tests: d->return d->error;Executed by:
executed 29 times by 6 tests: return d->error;Executed by:
executed 29 times by 6 tests: error;return d->error;Executed by:
executed 29 times by 6 tests: return d->error;Executed by:
| 29 | ||||||
| 89 | } | - | ||||||
| 90 | QStringList QSqlDriver::tables(QSql::TableType) const | - | ||||||
| 91 | { | - | ||||||
| 92 | return QStringList(); | - | ||||||
| 93 | } | - | ||||||
| 94 | - | |||||||
| 95 | - | |||||||
| 96 | - | |||||||
| 97 | - | |||||||
| 98 | - | |||||||
| 99 | - | |||||||
| 100 | - | |||||||
| 101 | QSqlIndex QSqlDriver::primaryIndex(const QString&) const | - | ||||||
| 102 | { | - | ||||||
| 103 | return QSqlIndex(); | - | ||||||
| 104 | } | - | ||||||
| 105 | QSqlRecord QSqlDriver::record(const QString & ) const | - | ||||||
| 106 | { | - | ||||||
| 107 | return QSqlRecord(); | - | ||||||
| 108 | } | - | ||||||
| 109 | QString QSqlDriver::escapeIdentifier(const QString &identifier, IdentifierType) const | - | ||||||
| 110 | { | - | ||||||
| 111 | return identifier; | - | ||||||
| 112 | } | - | ||||||
| 113 | bool QSqlDriver::isIdentifierEscaped(const QString &identifier, IdentifierType type) const | - | ||||||
| 114 | { | - | ||||||
| 115 | (void)type;; | - | ||||||
| 116 | return identifier.size() > 2 | - | ||||||
| 117 | && identifier.startsWith(QLatin1Char('"')) | - | ||||||
| 118 | && identifier.endsWith(QLatin1Char('"')); | - | ||||||
| 119 | } | - | ||||||
| 120 | QString QSqlDriver::stripDelimiters(const QString &identifier, IdentifierType type) const | - | ||||||
| 121 | { | - | ||||||
| 122 | QString ret; | - | ||||||
| 123 | if (isIdentifierEscaped(identifier, type)) { | - | ||||||
| 124 | ret = identifier.mid(1); | - | ||||||
| 125 | ret.chop(1); | - | ||||||
| 126 | } else { | - | ||||||
| 127 | ret = identifier; | - | ||||||
| 128 | } | - | ||||||
| 129 | return ret; | - | ||||||
| 130 | } | - | ||||||
| 131 | QString QSqlDriver::sqlStatement(StatementType type, const QString &tableName, | - | ||||||
| 132 | const QSqlRecord &rec, bool preparedStatement) const | - | ||||||
| 133 | { | - | ||||||
| 134 | int i; | - | ||||||
| 135 | QString s; | - | ||||||
| 136 | s.reserve(128); | - | ||||||
| 137 | switch (type) { | - | ||||||
| 138 | case executed 293 times by 3 tests: SelectStatement:case SelectStatement:Executed by:
executed 293 times by 3 tests: case SelectStatement:Executed by:
| 293 | ||||||
| 139 | for (i = 0; i < rec.count()
| 293-721 | ||||||
| 140 | if (rec.isGenerated(i)
| 0-721 | ||||||
| 141 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1String(", ")); executed 721 times by 3 tests: s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1String(", "));Executed by:
| 721 | ||||||
| 142 | } executed 721 times by 3 tests: end of blockExecuted by:
| 721 | ||||||
| 143 | if (s.isEmpty()
| 0-293 | ||||||
| 144 | return never executed: s;return s;never executed: return s; | 0 | ||||||
| 145 | s.chop(2); | - | ||||||
| 146 | s.prepend(QLatin1String("SELECT ")).append(QLatin1String(" FROM ")).append(tableName); | - | ||||||
| 147 | break; executed 293 times by 3 tests: break;Executed by:
| 293 | ||||||
| 148 | case executed 135 times by 3 tests: WhereStatement:case WhereStatement:Executed by:
executed 135 times by 3 tests: case WhereStatement:Executed by:
| 135 | ||||||
| 149 | { | - | ||||||
| 150 | const QString tableNamePrefix = tableName.isEmpty()
| 0-135 | ||||||
| 151 | ? QString() | - | ||||||
| 152 | : prepareIdentifier(tableName, QSqlDriver::TableName, this) + QLatin1Char('.'); | - | ||||||
| 153 | for (int i = 0; i < rec.count()
| 135-286 | ||||||
| 154 | if (!rec.isGenerated(i)
| 0-286 | ||||||
| 155 | continue; never executed: continue; | 0 | ||||||
| 156 | s.append(s.isEmpty() ? QLatin1String(i? " AND"WHERE ") : QLatin1String(" "WHEREAND ")); | - | ||||||
| 157 | s.append(tableNamePrefix); | - | ||||||
| 158 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)); | - | ||||||
| 159 | if (rec.isNull(i)
| 17-269 | ||||||
| 160 | s.append(QLatin1String(" IS NULL")); executed 17 times by 2 tests: s.append(QLatin1String(" IS NULL"));Executed by:
| 17 | ||||||
| 161 | else if (preparedStatement
| 117-152 | ||||||
| 162 | s.append(QLatin1String(" = ?")); executed 152 times by 3 tests: s.append(QLatin1String(" = ?"));Executed by:
| 152 | ||||||
| 163 | else | - | ||||||
| 164 | s.append(QLatin1String(" = ")).append(formatValue(rec.field(i))); executed 117 times by 3 tests: s.append(QLatin1String(" = ")).append(formatValue(rec.field(i)));Executed by:
| 117 | ||||||
| 165 | } | - | ||||||
| 166 | break; executed 135 times by 3 tests: break;Executed by:
| 135 | ||||||
| 167 | } | - | ||||||
| 168 | case executed 49 times by 2 tests: UpdateStatement:case UpdateStatement:Executed by:
executed 49 times by 2 tests: case UpdateStatement:Executed by:
| 49 | ||||||
| 169 | s.append(QLatin1String("UPDATE ")).append(tableName).append( | - | ||||||
| 170 | QLatin1String(" SET ")); | - | ||||||
| 171 | for (i = 0; i < rec.count()
| 49-153 | ||||||
| 172 | if (!rec.isGenerated(i)
| 64-89 | ||||||
| 173 | continue; executed 89 times by 2 tests: continue;Executed by:
| 89 | ||||||
| 174 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1Char('=')); | - | ||||||
| 175 | if (preparedStatement
| 0-64 | ||||||
| 176 | s.append(QLatin1Char('?')); executed 64 times by 2 tests: s.append(QLatin1Char('?'));Executed by:
| 64 | ||||||
| 177 | else | - | ||||||
| 178 | s.append(formatValue(rec.field(i))); never executed: s.append(formatValue(rec.field(i))); | 0 | ||||||
| 179 | s.append(QLatin1String(", ")); | - | ||||||
| 180 | } executed 64 times by 2 tests: end of blockExecuted by:
| 64 | ||||||
| 181 | if (s.endsWith(QLatin1String(", "))
| 0-49 | ||||||
| 182 | s.chop(2); executed 49 times by 2 tests: s.chop(2);Executed by:
| 49 | ||||||
| 183 | else | - | ||||||
| 184 | s.clear(); never executed: s.clear(); | 0 | ||||||
| 185 | break; executed 49 times by 2 tests: break;Executed by:
| 49 | ||||||
| 186 | case executed 22 times by 1 test: DeleteStatement:case DeleteStatement:Executed by:
executed 22 times by 1 test: case DeleteStatement:Executed by:
| 22 | ||||||
| 187 | s.append(QLatin1String("DELETE FROM ")).append(tableName); | - | ||||||
| 188 | break; executed 22 times by 1 test: break;Executed by:
| 22 | ||||||
| 189 | case executed 53 times by 2 tests: InsertStatement:case InsertStatement:Executed by:
executed 53 times by 2 tests: {case InsertStatement:Executed by:
| 53 | ||||||
| 190 | s.append(QLatin1String("INSERT INTO ")).append(tableName).append(QLatin1String(" (")); | - | ||||||
| 191 | QString vals; | - | ||||||
| 192 | for (i = 0; i < rec.count()
| 53-145 | ||||||
| 193 | if (!rec.isGenerated(i)
| 17-128 | ||||||
| 194 | continue; executed 17 times by 1 test: continue;Executed by:
| 17 | ||||||
| 195 | s.append(prepareIdentifier(rec.fieldName(i), QSqlDriver::FieldName, this)).append(QLatin1String(", ")); | - | ||||||
| 196 | if (preparedStatement
| 0-128 | ||||||
| 197 | vals.append(QLatin1Char('?')); executed 128 times by 2 tests: vals.append(QLatin1Char('?'));Executed by:
| 128 | ||||||
| 198 | else | - | ||||||
| 199 | vals.append(formatValue(rec.field(i))); never executed: vals.append(formatValue(rec.field(i))); | 0 | ||||||
| 200 | vals.append(QLatin1String(", ")); | - | ||||||
| 201 | } executed 128 times by 2 tests: end of blockExecuted by:
| 128 | ||||||
| 202 | if (vals.isEmpty()
| 1-52 | ||||||
| 203 | s.clear(); | - | ||||||
| 204 | } executed 1 time by 1 test: else {end of blockExecuted by:
| 1 | ||||||
| 205 | vals.chop(2); | - | ||||||
| 206 | s[s.length() - 2] = QLatin1Char(')'); | - | ||||||
| 207 | s.append(QLatin1String("VALUES (")).append(vals).append(QLatin1Char(')')); | - | ||||||
| 208 | } executed 52 times by 2 tests: end of blockExecuted by:
| 52 | ||||||
| 209 | break; executed 53 times by 2 tests: }break;Executed by:
| 53 | ||||||
| 210 | } | - | ||||||
| 211 | return executed 552 times by 4 tests: s;return s;Executed by:
executed 552 times by 4 tests: return s;Executed by:
| 552 | ||||||
| 212 | } | - | ||||||
| 213 | QString QSqlDriver::formatValue(const QSqlField &field, bool trimStrings) const | - | ||||||
| 214 | { | - | ||||||
| 215 | const QLatin1String nullTxt("NULL"); | - | ||||||
| 216 | - | |||||||
| 217 | QString r; | - | ||||||
| 218 | if (field.isNull()) | - | ||||||
| 219 | r = nullTxt; | - | ||||||
| 220 | else { | - | ||||||
| 221 | switch (field.type()) { | - | ||||||
| 222 | case QVariant::Int: | - | ||||||
| 223 | case QVariant::UInt: | - | ||||||
| 224 | if (field.value().type() == QVariant::Bool) | - | ||||||
| 225 | r = field.value().toBool() ? QLatin1String("1") : QLatin1String("0"); | - | ||||||
| 226 | else | - | ||||||
| 227 | r = field.value().toString(); | - | ||||||
| 228 | break; | - | ||||||
| 229 | - | |||||||
| 230 | case QVariant::Date: | - | ||||||
| 231 | if (field.value().toDate().isValid()) | - | ||||||
| 232 | r = QLatin1Char('\'') + field.value().toDate().toString(Qt::ISODate) | - | ||||||
| 233 | + QLatin1Char('\''); | - | ||||||
| 234 | else | - | ||||||
| 235 | r = nullTxt; | - | ||||||
| 236 | break; | - | ||||||
| 237 | case QVariant::Time: | - | ||||||
| 238 | if (field.value().toTime().isValid()) | - | ||||||
| 239 | r = QLatin1Char('\'') + field.value().toTime().toString(Qt::ISODate) | - | ||||||
| 240 | + QLatin1Char('\''); | - | ||||||
| 241 | else | - | ||||||
| 242 | r = nullTxt; | - | ||||||
| 243 | break; | - | ||||||
| 244 | case QVariant::DateTime: | - | ||||||
| 245 | if (field.value().toDateTime().isValid()) | - | ||||||
| 246 | r = QLatin1Char('\'') + | - | ||||||
| 247 | field.value().toDateTime().toString(Qt::ISODate) + QLatin1Char('\''); | - | ||||||
| 248 | else | - | ||||||
| 249 | r = nullTxt; | - | ||||||
| 250 | break; | - | ||||||
| 251 | - | |||||||
| 252 | case QVariant::String: | - | ||||||
| 253 | case QVariant::Char: | - | ||||||
| 254 | { | - | ||||||
| 255 | QString result = field.value().toString(); | - | ||||||
| 256 | if (trimStrings) { | - | ||||||
| 257 | int end = result.length(); | - | ||||||
| 258 | while (end && result.at(end-1).isSpace()) | - | ||||||
| 259 | end--; | - | ||||||
| 260 | result.truncate(end); | - | ||||||
| 261 | } | - | ||||||
| 262 | - | |||||||
| 263 | result.replace(QLatin1Char('\''), QLatin1String("''")); | - | ||||||
| 264 | r = QLatin1Char('\'') + result + QLatin1Char('\''); | - | ||||||
| 265 | break; | - | ||||||
| 266 | } | - | ||||||
| 267 | case QVariant::Bool: | - | ||||||
| 268 | r = QString::number(field.value().toBool()); | - | ||||||
| 269 | break; | - | ||||||
| 270 | case QVariant::ByteArray : { | - | ||||||
| 271 | if (hasFeature(BLOB)) { | - | ||||||
| 272 | QByteArray ba = field.value().toByteArray(); | - | ||||||
| 273 | QString res; | - | ||||||
| 274 | static const char hexchars[] = "0123456789abcdef"; | - | ||||||
| 275 | for (int i = 0; i < ba.size(); ++i) { | - | ||||||
| 276 | uchar s = (uchar) ba[i]; | - | ||||||
| 277 | res += QLatin1Char(hexchars[s >> 4]); | - | ||||||
| 278 | res += QLatin1Char(hexchars[s & 0x0f]); | - | ||||||
| 279 | } | - | ||||||
| 280 | r = QLatin1Char('\'') + res + QLatin1Char('\''); | - | ||||||
| 281 | break; | - | ||||||
| 282 | } | - | ||||||
| 283 | } | - | ||||||
| 284 | - | |||||||
| 285 | default: | - | ||||||
| 286 | r = field.value().toString(); | - | ||||||
| 287 | break; | - | ||||||
| 288 | } | - | ||||||
| 289 | } | - | ||||||
| 290 | return r; | - | ||||||
| 291 | } | - | ||||||
| 292 | QVariant QSqlDriver::handle() const | - | ||||||
| 293 | { | - | ||||||
| 294 | return QVariant(); | - | ||||||
| 295 | } | - | ||||||
| 296 | bool QSqlDriver::subscribeToNotification(const QString &name) | - | ||||||
| 297 | { | - | ||||||
| 298 | (void)name;; | - | ||||||
| 299 | return false; | - | ||||||
| 300 | } | - | ||||||
| 301 | bool QSqlDriver::unsubscribeFromNotification(const QString &name) | - | ||||||
| 302 | { | - | ||||||
| 303 | (void)name;; | - | ||||||
| 304 | return false; | - | ||||||
| 305 | } | - | ||||||
| 306 | QStringList QSqlDriver::subscribedToNotifications() const | - | ||||||
| 307 | { | - | ||||||
| 308 | return QStringList(); | - | ||||||
| 309 | } | - | ||||||
| 310 | void QSqlDriver::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) | - | ||||||
| 311 | { | - | ||||||
| 312 | QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 313 | d->precisionPolicy = precisionPolicy; | - | ||||||
| 314 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||
| 315 | QSql::NumericalPrecisionPolicy QSqlDriver::numericalPrecisionPolicy() const | - | ||||||
| 316 | { | - | ||||||
| 317 | returnconst QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 318 | return executed 2194 times by 10 tests: d->return d->precisionPolicy;Executed by:
executed 2194 times by 10 tests: return d->precisionPolicy;Executed by:
executed 2194 times by 10 tests: precisionPolicy;return d->precisionPolicy;Executed by:
executed 2194 times by 10 tests: return d->precisionPolicy;Executed by:
| 2194 | ||||||
| 319 | } | - | ||||||
| 320 | - | |||||||
| 321 | - | |||||||
| 322 | - | |||||||
| 323 | - | |||||||
| 324 | - | |||||||
| 325 | - | |||||||
| 326 | QSqlDriver::DbmsType QSqlDriver::dbmsType() const | - | ||||||
| 327 | { | - | ||||||
| 328 | returnconst QSqlDriverPrivate * const d = d_func()->(); | - | ||||||
| 329 | return executed 270 times by 8 tests: d->return d->dbmsType;Executed by:
executed 270 times by 8 tests: return d->dbmsType;Executed by:
executed 270 times by 8 tests: dbmsType;return d->dbmsType;Executed by:
executed 270 times by 8 tests: return d->dbmsType;Executed by:
| 270 | ||||||
| 330 | } | - | ||||||
| 331 | bool QSqlDriver::cancelQuery() | - | ||||||
| 332 | { | - | ||||||
| 333 | return false; | - | ||||||
| 334 | } | - | ||||||
| 335 | - | |||||||
| 336 | - | |||||||
| Switch to Source code | Preprocessed file |