Add support for non-public schemas with pgsql (#2341)

This commit is contained in:
Nicholas Hastings
2026-07-27 20:35:59 -04:00
parent bc69fc9f8e
commit cfa6193bb8
7 changed files with 24 additions and 3 deletions
+1
View File
@@ -13,6 +13,7 @@
"pass" ""
//"timeout" "0"
//"port" "0"
//"schema" "public" // PostgreSQL only
}
"storage-local"
+3
View File
@@ -127,6 +127,8 @@ SMCResult DatabaseConfBuilder::ReadSMC_KeyValue(const SMCStates *states, const c
}
} else if (strcmp(key, "database") == 0) {
m_ParseCurrent->database = value;
} else if (strcmp(key, "schema") == 0) {
m_ParseCurrent->schemaName = value;
} else if (strcmp(key, "host") == 0) {
m_ParseCurrent->host = value;
} else if (strcmp(key, "user") == 0) {
@@ -158,6 +160,7 @@ SMCResult DatabaseConfBuilder::ReadSMC_LeavingSection(const SMCStates *states)
m_ParseCurrent->info.host = m_ParseCurrent->host.c_str();
m_ParseCurrent->info.user = m_ParseCurrent->user.c_str();
m_ParseCurrent->info.pass = m_ParseCurrent->pass.c_str();
m_ParseCurrent->info.schemaName = m_ParseCurrent->schemaName.c_str();
/* Save it.. */
m_ParseCurrent->AddRef();
+1
View File
@@ -53,6 +53,7 @@ public:
std::string user;
std::string pass;
std::string database;
std::string schemaName;
IDBDriver *realDriver;
DatabaseInfo info;
};
+2
View File
@@ -137,11 +137,13 @@ PgDatabase::PgDatabase(PGconn *pgsql, const DatabaseInfo *info, bool persistent)
m_Database.assign(info->database);
m_User.assign(info->user);
m_Pass.assign(info->pass);
m_Schema.assign(info->dbiVersion >= 10 && info->schemaName ? info->schemaName : "");
m_Info.database = m_Database.c_str();
m_Info.host = m_Host.c_str();
m_Info.user = m_User.c_str();
m_Info.pass = m_Pass.c_str();
m_Info.schemaName = m_Schema.c_str();
m_Info.driver = NULL;
m_Info.maxTimeout = info->maxTimeout;
m_Info.port = info->port;
+1
View File
@@ -85,6 +85,7 @@ private:
String m_Database;
String m_User;
String m_Pass;
String m_Schema;
bool m_bPersistent;
};
+13 -2
View File
@@ -93,12 +93,14 @@ const char *PgDriver::GetProductName()
PGconn *Connect(const DatabaseInfo *info, char *error, size_t maxlength)
{
const char *keywords[7];
const char *values[7];
const char *keywords[8];
const char *values[8];
unsigned int count = 0;
char port[16];
char timeout[16];
char options[1024];
const char *searchPath = info->dbiVersion >= 10 ? info->schemaName : NULL;
keywords[count] = "host";
values[count++] = info->host;
@@ -131,6 +133,14 @@ PGconn *Connect(const DatabaseInfo *info, char *error, size_t maxlength)
values[count++] = timeout;
}
if (searchPath && searchPath[0] != '\0')
{
// libpq splits options on spaces; schema names containing spaces need escaping.
snprintf(options, sizeof(options), "-c search_path=%s", searchPath);
keywords[count] = "options";
values[count++] = options;
}
keywords[count] = NULL;
values[count] = NULL;
@@ -192,6 +202,7 @@ IDatabase *PgDriver::Connect(const DatabaseInfo *info, bool persistent, char *er
&& CompareField(info->user, other.user)
&& CompareField(info->pass, other.pass)
&& CompareField(info->database, other.database)
&& CompareField(info->dbiVersion >= 10 ? info->schemaName : NULL, other.schemaName)
&& (info->port == other.port))
{
db->IncReferenceCount();
+3 -1
View File
@@ -42,7 +42,7 @@
*/
#define SMINTERFACE_DBI_NAME "IDBI"
#define SMINTERFACE_DBI_VERSION 9
#define SMINTERFACE_DBI_VERSION 10
namespace SourceMod
{
@@ -634,6 +634,7 @@ namespace SourceMod
{
dbiVersion = SMINTERFACE_DBI_VERSION;
port = 0;
schemaName = "";
maxTimeout = 0;
}
unsigned int dbiVersion; /**< DBI Version for backwards compatibility */
@@ -644,6 +645,7 @@ namespace SourceMod
const char *driver; /**< Driver to use */
unsigned int port; /**< Port to use, 0=default */
unsigned int maxTimeout; /**< Maximum timeout, 0=default */
const char *schemaName; /**< Schema/search_path, "" or NULL = driver default */
};
/**