Mogelijkheid om query to kopieren als raw c++ string.

This commit is contained in:
Eelke Klein 2017-10-05 16:02:06 +02:00
parent a9534d543e
commit 33cf39b799
10 changed files with 70 additions and 18 deletions

View file

@ -68,6 +68,8 @@ set_property(TARGET gtest PROPERTY IMPORTED_LOCATION ${GTEST_LIBRARIES})
find_package(Threads) find_package(Threads)
add_definitions(-DQT_USE_QSTRINGBUILDER)
enable_testing() enable_testing()
add_subdirectory(core) add_subdirectory(core)

View file

@ -78,6 +78,7 @@ void ASyncDBConnection::async_connect_handler(boost::system::error_code ec, std:
} }
} }
void ASyncDBConnection::doStateCallback(State state) void ASyncDBConnection::doStateCallback(State state)
{ {
m_state = state; m_state = state;
@ -87,9 +88,7 @@ void ASyncDBConnection::doStateCallback(State state)
[this](const PGresult *result) { processNotice(result); }); [this](const PGresult *result) { processNotice(result); });
} }
emit onStateChanged(state); emit onStateChanged(state);
} }
void ASyncDBConnection::closeConnection() void ASyncDBConnection::closeConnection()

View file

@ -10,6 +10,7 @@ add_executable(pglab
ASyncWindow.cpp ASyncWindow.cpp
BackupDialog.cpp BackupDialog.cpp
BackupRestore.cpp BackupRestore.cpp
CodeBuilderConfiguration.cpp
ConnectionConfig.cpp ConnectionConfig.cpp
ConnectionList.cpp ConnectionList.cpp
ConnectionListModel.cpp ConnectionListModel.cpp

View file

@ -253,7 +253,6 @@ void MainWindow::on_actionCopy_triggered()
} }
void MainWindow::on_actionCopy_as_C_string_triggered() void MainWindow::on_actionCopy_as_C_string_triggered()
{ {
// Find which edit is active, copy the selected text or all text if no selection present // Find which edit is active, copy the selected text or all text if no selection present
@ -264,3 +263,12 @@ void MainWindow::on_actionCopy_as_C_string_triggered()
tab->copyQueryAsCString(); tab->copyQueryAsCString();
} }
} }
void MainWindow::on_actionCopy_as_raw_Cpp_string_triggered()
{
QueryTab *tab = GetActiveQueryTab();
if (tab) {
tab->copyQueryAsRawCppString();
}
}

View file

@ -82,6 +82,7 @@ private slots:
void on_actionShow_connection_manager_triggered(); void on_actionShow_connection_manager_triggered();
void on_actionCopy_triggered(); void on_actionCopy_triggered();
void on_actionCopy_as_C_string_triggered(); void on_actionCopy_as_C_string_triggered();
void on_actionCopy_as_raw_Cpp_string_triggered();
}; };
#endif // MAINWINDOW_H #endif // MAINWINDOW_H

View file

@ -45,7 +45,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>993</width> <width>993</width>
<height>24</height> <height>22</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuTest"> <widget class="QMenu" name="menuTest">
@ -89,6 +89,7 @@
</property> </property>
<addaction name="actionCopy"/> <addaction name="actionCopy"/>
<addaction name="actionCopy_as_C_string"/> <addaction name="actionCopy_as_C_string"/>
<addaction name="actionCopy_as_raw_Cpp_string"/>
</widget> </widget>
<addaction name="menuTest"/> <addaction name="menuTest"/>
<addaction name="menuEdit"/> <addaction name="menuEdit"/>
@ -287,6 +288,19 @@
<string>Ctrl+Alt+C</string> <string>Ctrl+Alt+C</string>
</property> </property>
</action> </action>
<action name="actionCopy_as_raw_Cpp_string">
<property name="icon">
<iconset>
<normalon>:/icons/token_shortland_character.png</normalon>
</iconset>
</property>
<property name="text">
<string>Copy as raw C++-string</string>
</property>
<property name="shortcut">
<string>Ctrl+Alt+C</string>
</property>
</action>
</widget> </widget>
<layoutdefault spacing="6" margin="11"/> <layoutdefault spacing="6" margin="11"/>
<resources> <resources>

View file

@ -200,7 +200,7 @@ void QueryTab::execute()
clearResult(); clearResult();
ui->messagesEdit->clear(); ui->messagesEdit->clear();
std::string cmd = getCommand(); std::string cmd = getCommandUtf8();
m_stopwatch.start(); m_stopwatch.start();
if (m_queryParamListController->empty()) if (m_queryParamListController->empty())
@ -232,7 +232,7 @@ void QueryTab::explain(bool analyze)
analyze_str = "ANALYZE, "; analyze_str = "ANALYZE, ";
} }
m_stopwatch.start(); m_stopwatch.start();
std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, BUFFERS, FORMAT JSON) " + getCommand(); std::string cmd = "EXPLAIN (" + analyze_str + "VERBOSE, BUFFERS, FORMAT JSON) " + getCommandUtf8();
m_dbConnection.send(cmd, m_dbConnection.send(cmd,
[this](Expected<std::shared_ptr<Pgsql::Result>> exp_res, qint64 ) [this](Expected<std::shared_ptr<Pgsql::Result>> exp_res, qint64 )
{ {
@ -449,7 +449,7 @@ void QueryTab::explain_ready(ExplainRoot::SPtr explain)
} }
} }
std::string QueryTab::getCommand() const QString QueryTab::getCommand() const
{ {
QString command; QString command;
QTextCursor cursor = ui->queryEdit->textCursor(); QTextCursor cursor = ui->queryEdit->textCursor();
@ -459,7 +459,12 @@ std::string QueryTab::getCommand() const
else { else {
command = ui->queryEdit->toPlainText(); command = ui->queryEdit->toPlainText();
} }
return command.toUtf8().data(); return command;
}
std::string QueryTab::getCommandUtf8() const
{
return getCommand().toUtf8().data();
} }
QTabWidget *QueryTab::getTabWidget() QTabWidget *QueryTab::getTabWidget()
@ -571,18 +576,27 @@ void QueryTab::clearResult()
void QueryTab::copyQueryAsCString() void QueryTab::copyQueryAsCString()
{ {
QString command; // QString command;
QTextCursor cursor = ui->queryEdit->textCursor(); // QTextCursor cursor = ui->queryEdit->textCursor();
if (cursor.hasSelection()) { // if (cursor.hasSelection()) {
command = cursor.selection().toPlainText(); // command = cursor.selection().toPlainText();
} // }
else { // else {
command = ui->queryEdit->toPlainText(); // command = ui->queryEdit->toPlainText();
} // }
QString command = getCommand();
QString cs = ConvertToMultiLineCString(command); QString cs = ConvertToMultiLineCString(command);
QApplication::clipboard()->setText(cs); QApplication::clipboard()->setText(cs);
} }
void QueryTab::copyQueryAsRawCppString()
{
//auto sql = getAllOrSelectedSql();
QString command = getCommand();
QString cs = ConvertToMultiLineRawCppString(command);
QApplication::clipboard()->setText(cs);
}
void QueryTab::exportData(const QString &file_name) void QueryTab::exportData(const QString &file_name)
{ {
auto widget = ui->tabWidget->currentWidget(); auto widget = ui->tabWidget->currentWidget();

View file

@ -67,6 +67,7 @@ public:
bool canClose(); bool canClose();
void copyQueryAsCString(); void copyQueryAsCString();
void copyQueryAsRawCppString();
void exportData(const QString &filename); void exportData(const QString &filename);
QString fileName() const { return m_fileName; } QString fileName() const { return m_fileName; }
@ -103,7 +104,8 @@ private:
void addLog(QString s); void addLog(QString s);
std::string getCommand() const; QString getCommand() const;
std::string getCommandUtf8() const;
void explain_ready(ExplainRoot::SPtr explain); void explain_ready(ExplainRoot::SPtr explain);
void query_ready(Expected<std::shared_ptr<Pgsql::Result>> dbres, qint64 elapsedms); void query_ready(Expected<std::shared_ptr<Pgsql::Result>> dbres, qint64 elapsedms);

View file

@ -136,3 +136,13 @@ QString ConvertToMultiLineCString(const QString &in)
} }
QString ConvertToMultiLineRawCppString(const QString &in)
{
const QString delim = "__SQL__";
QString out;
out.append("R\"" + delim + "(\n");
out.append(in.trimmed());
out.append("\n)" + delim + "\"");
return out;
}

View file

@ -8,6 +8,7 @@
QString msfloatToHumanReadableString(float ms); QString msfloatToHumanReadableString(float ms);
void copySelectionToClipboard(const QTableView *view); void copySelectionToClipboard(const QTableView *view);
QString ConvertToMultiLineCString(const QString &in); QString ConvertToMultiLineCString(const QString &in);
QString ConvertToMultiLineRawCppString(const QString &in);
void exportTable(const QTableView *view, QTextStream &out); void exportTable(const QTableView *view, QTextStream &out);
inline QString stdStrToQ(const std::string &s) inline QString stdStrToQ(const std::string &s)