From d3b0a875207a2e172892f4f141d7a760805b9489 Mon Sep 17 00:00:00 2001 From: Kevin Day Date: Thu, 6 Dec 2018 00:08:09 -0600 Subject: [PATCH] Progress: continue development on database abstraction --- .../database/classes/database_alter_aggregate.php | 12 +- .../database/classes/database_alter_coalation.php | 16 +- .../database/classes/database_alter_conversion.php | 14 +- .../database/classes/database_alter_database.php | 20 +- .../classes/database_alter_default_privileges.php | 264 +++++---------------- .../database/classes/database_alter_extension.php | 2 +- .../database_alter_foreign_data_wrapper.php | 2 +- common/database/classes/database_string.php | 1 + common/database/traits/database_cascade.php | 96 ++++++++ common/database/traits/database_for_role.php | 105 ++++++++ .../database/traits/database_grant_option_for.php | 87 +++++++ common/database/traits/database_handler.php | 6 +- common/database/traits/database_set_with_oids.php | 3 +- .../database/traits/database_with_grant_option.php | 87 +++++++ 14 files changed, 467 insertions(+), 248 deletions(-) create mode 100644 common/database/traits/database_cascade.php create mode 100644 common/database/traits/database_for_role.php create mode 100644 common/database/traits/database_grant_option_for.php create mode 100644 common/database/traits/database_with_grant_option.php diff --git a/common/database/classes/database_alter_aggregate.php b/common/database/classes/database_alter_aggregate.php index 9f10ee8..f3d9535 100644 --- a/common/database/classes/database_alter_aggregate.php +++ b/common/database/classes/database_alter_aggregate.php @@ -323,25 +323,25 @@ class c_database_alter_aggregate extends c_database_query { $value = NULL; if (is_string($this->rename_to)) { - $action = $aggregate_signatures . ' ' . $this->p_do_build_rename_to(); + $value = $aggregate_signatures . ' ' . $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $action = $aggregate_signatures . ' ' . $this->p_do_build_owner_to(); + $value = $aggregate_signatures . ' ' . $this->p_do_build_owner_to(); } else if (is_string($this->set_schema)) { - $action = $aggregate_signatures . ' ' . $this->p_do_build_set_schema(); + $value = $aggregate_signatures . ' ' . $this->p_do_build_set_schema(); } else { unset($aggregate_signatures); - unset($action); + unset($value); return new c_base_return_false(); } unset($aggregate_signatures); $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $this->name; - $this->value .= ' ' . $action; - unset($action); + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_coalation.php b/common/database/classes/database_alter_coalation.php index 0673de4..4137abc 100644 --- a/common/database/classes/database_alter_coalation.php +++ b/common/database/classes/database_alter_coalation.php @@ -133,30 +133,30 @@ class c_database_alter_coalation extends c_database_query { return new c_base_return_false(); } - $action = NULL; + $value = NULL; if (is_bool($this->refresh_version)) { if ($this->refresh_version) { - $action = c_database_string::REFRESH_VERSION; + $value = c_database_string::REFRESH_VERSION; } } else if (is_string($this->rename_to)) { - $action = $this->p_do_build_rename_to(); + $value = $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $action = $this->p_do_build_owner_to(); + $value = $this->p_do_build_owner_to(); } else if (is_string($this->set_schema)) { - $action = $this->p_do_build_set_schema(); + $value = $this->p_do_build_set_schema(); } else { - unset($action); + unset($value); return new c_base_return_false(); } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $this->name; - $this->value .= ' ' . $action; - unset($action); + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_conversion.php b/common/database/classes/database_alter_conversion.php index 36084fd..1cb406e 100644 --- a/common/database/classes/database_alter_conversion.php +++ b/common/database/classes/database_alter_conversion.php @@ -83,25 +83,25 @@ class c_database_alter_conversion extends c_database_query { return new c_base_return_false(); } - $action = NULL; + $value = NULL; if (is_string($this->rename_to)) { - $action = $this->p_do_build_rename_to(); + $value = $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $action = $this->p_do_build_owner_to(); + $value = $this->p_do_build_owner_to(); } else if (is_string($this->set_schema)) { - $action = $this->p_do_build_set_schema(); + $value = $this->p_do_build_set_schema(); } else { - unset($action); + unset($value); return new c_base_return_false(); } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $this->name; - $this->value .= ' ' . $action; - unset($action); + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_database.php b/common/database/classes/database_alter_database.php index 4e8001b..a3f85d6 100644 --- a/common/database/classes/database_alter_database.php +++ b/common/database/classes/database_alter_database.php @@ -147,35 +147,35 @@ class c_database_alter_database extends c_database_query { return new c_base_return_false(); } - $action = NULL; + $value = NULL; if ($this->option instanceof c_database_argument_database_option) { $this->option->do_build_argument(); - $action = $this->option->get_value_exact(); + $value = $this->option->get_value_exact(); } else if (is_string($this->rename_to)) { - $action = $this->p_do_build_rename_to(); + $value = $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $action = $this->p_do_build_owner_to(); + $value = $this->p_do_build_owner_to(); } else if (is_string($this->set_tablespace)) { - $action = $this->p_do_build_set_tablespace(); + $value = $this->p_do_build_set_tablespace(); } else if (is_array($this->set)) { - $action = $this->p_do_build_set(); + $value = $this->p_do_build_set(); } else if (is_array($this->reset)) { - $action = $this->p_do_build_reset(); + $value = $this->p_do_build_reset(); } else { - unset($action); + unset($value); return new c_base_return_false(); } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $this->name; - $this->value .= ' ' . $action; - unset($action); + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_default_privileges.php b/common/database/classes/database_alter_default_privileges.php index 1d3996c..f170be5 100644 --- a/common/database/classes/database_alter_default_privileges.php +++ b/common/database/classes/database_alter_default_privileges.php @@ -17,8 +17,12 @@ 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_cascade.php'); +require_once('common/database/traits/database_for_role.php'); +require_once('common/database/traits/database_grant_option_for.php'); +require_once('common/database/traits/database_in_schema.php'); +require_once('common/database/traits/database_with_grant_option.php'); /** * The class for building and returning a Postgresql ALTER DEFAULT PRIVILEGES query string. @@ -26,14 +30,16 @@ require_once('common/database/traits/database_action.php'); * @see: https://www.postgresql.org/docs/current/static/sql-alterdefaultpriveleges.html */ class c_database_alter_default_priveleges extends c_database_query { - use t_database_in_schema; use t_database_action; + use t_database_cascade; + use t_database_for_role; + use t_database_grant_option_for; + use t_database_in_schema; + use t_database_with_grant_option; protected const p_QUERY_COMMAND = 'alter default privileges'; protected $abbreviated; - protected $cascade; - protected $option_grant; protected $on; protected $privileges; protected $role_names; @@ -44,15 +50,17 @@ class c_database_alter_default_priveleges extends c_database_query { public function __construct() { parent::__construct(); - $this->action = NULL; - $this->in_schema = NULL; - - $this->abbreviated = NULL; - $this->cascade = NULL; - $this->option_grant = NULL; - $this->on = NULL; - $this->privileges = NULL; - $this->role_names = NULL; + $this->action = NULL; + $this->cascade = NULL; + $this->for_role = NULL; + $this->grant_option_for = NULL; + $this->in_schema = NULL; + $this->with_grant_option = NULL; + + $this->abbreviated = NULL; + $this->on = NULL; + $this->privileges = NULL; + $this->role_names = NULL; } /** @@ -62,11 +70,14 @@ class c_database_alter_default_priveleges extends c_database_query { parent::__destruct(); unset($this->action); + unset($this->cascade); + unset($this->for_role); + unset($this->grant_option_for); unset($this->in_schema); + unset($this->with_grant_option); unset($this->abbreviated); - unset($this->cascade); - unset($this->option_grant); + unset($this->on); unset($this->privileges); unset($this->role_names); } @@ -93,94 +104,6 @@ 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 - * The for role target to use. - * Set to TRUE to use (only) the current role, $append is considered FALSE. - * Set to NULL to disable. - * When NULL, this will remove all values. - * - * @return c_base_return_status - * TRUE on success, FALSE otherwise. - * FALSE with error bit set is returned on error. - */ - public function set_for_role_targets($target) { - if (is_null($target)) { - $this->target = NULL; - return new c_base_return_true(); - } - - if (is_string($target)) { - if (!is_array($this->for_role_targets)) { - $this->for_role_targets = []; - } - - $this->for_role_targets[] = $target; - - return new c_base_return_true(); - } - else if ($target === TRUE) { - $this->for_role_targets = TRUE; - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'target', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - - /** - * Enables/Disables the SQL WITH GRANT OPTION. - * - * @param bool|null $option_grant - * Set to TRUE to append the with grant option or grant option for. - * Set to FALSE to not append the with grant option or the grant option for. - * 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_option_grant($option_grant) { - if (!is_null($option_grant) && !is_bool($option_grant)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'option_grant', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - - $this->option_grant = $option_grant; - return new c_base_return_true(); - } - - /** * Assigns the SQL ON operation. * * @param int|null $on @@ -311,80 +234,6 @@ class c_database_alter_default_priveleges extends c_database_query { } /** - * Get assigned option. - * - * @param int|null $index - * (optional) Get the argument signature at the specified index. - * When NULL, all argument signatures are returned. - * This is always considered NULL when for role targets is set to the current role. - * - * @return c_base_return_array|c_base_return_string|c_base_return_bool|c_base_return_null - * An array of for role targets or NULL if not defined. - * TRUE is returned if the current role is to be used as the for role target. - * A string of a single for role target is returned if the $index is not NULL. - * NULL with the error bit set is returned on error. - */ - public function get_for_role_targets($index = NULL) { - if (is_null($this->for_role_targets)) { - return new c_base_return_null(); - } - - if ($this->for_role_targets === TRUE) { - return c_base_return_bool::s_new(TRUE); - } - - if (is_null($index)) { - if (is_array($this->for_role_targets)) { - return c_base_return_array::s_new($this->for_role_targets); - } - } - else { - if (is_int($index) && array_key_exists($index, $this->for_role_targets) && is_string($this->for_role_targets[$index])) { - return c_base_return_string::s_new($this->for_role_targets[$index]); - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'for_role_targets[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}' => 'for_role_targets', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); - return c_base_return_error::s_null($error); - } - - /** - * Get the WITH GRANT OPTION status. - * - * @return c_base_return_bool|c_base_return_null - * TRUE is returned if the with grant option or grant option for is enabled. - * FALSE is returned if the with grant option or grant option for is disabled. - * NULL is returned if undefined. - * FALSE with error bit set is returned on error. - */ - protected function get_option_grant() { - if (is_null($this->option_grant)) { - return new c_base_return_null(); - } - - return c_base_return_bool::s_new($this->option_grant); - } - - /** - * 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 @@ -451,29 +300,27 @@ class c_database_alter_default_priveleges extends c_database_query { return new c_base_return_false(); } - // [ FOR ROLE target_role [, ... ] ] - $action = NULL; - if (is_array($this->for_role_targets) && !empty($this->for_role_targets)) { - $action = c_database_string::FOR . ' ' . c_database_string::ROLE; - $action .= ' ' . implode(', ', $this->for_role_targets); + $value = NULL; + if ($this->for_role) { + $value .= $this->p_do_build_for_role(); } // [ IN SCHEMA schema_name [, ... ] ] if (is_array($this->in_schema) && !empty($this->in_schema)) { - $action .= is_null($action) ? '' : ' '; - $action .= $this->p_do_build_in_schema(); + $value .= is_null($value) ? '' : ' '; + $value .= $this->p_do_build_in_schema(); } if ($this->action === e_database_action::ACTION_GRANT) { - $action .= is_null($action) ? '' : ' '; - $action .= c_database_string::GRANT; + $value .= is_null($value) ? '' : ' '; + $value .= c_database_string::GRANT; } else if ($this->action === e_database_action::ACTION_REVOKE) { - $action .= is_null($action) ? '' : ' '; - $action .= c_database_string::REVOKE; + $value .= is_null($value) ? '' : ' '; + $value .= c_database_string::REVOKE; - if ($this->option_grant) { - $action .= ' ' . c_database_string::GRANT_OPTION_FOR; + if ($this->grant_option_for) { + $value .= ' ' . $this->p_do_build_grant_option_for(); } } @@ -520,35 +367,35 @@ class c_database_alter_default_priveleges extends c_database_query { } } - $action .= is_null($action) ? '' : ' '; - $action .= ltrim($privileges, ', '); + $value .= is_null($value) ? '' : ' '; + $value .= ltrim($privileges, ', '); unset($privileges); // ON ... switch($this->on) { case e_database_on::TABLES_TO: - $action .= ' ' . c_database_string::ON_TABLES_TO; + $value .= ' ' . c_database_string::ON_TABLES_TO; break; case e_database_on::SEQUENCES: - $action .= ' ' . c_database_string::ON_SEQUENCES; + $value .= ' ' . c_database_string::ON_SEQUENCES; break; case e_database_on::FUNCTIONS: - $action .= ' ' . c_database_string::ON_FUNCTIONS; + $value .= ' ' . c_database_string::ON_FUNCTIONS; break; case e_database_on::TYPES: - $action .= ' ' . c_database_string::ON_TYPES; + $value .= ' ' . c_database_string::ON_TYPES; break; case e_database_on::SCHEMAS: - $action .= ' ' . c_database_string::ON_SCHEMAS; + $value .= ' ' . c_database_string::ON_SCHEMAS; break; } // [ TO | FROM ] ... role names ... if ($this->action === e_database_action::GRANT) { - $action .= ' ' . c_database_string::TO; + $value .= ' ' . c_database_string::TO; } else if ($this->action === e_database_action::REVOKE) { - $action .= ' ' . c_database_string::FROM; + $value .= ' ' . c_database_string::FROM; } foreach ($this->role_names as $role_name) { @@ -557,30 +404,25 @@ class c_database_alter_default_priveleges extends c_database_query { } $role_name->do_build_argument(); - $action .= ' ' . $role_name->get_value_exact(); + $value .= ' ' . $role_name->get_value_exact(); } unset($role_name); if ($this->action === e_database_action::GRANT) { - // [ WITH GRANT OPTION ] - if ($this->option_grant) { - $action .= ' ' . c_database_string::WITH_GRANT_OPTION; + if ($this->with_grant_option) { + $value .= ' ' . $this->p_do_build_with_grant_option(); } } else if ($this->action === e_database_action::REVOKE) { - // [ CASCADE | RESTRICT ] - 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; + if (is_int($this->cascade)) { + $value .= ' ' . $this->p_do_build_cascade(); } } $this->value = static::p_QUERY_COMMAND; $this->value .= ' ' . $this->name; - $this->value .= ' ' . $action; - unset($action); + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_alter_extension.php b/common/database/classes/database_alter_extension.php index 6e29771..3046ced 100644 --- a/common/database/classes/database_alter_extension.php +++ b/common/database/classes/database_alter_extension.php @@ -1,7 +1,7 @@ cascade = NULL; + return new c_base_return_true(); + } + + if ($cascade === e_database_cascade::CASCADE || $cascade === e_database_cascade::RESTRICT) { + $this->cascade = $cascade; + return new c_base_return_true(); + } + + $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); + } + + /** + * Get the currently assigned cascade. + * + * @return c_base_return_int|c_base_return_null + * An integer containing the cascade setting on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_cascade() { + if (is_null($this->cascade)) { + return new c_base_return_null(); + } + + if (is_array($this->cascade)) { + return c_base_return_array::s_new($this->cascade); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'cascade', ':{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_cascade() { + if (is_null($this->cascade)) { + return NULL; + } + + $value = NULL; + if ($this->cascade['type'] === e_database_cascade::CASCADE) { + if (isset($this->cascade['name'])) { + $value = c_database_string::CASCADE; + } + } + else if ($this->cascade['type'] === e_database_cascade::RESTRICT) { + $value .= c_database_string::RESTRICT; + } + + return $value; + } +} diff --git a/common/database/traits/database_for_role.php b/common/database/traits/database_for_role.php new file mode 100644 index 0000000..c1216dd --- /dev/null +++ b/common/database/traits/database_for_role.php @@ -0,0 +1,105 @@ +for_role = NULL; + return new c_base_return_true(); + } + + if (!is_string($for_role)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'for_role', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_array($this->for_role)) { + $this->for_role = []; + } + + $this->for_role[] = $for_role; + return new c_base_return_true(); + } + + /** + * Get the currently assigned for role value. + * + * @param int|null $index + * (optional) Get the for role at the specified index. + * When NULL, all for role values are returned. + * + * @return c_base_return_string|c_base_return_array|c_base_return_null + * An array of for roles or NULL if not defined. + * A single for role is returned if $index is an integer. + * NULL with the error bit set is returned on error. + */ + public function get_for_role($index = NULL) { + if (is_null($this->for_role)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if (is_array($this->for_role)) { + return c_base_return_array::s_new($this->for_role); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->for_role) && is_string($this->for_role[$index])) { + return c_base_return_string::s_new($this->for_role[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'for_role[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}' => 'for_role', ':{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_for_role() { + if (is_null($this->for_role)) { + return NULL; + } + + return c_database_string::FOR_ROLE . ' ' . implode(', ', $this->for_role); + } +} diff --git a/common/database/traits/database_grant_option_for.php b/common/database/traits/database_grant_option_for.php new file mode 100644 index 0000000..01c3755 --- /dev/null +++ b/common/database/traits/database_grant_option_for.php @@ -0,0 +1,87 @@ +grant_option_for = NULL; + return new c_base_return_true(); + } + + if (!is_bool($grant_option_for)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'grant_option_for', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->grant_option_for = $grant_option_for; + return new c_base_return_true(); + } + + /** + * Get the currently assigned grant option for value. + * + * @return c_base_return_bool|c_base_return_null + * TRUE for GANT OPTION FOR on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_grant_option_for() { + if (is_null($this->grant_option_for)) { + return new c_base_return_null(); + } + + if (is_bool($this->grant_option_for)) { + return c_base_return_bool::s_new($this->grant_option_for); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'grant_option_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 on success. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_grant_option_for() { + if (is_null($this->grant_option_for)) { + return NULL; + } + + return $this->grant_option_for ? c_database_string::GRANT_OPTION_FOR : NULL; + } +} diff --git a/common/database/traits/database_handler.php b/common/database/traits/database_handler.php index bfb7a29..554f0df 100644 --- a/common/database/traits/database_handler.php +++ b/common/database/traits/database_handler.php @@ -82,7 +82,7 @@ trait t_database_handler { return c_base_return_array::s_new($this->handler); } - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'handler', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); return c_base_return_error::s_null($error); } @@ -101,12 +101,12 @@ trait t_database_handler { } $value = NULL; - if ($this->handler['type'] == e_database_handler::HANDLER) { + if ($this->handler['type'] === e_database_handler::HANDLER) { if (isset($this->handler['name'])) { $value = c_database_string::HANDLER . ' ' . $this->handler['name']; } } - else if ($this->handler['type'] == e_database_handler::NO_HANDLER) { + else if ($this->handler['type'] === e_database_handler::NO_HANDLER) { $value .= c_database_string::NO_HANDLER; } diff --git a/common/database/traits/database_set_with_oids.php b/common/database/traits/database_set_with_oids.php index 225f31b..c24aa86 100644 --- a/common/database/traits/database_set_with_oids.php +++ b/common/database/traits/database_set_with_oids.php @@ -24,7 +24,8 @@ trait t_database_set_with_oids { * Set the SET WITH OIDS value. * * @param bool|null $set_with_oids - * Set to TRUE for SET WITH OIDS an FALSE for SET WITHOUT OIDS. + * Set to TRUE for SET WITH OIDS. + * Set to FALSE for SET WITHOUT OIDS. * Set to NULL to disable. * * @return c_base_return_status diff --git a/common/database/traits/database_with_grant_option.php b/common/database/traits/database_with_grant_option.php new file mode 100644 index 0000000..1e78dd1 --- /dev/null +++ b/common/database/traits/database_with_grant_option.php @@ -0,0 +1,87 @@ +with_grant_option = NULL; + return new c_base_return_true(); + } + + if (!is_bool($with_grant_option)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'with_grant_option', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->with_grant_option = $with_grant_option; + return new c_base_return_true(); + } + + /** + * Get the currently assigned with grant option value. + * + * @return c_base_return_bool|c_base_return_null + * TRUE for WITH GANT OPTION on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_with_grant_option() { + if (is_null($this->with_grant_option)) { + return new c_base_return_null(); + } + + if (is_bool($this->with_grant_option)) { + return c_base_return_bool::s_new($this->with_grant_option); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'with_grant_option', ':{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_with_grant_option() { + if (is_null($this->with_grant_option)) { + return NULL; + } + + return $this->with_grant_option ? c_database_string::WITH_GRANT_OPTION : NULL; + } +} -- 1.8.3.1