File indexing completed on 2025-11-09 03:43:51

0001 /*
0002     File                 : ImportSQLDatabaseWidget.cpp
0003     Project              : LabPlot
0004     Description          : widget for the import from SQL databases
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2016-2023 Alexander Semke <alexander.semke@web.de>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef IMPORTSQLDATABASEWIDGET_H
0012 #define IMPORTSQLDATABASEWIDGET_H
0013 
0014 #include "backend/core/AbstractColumn.h"
0015 #include "backend/datasources/filters/AbstractFileFilter.h"
0016 #include "ui_importsqldatabasewidget.h"
0017 #include <QSqlDatabase>
0018 
0019 #ifdef HAVE_KF5_SYNTAX_HIGHLIGHTING
0020 #include <KSyntaxHighlighting/repository.h>
0021 namespace KSyntaxHighlighting {
0022 class SyntaxHighlighter;
0023 }
0024 #endif
0025 
0026 class QStandardItemModel;
0027 
0028 class ImportSQLDatabaseWidget : public QWidget {
0029     Q_OBJECT
0030 
0031 public:
0032     explicit ImportSQLDatabaseWidget(QWidget* parent = nullptr);
0033     ~ImportSQLDatabaseWidget() override;
0034 
0035     void read(AbstractDataSource*, AbstractFileFilter::ImportMode importMode = AbstractFileFilter::ImportMode::Replace);
0036     QString selectedTable() const;
0037     bool isValid() const;
0038     bool isNumericData() const;
0039 
0040 private:
0041     Ui::ImportSQLDatabaseWidget ui;
0042     QList<QString> m_vendorList;
0043     QList<QString> m_tableNamesList;
0044 
0045     QStringList m_columnNames; // names for all columns in the table or query resultset
0046     QVector<AbstractColumn::ColumnMode> m_columnModes; // modes for all columns in the table or query resultset
0047     QVector<AbstractColumn::ColumnMode> m_actualColumnModes; // names for the actual columns to be imported
0048     QStringList m_actualColumnNames; // names for the actual columns to be imported
0049 
0050     int m_cols{0}; // total number of columns in the table or in the query resultset
0051     int m_startCol{0};
0052     int m_endCol{0};
0053     int m_startRow{0};
0054     int m_endRow{0};
0055     int m_actualRows{0}; // actual number of rows in the resultset to be read
0056     int m_actualCols{0}; // actual number of columns in the resultset to be read
0057 
0058     QSqlDatabase m_db;
0059     QStandardItemModel* m_databaseTreeModel{nullptr};
0060     QString m_configPath;
0061     bool m_initializing{false};
0062     bool m_valid{false};
0063     bool m_numeric{false};
0064 #ifdef HAVE_KF5_SYNTAX_HIGHLIGHTING
0065     KSyntaxHighlighting::SyntaxHighlighter* m_highlighter;
0066     KSyntaxHighlighting::Repository m_repository;
0067 #endif
0068 
0069     bool prepareAndExecute(QSqlQuery&);
0070     void setValue(int col, int row, QStringView value);
0071     void readConnections();
0072     QString currentQuery(bool preview = false);
0073     void setInvalid();
0074     void setValid();
0075 
0076     // helper functions for unit tests
0077     friend class ImportSqlDatabaseTest;
0078     void setCustomQuery(bool);
0079     void setStartRow(int);
0080     void setEndRow(int);
0081     void setStartColumn(int);
0082     void setEndColumn(int);
0083     void setQuery(const QString&);
0084 
0085 public Q_SLOTS:
0086     void loadSettings();
0087 
0088 private Q_SLOTS:
0089     void showDatabaseManager();
0090     void connectionChanged();
0091     void importFromChanged(int);
0092     void refreshPreview();
0093 
0094 Q_SIGNALS:
0095     void completed(int);
0096     void stateChanged();
0097     void error(const QString&);
0098 };
0099 
0100 #endif // IMPORTSQLDATABASEWIDGET_H