Switched ConnectionConfig to QString from std::string to fit better into Qt framework
This commit is contained in:
parent
bcfd82c27d
commit
082293e58a
20 changed files with 1077 additions and 211 deletions
|
|
@ -2,9 +2,19 @@
|
|||
#include "util.h"
|
||||
#include <QCoreApplication>
|
||||
#include <QProcessEnvironment>
|
||||
#include <QStringBuilder>
|
||||
#include <QUrl>
|
||||
|
||||
namespace {
|
||||
|
||||
class registerMetaTypes {
|
||||
public:
|
||||
registerMetaTypes()
|
||||
{
|
||||
qRegisterMetaTypeStreamOperators<ConnectionConfig>("ConnectionConfig");
|
||||
}
|
||||
} registerMetaTypes_instance;
|
||||
|
||||
struct {
|
||||
SslMode mode;
|
||||
const char* string;
|
||||
|
|
@ -24,18 +34,17 @@ namespace {
|
|||
|
||||
} // end unnamed namespace
|
||||
|
||||
std::string SslModeToString(SslMode sm)
|
||||
{
|
||||
std::string result;
|
||||
QString SslModeToString(SslMode sm)
|
||||
{
|
||||
for (auto e : SslModeStringTable) {
|
||||
if (e.mode == sm) {
|
||||
result = e.string;
|
||||
return QString::fromUtf8(e.string);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return {};
|
||||
}
|
||||
|
||||
SslMode StringToSslMode(std::string s)
|
||||
SslMode StringToSslMode(QString s)
|
||||
{
|
||||
SslMode result = SslMode::allow;
|
||||
for (auto e : SslModeStringTable) {
|
||||
|
|
@ -43,14 +52,14 @@ SslMode StringToSslMode(std::string s)
|
|||
result = e.mode;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
std::vector<const char*> ConnectionConfig::s_keywords = {
|
||||
"host", "hostaddr", "port", "user", "password", "dbname",
|
||||
"sslmode", "sslcert", "sslkey", "sslrootcrt", "sslcrl",
|
||||
"client_encoding", "application_name", nullptr };
|
||||
//std::vector<const char*> ConnectionConfig::s_keywords = {
|
||||
// "host", "hostaddr", "port", "user", "password", "dbname",
|
||||
// "sslmode", "sslcert", "sslkey", "sslrootcrt", "sslcrl",
|
||||
// "client_encoding", "application_name", nullptr };
|
||||
|
||||
|
||||
ConnectionConfig::ConnectionConfig()
|
||||
|
|
@ -83,7 +92,7 @@ const QUuid &ConnectionConfig::uuid() const
|
|||
}
|
||||
|
||||
|
||||
void ConnectionConfig::setName(std::string desc)
|
||||
void ConnectionConfig::setName(const QString& desc)
|
||||
{
|
||||
if (m_name != desc) {
|
||||
m_dirty = true;
|
||||
|
|
@ -91,12 +100,12 @@ void ConnectionConfig::setName(std::string desc)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::name() const
|
||||
const QString& ConnectionConfig::name() const
|
||||
{
|
||||
return m_name;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setHost(std::string host)
|
||||
void ConnectionConfig::setHost(const QString& host)
|
||||
{
|
||||
if (m_host != host) {
|
||||
m_dirty = true;
|
||||
|
|
@ -104,12 +113,12 @@ void ConnectionConfig::setHost(std::string host)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::host() const
|
||||
const QString& ConnectionConfig::host() const
|
||||
{
|
||||
return m_host;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setHostAddr(std::string v)
|
||||
void ConnectionConfig::setHostAddr(const QString &v)
|
||||
{
|
||||
if (m_hostaddr != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -117,26 +126,25 @@ void ConnectionConfig::setHostAddr(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::hostAddr() const
|
||||
const QString& ConnectionConfig::hostAddr() const
|
||||
{
|
||||
return m_hostaddr;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setPort(unsigned short port)
|
||||
{
|
||||
auto p = std::to_string(port);
|
||||
if (m_port != p) {
|
||||
if (m_port != port) {
|
||||
m_dirty = true;
|
||||
m_port = p;
|
||||
m_port = port;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned short ConnectionConfig::port() const
|
||||
{
|
||||
return static_cast<unsigned short>(std::stoi(m_port));
|
||||
return m_port;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setUser(std::string v)
|
||||
void ConnectionConfig::setUser(const QString& v)
|
||||
{
|
||||
if (m_user != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -145,12 +153,12 @@ void ConnectionConfig::setUser(std::string v)
|
|||
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::user() const
|
||||
const QString& ConnectionConfig::user() const
|
||||
{
|
||||
return m_user;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setPassword(std::string v)
|
||||
void ConnectionConfig::setPassword(const QString& v)
|
||||
{
|
||||
if (m_password != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -158,12 +166,12 @@ void ConnectionConfig::setPassword(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::password() const
|
||||
const QString& ConnectionConfig::password() const
|
||||
{
|
||||
return m_password;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setDbname(std::string v)
|
||||
void ConnectionConfig::setDbname(const QString& v)
|
||||
{
|
||||
if (m_dbname != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -171,26 +179,25 @@ void ConnectionConfig::setDbname(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::dbname() const
|
||||
const QString& ConnectionConfig::dbname() const
|
||||
{
|
||||
return m_dbname;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setSslMode(SslMode m)
|
||||
{
|
||||
auto v = SslModeToString(m);
|
||||
if (m_sslMode != v) {
|
||||
if (m_sslMode != m) {
|
||||
m_dirty = true;
|
||||
m_sslMode = v;
|
||||
m_sslMode = m;
|
||||
}
|
||||
}
|
||||
|
||||
SslMode ConnectionConfig::sslMode() const
|
||||
{
|
||||
return StringToSslMode(m_sslMode);
|
||||
return m_sslMode;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setSslCert(std::string v)
|
||||
void ConnectionConfig::setSslCert(const QString& v)
|
||||
{
|
||||
if (m_sslCert != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -198,12 +205,12 @@ void ConnectionConfig::setSslCert(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::sslCert() const
|
||||
const QString& ConnectionConfig::sslCert() const
|
||||
{
|
||||
return m_sslCert;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setSslKey(std::string v)
|
||||
void ConnectionConfig::setSslKey(const QString& v)
|
||||
{
|
||||
if (m_sslKey != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -211,12 +218,12 @@ void ConnectionConfig::setSslKey(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::sslKey() const
|
||||
const QString& ConnectionConfig::sslKey() const
|
||||
{
|
||||
return m_sslKey;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setSslRootCert(std::string v)
|
||||
void ConnectionConfig::setSslRootCert(const QString& v)
|
||||
{
|
||||
if (m_sslRootCert != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -224,12 +231,12 @@ void ConnectionConfig::setSslRootCert(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::sslRootCert() const
|
||||
const QString& ConnectionConfig::sslRootCert() const
|
||||
{
|
||||
return m_sslRootCert;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setSslCrl(std::string v)
|
||||
void ConnectionConfig::setSslCrl(const QString& v)
|
||||
{
|
||||
if (m_sslCrl != v) {
|
||||
m_dirty = true;
|
||||
|
|
@ -237,36 +244,36 @@ void ConnectionConfig::setSslCrl(std::string v)
|
|||
}
|
||||
}
|
||||
|
||||
const std::string& ConnectionConfig::sslCrl() const
|
||||
const QString& ConnectionConfig::sslCrl() const
|
||||
{
|
||||
return m_sslCrl;
|
||||
}
|
||||
|
||||
|
||||
const char * const * ConnectionConfig::getKeywords() const
|
||||
{
|
||||
return s_keywords.data();
|
||||
}
|
||||
//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);
|
||||
//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();
|
||||
}
|
||||
// return m_values.data();
|
||||
//}
|
||||
|
||||
bool ConnectionConfig::isSameDatabase(const ConnectionConfig &rhs) const
|
||||
{
|
||||
|
|
@ -290,28 +297,111 @@ void ConnectionConfig::clean()
|
|||
|
||||
QString ConnectionConfig::makeLongDescription() const
|
||||
{
|
||||
std::string result(name());
|
||||
result += " (";
|
||||
result += user();
|
||||
result += "@";
|
||||
result += host();
|
||||
result += ":";
|
||||
result += std::to_string(port());
|
||||
result += "/";
|
||||
result += dbname();
|
||||
result += ")";
|
||||
return stdStrToQ(result);
|
||||
QString result;
|
||||
result = name() % " (" % user() % "@" % host() % ":" % QString::number(port()) % "/" % dbname() % ")";
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string ConnectionConfig::encodedPassword() const
|
||||
QByteArray ConnectionConfig::encodedPassword() const
|
||||
{
|
||||
return m_encodedPassword;
|
||||
}
|
||||
|
||||
void ConnectionConfig::setEncodedPassword(const std::string &encodedPassword)
|
||||
void ConnectionConfig::setEncodedPassword(const QByteArray &encodedPassword)
|
||||
{
|
||||
m_dirty = true;
|
||||
m_encodedPassword = 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 (contains_spaces || escapes > 0 || value.length() == 0) {
|
||||
QString result;
|
||||
result.reserve(2 + value.length() + escapes);
|
||||
result += '\'';
|
||||
for (auto&& c : value) {
|
||||
if (c == '\'' || c == '\\') {
|
||||
result += '\\';
|
||||
}
|
||||
result += c;
|
||||
}
|
||||
result += '\'';
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
QString ConnectionConfig::connectionString() const
|
||||
{
|
||||
QString s;
|
||||
s += "host="
|
||||
% escapeConnectionStringValue(m_host)
|
||||
% " port="
|
||||
% QString::number(m_port)
|
||||
% " user="
|
||||
% escapeConnectionStringValue(m_user);
|
||||
s += " password=";
|
||||
s += escapeConnectionStringValue(m_password);
|
||||
s += " dbname=";
|
||||
s += escapeConnectionStringValue(m_dbname);
|
||||
s += " sslmode=";
|
||||
s += SslModeToString(m_sslMode);
|
||||
if (!m_sslCert.isEmpty()) {
|
||||
s += " sslcert=";
|
||||
s += escapeConnectionStringValue(m_sslCert);
|
||||
}
|
||||
if (!m_sslKey.isEmpty()) {
|
||||
s += " sslkey=";
|
||||
s += escapeConnectionStringValue(m_sslKey);
|
||||
}
|
||||
if (!m_sslRootCert.isEmpty()) {
|
||||
s += " sslrootcrt=";
|
||||
s += escapeConnectionStringValue(m_sslRootCert);
|
||||
}
|
||||
if (!m_sslCrl.isEmpty()) {
|
||||
s += " sslCrl=";
|
||||
s += escapeConnectionStringValue(m_sslCrl);
|
||||
}
|
||||
s += " client_encoding=utf8";
|
||||
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;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -347,11 +437,11 @@ void ConnectionConfig::writeToEnvironment(QProcessEnvironment &env) const
|
|||
{
|
||||
strToEnv(env, "PGHOST", m_host);
|
||||
strToEnv(env, "PGHOSTADDR", m_hostaddr);
|
||||
strToEnv(env, "PGPORT", m_port);
|
||||
strToEnv(env, "PGPORT", QString::number(m_port));
|
||||
strToEnv(env, "PGDATABASE", m_dbname);
|
||||
strToEnv(env, "PGUSER", m_user);
|
||||
strToEnv(env, "PGPASSWORD", m_password);
|
||||
strToEnv(env, "PGSSLMODE", m_sslMode);
|
||||
strToEnv(env, "PGSSLMODE", SslModeToString(m_sslMode));
|
||||
strToEnv(env, "PGSSLCERT", m_sslCert);
|
||||
strToEnv(env, "PGSSLKEY", m_sslKey);
|
||||
strToEnv(env, "PGSSLROOTCERT", m_sslRootCert);
|
||||
|
|
@ -363,12 +453,12 @@ void ConnectionConfig::writeToEnvironment(QProcessEnvironment &env) const
|
|||
env.remove("PGREQUIRESSL");
|
||||
}
|
||||
|
||||
void ConnectionConfig::strToEnv(QProcessEnvironment &env, const QString &var, const std::string &val)
|
||||
void ConnectionConfig::strToEnv(QProcessEnvironment &env, const QString &var, const QString &val)
|
||||
{
|
||||
if (val.empty())
|
||||
if (val.isEmpty())
|
||||
env.remove(var);
|
||||
else
|
||||
env.insert(var, stdStrToQ(val));
|
||||
env.insert(var, val);
|
||||
}
|
||||
|
||||
void ConnectionGroup::erase(int idx, int count)
|
||||
|
|
@ -389,3 +479,16 @@ 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