* @brief Writes column definitions for the new session from the export schema. */
| 300 | * @brief Writes column definitions for the new session from the export schema. |
| 301 | */ |
| 302 | void Sessions::ExportWorker::writeColumnDefs(const DataModel::Frame& frame) |
| 303 | { |
| 304 | DataModel::Frame projectFrame; |
| 305 | bool haveProjectFrame = false; |
| 306 | { |
| 307 | QMutexLocker locker(m_projectSnapshotMutex); |
| 308 | if (!m_projectSnapshot->isEmpty()) { |
| 309 | const auto doc = QJsonDocument::fromJson(*m_projectSnapshot); |
| 310 | if (doc.isObject()) |
| 311 | haveProjectFrame = DataModel::read(projectFrame, doc.object()); |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | m_schema = haveProjectFrame ? DataModel::buildExportSchema(projectFrame) |
| 316 | : DataModel::buildExportSchema(frame); |
| 317 | |
| 318 | if (m_schema.columns.empty()) { |
| 319 | qWarning() << "[Sessions::Export] writeColumnDefs: schema has 0 columns --" |
| 320 | << "frame has" << frame.groups.size() << "groups, project snapshot " |
| 321 | << (haveProjectFrame ? "available" : "unavailable") |
| 322 | << ". Check that the project's groups are populated before export starts."; |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | if (!m_db) [[unlikely]] |
| 327 | return; |
| 328 | |
| 329 | QSqlQuery colQuery(*m_db); |
| 330 | if (!colQuery.prepare("INSERT INTO columns (session_id, unique_id, source_id, source_title, " |
| 331 | " group_title, title, units, widget, is_virtual) " |
| 332 | "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)")) { |
| 333 | qWarning() << "[Sessions::Export] columns INSERT prepare failed:" |
| 334 | << colQuery.lastError().text(); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | m_db->transaction(); |
| 339 | int written = 0; |
| 340 | for (const auto& col : m_schema.columns) { |
| 341 | colQuery.bindValue(0, m_sessionId); |
| 342 | colQuery.bindValue(1, col.uniqueId); |
| 343 | colQuery.bindValue(2, col.sourceId); |
| 344 | colQuery.bindValue(3, col.sourceTitle); |
| 345 | colQuery.bindValue(4, col.groupTitle); |
| 346 | colQuery.bindValue(5, col.title); |
| 347 | colQuery.bindValue(6, col.units); |
| 348 | colQuery.bindValue(7, col.widget); |
| 349 | colQuery.bindValue(8, col.isVirtual ? 1 : 0); |
| 350 | if (!colQuery.exec()) { |
| 351 | qWarning() << "[Sessions::Export] columns INSERT failed for uid" << col.uniqueId << ":" |
| 352 | << colQuery.lastError().text(); |
| 353 | continue; |
| 354 | } |
| 355 | ++written; |
| 356 | } |
| 357 | m_db->commit(); |
| 358 | |
| 359 | if (written == 0) { |