| 367 | } |
| 368 | |
| 369 | bool DBManager::createTable(QStringList& columns) const |
| 370 | { |
| 371 | if (_readonlyMode) |
| 372 | { |
| 373 | return false; |
| 374 | } |
| 375 | |
| 376 | if (columns.isEmpty()) |
| 377 | { |
| 378 | Error(_log, "Empty tables aren't supported!"); |
| 379 | return false; |
| 380 | } |
| 381 | |
| 382 | SqlDatabase* idb = getDB(); |
| 383 | // create table if required |
| 384 | SqlQuery query(idb); |
| 385 | if (!tableExists(_table)) |
| 386 | { |
| 387 | // empty tables aren't supported by sqlite, add one column |
| 388 | QString tcolumn = columns.takeFirst(); |
| 389 | // default CURRENT_TIMESTAMP is not supported by ALTER TABLE |
| 390 | if (!query.exec(QString("CREATE TABLE %1 ( %2 )").arg(_table, tcolumn))) |
| 391 | { |
| 392 | Error(_log, "Failed to create table: '%s' Error: %s", QSTRING_CSTR(_table), QSTRING_CSTR(idb->error())); |
| 393 | return false; |
| 394 | } |
| 395 | } |
| 396 | // create columns if required |
| 397 | int err = 0; |
| 398 | for (const auto& column : columns) |
| 399 | { |
| 400 | QString columnName = column.split(' ').at(0); |
| 401 | if (!idb->doesColumnExist(_table, columnName)) |
| 402 | { |
| 403 | if (!createColumn(column)) |
| 404 | { |
| 405 | err++; |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | if (err) |
| 410 | return false; |
| 411 | |
| 412 | return true; |
| 413 | } |
| 414 | |
| 415 | bool DBManager::createColumn(const QString& column) const |
| 416 | { |
nothing calls this directly
no test coverage detected