Big cleanup
This commit is contained in:
parent
d3080a08bb
commit
8b671090a0
55 changed files with 214 additions and 3967 deletions
|
|
@ -41,26 +41,16 @@ namespace {
|
|||
class ASyncDBConnectionThread {
|
||||
public:
|
||||
using t_CommandQueue = std::queue<Command>;
|
||||
// struct {
|
||||
// std::mutex m_mutex;
|
||||
// on_state_callback m_func;
|
||||
// } m_stateCallback;
|
||||
// struct {
|
||||
// std::mutex m_mutex;
|
||||
// on_notice_callback m_func;
|
||||
// } m_noticeCallback;
|
||||
|
||||
struct t_Command {
|
||||
std::mutex m_mutex;
|
||||
t_CommandQueue m_queue;
|
||||
//std::condition_variable m_newEvent;
|
||||
Win32Event m_newEvent;
|
||||
t_Command()
|
||||
: m_newEvent(Win32Event::Reset::Auto, Win32Event::Initial::Clear)
|
||||
{}
|
||||
} m_commandQueue;
|
||||
|
||||
// std::string m_initString;
|
||||
ConnectionConfig m_config;
|
||||
ASyncDBConnection::State m_state = ASyncDBConnection::State::NotConnected;
|
||||
|
||||
|
|
@ -274,20 +264,7 @@ void ASyncDBConnectionThread::doStateCallback(ASyncDBConnection::State state)
|
|||
|
||||
void ASyncDBConnectionThread::waitForAndSendCommand()
|
||||
{
|
||||
#if false
|
||||
using namespace std::chrono_literals;
|
||||
// lock the data
|
||||
std::unique_lock<std::mutex> lk(m_commandQueue.m_mutex);
|
||||
if (m_commandQueue.m_queue.empty()) {
|
||||
// no data wait till there is data
|
||||
m_commandQueue.m_newEvent.wait_for(lk, 1000ms);
|
||||
// can we use the predicate to reimplement the stop function???, []{return ready;});
|
||||
|
||||
}
|
||||
doNewCommand();
|
||||
|
||||
#else
|
||||
WaitHandleList whl;
|
||||
WaitHandleList whl;
|
||||
auto wait_result_new_command = whl.add(m_commandQueue.m_newEvent);
|
||||
auto wait_result_stop = whl.add(m_stopEvent);
|
||||
|
||||
|
|
@ -301,9 +278,8 @@ void ASyncDBConnectionThread::waitForAndSendCommand()
|
|||
if (res == wait_result_new_command) {
|
||||
doNewCommand();
|
||||
}
|
||||
if (res == wait_result_stop)
|
||||
return;
|
||||
#endif
|
||||
// Note if it was stop we can just return and function
|
||||
// above will stop looping because terminateRequested has been set too by stop
|
||||
}
|
||||
|
||||
void ASyncDBConnectionThread::doNewCommand()
|
||||
|
|
@ -365,30 +341,6 @@ bool ASyncDBConnectionThread::consumeResultInput()
|
|||
|
||||
void ASyncDBConnectionThread::waitForResult()
|
||||
{
|
||||
#if false
|
||||
int sock = m_connection.socket();
|
||||
|
||||
fd_set readfds;
|
||||
timeval timeout;
|
||||
bool finished = false;
|
||||
while (!finished && !terminateRequested) {
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(sock, &readfds);
|
||||
|
||||
timeout.tv_sec = 5;
|
||||
timeout.tv_usec = 0;
|
||||
|
||||
int select_result = select(sock + 1, &readfds, nullptr, nullptr, &timeout);
|
||||
if (select_result > 0) {
|
||||
if (FD_ISSET(sock, &readfds)) {
|
||||
if (consumeResultInput()) {
|
||||
finished = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
#else
|
||||
SOCKET sock = static_cast<SOCKET>(m_connection.socket());
|
||||
Win32Event socket_event(Win32Event::Reset::Manual, Win32Event::Initial::Clear);
|
||||
|
||||
|
|
@ -426,18 +378,10 @@ void ASyncDBConnectionThread::waitForResult()
|
|||
finished = true;
|
||||
}
|
||||
} // end while
|
||||
// When last result received, remove command from queue
|
||||
#endif
|
||||
}
|
||||
|
||||
void ASyncDBConnectionThread::processNotice(const PGresult *result)
|
||||
{
|
||||
// Pgsql::Result res(result);
|
||||
// std::lock_guard<std::mutex> lg(m_noticeCallback.m_mutex);
|
||||
// if (m_noticeCallback.m_func) {
|
||||
// Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||
// m_noticeCallback.m_func(details);
|
||||
// }
|
||||
Pgsql::ErrorDetails details = Pgsql::ErrorDetails::createErrorDetailsFromPGresult(result);
|
||||
Q_EMIT asyncConnObject->onNotice(details);
|
||||
}
|
||||
|
|
@ -486,13 +430,10 @@ void ASyncDBConnection::doStateCallback(State state)
|
|||
|
||||
void ASyncDBConnection::closeConnection()
|
||||
{
|
||||
// doStateCallback(State::NotConnected);
|
||||
// TODO also send cancel???
|
||||
m_threadData->stop();
|
||||
if (m_thread.joinable()) {
|
||||
m_thread.join();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool ASyncDBConnection::send(const std::string &command, on_result_callback on_result)
|
||||
|
|
|
|||
|
|
@ -48,28 +48,11 @@ public:
|
|||
bool send(const std::string &command, on_result_callback on_result);
|
||||
bool send(const std::string &command, Pgsql::Params params, on_result_callback on_result);
|
||||
|
||||
/** This version of send uses the signal onQueryResult and onQueryError to report back
|
||||
* the completion of the query.
|
||||
*/
|
||||
// bool send(const std::string &command, Pgsql::Params params = Pgsql::Params())
|
||||
// {
|
||||
// return send(command, params, [this] (Expected<std::shared_ptr<Pgsql::Result>> res, qint64) {
|
||||
// if (res.valid()) {
|
||||
// emit onQueryResult(res.get());
|
||||
// }
|
||||
// else {
|
||||
// emit onQueryError();
|
||||
// }
|
||||
// });
|
||||
// }
|
||||
|
||||
bool cancel();
|
||||
|
||||
Q_SIGNALS:
|
||||
void onStateChanged(ASyncDBConnection::State state);
|
||||
void onNotice(Pgsql::ErrorDetails notice);
|
||||
// void onQueryResult(std::shared_ptr<Pgsql::Result> result);
|
||||
// void onQueryError();
|
||||
|
||||
private:
|
||||
Pgsql::Connection m_connection;
|
||||
|
|
|
|||
|
|
@ -28,23 +28,21 @@ namespace {
|
|||
|
||||
QString SslModeToString(SslMode sm)
|
||||
{
|
||||
for (auto e : SslModeStringTable) {
|
||||
if (e.mode == sm) {
|
||||
for (auto e : SslModeStringTable)
|
||||
if (e.mode == sm)
|
||||
return QString::fromUtf8(e.string);
|
||||
}
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
SslMode StringToSslMode(QString s)
|
||||
{
|
||||
SslMode result = SslMode::allow;
|
||||
for (auto e : SslModeStringTable) {
|
||||
if (e.string == s) {
|
||||
for (auto e : SslModeStringTable)
|
||||
if (e.string == s)
|
||||
result = e.mode;
|
||||
}
|
||||
}
|
||||
return {};
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ConnectionConfig::ConnectionConfig()
|
||||
|
|
@ -64,7 +62,8 @@ void ConnectionConfig::setParent(ConnectionGroup *grp)
|
|||
|
||||
void ConnectionConfig::setUuid(const QUuid &uuid)
|
||||
{
|
||||
if (uuid != m_uuid) {
|
||||
if (uuid != m_uuid)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_uuid = uuid;
|
||||
}
|
||||
|
|
@ -79,7 +78,8 @@ const QUuid &ConnectionConfig::uuid() const
|
|||
|
||||
void ConnectionConfig::setName(const QString& desc)
|
||||
{
|
||||
if (m_name != desc) {
|
||||
if (m_name != desc)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_name = std::move(desc);
|
||||
}
|
||||
|
|
@ -92,7 +92,8 @@ const QString& ConnectionConfig::name() const
|
|||
|
||||
void ConnectionConfig::setHost(const QString& host)
|
||||
{
|
||||
if (m_host != host) {
|
||||
if (m_host != host)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_host = std::move(host);
|
||||
}
|
||||
|
|
@ -105,7 +106,8 @@ const QString& ConnectionConfig::host() const
|
|||
|
||||
void ConnectionConfig::setHostAddr(const QString &v)
|
||||
{
|
||||
if (m_hostaddr != v) {
|
||||
if (m_hostaddr != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_hostaddr = std::move(v);
|
||||
}
|
||||
|
|
@ -118,7 +120,8 @@ const QString& ConnectionConfig::hostAddr() const
|
|||
|
||||
void ConnectionConfig::setPort(unsigned short port)
|
||||
{
|
||||
if (m_port != port) {
|
||||
if (m_port != port)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_port = port;
|
||||
}
|
||||
|
|
@ -131,7 +134,8 @@ unsigned short ConnectionConfig::port() const
|
|||
|
||||
void ConnectionConfig::setUser(const QString& v)
|
||||
{
|
||||
if (m_user != v) {
|
||||
if (m_user != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_user = v;
|
||||
}
|
||||
|
|
@ -145,7 +149,8 @@ const QString& ConnectionConfig::user() const
|
|||
|
||||
void ConnectionConfig::setPassword(const QString& v)
|
||||
{
|
||||
if (m_password != v) {
|
||||
if (m_password != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_password = v;
|
||||
}
|
||||
|
|
@ -158,7 +163,8 @@ const QString& ConnectionConfig::password() const
|
|||
|
||||
void ConnectionConfig::setDbname(const QString& v)
|
||||
{
|
||||
if (m_dbname != v) {
|
||||
if (m_dbname != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_dbname = v;
|
||||
}
|
||||
|
|
@ -171,7 +177,8 @@ const QString& ConnectionConfig::dbname() const
|
|||
|
||||
void ConnectionConfig::setSslMode(SslMode m)
|
||||
{
|
||||
if (m_sslMode != m) {
|
||||
if (m_sslMode != m)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_sslMode = m;
|
||||
}
|
||||
|
|
@ -184,7 +191,8 @@ SslMode ConnectionConfig::sslMode() const
|
|||
|
||||
void ConnectionConfig::setSslCert(const QString& v)
|
||||
{
|
||||
if (m_sslCert != v) {
|
||||
if (m_sslCert != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_sslCert = std::move(v);
|
||||
}
|
||||
|
|
@ -197,7 +205,8 @@ const QString& ConnectionConfig::sslCert() const
|
|||
|
||||
void ConnectionConfig::setSslKey(const QString& v)
|
||||
{
|
||||
if (m_sslKey != v) {
|
||||
if (m_sslKey != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_sslKey = std::move(v);
|
||||
}
|
||||
|
|
@ -210,7 +219,8 @@ const QString& ConnectionConfig::sslKey() const
|
|||
|
||||
void ConnectionConfig::setSslRootCert(const QString& v)
|
||||
{
|
||||
if (m_sslRootCert != v) {
|
||||
if (m_sslRootCert != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_sslRootCert = std::move(v);
|
||||
}
|
||||
|
|
@ -223,7 +233,8 @@ const QString& ConnectionConfig::sslRootCert() const
|
|||
|
||||
void ConnectionConfig::setSslCrl(const QString& v)
|
||||
{
|
||||
if (m_sslCrl != v) {
|
||||
if (m_sslCrl != v)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_sslCrl = std::move(v);
|
||||
}
|
||||
|
|
@ -234,32 +245,6 @@ const QString& ConnectionConfig::sslCrl() const
|
|||
return m_sslCrl;
|
||||
}
|
||||
|
||||
|
||||
//const char * const * ConnectionConfig::getKeywords() const
|
||||
//{
|
||||
// return s_keywords.data();
|
||||
//}
|
||||
|
||||
//const char * const * ConnectionConfig::getValues() const
|
||||
//{
|
||||
// m_values.resize(s_keywords.size(), nullptr);
|
||||
// m_values[0] = valuePtr(m_host);
|
||||
// m_values[1] = valuePtr(m_hostaddr);
|
||||
// m_values[2] = valuePtr(m_port);
|
||||
// m_values[3] = valuePtr(m_user);
|
||||
// m_values[4] = valuePtr(m_password);
|
||||
// m_values[5] = valuePtr(m_dbname);
|
||||
// m_values[6] = valuePtr(m_sslMode);
|
||||
// m_values[7] = valuePtr(m_sslCert);
|
||||
// m_values[8] = valuePtr(m_sslKey);
|
||||
// m_values[9] = valuePtr(m_sslRootCert);
|
||||
// m_values[10] = valuePtr(m_sslCrl);
|
||||
// m_values[11] = "utf8";
|
||||
// m_values[12] = valuePtr(m_applicationName);
|
||||
|
||||
// return m_values.data();
|
||||
//}
|
||||
|
||||
bool ConnectionConfig::isSameDatabase(const ConnectionConfig &rhs) const
|
||||
{
|
||||
return m_host == rhs.m_host
|
||||
|
|
@ -298,40 +283,32 @@ void ConnectionConfig::setEncodedPassword(const QByteArray &encodedPassword)
|
|||
m_encodedPassword = encodedPassword;
|
||||
}
|
||||
|
||||
//void ConnectionConfig::write(QDataStream &out) const
|
||||
//{
|
||||
//// out <<
|
||||
//}
|
||||
|
||||
//void ConnectionConfig::read(QDataStream &in)
|
||||
//{
|
||||
|
||||
//}
|
||||
|
||||
QString ConnectionConfig::escapeConnectionStringValue(const QString &value)
|
||||
{
|
||||
bool contains_spaces = false;
|
||||
int escapes = 0;
|
||||
for (auto&& c : value)
|
||||
if (c == ' ') contains_spaces = true;
|
||||
else if (c == '\'' || c == '\\') ++escapes;
|
||||
if (c == ' ')
|
||||
contains_spaces = true;
|
||||
else if (c == '\'' || c == '\\')
|
||||
++escapes;
|
||||
|
||||
if (contains_spaces || escapes > 0 || value.length() == 0) {
|
||||
if (contains_spaces || escapes > 0 || value.length() == 0)
|
||||
{
|
||||
QString result;
|
||||
result.reserve(2 + value.length() + escapes);
|
||||
result += '\'';
|
||||
for (auto&& c : value) {
|
||||
if (c == '\'' || c == '\\') {
|
||||
for (auto&& c : value)
|
||||
{
|
||||
if (c == '\'' || c == '\\')
|
||||
result += '\\';
|
||||
}
|
||||
result += c;
|
||||
}
|
||||
result += '\'';
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
else
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
QString ConnectionConfig::connectionString() const
|
||||
|
|
@ -349,19 +326,23 @@ QString ConnectionConfig::connectionString() const
|
|||
s += escapeConnectionStringValue(m_dbname);
|
||||
s += " sslmode=";
|
||||
s += SslModeToString(m_sslMode);
|
||||
if (!m_sslCert.isEmpty()) {
|
||||
if (!m_sslCert.isEmpty())
|
||||
{
|
||||
s += " sslcert=";
|
||||
s += escapeConnectionStringValue(m_sslCert);
|
||||
}
|
||||
if (!m_sslKey.isEmpty()) {
|
||||
if (!m_sslKey.isEmpty())
|
||||
{
|
||||
s += " sslkey=";
|
||||
s += escapeConnectionStringValue(m_sslKey);
|
||||
}
|
||||
if (!m_sslRootCert.isEmpty()) {
|
||||
if (!m_sslRootCert.isEmpty())
|
||||
{
|
||||
s += " sslrootcrt=";
|
||||
s += escapeConnectionStringValue(m_sslRootCert);
|
||||
}
|
||||
if (!m_sslCrl.isEmpty()) {
|
||||
if (!m_sslCrl.isEmpty())
|
||||
{
|
||||
s += " sslCrl=";
|
||||
s += escapeConnectionStringValue(m_sslCrl);
|
||||
}
|
||||
|
|
@ -369,55 +350,9 @@ QString ConnectionConfig::connectionString() const
|
|||
s += " application_name=";
|
||||
s += escapeConnectionStringValue(m_applicationName);
|
||||
|
||||
// "host", "hostaddr", "port", "user", "password", "dbname",
|
||||
// "sslmode", "sslcert", "sslkey", "sslrootcrt", "sslcrl",
|
||||
// "client_encoding", "application_name", nullptr };
|
||||
// m_values[0] = valuePtr(m_host);
|
||||
// m_values[1] = valuePtr(m_hostaddr);
|
||||
// m_values[2] = valuePtr(m_port);
|
||||
// m_values[3] = valuePtr(m_user);
|
||||
// m_values[4] = valuePtr(m_password);
|
||||
// m_values[5] = valuePtr(m_dbname);
|
||||
// m_values[6] = valuePtr(m_sslMode);
|
||||
// m_values[7] = valuePtr(m_sslCert);
|
||||
// m_values[8] = valuePtr(m_sslKey);
|
||||
// m_values[9] = valuePtr(m_sslRootCert);
|
||||
// m_values[10] = valuePtr(m_sslCrl);
|
||||
// m_values[11] = "utf8";
|
||||
// m_values[12] = valuePtr(m_applicationName);
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
PGHOST behaves the same as the host connection parameter.
|
||||
PGHOSTADDR behaves the same as the hostaddr connection parameter. This can be set instead of or in addition to PGHOST to avoid DNS lookup overhead.
|
||||
PGPORT behaves the same as the port connection parameter.
|
||||
PGDATABASE behaves the same as the dbname connection parameter.
|
||||
PGUSER behaves the same as the user connection parameter.
|
||||
PGPASSWORD behaves the same as the password connection parameter. Use of this environment variable is not recommended for security reasons, as some operating systems allow non-root users to see process environment variables via ps; instead consider using the ~/.pgpass file (see Section 31.15).
|
||||
PGPASSFILE specifies the name of the password file to use for lookups. If not set, it defaults to ~/.pgpass (see Section 31.15).
|
||||
PGSERVICE behaves the same as the service connection parameter.
|
||||
PGSERVICEFILE specifies the name of the per-user connection service file. If not set, it defaults to ~/.pg_service.conf (see Section 31.16).
|
||||
PGREALM sets the Kerberos realm to use with PostgreSQL, if it is different from the local realm. If PGREALM is set, libpq applications will attempt authentication with servers for this realm and use separate ticket files to avoid conflicts with local ticket files. This environment variable is only used if GSSAPI authentication is selected by the server.
|
||||
PGOPTIONS behaves the same as the options connection parameter.
|
||||
PGAPPNAME behaves the same as the application_name connection parameter.
|
||||
PGSSLMODE behaves the same as the sslmode connection parameter.
|
||||
PGREQUIRESSL behaves the same as the requiressl connection parameter.
|
||||
PGSSLCOMPRESSION behaves the same as the sslcompression connection parameter.
|
||||
PGSSLCERT behaves the same as the sslcert connection parameter.
|
||||
PGSSLKEY behaves the same as the sslkey connection parameter.
|
||||
PGSSLROOTCERT behaves the same as the sslrootcert connection parameter.
|
||||
PGSSLCRL behaves the same as the sslcrl connection parameter.
|
||||
PGREQUIREPEER behaves the same as the requirepeer connection parameter.
|
||||
PGKRBSRVNAME behaves the same as the krbsrvname connection parameter.
|
||||
PGGSSLIB behaves the same as the gsslib connection parameter.
|
||||
PGCONNECT_TIMEOUT behaves the same as the connect_timeout connection parameter.
|
||||
PGCLIENTENCODING behaves the same as the client_encoding connection parameter.
|
||||
|
||||
*/
|
||||
|
||||
void ConnectionConfig::writeToEnvironment(QProcessEnvironment &env) const
|
||||
{
|
||||
strToEnv(env, "PGHOST", m_host);
|
||||
|
|
@ -464,16 +399,3 @@ void ConnectionGroup::update(int idx, const ConnectionConfig &cc)
|
|||
*node = cc;
|
||||
node->setParent(this);
|
||||
}
|
||||
|
||||
//QDataStream &operator<<(QDataStream &out, const ConnectionConfig &cc)
|
||||
//{
|
||||
// cc.write(out);
|
||||
// return out;
|
||||
//}
|
||||
|
||||
//QDataStream &operator>>(QDataStream &in, ConnectionConfig &cc)
|
||||
//{
|
||||
// //in>>myObj.uId>>myObj.passwd>>myObj.statusType;
|
||||
// cc.read(in);
|
||||
// return in;
|
||||
//}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue