mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 02:50:46 -06:00
SqlExecutionArea: Support exporting result to CSV file
Add a button which allows the user to save the query results to a CSV file. Add some basic preparations for saving the result as a view.
This commit is contained in:
@@ -8,18 +8,27 @@
|
||||
#include "PreferencesDialog.h"
|
||||
#include "sqlitetablemodel.h"
|
||||
|
||||
ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent)
|
||||
ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, QWidget* parent, const QString& query)
|
||||
: QDialog(parent),
|
||||
ui(new Ui::ExportCsvDialog),
|
||||
pdb(db)
|
||||
pdb(db),
|
||||
m_sQuery(query)
|
||||
{
|
||||
// Create UI
|
||||
ui->setupUi(this);
|
||||
|
||||
// Get list of tables to export
|
||||
objectMap objects = pdb->getBrowsableObjects();
|
||||
for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i)
|
||||
ui->comboTable->addItem(QIcon(QString(":icons/%1").arg(i.value().gettype())), i.value().getname());
|
||||
// If a SQL query was specified hide the table combo box. If not fill it with tables to export
|
||||
if(query.isEmpty())
|
||||
{
|
||||
// Get list of tables to export
|
||||
objectMap objects = pdb->getBrowsableObjects();
|
||||
for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i)
|
||||
ui->comboTable->addItem(QIcon(QString(":icons/%1").arg(i.value().gettype())), i.value().getname());
|
||||
} else {
|
||||
// Hide table combo box
|
||||
ui->labelTable->setVisible(false);
|
||||
ui->comboTable->setVisible(false);
|
||||
}
|
||||
}
|
||||
|
||||
ExportCsvDialog::~ExportCsvDialog()
|
||||
@@ -39,9 +48,18 @@ void ExportCsvDialog::accept()
|
||||
// Only if the user hasn't clicked the cancel button
|
||||
if(fileName.size() > 0)
|
||||
{
|
||||
unsigned int first_column;
|
||||
|
||||
// Get data from selected table
|
||||
SqliteTableModel tableModel(this, pdb);
|
||||
tableModel.setTable(ui->comboTable->currentText());
|
||||
if(m_sQuery.isEmpty())
|
||||
{
|
||||
tableModel.setTable(ui->comboTable->currentText());
|
||||
first_column = 1;
|
||||
} else {
|
||||
tableModel.setQuery(m_sQuery);
|
||||
first_column = 0;
|
||||
}
|
||||
while(tableModel.canFetchMore())
|
||||
tableModel.fetchMore();
|
||||
|
||||
@@ -62,7 +80,7 @@ void ExportCsvDialog::accept()
|
||||
// Put field names in first row if user wants to have them
|
||||
if(ui->checkHeader->isChecked())
|
||||
{
|
||||
for(int i=1;i<tableModel.columnCount();i++)
|
||||
for(int i=first_column;i<tableModel.columnCount();i++)
|
||||
{
|
||||
stream << quoteChar << tableModel.headerData(i, Qt::Horizontal).toString() << quoteChar;
|
||||
if(i < tableModel.columnCount() - 1)
|
||||
@@ -75,7 +93,7 @@ void ExportCsvDialog::accept()
|
||||
// Get and write actual data
|
||||
for(int i=0;i<tableModel.totalRowCount();i++)
|
||||
{
|
||||
for(int j=1;j<tableModel.columnCount();j++)
|
||||
for(int j=first_column;j<tableModel.columnCount();j++)
|
||||
{
|
||||
QString content = tableModel.data(tableModel.index(i, j)).toString();
|
||||
stream << quoteChar << content.replace(quoteChar, quotequoteChar) << quoteChar;
|
||||
|
||||
@@ -13,7 +13,7 @@ class ExportCsvDialog : public QDialog
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit ExportCsvDialog(DBBrowserDB* db, QWidget* parent = 0);
|
||||
explicit ExportCsvDialog(DBBrowserDB* db, QWidget* parent = 0, const QString& query = "");
|
||||
~ExportCsvDialog();
|
||||
|
||||
private slots:
|
||||
@@ -22,6 +22,8 @@ private slots:
|
||||
private:
|
||||
Ui::ExportCsvDialog* ui;
|
||||
DBBrowserDB* pdb;
|
||||
|
||||
QString m_sQuery;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Export table to CSV</string>
|
||||
<string>Export data as CSV</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
|
||||
@@ -675,6 +675,7 @@ void MainWindow::executeQuery()
|
||||
{
|
||||
statusMessage = tr("%1 Rows returned from: %2").arg(
|
||||
sqlWidget->getModel()->totalRowCount()).arg(queryPart);
|
||||
sqlWidget->enableSaveButton(true);
|
||||
sql3status = SQLITE_OK;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -6,9 +6,12 @@
|
||||
#include "sqlitetablemodel.h"
|
||||
#include "sqlitedb.h"
|
||||
#include "PreferencesDialog.h"
|
||||
#include "ExportCsvDialog.h"
|
||||
#include <QMenu>
|
||||
|
||||
SqlExecutionArea::SqlExecutionArea(QWidget* parent, DBBrowserDB* db) :
|
||||
SqlExecutionArea::SqlExecutionArea(QWidget* parent, DBBrowserDB* _db) :
|
||||
QWidget(parent),
|
||||
db(_db),
|
||||
ui(new Ui::SqlExecutionArea)
|
||||
{
|
||||
// Create UI
|
||||
@@ -20,6 +23,12 @@ SqlExecutionArea::SqlExecutionArea(QWidget* parent, DBBrowserDB* db) :
|
||||
// Create model
|
||||
model = new SqliteTableModel(this, db, PreferencesDialog::getSettingsValue("db", "prefetchsize").toInt());
|
||||
ui->tableResult->setModel(model);
|
||||
|
||||
// Create popup menu for save button
|
||||
menuPopupSave = new QMenu(this);
|
||||
menuPopupSave->addAction(ui->actionExportCsv);
|
||||
//menuPopupSave->addAction(ui->actionSaveAsView);
|
||||
ui->buttonSave->setMenu(menuPopupSave);
|
||||
}
|
||||
|
||||
SqlExecutionArea::~SqlExecutionArea()
|
||||
@@ -52,3 +61,19 @@ SqlTextEdit* SqlExecutionArea::getEditor()
|
||||
{
|
||||
return ui->editEditor;
|
||||
}
|
||||
|
||||
void SqlExecutionArea::enableSaveButton(bool enable)
|
||||
{
|
||||
ui->buttonSave->setEnabled(enable);
|
||||
}
|
||||
|
||||
void SqlExecutionArea::saveAsCsv()
|
||||
{
|
||||
ExportCsvDialog dialog(db, this, model->query());
|
||||
dialog.exec();
|
||||
}
|
||||
|
||||
void SqlExecutionArea::saveAsView()
|
||||
{
|
||||
// TODO
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
class SQLiteSyntaxHighlighter;
|
||||
class SqliteTableModel;
|
||||
class DBBrowserDB;
|
||||
class QMenu;
|
||||
|
||||
namespace Ui {
|
||||
class SqlExecutionArea;
|
||||
@@ -16,7 +17,7 @@ class SqlExecutionArea : public QWidget
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit SqlExecutionArea(QWidget* parent = 0, DBBrowserDB* db = 0);
|
||||
explicit SqlExecutionArea(QWidget* parent = 0, DBBrowserDB* _db = 0);
|
||||
~SqlExecutionArea();
|
||||
|
||||
QString getSql() const;
|
||||
@@ -28,11 +29,16 @@ public:
|
||||
public slots:
|
||||
virtual void setTableNames(const QStringList& tables);
|
||||
virtual void finishExecution(const QString& result);
|
||||
virtual void enableSaveButton(bool enable);
|
||||
virtual void saveAsCsv();
|
||||
virtual void saveAsView();
|
||||
|
||||
private:
|
||||
DBBrowserDB* db;
|
||||
Ui::SqlExecutionArea* ui;
|
||||
SQLiteSyntaxHighlighter* highlighter;
|
||||
SqliteTableModel* model;
|
||||
QMenu* menuPopupSave;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>434</width>
|
||||
<height>451</height>
|
||||
<width>367</width>
|
||||
<height>432</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
@@ -35,13 +35,44 @@
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="labelErrors">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="labelErrors">
|
||||
<property name="text">
|
||||
<string/>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QToolButton" name="buttonSave">
|
||||
<property name="enabled">
|
||||
<bool>false</bool>
|
||||
</property>
|
||||
<property name="icon">
|
||||
<iconset resource="icons/icons.qrc">
|
||||
<normaloff>:/icons/save_table</normaloff>:/icons/save_table</iconset>
|
||||
</property>
|
||||
<property name="popupMode">
|
||||
<enum>QToolButton::InstantPopup</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</item>
|
||||
</layout>
|
||||
<action name="actionExportCsv">
|
||||
<property name="text">
|
||||
<string>Export to &CSV</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionSaveAsView">
|
||||
<property name="text">
|
||||
<string>Save as &view</string>
|
||||
</property>
|
||||
<property name="toolTip">
|
||||
<string>Save as view</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
@@ -55,6 +86,45 @@
|
||||
<header>ExtendedTableWidget.h</header>
|
||||
</customwidget>
|
||||
</customwidgets>
|
||||
<resources/>
|
||||
<connections/>
|
||||
<resources>
|
||||
<include location="icons/icons.qrc"/>
|
||||
</resources>
|
||||
<connections>
|
||||
<connection>
|
||||
<sender>actionExportCsv</sender>
|
||||
<signal>activated()</signal>
|
||||
<receiver>SqlExecutionArea</receiver>
|
||||
<slot>saveAsCsv()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>-1</x>
|
||||
<y>-1</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>183</x>
|
||||
<y>215</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
<connection>
|
||||
<sender>actionSaveAsView</sender>
|
||||
<signal>activated()</signal>
|
||||
<receiver>SqlExecutionArea</receiver>
|
||||
<slot>saveAsView()</slot>
|
||||
<hints>
|
||||
<hint type="sourcelabel">
|
||||
<x>-1</x>
|
||||
<y>-1</y>
|
||||
</hint>
|
||||
<hint type="destinationlabel">
|
||||
<x>183</x>
|
||||
<y>215</y>
|
||||
</hint>
|
||||
</hints>
|
||||
</connection>
|
||||
</connections>
|
||||
<slots>
|
||||
<slot>saveAsCsv()</slot>
|
||||
<slot>saveAsView()</slot>
|
||||
</slots>
|
||||
</ui>
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
<file alias="open_sql">page_white_database.png</file>
|
||||
<file alias="load_extension">plugin_add.png</file>
|
||||
<file alias="remove_extension">plugin_delete.png</file>
|
||||
<file alias="save_table">table_save.png</file>
|
||||
</qresource>
|
||||
<qresource prefix="/oldimages">
|
||||
<file alias="128">oldimages/128.png</file>
|
||||
|
||||
BIN
src/icons/table_save.png
Normal file
BIN
src/icons/table_save.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 723 B |
@@ -26,6 +26,7 @@ public:
|
||||
bool removeRows(int row, int count, const QModelIndex& parent = QModelIndex());
|
||||
|
||||
void setQuery(const QString& sQuery, bool dontClearHeaders = false);
|
||||
QString query() const { return m_sQuery; }
|
||||
void setTable(const QString& table);
|
||||
void setChunkSize(size_t chunksize);
|
||||
void sort(int column, Qt::SortOrder order = Qt::AscendingOrder);
|
||||
|
||||
Reference in New Issue
Block a user