From 51364b6f76753a079fc46546697d9280d786b70d Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Sat, 2 Feb 2019 19:40:45 -0600 Subject: [PATCH] Progress: continue development on database abstraction --- .../database/classes/database_alter_tablespace.php | 42 +++++- .../database_alter_text_search_configuration.php | 77 +++++++++- .../database_alter_text_search_dictionary.php | 48 ++++++- .../classes/database_alter_text_search_parser.php | 33 ++++- .../database_alter_text_search_template.php | 34 ++++- common/database/classes/database_alter_trigger.php | 34 ++++- common/database/classes/database_alter_type.php | 68 ++++++++- common/database/classes/database_string.php | 11 +- .../database/enumerations/database_mapping_for.php | 19 +++ common/database/enumerations/database_position.php | 17 +++ .../traits/database_action_mapping_for.php | 160 +++++++++++++++++++++ common/database/traits/database_action_not_of.php | 2 +- common/database/traits/database_add_table.php | 4 - common/database/traits/database_add_value.php | 153 ++++++++++++++++++++ common/database/traits/database_set_schema.php | 17 ++- .../database/traits/database_with_dictionary.php | 87 +++++++++++ 16 files changed, 767 insertions(+), 39 deletions(-) create mode 100644 common/database/enumerations/database_mapping_for.php create mode 100644 common/database/enumerations/database_position.php create mode 100644 common/database/traits/database_action_mapping_for.php create mode 100644 common/database/traits/database_add_value.php create mode 100644 common/database/traits/database_with_dictionary.php diff --git a/common/database/classes/database_alter_tablespace.php b/common/database/classes/database_alter_tablespace.php index bf3857e..240bf35 100644 --- a/common/database/classes/database_alter_tablespace.php +++ b/common/database/classes/database_alter_tablespace.php @@ -10,14 +10,26 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_owner_to.php'); +require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_reset.php'); +require_once('common/database/traits/database_set.php'); + /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql ALTER TABLESPACE query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertablespace.html */ class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; + use t_database_name; + use t_database_owner_to; + use t_database_rename_to; + use t_database_reset; + use t_database_set; + + protected const p_QUERY_COMMAND = 'alter tablespace'; /** @@ -25,6 +37,12 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->name = NULL; + $this->owner_to = NULL; + $this->rename_to = NULL; + $this->reset = NULL; + $this->set = NULL; } /** @@ -32,6 +50,12 @@ class c_database_alter_coalation extends c_database_query { */ public function __destruct() { parent::__destruct(); + + unset($this->name); + unset($this->owner_to); + unset($this->rename_to); + unset($this->reset); + unset($this->set); } /** @@ -64,6 +88,18 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->owner_to)) { + $value .= ' ' . $this->p_do_build_owner_to(); + } + else if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (isset($this->reset)) { + $value .= ' ' . $this->p_do_build_reset(); + } + else if (isset($this->set)) { + $value .= ' ' . $this->p_do_build_set(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_alter_text_search_configuration.php b/common/database/classes/database_alter_text_search_configuration.php index bf3857e..d08a896 100644 --- a/common/database/classes/database_alter_text_search_configuration.php +++ b/common/database/classes/database_alter_text_search_configuration.php @@ -10,14 +10,30 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/enumerations/database_mapping_for.php'); + +require_once('common/database/traits/database_mapping_for.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_owner_to.php'); +require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_set_schema.php'); +require_once('common/database/traits/database_with_dictionary.php'); + /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql TEXT SEARCH CONFIGURATION query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertextsearchconfiguration.html */ -class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; +class c_database_alter_text_search_configuration extends c_database_query { + use t_database_mapping_for; + use t_database_name; + use t_database_owner_to; + use t_database_rename_to; + use t_database_set_schema; + use t_database_with_dictionary; + + protected const p_QUERY_COMMAND = 'alter text search configuration'; /** @@ -25,12 +41,26 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->mapping_for = NULL; + $this->name = NULL; + $this->owner_to = NULL; + $this->rename_to = NULL; + $this->set_schema = NULL; + $this->with_dictionary = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->mapping_for); + unset($this->name); + unset($this->owner_to); + unset($this->rename_to); + unset($this->set_schema); + unset($this->with_dictionary); + parent::__destruct(); } @@ -64,6 +94,45 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->owner_to)) { + $value .= ' ' . $this->p_do_build_owner_to(); + } + else if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (isset($this->set_schema)) { + $value .= ' ' . $this->p_do_build_set_schema(); + } + else if (isset($this->mapping_for['type'])) { + $value .= ' ' . $this->p_do_build_mapping_for(); + + if ($this->mapping_for['type'] === e_database_mapping_for::REPLACE) { + if (isset($this->with_dictionary) && count($this->with_dictionary) == 1) { + // when mapping is REPLACE, there should only be a single with_dictionary entry. + $value .= ' ' . $this->p_do_build_with_dictionary(); + } + else { + unset($value); + return new c_base_return_false(); + } + } + else if ($this->mapping_for['type'] !== e_database_mapping_for::DROP) { + if (isset($this->with_dictionary)) { + $value .= ' ' . $this->p_do_build_with_dictionary(); + } + else { + unset($value); + return new c_base_return_false(); + } + } + } + else if (isset($this->mapping_replace)) { + $value .= ' ' . $this->p_do_build_mapping_replace(); + } + else { + unset($value); + return new c_base_return_false(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_alter_text_search_dictionary.php b/common/database/classes/database_alter_text_search_dictionary.php index bf3857e..cf37e69 100644 --- a/common/database/classes/database_alter_text_search_dictionary.php +++ b/common/database/classes/database_alter_text_search_dictionary.php @@ -10,14 +10,26 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_options.php'); +require_once('common/database/traits/database_owner_to.php'); +require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_set_schema.php'); + /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql ALTER TEXT SEARCH DICTIONARY query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertextsearchdictionary.html */ -class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; +class c_database_alter_text_search_dictionary extends c_database_query { + use t_database_name; + use t_database_options; + use t_database_owner_to; + use t_database_rename_to; + use t_database_set_schema; + + protected const p_QUERY_COMMAND = 'alter text search dictionary'; /** @@ -25,12 +37,24 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->name = NULL; + $this->options = NULL; + $this->owner_to = NULL; + $this->rename_to = NULL; + $this->set_schema = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->name); + unset($this->options); + unset($this->owner_to); + unset($this->rename_to); + unset($this->set_schema); + parent::__destruct(); } @@ -64,6 +88,22 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->options)) { + $value .= ' ' . $this->p_do_build_options(); + } + else if (isset($this->owner_to)) { + $value .= ' ' . $this->p_do_build_owner_to(); + } + else if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (isset($this->set_schema)) { + $value .= ' ' . $this->p_do_build_set_schema(); + } + else { + unset($value); + return new c_base_return_false(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_alter_text_search_parser.php b/common/database/classes/database_alter_text_search_parser.php index bf3857e..a639363 100644 --- a/common/database/classes/database_alter_text_search_parser.php +++ b/common/database/classes/database_alter_text_search_parser.php @@ -10,14 +10,21 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_set_schema.php'); /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql ALTER TEXT SEARCH PARSER query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertextsearchparser.html */ -class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; +class c_database_alter_text_search_parser extends c_database_query { + use t_database_name; + use t_database_rename_to; + use t_database_set_schema; + + protected const p_QUERY_COMMAND = 'alter text search parser'; /** @@ -25,12 +32,20 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->name = NULL; + $this->rename_to = NULL; + $this->set_schema = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->name); + unset($this->rename_to); + unset($this->set_schema); + parent::__destruct(); } @@ -64,6 +79,16 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (isset($this->set_schema)) { + $value .= ' ' . $this->p_do_build_set_schema(); + } + else { + unset($value); + return new c_base_return_false(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_alter_text_search_template.php b/common/database/classes/database_alter_text_search_template.php index bf3857e..91fc618 100644 --- a/common/database/classes/database_alter_text_search_template.php +++ b/common/database/classes/database_alter_text_search_template.php @@ -10,14 +10,22 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_set_schema.php'); + /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql ALTER TEXT SEARCH TEMPLATE query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertextsearchtemplate.html */ -class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; +class c_database_alter_text_search_template extends c_database_query { + use t_database_name; + use t_database_rename_to; + use t_database_set_schema; + + protected const p_QUERY_COMMAND = 'alter text search template'; /** @@ -25,12 +33,20 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->name = NULL; + $this->rename_to = NULL; + $this->set_schema = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->name); + unset($this->rename_to); + unset($this->set_schema); + parent::__destruct(); } @@ -64,6 +80,16 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (isset($this->set_schema)) { + $value .= ' ' . $this->p_do_build_set_schema(); + } + else { + unset($value); + return new c_base_return_false(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_alter_trigger.php b/common/database/classes/database_alter_trigger.php index bf3857e..b087072 100644 --- a/common/database/classes/database_alter_trigger.php +++ b/common/database/classes/database_alter_trigger.php @@ -10,14 +10,22 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/traits/database_depends_on_extension.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_rename_to.php'); + /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql ALTER TRIGGER query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertrigger.html */ -class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; +class c_database_alter_trigger extends c_database_query { + use t_database_depends_on_extension; + use t_database_name; + use t_database_rename_to; + + protected const p_QUERY_COMMAND = 'alter trigger'; /** @@ -25,12 +33,20 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->depends_on_extension = NULL; + $this->name = NULL; + $this->rename_to = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->depends_on_extension); + unset($this->name); + unset($this->rename_to); + parent::__destruct(); } @@ -64,6 +80,16 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->depends_on_extension)) { + $value .= ' ' . $this->p_do_build_depends_on_extension(); + } + else if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else { + unset($value); + return new c_base_return_false(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_alter_type.php b/common/database/classes/database_alter_type.php index bf3857e..32fe198 100644 --- a/common/database/classes/database_alter_type.php +++ b/common/database/classes/database_alter_type.php @@ -10,14 +10,31 @@ require_once('common/base/classes/base_return.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/traits/database_add_value.php'); +require_once('common/database/traits/database_name.php'); +require_once('common/database/traits/database_owner_to.php'); +require_once('common/database/traits/database_rename_attribute.php'); +require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_rename_value.php'); +require_once('common/database/traits/database_set_schema.php'); +require_once('common/database/traits/database_type_action.php'); /** - * The class for building and returning a Postgresql ALTER COALATION query string. + * The class for building and returning a Postgresql ALTER TYPE query string. * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-altertype.html */ -class c_database_alter_coalation extends c_database_query { - protected const p_QUERY_COMMAND = 'alter coalation'; +class c_database_alter_type extends c_database_query { + use t_database_add_value; + use t_database_name; + use t_database_owner_to; + use t_database_rename_attribute; + use t_database_rename_to; + use t_database_rename_value; + use t_database_set_schema; + use t_database_type_action; + + protected const p_QUERY_COMMAND = 'alter type'; /** @@ -25,12 +42,30 @@ class c_database_alter_coalation extends c_database_query { */ public function __construct() { parent::__construct(); + + $this->add_value = NULL; + $this->name = NULL; + $this->owner_to = NULL; + $this->rename_attribute = NULL; + $this->rename_to = NULL; + $this->rename_value = NULL; + $this->set_schema = NULL; + $this->type_action = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->add_value); + unset($this->name); + unset($this->owner_to); + unset($this->rename_attribute); + unset($this->rename_to); + unset($this->rename_value); + unset($this->set_schema); + unset($this->type_action); + parent::__destruct(); } @@ -64,6 +99,31 @@ class c_database_alter_coalation extends c_database_query { } $value = $this->p_do_build_name(); + if (isset($this->add_value)) { + $value .= ' ' . $this->p_do_build_add_value(); + } + else if (isset($this->owner_to)) { + $value .= ' ' . $this->p_do_build_owner_to(); + } + else if (isset($this->rename_attribute)) { + $value .= ' ' . $this->p_do_build_rename_attribute(); + } + else if (isset($this->rename_to)) { + $value .= ' ' . $this->p_do_build_rename_to(); + } + else if (isset($this->rename_value)) { + $value .= ' ' . $this->p_do_build_rename_value(); + } + else if (isset($this->set_schema)) { + $value .= ' ' . $this->p_do_build_set_schema(); + } + else if (isset($this->type_action)) { + $value .= ' ' . $this->p_do_build_type_action(); + } + else { + unset($value); + return new c_base_return_false(); + } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $value; diff --git a/common/database/classes/database_string.php b/common/database/classes/database_string.php index 0cf7893..b3359eb 100644 --- a/common/database/classes/database_string.php +++ b/common/database/classes/database_string.php @@ -15,6 +15,8 @@ class c_database_string { public const ADD = 'add'; public const ADD_COLUMN = 'add column'; public const ADD_TABLE = 'add table'; + public const ADD_VALUE = 'add value'; + public const AFTER = 'after'; public const AGGREGATE = 'aggregate'; public const ALL = 'all'; public const ALLOW_CONNECTIONS = 'allow_connections'; @@ -36,8 +38,9 @@ class c_database_string { 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 BYPASSRLS = 'bypassrls'; + public const BEFORE = 'before'; public const BUFFERING = 'buffering'; + public const BYPASSRLS = 'bypassrls'; public const CACHE = 'cache'; public const CALLED_ON_NULL_INPUT = 'called on null input'; public const CASCADE = 'cascade'; @@ -70,6 +73,7 @@ class c_database_string { public const DROP = 'drop'; public const DROP_CONSTRAINT = 'drop constraint'; public const DROP_DEFAULT = 'drop default'; + public const DROP_MAPPING = 'drop mapping'; public const DROP_TABLE = 'drop table'; public const ENABLE = 'enable'; public const ENABLED = 'enabled'; @@ -103,6 +107,7 @@ class c_database_string { public const GIN_PENDING_LIST_LIMIT = 'gin_pending_list_limit'; public const HANDLER = 'handler'; public const IF_EXISTS = 'if exists'; + public const IF_NOT_EXISTS = 'if not exists'; public const IMMUTABLE = 'immutable'; public const IN = 'in'; public const IN_DATABASE = 'in database'; @@ -118,6 +123,9 @@ class c_database_string { public const LOGIN = 'login'; public const LOG_AUTOVACUUM_MIN_DURATION = 'log_autovacuum_min_duration'; public const MAIN = 'main'; + public const MAPPING = 'mapping'; + public const MAPPING_FOR = 'mapping for'; + public const MAPPING_REPLACE = 'mapping replace'; public const MATERIALIZED_VIEW = 'materialized view'; public const MAXVALUE = 'maxvalue'; public const MINVALUE = 'minvalue'; @@ -241,6 +249,7 @@ class c_database_string { public const VOLATILE = 'volatile'; public const VIEW = 'view'; public const WITH = 'with'; + public const WITH_DICTIONARY = 'with dictionary'; public const WITHOUT_CLUSTER = 'without cluster'; public const WITH_CHECK = 'with check'; public const WITH_GRANT_OPTION = 'with grant option'; diff --git a/common/database/enumerations/database_mapping_for.php b/common/database/enumerations/database_mapping_for.php new file mode 100644 index 0000000..d642da7 --- /dev/null +++ b/common/database/enumerations/database_mapping_for.php @@ -0,0 +1,19 @@ +mapping_for = NULL; + return new c_base_return_true(); + } + + switch ($type) { + case e_database_mapping_for::ADD: + case e_database_mapping_for::ALTER: + case e_database_mapping_for::DROP: + case e_database_mapping_for::REPLACE: + $this->mapping_for['type'] = $type; + break; + case NULL: + break; + default: + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'type', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_null($token) && !is_string($token)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'token', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_array($this->mapping_for)) { + $this->mapping_for = [ + 'type' => NULL, + 'values' => [], + 'if_exists' => NULL, + ]; + } + + if (is_int($type)) { + $this->mapping_for['type'] = $type; + } + + if (is_string($token)) { + $placeholder = $this->add_placeholder($token); + if ($placeholder->has_error()) { + return c_base_return_error::s_false($placeholder->get_error()); + } + + $this->mapping_for['values'][] = $placeholder; + unset($placeholder); + } + + if (is_bool($if_exists)) { + $this->mapping_for['if_exists'] = $if_exists; + } + + return new c_base_return_true(); + } + + /** + * Get the currently assigned settings. + * + * @return c_base_return_array|c_base_return_null + * An array containing the add table settings. + * NULL is returned if not set (add table not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_mapping_for() { + if (is_null($this->mapping_for)) { + return new c_base_return_null(); + } + + if (isset($this->mapping_for)) { + return c_base_return_array::s_new($this->mapping_for); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'mapping_for', ':{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. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_mapping_for() { + $value = NULL; + + if ($this->mapping_for['type'] === e_database_mapping_for::ADD) { + $value = c_database_string::ADD . ' ' . c_database_string::MAPPING_FOR; + } + else if ($this->mapping_for['type'] === e_database_mapping_for::ALTER) { + $value = c_database_string::ALTER . ' ' . c_database_string::MAPPING_FOR; + } + else if ($this->mapping_for['type'] === e_database_mapping_for::DROP) { + $value = c_database_string::DROP_MAPPING; + + if ($this->mapping_for['if_exists']) { + $value .= ' ' . c_database_string::IF_EXISTS; + } + + $value .= ' ' . c_database_string::FOR; + } + else if ($this->mapping_for['type'] === e_database_mapping_for::REPLACE) { + $value = c_database_string::ALTER . ' ' . c_database_string::MAPPING; + + if (isset($this->mapping_for['values'])) { + $value .= ' ' . c_database_string::FOR; + } + } + + if (isset($this->mapping_for['values'])) { + $value .= ' ' . implode(', ', $this->mapping_for['values']); + } + + if ($this->mapping_for['type'] === e_database_mapping_for::REPLACE) { + $value .= ' ' . c_database_string::REPLACE; + } + + return $value; + } +} diff --git a/common/database/traits/database_action_not_of.php b/common/database/traits/database_action_not_of.php index 0d5872c..1fa489b 100644 --- a/common/database/traits/database_action_not_of.php +++ b/common/database/traits/database_action_not_of.php @@ -59,7 +59,7 @@ trait t_database_action_not_of { } if (is_bool($this->action_not_of)) { - return c_base_return_bool:s_new($this->action_not_of); + return c_base_return_bool::s_new($this->action_not_of); } $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'action_not_of', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); diff --git a/common/database/traits/database_add_table.php b/common/database/traits/database_add_table.php index a84b2be..2574a7b 100644 --- a/common/database/traits/database_add_table.php +++ b/common/database/traits/database_add_table.php @@ -78,10 +78,6 @@ trait t_database_add_table { /** * Get the currently assigned add table settings. * - * @param int|null $index - * (optional) Get the add table settings at the specified index. - * When NULL, all add table settings are returned. - * * @return c_base_return_array|c_base_return_null * An array containing the add table settings. * NULL is returned if not set (add table not to be used). diff --git a/common/database/traits/database_add_value.php b/common/database/traits/database_add_value.php new file mode 100644 index 0000000..319ab4c --- /dev/null +++ b/common/database/traits/database_add_value.php @@ -0,0 +1,153 @@ +add_value = NULL; + return new c_base_return_true(); + } + + if (!is_string($new_enum_value)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'new_enum_value', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_bool($if_not_exists)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'if_not_exists', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + switch ($position) { + case e_database_position::AFTER: + case e_database_position::BEFORE: + case NULL: + break; + default: + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'position', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_null($neighbor_enum_value) && !is_string($neighbor_enum_value)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'neighbor_enum_value', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $placeholder_new_enum_value = $this->add_placeholder($new_enum_value); + if ($placeholder_new_enum_value->has_error()) { + return c_base_return_error::s_false($placeholder_new_enum_value->get_error()); + } + + $placeholder_neighbor_enum_value = NULL; + if (is_string($neighbor_enum_value)) { + unset($placeholder_new_enum_value); + $placeholder_neighbor_enum_value = $this->add_placeholder($neighbor_enum_value); + if ($placeholder_neighbor_enum_value->has_error()) { + return c_base_return_error::s_false($placeholder_neighbor_enum_value->get_error()); + } + } + + $this->add_value = [ + 'new_enum_value' => $placeholder_new_enum_value, + 'if_not_exists' => $if_not_exists, + 'position' => $position, + 'neighbor_enum_value' => $placeholder_neighbor_enum_value, + ]; + unset($placeholder_new_enum_value); + unset($placeholder_neighbor_enum_value); + + return new c_base_return_true(); + } + + /** + * Get the currently assigned settings. + * + * @return c_base_return_array|c_base_return_null + * An array containing the settings. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_add_value() { + if (is_null($this->add_value)) { + return new c_base_return_null(); + } + + if (is_array($this->add_value)) { + return c_base_return_array::s_new($this->add_value); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'add_value', ':{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. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_add_value() { + $value = c_database_string::ADD_VALUE; + $value .= ' \'' . $this->add_value['new_enum_value'] . '\''; + + if ($this->add_value['if_not_exists']) { + $value .= ' ' . c_database_string::IF_NOT_EXISTS; + } + + if (is_int($this->add_value['position'])) { + if ($this->add_value['position'] === e_database_position::AFTER) { + $value .= ' ' . c_database_string::AFTER; + } + else if ($this->add_value['position'] === e_database_position::BEFORE) { + $value .= ' ' . c_database_string::BEFORE; + } + + $value .= ' \'' . $this->add_value['neighbor_enum_value'] . '\''; + } + + return $value; + } +} diff --git a/common/database/traits/database_set_schema.php b/common/database/traits/database_set_schema.php index 05a597c..3fffb66 100644 --- a/common/database/traits/database_set_schema.php +++ b/common/database/traits/database_set_schema.php @@ -19,9 +19,9 @@ trait t_database_set_schema { protected $set_schema; /** - * Set the RENAME TO settings. + * Set the SET SCHEMA settings. * - * @param string|null $set_schema + * @param string|null $name * The schema name. * Set to NULL to disable. * @@ -29,13 +29,18 @@ trait t_database_set_schema { * TRUE on success, FALSE otherwise. * FALSE with the error bit set is returned on error. */ - public function set_set_schema($set_schema) { - if (!is_null($set_schema) && !is_string($set_schema)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'set_schema', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + public function set_set_schema($name) { + if (is_null($name)) { + $this->set_schema = NULL; + return new c_base_return_true(); + } + + if (!is_null($name) && !is_string($name)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); return c_base_return_error::s_false($error); } - $placeholder = $this->add_placeholder($set_schema); + $placeholder = $this->add_placeholder($name); if ($placeholder->has_error()) { return c_base_return_error::s_false($placeholder->get_error()); } diff --git a/common/database/traits/database_with_dictionary.php b/common/database/traits/database_with_dictionary.php new file mode 100644 index 0000000..f4356dd --- /dev/null +++ b/common/database/traits/database_with_dictionary.php @@ -0,0 +1,87 @@ +with_dictionary = NULL; + return new c_base_return_true(); + } + + if (!is_string($name)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $placeholder = $this->add_placeholder($name); + if ($placeholder->has_error()) { + return c_base_return_error::s_false($placeholder->get_error()); + } + + $this->with_dictionary = $placeholder; + unset($placeholder); + + return new c_base_return_true(); + } + + /** + * Get the currently assigned dictionary name to set to. + * + * @return i_database_query_placeholder|c_base_return_null + * A dictionary name query placeholder on success. + * NULL is returned if not set (set schema is not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_with_dictionary() { + if (is_null($this->with_dictionary)) { + return new c_base_return_null(); + } + + if (isset($this->with_dictionary)) { + return clone($this->with_dictionary); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'with_dictionary', ':{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. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_with_dictionary() { + return c_database_string::WITH_DICTIONARY . ' ' . $this->with_dictionary; + } +} -- 1.8.3.1