Skip to content

Commit

Permalink
Merge pull request #9 from dstrande/20240802-button-query
Browse files Browse the repository at this point in the history
Query button and custom text box for query
  • Loading branch information
dstrande authored Aug 2, 2024
2 parents c9017f1 + 27ddc76 commit ca3956d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
32 changes: 24 additions & 8 deletions src/sqlexplorer.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#include "sqlexplorer.h"
#include "./ui_sqlexplorer.h"
#include "sqlexplorer.h"
#include "credentials.h"

#include <QDebug>
#include <QLibraryInfo>
#include <pqxx/pqxx>
#include <iostream>
#include <QStandardItemModel>


sqlExplorer::sqlExplorer(QWidget *parent)
: QMainWindow(parent)
Expand All @@ -19,14 +21,21 @@ sqlExplorer::sqlExplorer(QWidget *parent)
// //Connect ui components to respective slots
// connect(ui->queryEditor,&QTextEdit::textChanged,
// this,&Widget::weight_changed);
connect(ui->queryButton,&QPushButton::pressed,
this,&sqlExplorer::queryCommand);

printf("pqxx VERSION: %s\n", PQXX_VERSION);
qDebug() << "Version:" << QLibraryInfo::version();

queryCommand();
}

void sqlExplorer::queryCommand()
// void sqlExplorer::newQuery() // std::string queryText
// {
// queryCommand(queryText);
// }

void sqlExplorer::queryCommand() // std::string queryText
{
try{
std::string combine = combinedCreds();
Expand All @@ -37,18 +46,25 @@ void sqlExplorer::queryCommand()
pqxx::work txn{dbConn};

// pqxx::result r = txn.exec("SELECT table_name FROM information_schema.tables WHERE table_type='BASE TABLE' AND table_schema='public';");
queryText = ui->queryEditor->toPlainText();
pqxx::result r = txn.exec(queryText.toStdString());

std::cout << r.columns() << "\n";
std::cout << r.size() << "\n";

std::cout << r[1][1] << "\n";
// std::cout << typeid(r.columns()).name() << "\n";

model = new QStandardItemModel(r.size(), r.columns());

for (auto row : r)
{
for (auto col : row)
{std::cout << col << "\n";}
for (int row = 0; row < model->rowCount(); ++row) {
for (int column = 0; column < model->columnCount(); ++column) {
QStandardItem *item = new QStandardItem(QString(r[row][column].c_str())); // "row %0, column %1").arg(row).arg(column
model->setItem(row, column, item);
}
}

ui->resultsView->setModel(model);
ui->resultsView->resizeColumnsToContents();


// std::cout << "Columns:\n";
// for (pqxx::row_size_type col = 0; col < r.columns(); ++col)
Expand Down
14 changes: 10 additions & 4 deletions src/sqlexplorer.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#ifndef SQLSEARCH_H
#define SQLSEARCH_H
#ifndef SQLEXPLORER_H
#define SQLEXPLORER_H

#include <QMainWindow>
#include "query.h"


class QStandardItemModel;

QT_BEGIN_NAMESPACE
namespace Ui {
class sqlExplorer;
Expand All @@ -20,9 +23,12 @@ class sqlExplorer : public QMainWindow

private:
Ui::sqlExplorer *ui;
QStandardItemModel *model;
QString queryText;

private slots:
void queryCommand();
void queryCommand(); // std::string queryText
// void newQuery(std::string queryText);
};
#endif // SQLSEARCH_H

#endif // SQLEXPLORER_H

0 comments on commit ca3956d

Please sign in to comment.