Big cleanup

This commit is contained in:
eelke 2022-05-26 08:25:31 +02:00
parent d3080a08bb
commit 8b671090a0
55 changed files with 214 additions and 3967 deletions

View file

@ -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;
//}