From: Kevin Day Date: Mon, 3 Dec 2018 03:41:07 +0000 (-0600) Subject: Update: rename 'database_option' to 'database_cascade', and cleanup relating code X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=2a62e5e1f347816059f7c2bfa785b31c7fe5925f;p=koopa Update: rename 'database_option' to 'database_cascade', and cleanup relating code The use of 'option' is not quite accurate, so I renamed it to the more aptly named 'cascade'. The constraint code that conditionally utilized cascade has been rewritten to be stored in a single variable (as an array). There is no cascade trait like there was an option trait because its use is different from case to case. --- diff --git a/common/database/classes/database_alter_default_privileges.php b/common/database/classes/database_alter_default_privileges.php index ac6c43f..35a9808 100644 --- a/common/database/classes/database_alter_default_privileges.php +++ b/common/database/classes/database_alter_default_privileges.php @@ -9,15 +9,16 @@ require_once('common/base/classes/base_error.php'); require_once('common/base/classes/base_return.php'); require_once('common/database/enumerations/database_action.php'); +require_once('common/database/enumerations/database_cascade.php'); require_once('common/database/enumerations/database_on.php'); -require_once('common/database/enumerations/database_option.php'); require_once('common/database/enumerations/database_privilege.php'); require_once('common/database/classes/database_query.php'); +require_once('common/database/enumerations/database_cascade.php'); + require_once('common/database/traits/database_in_schema.php'); require_once('common/database/traits/database_action.php'); -require_once('common/database/traits/database_option.php'); /** * The class for building and returning a Postgresql ALTER DEFAULT PRIVILEGES query string. @@ -27,11 +28,11 @@ require_once('common/database/traits/database_option.php'); class c_database_alter_default_priveleges extends c_database_query { use t_database_in_schema; use t_database_action; - use t_database_option; protected const pr_QUERY_COMMAND = 'alter default privileges'; protected $abbreviated; + protected $cascade; protected $option_grant; protected $on; protected $privileges; @@ -43,11 +44,11 @@ class c_database_alter_default_priveleges extends c_database_query { public function __construct() { parent::__construct(); - $this->in_schema = NULL; $this->action = NULL; - $this->option = NULL; + $this->in_schema = NULL; $this->abbreviated = NULL; + $this->cascade = NULL; $this->option_grant = NULL; $this->on = NULL; $this->privileges = NULL; @@ -60,10 +61,11 @@ class c_database_alter_default_priveleges extends c_database_query { public function __destruct() { parent::__destruct(); - unset($this->in_schema); unset($this->action); + unset($this->in_schema); unset($this->abbreviated); + unset($this->cascade); unset($this->option_grant); unset($this->privileges); unset($this->role_names); @@ -91,6 +93,36 @@ class c_database_alter_default_priveleges extends c_database_query { } /** + * Assigns the SQL CASCADE/RESTRICT option. + * + * @param int|null $cascade + * Whether or not to use CASCADE/RESTRICT in the query. + * Set to NULL to disable. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + * FALSE with error bit set is returned on error. + */ + public function set_cascade($cascade) { + if (is_null($cascade)) { + $this->cascade = NULL; + return new c_base_return_true(); + } + + switch ($cascade) { + case e_database_cascade::CASCADE: + case e_database_cascade::RESTRICT: + $this->cascade = $cascade; + return new c_base_return_true(); + default: + break; + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'cascade', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** * Set the FOR ROLE role targets. * * @param string|bool|null $target @@ -373,6 +405,22 @@ class c_database_alter_default_priveleges extends c_database_query { } /** + * Get the CASCADE/RESTRICT operation status. + * + * @return c_base_return_int|c_base_return_null + * Integer representing the on operation is returned on success. + * NULL is returned if undefined. + * FALSE with error bit set is returned on error. + */ + protected function get_cascade() { + if (is_null($this->cascade)) { + return new c_base_return_null(); + } + + return c_base_return_int::s_new($this->cascade); + } + + /** * Get the ON operation status. * * @return c_base_return_int|c_base_return_null @@ -407,11 +455,11 @@ class c_database_alter_default_priveleges extends c_database_query { if (is_null($index)) { if (is_array($this->privileges)) { - return c_base_return_array::s_new($this->aggregate_signatures); + return c_base_return_array::s_new($this->privileges); } } else { - if (is_int($index) && array_key_exists($index, $this->aggregate_signatures) && $this->aggregate_signatures[$index] instanceof c_database_argument_aggregate_signature) { + if (is_int($index) && array_key_exists($index, $this->privileges) && $this->privileges[$index] instanceof c_database_argument_aggregate_signature) { return clone($this->privileges[$index]); } @@ -557,11 +605,12 @@ class c_database_alter_default_priveleges extends c_database_query { } else if ($this->action === e_database_action::REVOKE) { // [ CASCADE | RESTRICT ] - $option = $this->p_do_build_option(); - if (is_string($option)) { - $action .= ' ' . $option; + if ($this->cascade === e_database_option::CASCADE) { + $value .= ' ' . c_database_string::CASCADE; + } + else if ($this->cascade === e_database_option::RESTRICT) { + $value .= ' ' . c_database_string::RESTRICT; } - unset($option); } $this->value = static::pr_QUERY_COMMAND; diff --git a/common/database/classes/database_alter_domain.php b/common/database/classes/database_alter_domain.php index 0e7290d..395399e 100644 --- a/common/database/classes/database_alter_domain.php +++ b/common/database/classes/database_alter_domain.php @@ -9,7 +9,7 @@ require_once('common/base/classes/base_error.php'); require_once('common/base/classes/base_return.php'); require_once('common/database/enumerations/database_action.php'); -require_once('common/database/enumerations/database_option.php'); +require_once('common/database/enumerations/database_cascade.php'); require_once('common/database/classes/database_query.php'); @@ -18,7 +18,6 @@ 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_action.php'); -require_once('common/database/traits/database_option.php'); /** * The class for building and returning a Postgresql ALTER DOMAIN query string. @@ -34,47 +33,42 @@ class c_database_alter_coalation extends c_database_query { use t_database_set_schema; use t_database_action; use t_database_action_property; - use t_database_option; protected const pr_QUERY_COMMAND = 'alter domain'; protected $expression; - protected $contraint; + protected $constraint; /** * Class constructor. */ public function __construct() { parent::__construct(); - +; + $this->action = NULL; + $this->action_property = NULL; $this->name = NULL; $this->owner_to = NULL; $this->rename_to = NULL; - $this->set_schema = NULL; - $this->action = NULL; - $this->action_property = NULL; - $this->option = NULL; + $this->set_schema = NULL - $this->expression = NULL; - $this->contraint = NULL; - $this->contraint_new = NULL; + $this->expression = NULL; + $this->constraint = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->action); + unset($this->action_property); unset($this->name); unset($this->owner_to); unset($this->rename_to); unset($this->set_schema); - unset($this->action); - unset($this->action_property); - unset($this->option); unset($this->expression); - unset($this->contraint); - unset($this->contraint_new); + unset($this->constraint); parent::__destruct(); } @@ -134,43 +128,44 @@ class c_database_alter_coalation extends c_database_query { * @param string|null $constraint * The constraint string to use. * Set to NULL to disable. - * @param string|null $constraint_new - * When the constraint + * @param string|null $new_name + * The new constraint name, when applicable. + * Set to NULL to disable. + * @param int|null $cascade + * Set eithe CASCADE/RESTRICT, when applicable. * Set to NULL to disable. * * @return c_base_return_status * TRUE on success, FALSE otherwise. * FALSE with error bit set is returned on error. */ - public function set_constraint($constraint, $constraint_new = NULL) { + public function set_constraint($constraint, $new_name = NULL, $cascade = NULL) { if (is_null($constraint)) { - if (is_null($constraint_new)) { - $this->constraint = NULL; - $this->constraint_new = NULL; - return new c_base_return_true(); - } - - if (is_string($constraint_new)) { - $this->constraint = NULL; - $this->constraint_new = $constraint_new; - return new c_base_return_true(); - } + $this->constraint = NULL; + return new c_base_return_true(); } - else if (is_string($constraint)) { - if (is_null($constraint_new)) { - $this->constraint = $constraint; - $this->constraint_new = NULL; - return new c_base_return_true(); - } - else if (is_string($constraint_new)) { - $this->constraint = $constraint; - $this->constraint_new = $constraint_new; - return new c_base_return_true(); - } + + if (!is_string($constraint)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'constraint', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); } - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'expression', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); + if (!is_null($new_name) && !is_string($new_name)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'new_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_null($cascade) && $cascade !== e_database_cascade::CASCADE && $cascade !== e_database_cascade::RESTRICT) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'cascade', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->constraint = [ + 'name' => $constraint, + 'name_new' => $new_name, + 'cascade' => $cascade, + ]; + return new c_base_return_true(); } /** @@ -202,7 +197,23 @@ class c_database_alter_coalation extends c_database_query { return new c_base_return_null(); } - return c_base_return_string::s_new($this->constraint); + return c_base_return_string::s_new($this->constraint['name']); + } + + /** + * Get constraint cascade option. + * + * @return c_base_return_int|c_base_return_null + * An integer representing the constraint cascade option. + * NULL is returned if undefined. + * FALSE with error bit set is returned on error. + */ + public function get_constraint_cascade() { + if (is_null($this->constraint) || is_null($this->constraint['cascade'])) { + return new c_base_return_null(); + } + + return c_base_return_int::s_new($this->constraint['cascade']); } /** @@ -215,12 +226,12 @@ class c_database_alter_coalation extends c_database_query { * NULL is returned if undefined. * FALSE with error bit set is returned on error. */ - public function get_constraint_new() { - if (is_null($this->constraint_new)) { + public function get_constraint_new_name() { + if (is_null($this->constraint) || is_null($this->constraint['name_new'])) { return new c_base_return_null(); } - return c_base_return_string::s_new($this->constraint_new); + return c_base_return_string::s_new($this->constraint['name_new']); } /** @@ -234,13 +245,13 @@ class c_database_alter_coalation extends c_database_query { $action = NULL; switch ($this->action) { case e_database_action::ADD: - if (!is_string($this->constraint)) { + if (!is_array($this->constraint)) { unset($action); return new c_base_return_false(); } $action = c_database_string::ADD; - $action .= ' ' . $this->constraint; + $action .= ' ' . $this->constraint['name']; if ($this->property === e_database_property::NOT_VALID) { $action .= ' ' . c_database_string::NOT_VALID; @@ -255,7 +266,7 @@ class c_database_alter_coalation extends c_database_query { break; case e_database_action::DROP_CONSTRAINT: - if (!is_string($this->constraint)) { + if (!is_array($this->constraint['name'])) { unset($action); return new c_base_return_false(); } @@ -265,13 +276,14 @@ class c_database_alter_coalation extends c_database_query { $action .= ' ' . c_database_string::IF_EXISTS; } - $action .= ' ' . $this->constraint; + $action .= ' ' . $this->constraint['name']; - $option = $this->p_do_build_option(); - if (is_string($option)) { - $action .= ' ' . $option; + if ($this->constraint['cascade'] === e_database_cascade::CASCADE) { + $action .= ' ' . c_database_string::CASCADE; + } + else if ($this->constraint['cascade'] === e_database_cascade::RESTRICT) { + $action .= ' ' . c_database_string::RESTRICT; } - unset($option); break; case e_database_action::DROP_DEFAULT: @@ -288,12 +300,12 @@ class c_database_alter_coalation extends c_database_query { break; case e_database_action::RENAME_CONSTRAINT: - if (!is_string($this->constraint) || !is_string($this->constraint_new)) { + if (!is_string($this->constraint['name']) || !is_string($this->constraint['name_new'])) { unset($action); return new c_base_return_false(); } - $action = c_database_string::RENAME_CONSTRAINT . ' ' . $this->constraint . ' ' . c_database_string::TO . ' ' . $this->constraint_new; + $action = c_database_string::RENAME_CONSTRAINT . ' ' . $this->constraint['name'] . ' ' . c_database_string::TO . ' ' . $this->constraint['name_new']; break; case e_database_action::RENAME_TO: @@ -331,12 +343,12 @@ class c_database_alter_coalation extends c_database_query { break; case e_database_action::VALIDATE_CONSTRAINT: - if (!is_string($this->constraint)) { + if (!is_array($this->constraint)) { unset($action); return new c_base_return_false(); } - $action = c_database_string::VALIDATE_CONSTRAINT . ' ' . $this->constraint; + $action = c_database_string::VALIDATE_CONSTRAINT . ' ' . $this->constraint['name']; break; default: diff --git a/common/database/enumerations/database_cascade.php b/common/database/enumerations/database_cascade.php new file mode 100644 index 0000000..6bf6190 --- /dev/null +++ b/common/database/enumerations/database_cascade.php @@ -0,0 +1,17 @@ +option = NULL; - return new c_base_return_true(); - } - - if (is_int($option)) { - $this->option = $option; - return new c_base_return_true(); - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'option', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - - /** - * Get the currently assigned option. - * - * @return c_base_return_int|c_base_return_null - * Integer representing the option is returned on success. - * NULL is returned if undefined. - * FALSE with error bit set is returned on error. - */ - protected function get_option() { - if (is_null($this->option)) { - return new c_base_return_null(); - } - - return c_base_return_int::s_new($this->option); - } - - /** - * 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_option() { - $value = NULL; - - if ($this->option === e_database_option::CASCADE) { - $value = c_database_string::CASCADE; - } - else if ($this->option === e_database_option::RESTRICT) { - $value = c_database_string::RESTRICT; - } - - return $value; - } -}