From f83fa173d99adf6a1995158656b5d92fe9317e40 Mon Sep 17 00:00:00 2001 From: Damian Mooyman Date: Mon, 4 Jul 2016 13:53:34 +1200 Subject: [PATCH] Move config into private statics for future maintainability --- _config/postgresql.yml | 32 ---------------- code/PostgreSQLDatabase.php | 75 ++++++++++++++++++++++++++++++++++--- 2 files changed, 70 insertions(+), 37 deletions(-) delete mode 100644 _config/postgresql.yml diff --git a/_config/postgresql.yml b/_config/postgresql.yml deleted file mode 100644 index 3854d83..0000000 --- a/_config/postgresql.yml +++ /dev/null @@ -1,32 +0,0 @@ -PostgreSQLDatabase: -# Determines whether to check a database exists on the host by -# querying the 'postgres' database and running createDatabase. -# -# Some locked down systems prevent access to the 'postgres' table in -# which case you need to set this to false. -# -# If allow_query_master_postgres is false, and model_schema_as_database is also false, -# then attempts to create or check databases beyond the initial connection will -# result in a runtime error. - allow_query_master_postgres: true -# For instances where multiple databases are used beyond the initial connection -# you may set this option to true to force database switches to switch schemas -# instead of using databases. This may be useful if the database user does not -# have cross-database permissions, and in cases where multiple databases are used -# (such as in running test cases). -# -# If this is true then the database will only be set during the initial connection, -# and attempts to change to this database will use the 'public' schema instead -# -# If this is false then errors may be generated during some cross database operations. - model_schema_as_database: true -# Override the language that tsearch uses. By default it is 'english, but -# could be any of the supported languages that can be found in the -# pg_catalog.pg_ts_config table. - search_language: 'english' -# These two values describe how T-search will work. -# You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin) -# Combinations of these two will also work, so you'll need to pick -# one which works best for you - default_fts_cluster_method: 'GIN' - default_fts_search_method: '@@@' \ No newline at end of file diff --git a/code/PostgreSQLDatabase.php b/code/PostgreSQLDatabase.php index dd7657e..5e43424 100644 --- a/code/PostgreSQLDatabase.php +++ b/code/PostgreSQLDatabase.php @@ -2,6 +2,7 @@ namespace SilverStripe\PostgreSQL; +use SilverStripe\Framework\Core\Configurable; use SilverStripe\ORM\DB; use SilverStripe\ORM\DataObject; use SilverStripe\ORM\ArrayList; @@ -19,6 +20,8 @@ use PaginatedList; */ class PostgreSQLDatabase extends SS_Database { + use Configurable; + /** * Database schema manager object * @@ -33,8 +36,66 @@ class PostgreSQLDatabase extends SS_Database */ protected $schema; + /** + * Toggle if transactions are supported. Defaults to true. + * + * @var bool + */ protected $supportsTransactions = true; + /** + * Determines whether to check a database exists on the host by + * querying the 'postgres' database and running createDatabase. + * + * Some locked down systems prevent access to the 'postgres' table in + * which case you need to set this to false. + * + * If allow_query_master_postgres is false, and model_schema_as_database is also false, + * then attempts to create or check databases beyond the initial connection will + * result in a runtime error. + * + * @config + * @var bool + */ + private static $allow_query_master_postgres = true; + + /** + * For instances where multiple databases are used beyond the initial connection + * you may set this option to true to force database switches to switch schemas + * instead of using databases. This may be useful if the database user does not + * have cross-database permissions, and in cases where multiple databases are used + * (such as in running test cases). + * + * If this is true then the database will only be set during the initial connection, + * and attempts to change to this database will use the 'public' schema instead + * + * If this is false then errors may be generated during some cross database operations. + */ + private static $model_schema_as_database = true; + + /** + * Override the language that tsearch uses. By default it is 'english, but + * could be any of the supported languages that can be found in the + * pg_catalog.pg_ts_config table. + */ + private static $search_language = 'english'; + + /* + * Describe how T-search will work. + * You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin) + * Combinations of these two will also work, so you'll need to pick + * one which works best for you + */ + private static $default_fts_cluster_method = 'GIN'; + + /* + * Describe how T-search will work. + * You can use either GiST or GIN, and '@@' (gist) or '@@@' (gin) + * Combinations of these two will also work, so you'll need to pick + * one which works best for you + */ + private static $default_fts_search_method = '@@@'; + const MASTER_DATABASE = 'postgres'; const MASTER_SCHEMA = 'public'; @@ -46,7 +107,7 @@ class PostgreSQLDatabase extends SS_Database */ public static function default_fts_cluster_method() { - return Config::inst()->get('SilverStripe\\PostgreSQL\\PostgreSQLDatabase', 'default_fts_cluster_method'); + return static::config()->default_fts_cluster_method; } /** @@ -56,7 +117,7 @@ class PostgreSQLDatabase extends SS_Database */ public static function default_fts_search_method() { - return Config::inst()->get('SilverStripe\\PostgreSQL\\PostgreSQLDatabase', 'default_fts_search_method'); + return static::config()->default_fts_search_method; } /** @@ -69,10 +130,12 @@ class PostgreSQLDatabase extends SS_Database * If allow_query_master_postgres is false, and model_schema_as_database is also false, * then attempts to create or check databases beyond the initial connection will * result in a runtime error. + * + * @return bool */ public static function allow_query_master_postgres() { - return Config::inst()->get('SilverStripe\\PostgreSQL\\PostgreSQLDatabase', 'allow_query_master_postgres'); + return static::config()->allow_query_master_postgres; } /** @@ -84,10 +147,12 @@ class PostgreSQLDatabase extends SS_Database * * If this is true then the database will only be set during the initial connection, * and attempts to change to this database will use the 'public' schema instead + * + * @return bool */ public static function model_schema_as_database() { - return Config::inst()->get('SilverStripe\\PostgreSQL\\PostgreSQLDatabase', 'model_schema_as_database'); + return static::config()->model_schema_as_database; } /** @@ -99,7 +164,7 @@ class PostgreSQLDatabase extends SS_Database */ public static function search_language() { - return Config::inst()->get('SilverStripe\\PostgreSQL\\PostgreSQLDatabase', 'search_language'); + return static::config()->search_language; } /**