MCPcopy Create free account
hub / github.com/KDE/labplot / prepareAndExecute

Method prepareAndExecute

src/frontend/datasources/ImportSQLDatabaseWidget.cpp:520–607  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

518}
519
520bool ImportSQLDatabaseWidget::prepareAndExecute(QSqlQuery& q) {
521 // when reading from a table, the total number of rows to be read is determined below
522 // via a SELECT COUNT(*) statement and we don't need to navigate back and forth in the resultset.
523 // So, in this case we can use QSqlQuery::setForwardOnly() to reduce the memory consumption
524 const bool customQuery = (ui.cbImportFrom->currentIndex() != 0);
525 if (!customQuery)
526 q.setForwardOnly(true);
527
528 WAIT_CURSOR;
529 q.prepare(currentQuery());
530 if (!q.exec() || !q.isActive()) {
531 RESET_CURSOR;
532 if (!q.lastError().databaseText().isEmpty())
533 Q_EMIT error(i18n("Failed to execute the query") + QStringLiteral(" \n") + q.lastError().databaseText());
534 else
535 Q_EMIT error(i18n("Failed to execute the query"));
536
537 setInvalid();
538 return false;
539 }
540
541 // determine the number of rows and columns to read
542 m_cols = q.record().count(); // total number of columns
543 m_actualCols = m_cols; // actual number of columns in the resultset to be read
544 m_actualRows = 0; // actual number of rows in the resultset to be read
545 m_startCol = 0;
546 m_endCol = m_cols - 1;
547 m_startRow = 0;
548 m_endRow = 0;
549 if (!customQuery) {
550 // determine the total number of records in the table
551 const QString& tableName = ui.lwTables->currentItem()->text();
552 QSqlQuery countQuery(QStringLiteral("SELECT COUNT(*) FROM ") + tableName);
553 while (countQuery.next())
554 m_actualRows = countQuery.value(0).toInt();
555
556 // columns to read
557 m_startCol = ui.sbStartColumn->value() - 1;
558 if (ui.sbEndColumn->value() != -1) {
559 m_endCol = ui.sbEndColumn->value() - 1;
560 if (m_endCol >= m_cols)
561 m_endCol = m_cols - 1;
562 }
563
564 m_actualCols = m_endCol - m_startCol + 1;
565
566 // determine the names and modes for columns to be read
567 if (m_startCol != 0 || m_endCol != m_cols - 1) {
568 for (int col = m_startCol; col <= m_endCol; ++col) {
569 m_actualColumnModes << m_columnModes.at(col);
570 m_actualColumnNames << m_columnNames.at(col);
571 }
572 } else {
573 // all columns are read
574 m_actualColumnModes = m_columnModes;
575 m_actualColumnNames = m_columnNames;
576 }
577

Callers

nothing calls this directly

Calls 10

execMethod · 0.80
isActiveMethod · 0.80
lastErrorMethod · 0.80
currentItemMethod · 0.80
nextMethod · 0.80
currentIndexMethod · 0.45
isEmptyMethod · 0.45
countMethod · 0.45
textMethod · 0.45
valueMethod · 0.45

Tested by

no test coverage detected