From f4e461c26ef9282bc303cdc93e73b737939b196d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Mon, 10 Dec 2018 23:48:31 -0600 Subject: [PATCH] Progress: continue development on database abstraction --- .../database/classes/database_alter_function.php | 2 +- common/database/classes/database_alter_group.php | 22 +- common/database/classes/database_alter_index.php | 101 +++++++- .../database/classes/database_alter_language.php | 8 +- common/database/classes/database_string.php | 277 +++++++++++---------- .../enumerations/database_storage_parameter.php | 36 +++ common/database/traits/database_no_wait.php | 87 +++++++ common/database/traits/database_owned_by.php | 119 +++++++++ .../traits/database_reset_storage_parameter.php | 138 ++++++++++ common/database/traits/database_set.php | 8 +- .../traits/database_set_storage_parameter.php | 151 +++++++++++ common/database/traits/database_set_tablespace.php | 2 +- 12 files changed, 802 insertions(+), 149 deletions(-) create mode 100644 common/database/enumerations/database_storage_parameter.php create mode 100644 common/database/traits/database_no_wait.php create mode 100644 common/database/traits/database_owned_by.php create mode 100644 common/database/traits/database_reset_storage_parameter.php create mode 100644 common/database/traits/database_set_storage_parameter.php diff --git a/common/database/classes/database_alter_function.php b/common/database/classes/database_alter_function.php index c2bf9b8..f0bade5 100644 --- a/common/database/classes/database_alter_function.php +++ b/common/database/classes/database_alter_function.php @@ -24,7 +24,7 @@ require_once('common/database/traits/database_set_schema.php'); * * @see: https://www.postgresql.org/docs/current/static/sql-alterfunction.html */ -class c_database_alter_coalation extends c_database_query { +class c_database_alter_function extends c_database_query { use t_database_argument_type; use t_database_depends_on_extension; use t_database_function_action; diff --git a/common/database/classes/database_alter_group.php b/common/database/classes/database_alter_group.php index e5926c7..cea116a 100644 --- a/common/database/classes/database_alter_group.php +++ b/common/database/classes/database_alter_group.php @@ -11,6 +11,8 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); require_once('common/database/traits/database_add_user.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_rename_to.php'); require_once('common/database/traits/database_role_specification.php'); /** @@ -18,8 +20,10 @@ require_once('common/database/traits/database_role_specification.php'); * * @see: https://www.postgresql.org/docs/current/static/sql-altergroup.html */ -class c_database_alter_coalation extends c_database_query { +class c_database_alter_group extends c_database_query { use t_database_add_user; + use t_database_name; + use t_database_rename_to; use t_database_role_specification; protected const p_QUERY_COMMAND = 'alter group'; @@ -32,6 +36,8 @@ class c_database_alter_coalation extends c_database_query { parent::__construct(); $this->add_user = NULL; + $this->name = NULL; + $this->rename_to = NULL; $this->role_specification = NULL; } @@ -40,6 +46,8 @@ class c_database_alter_coalation extends c_database_query { */ public function __destruct() { unset($this->add_user); + unset($this->name); + unset($this->rename_to); unset($this->role_specification); parent::__destruct(); @@ -70,13 +78,13 @@ class c_database_alter_coalation extends c_database_query { * Implements do_build(). */ public function do_build() { - if (!is_string($this->name)) { - return new c_base_return_false(); - } - - if ((is_int($this->role_specification) || is_string($this->role_specification)) && is_array($this->add_user)) { + if (is_array($this->add_user) && (is_int($this->role_specification) || is_string($this->role_specification))) { $value = $this->p_do_build_role_specification(); - $value = ' ' . $this->p_do_build_add_user(); + $value .= ' ' . $this->p_do_build_add_user(); + } + else if (!is_string($this->name) && !is_string($this->rename_to)) { + $value = $this->p_do_build_name(); + $value .= ' ' . $this->p_do_build_rename_to(); } else { return new c_base_return_false(); diff --git a/common/database/classes/database_alter_index.php b/common/database/classes/database_alter_index.php index 54885a7..5383ba0 100644 --- a/common/database/classes/database_alter_index.php +++ b/common/database/classes/database_alter_index.php @@ -1,7 +1,7 @@ depends_on_extension = NULL; + $this->if_exists = NULL; + $this->name = NULL; + $this->no_wait = NULL; + $this->owned_by = NULL; + $this->rename_to = NULL; + $this->reset_storage_parameter = NULL; + $this->set_storage_parameter = NULL; + $this->set_tablespace = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->depends_on_extension); + unset($this->if_exists); + unset($this->name); + unset($this->no_wait); + unset($this->owned_by); + unset($this->rename_to); + unset($this->reset_storage_parameter); + unset($this->set_storage_parameter); + unset($this->set_tablespace); + parent::__destruct(); } @@ -59,9 +99,56 @@ class c_database_alter_coalation extends c_database_query { * Implements do_build(). */ public function do_build() { - $this->value = NULL; + if (!is_string($this->name)) { + return new c_base_return_false(); + } + + $if_exists = NULL; + if (is_bool($this->if_exists)) { + $if_exists = ' ' . $this->p_do_build_if_exists(); + } + + $value = $this->name; + if (is_string($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (is_string($this->set_tablespace)) { + $value .= ' ' . $this->p_do_build_set_tablespace(); + } + else if (is_string($this->depends_on_extension)) { + $if_exists = NULL; + $value .= ' ' . $this->p_do_build_depends_on_extension(); + } + else if (is_array($this->set_storage_parameter)) { + $value .= ' ' . $this->p_do_build_set_storage_parameter(); + } + else if (is_array($this->reset_storage_parameter)) { + $value .= ' ' . $this->p_do_build_reset_storage_parameter(); + } + else if (is_string($this->set_tablespace)) { + $if_exists = NULL; + $value = c_database_string::ALL_IN_TABLESPACE . ' ' . $value; + + if (is_array($this->owned_by)) { + $value .= ' ' . $this->p_do_build_owned_by(); + } + + $value .= ' ' . $this->p_do_build_set_tablespace(); + + if (is_bool($this->no_wait)) { + $value .= ' ' . $this->p_do_build_no_wait(); + } + } + + $this->value = static::p_QUERY_COMMAND; + + if (is_string($if_exists)) { + $this->value .= $if_exists; + } + unset($if_exists); - // @todo + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_language.php b/common/database/classes/database_alter_language.php index 54885a7..1cd8887 100644 --- a/common/database/classes/database_alter_language.php +++ b/common/database/classes/database_alter_language.php @@ -59,10 +59,14 @@ class c_database_alter_coalation extends c_database_query { * Implements do_build(). */ public function do_build() { - $this->value = NULL; + if (!is_string($this->name)) { + return new c_base_return_false(); + } // @todo - return new c_base_return_true(); + $this->value = static::p_QUERY_COMMAND; + $this->value .= ' ' . $value; + unset($value); } } diff --git a/common/database/classes/database_string.php b/common/database/classes/database_string.php index c57cd15..4ae4911 100644 --- a/common/database/classes/database_string.php +++ b/common/database/classes/database_string.php @@ -11,131 +11,154 @@ namespace n_koopa; * A collection of strings used for generating SQL. */ class c_database_string { - public const ACCESS_METHOD = 'access method'; - public const ADD = 'add'; - public const AGGREGATE = 'aggregate'; - public const ALL = 'all'; - public const ALLOW_CONNECTIONS = 'allow_connections'; - public const AS = 'as'; - public const ASCEND = 'asc'; - public const CALLED_ON_NULL_INPUT = 'called on null input'; - public const CASCADE = 'cascade'; - public const CAST = 'cast'; - public const COLLATION = 'collation'; - public const CONNECTION_LIMIT = 'connection limit'; - public const CONVERSION = 'conversion'; - public const COST = 'cost'; - public const CREATE = 'create'; - public const DEFAULT = 'default'; - public const DELETE = 'delete'; - public const DEPENDS_ON_EXTENSION = 'depends on extension'; - public const DESCEND = 'desc'; - public const DISABLE_TRIGGER = 'disable trigger'; - public const DOMAIN = 'domain'; - public const DROP = 'drop'; - public const DROP_CONSTRAINT = 'drop constraint'; - public const DROP_DEFAULT = 'drop default'; - public const ENABLE_ALWAYS_TRIGGER = 'enable always trigger'; - public const ENABLE_REPLICA_TRIGGER = 'enable replica trigger'; - public const ENABLE_TRIGGER = 'enable trigger'; - public const EVENT_TRIGGER = 'event trigger'; - public const EXECUTE = 'execute'; - public const EXTERNAL = 'external'; - public const FALSE = 'false'; - public const FOREIGN_DATA_WRAPPER = 'foreign data wrapper'; - public const FOREIGN_TABLE = 'foreign table'; - public const FOR = 'for'; - public const FOR_ROLE = 'for role'; - public const FROM_CURRENT = 'from current'; - public const FUNCTION = 'function'; - public const GRANT = 'grant'; - public const GRANT_OPTION_FOR = 'grant option for'; - public const GROUP = 'group'; - public const GROUP_BY = 'group by'; - public const HANDLER = 'handler'; - public const IF_EXISTS = 'if exists'; - public const IMMUTABLE = 'immutable'; - public const IN = 'in'; - public const INOUT = 'inout'; - public const IN_SCHEMA = 'in schema'; - public const INHERIT = 'inherit'; - public const INSERT = 'insert'; - public const IS_TEMPLATE = 'is_template'; - public const LANGUAGE = 'language'; - public const LEAKPROOF = 'leakproof'; - public const MATERIALIZED_VIEW = 'materialized view'; - public const NO_HANDLER = 'no handler'; - public const NO_INHERIT = 'no inherit'; - public const NO_VALIDATOR = 'no validator'; - public const NOT_LEAKPROOF = 'not leakproof'; - public const NOT_NULL = 'not null'; - public const NOT_VALID = 'not valid'; - public const ON_FUNCTIONS = 'on functons'; - public const ON_SCHEMAS = 'on schemas'; - public const ON_SEQUENCES = 'on sequences'; - public const ON_TABLES_TO = 'on tables to'; - public const ON_TYPES = 'on types'; - public const ONLY = 'only'; - public const OPERATOR_CLASS = 'operator class'; - public const OPERATOR_FAMILY = 'operator family'; - public const OPTIONS = 'options'; - public const ORDER_BY = 'order by'; - public const OUT = 'out'; - public const OWNER_TO = 'owner to'; - public const PARALLEL = 'parallel'; - public const PROCEDURAL = 'procedural'; - public const PUBLIC = 'public'; - public const REFERENCES = 'references'; - public const REFRESH_VERSION = 'refresh version'; - public const RENAME_TO = 'rename to'; - public const RENAME_COLUMN = 'rename column'; - public const RENAME_CONSTRAINT = 'rename constraint'; - public const RESET = 'reset'; - public const RESET_ALL = 'reset all'; - public const RESTRICT = 'restrict'; - public const RESTRICTED = 'restricted'; - public const RETURNS_NULL_ON_NULL_INPUT = 'returns null on null input'; - public const REVOKE = 'revoke'; - public const ROLE = 'role'; - public const ROWS = 'rows'; - public const SAFE = 'safe'; - public const SCHEMA = 'schema'; - public const SECURITY_DEFINER = 'security definer'; - public const SECURITY_INVOKER = 'security invoker'; - public const SELECT = 'select'; - public const SEQUENCE = 'sequence'; - public const SERVER = 'server'; - public const SET = 'set'; - public const SET_DEFAULT = 'set default'; - public const SET_SCHEMA = 'set schema'; - public const SET_TABLESPACE = 'set tablespace'; - public const SET_WITH_OIDS = 'set with oids'; - public const SET_WITHOUT_OIDS = 'set without oids'; - public const STABLE = 'stable'; - public const STRICT = 'strict'; - public const TABLE = 'table'; - public const TEXT_SEARCH_CONFIGURATION = 'text search configuration'; - public const TEXT_SEARCH_DICTIONARY = 'text search dictionary'; - public const TEXT_SEARCH_PARSER = 'text search parser'; - public const TEXT_SEARCH_TEMPLATE = 'text search template'; - public const TRANSFORM_FOR = 'transform for'; - public const TRIGGER = 'trigger'; - public const TRUE = 'true'; - public const TRUNCATE = 'truncate'; - public const TYPE = 'type'; - public const TO = 'to'; - public const UNSAFE = 'unsafe'; - public const UPDATE = 'update'; - public const USAGE = 'usage'; - public const USER = 'user'; - public const USER_CURRENT = 'current_user'; - public const USER_SESSION = 'session_user'; - public const USING = 'using'; - public const VALIDATOR = 'validator'; - public const VALIDATE_CONSTRAINT = 'validate constraint'; - public const VARIADIC = 'variadic'; - public const VOLATILE = 'volatile'; - public const VIEW = 'view'; - public const WITH = 'with'; - public const WITH_GRANT_OPTION = 'with grant option'; + public const ACCESS_METHOD = 'access method'; + public const ADD = 'add'; + public const AGGREGATE = 'aggregate'; + public const ALL = 'all'; + public const ALLOW_CONNECTIONS = 'allow_connections'; + public const AS = 'as'; + public const ASCEND = 'asc'; + public const AUTOSUMMARIZE = 'autosummarize'; + public const AUTOVACUUM_ANALYZE_SCALE_FACTOR = 'autovacuum_analyze_scale_factor'; + public const AUTOVACUUM_ANALYZE_THRESHOLD = 'autovacuum_analyze_threshold'; + public const AUTOVACUUM_COST_DELAY = 'autovacuum_cost_delay'; + public const AUTOVACUUM_COST_LIMIT = 'autovacuum_cost_limit'; + public const AUTOVACUUM_ENABLED = 'autovacuum_enabled'; + public const AUTOVACUUM_FREEZE_MIN_AGE = 'autovacuum_freeze_min_age'; + public const AUTOVACUUM_FREEZE_MAX_AGE = 'autovacuum_freeze_max_age'; + public const AUTOVACUUM_FREEZE_TABLE_AGE = 'autovacuum_freeze_table_age'; + public const AUTOVACUUM_MULTIXACT_FREEZE_MIN_AGE = 'autovacuum_multixact_freeze_min_age'; + public const AUTOVACUUM_MULTIXACT_FREEZE_TABLE_AGE = 'autovacuum_multixact_freeze_table_age'; + public const AUTOVACUUM_SCALE_FACTOR = 'autovacuum_scale_factor'; + public const AUTOVACUUM_VACUUM_THRESHOLD = 'autovacuum_vacuum_threshold'; + public const BUFFERING = 'buffering'; + public const CALLED_ON_NULL_INPUT = 'called on null input'; + public const CASCADE = 'cascade'; + public const CAST = 'cast'; + public const COLLATION = 'collation'; + public const CONNECTION_LIMIT = 'connection limit'; + public const CONVERSION = 'conversion'; + public const COST = 'cost'; + public const CREATE = 'create'; + public const DEFAULT = 'default'; + public const DELETE = 'delete'; + public const DEPENDS_ON_EXTENSION = 'depends on extension'; + public const DESCEND = 'desc'; + public const DISABLE_TRIGGER = 'disable trigger'; + public const DOMAIN = 'domain'; + public const DROP = 'drop'; + public const DROP_CONSTRAINT = 'drop constraint'; + public const DROP_DEFAULT = 'drop default'; + public const ENABLE_ALWAYS_TRIGGER = 'enable always trigger'; + public const ENABLE_REPLICA_TRIGGER = 'enable replica trigger'; + public const ENABLE_TRIGGER = 'enable trigger'; + public const EVENT_TRIGGER = 'event trigger'; + public const EXECUTE = 'execute'; + public const EXTERNAL = 'external'; + public const FALSE = 'false'; + public const FAST_UPDATE = 'fastupdate'; + public const FILL_FACTOR = 'fillfactor'; + public const FOREIGN_DATA_WRAPPER = 'foreign data wrapper'; + public const FOREIGN_TABLE = 'foreign table'; + public const FOR = 'for'; + public const FOR_ROLE = 'for role'; + public const FROM_CURRENT = 'from current'; + public const FUNCTION = 'function'; + public const GRANT = 'grant'; + public const GRANT_OPTION_FOR = 'grant option for'; + public const GROUP = 'group'; + public const GROUP_BY = 'group by'; + public const GIN_PENDING_LIST_LIMIT = 'gin_pending_list_limit'; + public const HANDLER = 'handler'; + public const IF_EXISTS = 'if exists'; + public const IMMUTABLE = 'immutable'; + public const IN = 'in'; + public const INOUT = 'inout'; + public const IN_SCHEMA = 'in schema'; + public const INHERIT = 'inherit'; + public const INSERT = 'insert'; + public const IS_TEMPLATE = 'is_template'; + public const LANGUAGE = 'language'; + public const LEAKPROOF = 'leakproof'; + public const LOG_AUTOVACUUM_MIN_DURATION = 'log_autovacuum_min_duration'; + public const MATERIALIZED_VIEW = 'materialized view'; + public const NO_HANDLER = 'no handler'; + public const NO_INHERIT = 'no inherit'; + public const NO_VALIDATOR = 'no validator'; + public const NO_WAIT = 'no wait'; + public const NOT_LEAKPROOF = 'not leakproof'; + public const NOT_NULL = 'not null'; + public const NOT_VALID = 'not valid'; + public const ON_FUNCTIONS = 'on functons'; + public const ON_SCHEMAS = 'on schemas'; + public const ON_SEQUENCES = 'on sequences'; + public const ON_TABLES_TO = 'on tables to'; + public const ON_TYPES = 'on types'; + public const ONLY = 'only'; + public const OPERATOR_CLASS = 'operator class'; + public const OPERATOR_FAMILY = 'operator family'; + public const OPTIONS = 'options'; + public const ORDER_BY = 'order by'; + public const OUT = 'out'; + public const OWNED_BY = 'owned by'; + public const OWNER_TO = 'owner to'; + public const PAGES_PER_RANGE = 'pages_per_range'; + public const PARALLEL = 'parallel'; + public const PARALLEL_WORKERS = 'parallel_workers'; + public const PROCEDURAL = 'procedural'; + public const PUBLIC = 'public'; + public const REFERENCES = 'references'; + public const REFRESH_VERSION = 'refresh version'; + public const RENAME_TO = 'rename to'; + public const RENAME_COLUMN = 'rename column'; + public const RENAME_CONSTRAINT = 'rename constraint'; + public const RESET = 'reset'; + public const RESET_ALL = 'reset all'; + public const RESTRICT = 'restrict'; + public const RESTRICTED = 'restricted'; + public const RETURNS_NULL_ON_NULL_INPUT = 'returns null on null input'; + public const REVOKE = 'revoke'; + public const ROLE = 'role'; + public const ROWS = 'rows'; + public const SAFE = 'safe'; + public const SCHEMA = 'schema'; + public const SECURITY_DEFINER = 'security definer'; + public const SECURITY_INVOKER = 'security invoker'; + public const SELECT = 'select'; + public const SEQUENCE = 'sequence'; + public const SERVER = 'server'; + public const SET = 'set'; + public const SET_DEFAULT = 'set default'; + public const SET_SCHEMA = 'set schema'; + public const SET_TABLESPACE = 'set tablespace'; + public const SET_WITH_OIDS = 'set with oids'; + public const SET_WITHOUT_OIDS = 'set without oids'; + public const STABLE = 'stable'; + public const STRICT = 'strict'; + public const TABLE = 'table'; + public const TEXT_SEARCH_CONFIGURATION = 'text search configuration'; + public const TEXT_SEARCH_DICTIONARY = 'text search dictionary'; + public const TEXT_SEARCH_PARSER = 'text search parser'; + public const TEXT_SEARCH_TEMPLATE = 'text search template'; + public const TRANSFORM_FOR = 'transform for'; + public const TRIGGER = 'trigger'; + public const TRUE = 'true'; + public const TRUNCATE = 'truncate'; + public const TYPE = 'type'; + public const TO = 'to'; + public const UNSAFE = 'unsafe'; + public const UPDATE = 'update'; + public const USAGE = 'usage'; + public const USER = 'user'; + public const USER_CATALOG_TABLE = 'user_catalog_table'; + public const USER_CURRENT = 'current_user'; + public const USER_SESSION = 'session_user'; + public const USING = 'using'; + public const VALIDATOR = 'validator'; + public const VALIDATE_CONSTRAINT = 'validate constraint'; + public const VARIADIC = 'variadic'; + public const VOLATILE = 'volatile'; + public const VIEW = 'view'; + public const WITH = 'with'; + public const WITH_GRANT_OPTION = 'with grant option'; } diff --git a/common/database/enumerations/database_storage_parameter.php b/common/database/enumerations/database_storage_parameter.php new file mode 100644 index 0000000..3ff59a9 --- /dev/null +++ b/common/database/enumerations/database_storage_parameter.php @@ -0,0 +1,36 @@ +no_wait = NULL; + return new c_base_return_true(); + } + + if (!is_bool($no_wait)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'no_wait', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->no_wait = $no_wait; + return new c_base_return_true(); + } + + /** + * Get the currently assigned no wait value. + * + * @return c_base_return_bool|c_base_return_null + * TRUE for NOWAIT on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_no_wait() { + if (is_null($this->no_wait)) { + return new c_base_return_null(); + } + + if (is_bool($this->no_wait)) { + return c_base_return_bool::s_new($this->no_wait); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'no_wait', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Perform the common build process for this trait. + * + * As an internal trait method, the caller is expected to perform any appropriate validation. + * + * @return string|null + * A string is returned on success. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_no_wait() { + if (is_null($this->no_wait)) { + return NULL; + } + + return $this->no_wait ? c_database_string::NO_WAIT : NULL; + } +} diff --git a/common/database/traits/database_owned_by.php b/common/database/traits/database_owned_by.php new file mode 100644 index 0000000..196a953 --- /dev/null +++ b/common/database/traits/database_owned_by.php @@ -0,0 +1,119 @@ +owned_by = NULL; + return new c_base_return_true(); + } + + if (is_int($owned_by)) { + // no reason to add any owned_by once ALL is present. + if ($this->owned_by === e_database_owned_by::ALL) { + return new c_base_return_true(); + } + + if ($owned_by === e_database_owned_by::ALL) { + $this->owned_by = e_database_owned_by::ALL; + } + else { + if (!is_array($this->owned_by)) { + $this->owned_by = []; + } + + $this->owned_by[] = $owned_by; + } + + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'owned_by', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Get the owned_by. + * + * @param int|null $index + * (optional) Get the owned_by at the specified index. + * When NULL, all owned_by are returned. + * + * @return c_base_return_int|c_base_return_array|c_base_return_null + * An array of owned_by or NULL if not defined. + * A single owned_by is returned if $index is an integer. + * NULL with the error bit set is returned on error. + */ + public function get_owned_by($index = NULL) { + if (is_null($this->owned_by)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if ($this->owned_by === e_database_owned_by::ALL) { + return c_base_return_array::s_new([$this->owned_by]); + } + else if (is_array($this->owned_by)) { + return c_base_return_array::s_new($this->owned_by); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->owned_by) && is_int($this->owned_by[$index])) { + return clone($this->owned_by[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'owned_by[index]', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'owned_by', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Perform the common build process for this trait. + * + * As an internal trait method, the caller is expected to perform any appropriate validation. + * + * @return string|null + * A string is returned on success. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_owned_by() { + if (is_null($this->owned_by)) { + return NULL; + } + + return c_database_string::OWNED_BY . ' ' . implode(', ', $this->owned_by); + } +} diff --git a/common/database/traits/database_reset_storage_parameter.php b/common/database/traits/database_reset_storage_parameter.php new file mode 100644 index 0000000..660c855 --- /dev/null +++ b/common/database/traits/database_reset_storage_parameter.php @@ -0,0 +1,138 @@ +reset_storage_parameter = NULL; + return new c_base_return_true(); + } + + switch ($storage_parameter) { + case e_database_storage_parameter::AUTOSUMMARIZE: + case e_database_storage_parameter::BUFFERING: + case e_database_storage_parameter::FAST_UPDATE: + case e_database_storage_parameter::FILL_FACTOR: + case e_database_storage_parameter::GIN_PENDING_LIST_LIMIT: + case e_database_storage_parameter::PAGES_PER_RANGE: + break; + default: + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'storage_parameter', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_array($this->reset_storage_parameter)) { + $this->reset_storage_parameter = []; + } + + $this->reset_storage_parameter[] = $storage_parameter; + return new c_base_return_true(); + } + + /** + * Get the currently assigned RESET storage parameter at the specified index. + * + * @param int|null $index + * (optional) Get the index storage parameter type at the specified index. + * When NULL, all index storage parameter types are returned. + * + * @return c_base_return_array|c_base_return_int|c_base_return_null + * A code or an array of codes representing the argument_type on success. + * NULL is returned if not set (reset_storage_parameter tablespace is not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_reset_storage_parameter($index = NULL) { + if (is_null($this->reset_storage_parameter)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if (is_array($this->reset_storage_parameter)) { + return c_base_return_array::s_new($this->reset_storage_parameter); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->reset_storage_parameter) && is_int($this->reset_storage_parameter[$index])) { + return c_base_return_int::s_new($this->reset_storage_parameter[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'reset_storage_parameter[index]', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'reset_storage_parameter', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Perform the common build process for this trait. + * + * As an internal trait method, the caller is expected to perform any appropriate validation. + * + * @return string|null + * A string is returned on success. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_reset_storage_parameter() { + if (is_null($this->reset_storage_parameter)) { + return NULL; + } + + $values = []; + foreach ($this->reset_storage_parameter as $storage_parameter) { + if ($storage_parameter === e_database_storage_parameter::AUTOSUMMARIZE) { + $values[] = c_database_string::AUTOSUMMARIZE; + } + else if ($storage_parameter === e_database_storage_parameter::BUFFERING) { + $values[] = c_database_string::BUFFERING; + } + else if ($storage_parameter === e_database_storage_parameter::FAST_UPDATE) { + $values[] = c_database_string::FAST_UPDATE; + } + else if ($storage_parameter === e_database_storage_parameter::FILL_FACTOR) { + $values[] = c_database_string::FILL_FACTOR; + } + else if ($storage_parameter === e_database_storage_parameter::GIN_PENDING_LIST_LIMIT) { + $values[] = c_database_string::GIN_PENDING_LIST_LIMIT; + } + else if ($storage_parameter === e_database_storage_parameter::PAGES_PER_RANGE) { + $values[] = c_database_string::PAGES_PER_RANGE; + } + } + unset($storage_parameter); + + return c_database_string::RESET . ' ' . implode(', ', $values); + } +} diff --git a/common/database/traits/database_set.php b/common/database/traits/database_set.php index ec8377b..dbdf4bf 100644 --- a/common/database/traits/database_set.php +++ b/common/database/traits/database_set.php @@ -3,7 +3,7 @@ * @file * Provides traits for specific Postgesql Queries. * - * These traits are associated with set/reset. + * These traits are associated with set. * * @see: https://www.postgresql.org/docs/current/static/sql-commands.html */ @@ -33,7 +33,7 @@ trait t_database_set { * (optional) When non-NULL this is the configuration parameter. * When NULL, DEFAULT is used if applicablem otherwise this is ignored. * @param string|null $value - * (optional) When non-NULL this is the value. + * (optional) When non-NULL this is the value associated with the parameter. * When NULL, this is ignored. * * @return c_base_return_status @@ -51,13 +51,13 @@ trait t_database_set { return c_base_return_error::s_false($error); } - if ($set == e_database_set::TO || $set == e_database_set::EQUAL) { + if ($set === e_database_set::TO || $set === e_database_set::EQUAL) { if (!is_null($parameter) || !is_string($parameter)) { $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'parameter', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } - if (!is_null($value) || !is_string($value)) { + if (!is_null($value) && !is_string($value)) { $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'value', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } diff --git a/common/database/traits/database_set_storage_parameter.php b/common/database/traits/database_set_storage_parameter.php new file mode 100644 index 0000000..8679990 --- /dev/null +++ b/common/database/traits/database_set_storage_parameter.php @@ -0,0 +1,151 @@ +set_storage_parameter = NULL; + return new c_base_return_true(); + } + + switch ($storage_parameter) { + case e_database_storage_parameter::AUTOSUMMARIZE: + case e_database_storage_parameter::BUFFERING: + case e_database_storage_parameter::FAST_UPDATE: + case e_database_storage_parameter::FILL_FACTOR: + case e_database_storage_parameter::GIN_PENDING_LIST_LIMIT: + case e_database_storage_parameter::PAGES_PER_RANGE: + break; + default: + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'storage_parameter', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_string($value)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'value', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_array($this->set_storage_parameter)) { + $this->set_storage_parameter = []; + } + + $this->set_storage_parameter[] = [ + 'type' => $storage_parameter, + 'value' => $value, + ]; + return new c_base_return_true(); + } + + /** + * Get the currently assigned SET index storage parameter at the specified index. + * + * @param int|null $index + * (optional) Get the index storage parameter type at the specified index. + * When NULL, all index storage parameter types are returned. + * + * @return c_base_return_array|c_base_return_null + * An array containing the set index storage parameter settings on success. + * NULL is returned if not set (set index storage parameter not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_set_storage_parameter($index = NULL) { + if (is_null($this->set_storage_parameter)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if (is_array($this->set_storage_parameter)) { + return c_base_return_array::s_new($this->set_storage_parameter); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->set_storage_parameter) && is_int($this->set_storage_parameter[$index])) { + return c_base_return_array::s_new($this->set_storage_parameter[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'set_storage_parameter[index]', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'set_storage_parameter', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Perform the common build process for this trait. + * + * As an internal trait method, the caller is expected to perform any appropriate validation. + * + * @return string|null + * A string is returned on success. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_set_storage_parameter() { + if (is_null($this->set_storage_parameter)) { + return NULL; + } + + $values = []; + foreach ($this->set_storage_parameter as $storage_parameter => $value) { + if ($storage_parameter === e_database_storage_parameter::AUTOSUMMARIZE) { + $values[] = c_database_string::AUTOSUMMARIZE . ' = ' . $value; + } + else if ($storage_parameter === e_database_storage_parameter::BUFFERING) { + $values[] = c_database_string::BUFFERING . ' = ' . $value; + } + else if ($storage_parameter === e_database_storage_parameter::FAST_UPDATE) { + $values[] = c_database_string::FAST_UPDATE . ' = ' . $value; + } + else if ($storage_parameter === e_database_storage_parameter::FILL_FACTOR) { + $values[] = c_database_string::FILL_FACTOR . ' = ' . $value; + } + else if ($storage_parameter === e_database_storage_parameter::GIN_PENDING_LIST_LIMIT) { + $values[] = c_database_string::GIN_PENDING_LIST_LIMIT . ' = ' . $value; + } + else if ($storage_parameter === e_database_storage_parameter::PAGES_PER_RANGE) { + $values[] = c_database_string::PAGES_PER_RANGE . ' = ' . $value ; + } + } + unset($storage_parameter); + unset($value); + + return c_database_string::SET . ' ' . implode(', ', $values); + } +} diff --git a/common/database/traits/database_set_tablespace.php b/common/database/traits/database_set_tablespace.php index 7246573..ff8dfbc 100644 --- a/common/database/traits/database_set_tablespace.php +++ b/common/database/traits/database_set_tablespace.php @@ -76,6 +76,6 @@ trait t_database_set_tablespace { return NULL; } - return c_database_string::SET_TABLESPACE . ' (' . $this->set_tablespace . ')'; + return c_database_string::SET_TABLESPACE . ' ' . $this->set_tablespace; } } -- 1.8.3.1