From: Kevin Day Date: Fri, 7 Dec 2018 04:57:49 +0000 (-0600) Subject: Progress: continue development on database abstraction X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=085545ff0e63c0e4802de6f397afd2be4e24384c;p=koopa Progress: continue development on database abstraction --- diff --git a/common/database/classes/database_alter_aggregate.php b/common/database/classes/database_alter_aggregate.php index f3d9535..51dbb8c 100644 --- a/common/database/classes/database_alter_aggregate.php +++ b/common/database/classes/database_alter_aggregate.php @@ -186,6 +186,10 @@ class c_database_alter_aggregate extends c_database_query { /** * Get the total aggregate signatures. * + * @param int|null $index + * (optional) Get the aggregate signature at the specified index. + * When NULL, all aggregate signatures are returned. + * * @return c_base_return_int * The total number of aggregate signatures. * 0 with the error bit set is returned on error. @@ -278,7 +282,6 @@ class c_database_alter_aggregate extends c_database_query { $aggregate_signatures = ' *'; } else { - // [ argument_mode ] [ argument_name ] argument_type [, ...] foreach ($this->aggregate_signatures as $aggregate_signature) { if ($aggregate_signature instanceof c_database_argument_aggregate_signature) { if (is_null($aggregate_signatures)) { @@ -294,7 +297,6 @@ class c_database_alter_aggregate extends c_database_query { } unset($aggregate_signature); - // ORDER BY [ argument_mode ] [ argument_name ] argument_type [, ...] $order_by_signatures = NULL; foreach ($this->order_by_signatures as $order_by_signature) { if ($order_by_signature instanceof c_database_argument_aggregate_signature_base) { @@ -321,15 +323,15 @@ class c_database_alter_aggregate extends c_database_query { unset($order_by_signatures); } - $value = NULL; + $value = $this->p_do_build_name() . ' '; if (is_string($this->rename_to)) { - $value = $aggregate_signatures . ' ' . $this->p_do_build_rename_to(); + $value .= $aggregate_signatures . ' ' . $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $value = $aggregate_signatures . ' ' . $this->p_do_build_owner_to(); + $value .= $aggregate_signatures . ' ' . $this->p_do_build_owner_to(); } else if (is_string($this->set_schema)) { - $value = $aggregate_signatures . ' ' . $this->p_do_build_set_schema(); + $value .= $aggregate_signatures . ' ' . $this->p_do_build_set_schema(); } else { unset($aggregate_signatures); @@ -339,7 +341,6 @@ class c_database_alter_aggregate extends c_database_query { unset($aggregate_signatures); $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; $this->value .= ' ' . $value; unset($value); diff --git a/common/database/classes/database_alter_coalation.php b/common/database/classes/database_alter_coalation.php index 4137abc..27604ca 100644 --- a/common/database/classes/database_alter_coalation.php +++ b/common/database/classes/database_alter_coalation.php @@ -133,20 +133,20 @@ class c_database_alter_coalation extends c_database_query { return new c_base_return_false(); } - $value = NULL; + $value = $this->p_do_build_name(); if (is_bool($this->refresh_version)) { if ($this->refresh_version) { - $value = c_database_string::REFRESH_VERSION; + $value .= c_database_string::REFRESH_VERSION; } } else if (is_string($this->rename_to)) { - $value = $this->p_do_build_rename_to(); + $value .= $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $value = $this->p_do_build_owner_to(); + $value .= $this->p_do_build_owner_to(); } else if (is_string($this->set_schema)) { - $value = $this->p_do_build_set_schema(); + $value .= $this->p_do_build_set_schema(); } else { unset($value); @@ -154,7 +154,6 @@ class c_database_alter_coalation extends c_database_query { } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; $this->value .= ' ' . $value; unset($value); diff --git a/common/database/classes/database_alter_conversion.php b/common/database/classes/database_alter_conversion.php index 1cb406e..f8fbfb1 100644 --- a/common/database/classes/database_alter_conversion.php +++ b/common/database/classes/database_alter_conversion.php @@ -83,15 +83,15 @@ class c_database_alter_conversion extends c_database_query { return new c_base_return_false(); } - $value = NULL; + $value = $this->p_do_build_name() . ' '; if (is_string($this->rename_to)) { - $value = $this->p_do_build_rename_to(); + $value .= $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $value = $this->p_do_build_owner_to(); + $value .= $this->p_do_build_owner_to(); } else if (is_string($this->set_schema)) { - $value = $this->p_do_build_set_schema(); + $value .= $this->p_do_build_set_schema(); } else { unset($value); @@ -99,7 +99,6 @@ class c_database_alter_conversion extends c_database_query { } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; $this->value .= ' ' . $value; unset($value); diff --git a/common/database/classes/database_alter_database.php b/common/database/classes/database_alter_database.php index a3f85d6..a19f4a0 100644 --- a/common/database/classes/database_alter_database.php +++ b/common/database/classes/database_alter_database.php @@ -147,25 +147,27 @@ class c_database_alter_database extends c_database_query { return new c_base_return_false(); } - $value = NULL; + $value = $this->p_do_build_name() . ' '; if ($this->option instanceof c_database_argument_database_option) { - $this->option->do_build_argument(); - $value = $this->option->get_value_exact(); + // @todo: review this, the code may and probably should be converted into a trait. + if ($this->option->do_build_argument() instanceof c_base_return_true) { + $value .= $this->option->get_value_exact(); + } } else if (is_string($this->rename_to)) { - $value = $this->p_do_build_rename_to(); + $value .= $this->p_do_build_rename_to(); } else if (is_string($this->owner_to)) { - $value = $this->p_do_build_owner_to(); + $value .= $this->p_do_build_owner_to(); } else if (is_string($this->set_tablespace)) { - $value = $this->p_do_build_set_tablespace(); + $value .= $this->p_do_build_set_tablespace(); } else if (is_array($this->set)) { - $value = $this->p_do_build_set(); + $value .= $this->p_do_build_set(); } else if (is_array($this->reset)) { - $value = $this->p_do_build_reset(); + $value .= $this->p_do_build_reset(); } else { unset($value); @@ -173,7 +175,6 @@ class c_database_alter_database extends c_database_query { } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; $this->value .= ' ' . $value; unset($value); diff --git a/common/database/classes/database_alter_default_privileges.php b/common/database/classes/database_alter_default_privileges.php index f170be5..478e75a 100644 --- a/common/database/classes/database_alter_default_privileges.php +++ b/common/database/classes/database_alter_default_privileges.php @@ -8,20 +8,22 @@ namespace n_koopa; 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_privilege.php'); require_once('common/database/classes/database_query.php'); require_once('common/database/enumerations/database_cascade.php'); +require_once('common/database/enumerations/database_grant.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.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_on.php'); +require_once('common/database/traits/database_privilege.php'); +require_once('common/database/traits/database_to_role.php'); require_once('common/database/traits/database_with_grant_option.php'); /** @@ -30,37 +32,33 @@ require_once('common/database/traits/database_with_grant_option.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_action; use t_database_cascade; use t_database_for_role; + use t_database_grant; use t_database_grant_option_for; use t_database_in_schema; + use t_database_on; + use t_database_privilege; + use t_database_to_role; use t_database_with_grant_option; protected const p_QUERY_COMMAND = 'alter default privileges'; - protected $abbreviated; - protected $on; - protected $privileges; - protected $role_names; - /** * Class constructor. */ public function __construct() { parent::__construct(); - $this->action = NULL; $this->cascade = NULL; $this->for_role = NULL; + $this->grant = NULL; $this->grant_option_for = NULL; $this->in_schema = NULL; + $this->on = NULL; + $this->privilege = NULL; + $this->to_role = NULL; $this->with_grant_option = NULL; - - $this->abbreviated = NULL; - $this->on = NULL; - $this->privileges = NULL; - $this->role_names = NULL; } /** @@ -69,17 +67,15 @@ class c_database_alter_default_priveleges extends c_database_query { public function __destruct() { parent::__destruct(); - unset($this->action); unset($this->cascade); unset($this->for_role); + unset($this->grant); unset($this->grant_option_for); unset($this->in_schema); - unset($this->with_grant_option); - - unset($this->abbreviated); unset($this->on); - unset($this->privileges); - unset($this->role_names); + unset($this->privilege); + unset($this->to_role); + unset($this->with_grant_option); } /** @@ -104,323 +100,64 @@ class c_database_alter_default_priveleges extends c_database_query { } /** - * Assigns the SQL ON operation. - * - * @param int|null $on - * Whether or not to use the ON operation 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_on($on) { - if (is_null($on)) { - $this->on = NULL; - return new c_base_return_true(); - } - - switch ($on) { - case e_database_on::TABLES_TO: - case e_database_on::SEQUENCES: - case e_database_on::FUNCTIONS: - case e_database_on::TYPES: - case e_database_on::SCHEMAS: - $this->on = $on; - return new c_base_return_true(); - default: - break; - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'on', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - - /** - * Assigns the SQL privileges. - * - * @param int|null $privilege - * Whether or not to use the ON operation in the query. - * 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_privilege($privilege) { - if (is_null($privilege)) { - $this->privileges = NULL; - return new c_base_return_true(); - } - - if (is_int($privilege)) { - if (!is_array($this->privilege)) { - $this->privileges = []; - } - - $this->privileges[] = $privilege; - return new c_base_return_true(); - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'privilege', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - - /** - * Set the role names. - * - * @param c_database_argument_role_name|null $role_name - * The role name to use. - * 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_role_name($role_name) { - if (is_null($role_name)) { - $this->role_names = NULL; - return new c_base_return_true(); - } - - if ($role_name instanceof c_database_argument_role_name) { - if (!is_array($this->role_names)) { - $this->role_names = []; - } - - $this->role_names[] = $role_name; - return new c_base_return_true(); - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'role_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); - return c_base_return_error::s_false($error); - } - - /** - * Get the role names. - * - * @param int|null $index - * (optional) Get the role name at the specified index. - * When NULL, all role names are returned. - * - * @return c_database_argument_role_name|c_base_return_array|c_base_return_null - * An array of role names or NULL if not defined. - * A single role name is returned if $index is an integer. - * NULL with the error bit set is returned on error. - */ - public function get_role_name($index = NULL) { - if (is_null($this->role_names)) { - return new c_base_return_null(); - } - - if (is_null($index)) { - if (is_array($this->role_names)) { - return c_base_return_array::s_new($this->role_names); - } - } - else { - if (is_int($index) && array_key_exists($index, $this->role_names) && $this->role_names[$index] instanceof c_database_argument_role_name) { - return $this->role_names[$index]; - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'role_names[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}' => 'role_names', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); - return c_base_return_error::s_null($error); - } - - /** - * Get the ON 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_on() { - if (is_null($this->on)) { - return new c_base_return_null(); - } - - return c_base_return_int::s_new($this->on); - } - - /** - * Get the privileges. - * - * @param int|null $index - * (optional) Get the privilege at the specified index. - * When NULL, all privileges are returned. - * - * @return int|c_base_return_array|c_base_return_null - * An array of privileges or NULL if not defined. - * A single privilege is returned if $index is an integer. - * NULL with the error bit set is returned on error. - */ - public function get_privilege($index = NULL) { - if (is_null($this->privileges)) { - return new c_base_return_null(); - } - - if (is_null($index)) { - if (is_array($this->privileges)) { - return c_base_return_array::s_new($this->privileges); - } - } - else { - if (is_int($index) && array_key_exists($index, $this->privileges) && $this->privileges[$index] instanceof c_database_argument_aggregate_signature) { - return clone($this->privileges[$index]); - } - - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'privileges[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}' => 'privileges', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); - return c_base_return_error::s_null($error); - } - - /** * Implements do_build(). */ public function do_build() { - if (is_null($this->grant) || !(is_array($this->role_names) && !empty($this->role_names))) { + if (is_null($this->grant) || !(is_array($this->to_role) && !empty($this->to_role))) { return new c_base_return_false(); } - switch ($this->action) { - case e_database_action::_GRANT: - case e_database_action::_REVOKE: - break; - default: - return new c_base_return_false(); - } - $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)) { $value .= is_null($value) ? '' : ' '; $value .= $this->p_do_build_in_schema(); } - if ($this->action === e_database_action::ACTION_GRANT) { + if (is_int($this->grant)) { $value .= is_null($value) ? '' : ' '; - $value .= c_database_string::GRANT; - } - else if ($this->action === e_database_action::ACTION_REVOKE) { - $value .= is_null($value) ? '' : ' '; - $value .= c_database_string::REVOKE; + $value .= $this->p_do_build_grant(); - if ($this->grant_option_for) { - $value .= ' ' . $this->p_do_build_grant_option_for(); - } - } - - // [ { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER | USAGE | EXECUTE | CREATE } [, ...] | ALL ] - $privileges = ''; - foreach ($this->privileges as $privilege) { - $privileges .= ', '; - switch ($privilege) { - case e_database_privilege::SELECT: - $privileges .= c_database_string::SELECT; - break; - case e_database_privilege::INSERT: - $privileges .= c_database_string::INSERT; - break; - case e_database_privilege::UPDATE: - $privileges .= c_database_string::UPDATE; - break; - case e_database_privilege::DELETE: - $privileges .= c_database_string::DELETE; - break; - case e_database_privilege::TRUNCATE: - $privileges .= c_database_string::TRUNCATE; - break; - case e_database_privilege::REFERENCES: - $privileges .= c_database_string::REFERENCES; - break; - case e_database_privilege::TRIGGER: - $privileges .= c_database_string::TRIGGER; - break; - case e_database_privilege::USAGE: - $privileges .= c_database_string::USAGE; - break; - case e_database_privilege::EXECUTE: - $privileges .= c_database_string::EXECUTE; - break; - case e_database_privilege::CREATE: - $privileges .= c_database_string::CREATE; - break; - case e_database_privilege::ALL: - $privileges .= c_database_string::ALL; - break; - default: - break; + if ($this->grant === e_database_action::ACTION_REVOKE) { + if ($this->grant_option_for) { + $value .= ' ' . $this->p_do_build_grant_option_for(); + } } } $value .= is_null($value) ? '' : ' '; - $value .= ltrim($privileges, ', '); - unset($privileges); + $value .= $this->p_do_build_privilege(); - // ON ... - switch($this->on) { - case e_database_on::TABLES_TO: - $value .= ' ' . c_database_string::ON_TABLES_TO; - break; - case e_database_on::SEQUENCES: - $value .= ' ' . c_database_string::ON_SEQUENCES; - break; - case e_database_on::FUNCTIONS: - $value .= ' ' . c_database_string::ON_FUNCTIONS; - break; - case e_database_on::TYPES: - $value .= ' ' . c_database_string::ON_TYPES; - break; - case e_database_on::SCHEMAS: - $value .= ' ' . c_database_string::ON_SCHEMAS; - break; + if (is_int($this->on)) { + $value .= ' ' . $this->p_do_build_on(); } - // [ TO | FROM ] ... role names ... - if ($this->action === e_database_action::GRANT) { + if ($this->grant === e_database_action::GRANT) { $value .= ' ' . c_database_string::TO; } - else if ($this->action === e_database_action::REVOKE) { + else if ($this->grant === e_database_action::REVOKE) { $value .= ' ' . c_database_string::FROM; } - foreach ($this->role_names as $role_name) { - if (!($role_name instanceof c_database_argument_role_name)) { - continue; - } - - $role_name->do_build_argument(); - $value .= ' ' . $role_name->get_value_exact(); + if (is_array($this->to_role)) { + $value .= ' ' . $this->p_do_build_to_role(); } - unset($role_name); - if ($this->action === e_database_action::GRANT) { + if ($this->grant === e_database_action::GRANT) { if ($this->with_grant_option) { $value .= ' ' . $this->p_do_build_with_grant_option(); } } - else if ($this->action === e_database_action::REVOKE) { + else if ($this->grant === e_database_action::REVOKE) { if (is_int($this->cascade)) { $value .= ' ' . $this->p_do_build_cascade(); } } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; $this->value .= ' ' . $value; unset($value); diff --git a/common/database/classes/database_alter_domain.php b/common/database/classes/database_alter_domain.php index 0106a57..17e185a 100644 --- a/common/database/classes/database_alter_domain.php +++ b/common/database/classes/database_alter_domain.php @@ -48,7 +48,7 @@ class c_database_alter_coalation extends c_database_query { $this->name = NULL; $this->owner_to = NULL; $this->rename_to = NULL; - $this->set_schema = NULL + $this->set_schema = NULL; $this->expression = NULL; $this->constraint = NULL; @@ -239,124 +239,123 @@ class c_database_alter_coalation extends c_database_query { return new c_base_return_false(); } - $action = NULL; + $value = $this->p_do_build_name() . ' '; switch ($this->action) { case e_database_action::ADD: if (!is_array($this->constraint)) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = c_database_string::ADD; - $action .= ' ' . $this->constraint['name']; + $value .= c_database_string::ADD; + $value .= ' ' . $this->constraint['name']; if ($this->property === e_database_property::NOT_VALID) { - $action .= ' ' . c_database_string::NOT_VALID; + $value .= ' ' . c_database_string::NOT_VALID; } break; case e_database_action::DROP: - $action = c_database_string::DROP; + $value .= c_database_string::DROP; if ($this->property === e_database_property::NOT_NULL) { - $action .= ' ' . c_database_string::NOT_NULL; + $value .= ' ' . c_database_string::NOT_NULL; } break; case e_database_action::DROP_CONSTRAINT: if (!is_array($this->constraint['name'])) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = c_database_string::DROP_CONSTRAINT; + $value .= c_database_string::DROP_CONSTRAINT; if ($this->property === e_database_property::IF_EXISTS) { - $action .= ' ' . c_database_string::IF_EXISTS; + $value .= ' ' . c_database_string::IF_EXISTS; } - $action .= ' ' . $this->constraint['name']; + $value .= ' ' . $this->constraint['name']; if ($this->constraint['cascade'] === e_database_cascade::CASCADE) { - $action .= ' ' . c_database_string::CASCADE; + $value .= ' ' . c_database_string::CASCADE; } else if ($this->constraint['cascade'] === e_database_cascade::RESTRICT) { - $action .= ' ' . c_database_string::RESTRICT; + $value .= ' ' . c_database_string::RESTRICT; } break; case e_database_action::DROP_DEFAULT: - $action = c_database_string::DROP_DEFAULT; + $value .= c_database_string::DROP_DEFAULT; break; case e_database_action::OWNER_TO: if (!is_string($this->owner_to)) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = $this->p_do_build_owner_to(); + $value .= $this->p_do_build_owner_to(); break; case e_database_action::RENAME_CONSTRAINT: if (!is_string($this->constraint['name']) || !is_string($this->constraint['name_new'])) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = c_database_string::RENAME_CONSTRAINT . ' ' . $this->constraint['name'] . ' ' . c_database_string::TO . ' ' . $this->constraint['name_new']; + $value .= c_database_string::RENAME_CONSTRAINT . ' ' . $this->constraint['name'] . ' ' . c_database_string::TO . ' ' . $this->constraint['name_new']; break; case e_database_action::RENAME_TO: if (!is_string($this->rename_to)) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = $this->p_do_build_rename_to(); + $value .= $this->p_do_build_rename_to(); break; case e_database_action::SET: - $action = c_database_string::SET; + $value = c_database_string::SET; if ($this->property === e_database_property::NOT_NULL) { - $action .= ' ' . c_database_string::NOT_NULL; + $value .= ' ' . c_database_string::NOT_NULL; } break; case e_database_action::SET_DEFAULT: if (!is_string($this->expression)) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = c_database_string::SET_DEFAULT . ' ' . $this->expression; + $value .= c_database_string::SET_DEFAULT . ' ' . $this->expression; break; case e_database_action::SET_SCHEMA: if (!is_string($this->set_schema)) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = $this->p_do_build_set_schema(); + $value .= $this->p_do_build_set_schema(); break; case e_database_action::VALIDATE_CONSTRAINT: if (!is_array($this->constraint)) { - unset($action); + unset($value); return new c_base_return_false(); } - $action = c_database_string::VALIDATE_CONSTRAINT . ' ' . $this->constraint['name']; + $value .= c_database_string::VALIDATE_CONSTRAINT . ' ' . $this->constraint['name']; break; default: - 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_event_trigger.php b/common/database/classes/database_alter_event_trigger.php index 9f5394a..deb861c 100644 --- a/common/database/classes/database_alter_event_trigger.php +++ b/common/database/classes/database_alter_event_trigger.php @@ -87,50 +87,50 @@ class c_database_alter_coalation extends c_database_query { return new c_base_return_false(); } - $action = NULL; + $value = $this->p_do_build_name() . ' '; switch($this->action) { case e_database_action::DISABLE: - $this->action = c_database_string::DISABLE; + $value .= c_database_string::DISABLE; break; case e_database_action::ENABLE: - $action = c_database_string::ENABLE; + $value .= c_database_string::ENABLE; if ($this->action_property === e_database_property::REPLICA) { - $action .= ' ' . c_database_string::REPLICA; + $value .= ' ' . c_database_string::REPLICA; } else if ($this->action_property === e_database_property::ALWAYS) { - $action .= ' ' . c_database_string::ALWAYS; + $value .= ' ' . c_database_string::ALWAYS; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_action::OWNER_TO: if (is_string($this->owner_to)) { - $action = $this->p_do_build_owner_to(); + $value .= $this->p_do_build_owner_to(); } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_action::RENAME_TO: if (is_string($this->rename_to)) { - $action = $this->p_do_build_rename_to(); + $value .= $this->p_do_build_rename_to(); } else { - unset($action); + unset($value); return new c_base_return_false(); } break; default: - unset($action); + unset($value); return new c_base_return_false(); } $this->value = static::p_QUERY_COMMAND; - $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 3046ced..03f00bf 100644 --- a/common/database/classes/database_alter_extension.php +++ b/common/database/classes/database_alter_extension.php @@ -85,60 +85,60 @@ class c_database_alter_extension extends c_database_query { return new c_base_return_false(); } - $action = $this->name . ' '; + $value = $this->p_do_build_name() . ' '; switch($this->action) { case e_database_action::UPDATE: - $action .= c_database_string::UPDATE; + $value .= c_database_string::UPDATE; if (is_string($this->action_parameter)) { - $action .= ' ' . c_database_string::TO . ' ' . $this->action_parameter; + $value .= ' ' . c_database_string::TO . ' ' . $this->action_parameter; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_action::SET_SCHEMA: 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(); } break; case e_database_action::ADD: - $action .= c_database_string::ADD; + $value .= c_database_string::ADD; if ($this->action_parameter instanceof c_database_member_object && $this->action_parameter->do_build() instanceof c_base_return_true) { - $action .= ' ' . $this->action_parameter->get_value(); + $value .= ' ' . $this->action_parameter->get_value(); } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_action::DROP: - $action .= c_database_string::DROP; + $value .= c_database_string::DROP; if ($this->action_parameter instanceof c_database_member_object) { if ($this->action_parameter->do_build() instanceof c_base_return_true) { - $action .= ' ' . $this->action_parameter->get_value(); + $value .= ' ' . $this->action_parameter->get_value(); } else { - unset($action); + unset($value); return new c_base_return_false(); } } else { - unset($action); + unset($value); return new c_base_return_false(); } break; default: - unset($action); + unset($value); return new c_base_return_false(); } - unset($action); + unset($value); - $this->value = $action; + $this->value = $value; return new c_base_return_true(); } } diff --git a/common/database/classes/database_alter_foreign_data_wrapper.php b/common/database/classes/database_alter_foreign_data_wrapper.php index a9cd2ce..8376662 100644 --- a/common/database/classes/database_alter_foreign_data_wrapper.php +++ b/common/database/classes/database_alter_foreign_data_wrapper.php @@ -126,7 +126,7 @@ class c_database_alter_foreign_data_wrapper extends c_database_query { } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; + $this->value .= ' ' . $this->p_do_build_name(); $this->value .= ' ' . $action; unset($action); diff --git a/common/database/classes/database_alter_foreign_table.php b/common/database/classes/database_alter_foreign_table.php index 052c001..5deb5a9 100644 --- a/common/database/classes/database_alter_foreign_table.php +++ b/common/database/classes/database_alter_foreign_table.php @@ -353,7 +353,7 @@ class c_database_alter_foreign_table extends c_database_query { } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $this->name; + $this->value .= ' ' . $this->p_do_build_name(); if ($this->include_descendents) { $this->value .' *'; diff --git a/common/database/classes/database_alter_function.php b/common/database/classes/database_alter_function.php index bc3389e..53a9c39 100644 --- a/common/database/classes/database_alter_function.php +++ b/common/database/classes/database_alter_function.php @@ -1,7 +1,7 @@ argument_type = NULL; + $this->depends_on_extension = NULL; + $this->function_action = NULL; + $this->name = NULL; + $this->owner_to = NULL; + $this->rename_to = NULL; + $this->restrict = NULL; + $this->set_schema = NULL; } /** * Class destructor. */ public function __destruct() { + unset($this->argument_type); + unset($this->depends_on_extension); + unset($this->function_action); + unset($this->name); + unset($this->owner_to); + unset($this->rename_to); + unset($this->restrict); + unset($this->set_schema); + parent::__destruct(); } @@ -61,9 +95,43 @@ 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(); + } + + $value = $this->p_do_build_name(); + + if (is_array($this->argument_type) && !empty($this->argument_type)) { + $value .= ' ' . $this->p_do_build_argument_type(); + } + + if (is_array($this->function_action)) { + $value .= ' ' . $this->p_do_build_function_action(); + + if ($this->restrict) { + $value .= ' ' . $this->p_do_build_restrict(); + } + } + else if (is_string($this->rename_to)) { + $value .= $this->p_do_build_rename_to(); + } + else if (is_string($this->owner_to)) { + $value .= $this->p_do_build_owner_to(); + } + else if (is_string($this->set_schema)) { + $value .= $this->p_do_build_set_schema(); + } + else if (is_string($this->depends_on_extension)) { + $value .= $this->p_do_build_depends_on_extension(); + } + else { + unset($value); + return new c_base_return_false(); + } - // @todo + $this->value = static::p_QUERY_COMMAND; + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_member_object.php b/common/database/classes/database_member_object.php index 00220c2..f8c445a 100644 --- a/common/database/classes/database_member_object.php +++ b/common/database/classes/database_member_object.php @@ -79,150 +79,150 @@ class c_database_member_object extends c_database_query { return new c_base_return_false(); } - $action = NULL; + $value = NULL; switch($this->action) { case e_database_method_object::ACCESS_METHOD: - $action = c_database_string::ACCESS_METHOD . ' ' . $this->name; + $value = c_database_string::ACCESS_METHOD . ' ' . $this->p_do_build_name(); break; case e_database_method_object::AGGREGATE: - $action = c_database_string::AGGREGATE . ' ' . $this->name; + $value = c_database_string::AGGREGATE . ' ' . $this->p_do_build_name(); // TODO: as a query argument? if ($this->action_parameter instanceof c_database_alter_aggregate && $this->action_parameter->do_build()) { - $action .= ' ( ' . $this->action_parameter->get_value() . ' )'; + $value .= ' ( ' . $this->action_parameter->get_value() . ' )'; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_method_object::CAST: // uses 'name' property to represent AS 'source_type' - $action = c_database_string::CAST; + $value = c_database_string::CAST; if (is_string($this->action_parameter)) { - $action .= ' ' . $this->name . ' ' . c_database_string::AS . ' ( ' . $this->action_parameter . ' )'; + $value .= ' ' . $this->p_do_build_name() . ' ' . c_database_string::AS . ' ( ' . $this->action_parameter . ' )'; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_method_object::COLLATION: - $action = c_database_string::COLLATION . ' ' . $this->name; + $value = c_database_string::COLLATION . ' ' . $this->p_do_build_name(); break; case e_database_method_object::CONVERSION: - $action = c_database_string::CONVERSION . ' ' . $this->name; + $value = c_database_string::CONVERSION . ' ' . $this->p_do_build_name(); break; case e_database_method_object::DOMAIN: - $action = c_database_string::DOMAIN . ' ' . $this->name; + $value = c_database_string::DOMAIN . ' ' . $this->p_do_build_name(); break; case e_database_method_object::EVENT_TRIGGER: - $action = c_database_string::EVENT_TRIGGER . ' ' . $this->name; + $value = c_database_string::EVENT_TRIGGER . ' ' . $this->p_do_build_name(); break; case e_database_method_object::FOREIGN_DATA_WRAPPER: - $action = c_database_string::FOREIGN_DATA_WRAPPER . ' ' . $this->name; + $value = c_database_string::FOREIGN_DATA_WRAPPER . ' ' . $this->p_do_build_name(); break; case e_database_method_object::FOREIGN_TABLE: - $action = c_database_string::FOREIGN_TABLE . ' ' . $this->name; + $value = c_database_string::FOREIGN_TABLE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::FUNCTION: - $action = c_database_string::FUNCTION . ' ' . $this->name; + $value = c_database_string::FUNCTION . ' ' . $this->p_do_build_name(); // TODO: finish adding support for function // TODO: as a query argument? /*if ($this->action_parameter instanceof c_database_function && $this->action_parameter->do_build()) { - $action .= ' ( ' . $this->action_parameter->get_value() . ' )'; + $value .= ' ( ' . $this->action_parameter->get_value() . ' )'; } else { - unset($action); + unset($value); return new c_base_return_false(); }*/ break; case e_database_method_object::MATERIALIZED_VIEW: - $action = c_database_string::MATERIALIZED_VIEW . ' ' . $this->name; + $value = c_database_string::MATERIALIZED_VIEW . ' ' . $this->p_do_build_name(); break; case e_database_method_object::OPERATOR: - $action = c_database_string::OPERATOR . ' ' . $this->name; + $value = c_database_string::OPERATOR . ' ' . $this->p_do_build_name(); if (isset($this->action_parameter[0]) && is_string($this->action_parameter[0]) && isset($this->action_parameter[1]) && is_string($this->action_parameter[1])) { - $action .= ' ' . $this->action_parameter[0] . ' ( ' . $this->action_parameter[0] . ', ' . $this->action_parameter[1] . ' )'; + $value .= ' ' . $this->action_parameter[0] . ' ( ' . $this->action_parameter[0] . ', ' . $this->action_parameter[1] . ' )'; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_method_object::OPERATOR_CLASS: - $action = c_database_string::OPERATOR_CLASS . ' ' . $this->name; + $value = c_database_string::OPERATOR_CLASS . ' ' . $this->p_do_build_name(); if (is_string($this->action_parameter)) { - $action .= ' ' . c_database_string::USING . ' ' . $this->action_parameter; + $value .= ' ' . c_database_string::USING . ' ' . $this->action_parameter; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_method_object::OPERATOR_FAMILY: - $action = c_database_string::OPERATOR_FAMILY . ' ' . $this->name; + $value = c_database_string::OPERATOR_FAMILY . ' ' . $this->p_do_build_name(); if (is_string($this->action_parameter)) { - $action .= ' ' . c_database_string::USING . ' ' . $this->action_parameter; + $value .= ' ' . c_database_string::USING . ' ' . $this->action_parameter; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_method_object::LANGUAGE: - $action = c_database_string::LANGUAGE . ' ' . $this->name; + $value = c_database_string::LANGUAGE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::PROCEDURAL: - $action = c_database_string::PROCEDURAL . ' ' . c_database_string::LANGUAGE . ' ' . $this->name; + $value = c_database_string::PROCEDURAL . ' ' . c_database_string::LANGUAGE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::SCHEMA: - $action = c_database_string::SCHEMA . ' ' . $this->name; + $value = c_database_string::SCHEMA . ' ' . $this->p_do_build_name(); break; case e_database_method_object::SEQUENCE: - $action = c_database_string::SEQUENCE . ' ' . $this->name; + $value = c_database_string::SEQUENCE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::SERVER: - $action = c_database_string::SERVER . ' ' . $this->name; + $value = c_database_string::SERVER . ' ' . $this->p_do_build_name(); break; case e_database_method_object::TABLE: - $action = c_database_string::TABLE . ' ' . $this->name; + $value = c_database_string::TABLE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::TEXT_SEARCH_CONFIGURATION: - $action = c_database_string::TEXT_SEARCH_CONFIGURATION . ' ' . $this->name; + $value = c_database_string::TEXT_SEARCH_CONFIGURATION . ' ' . $this->p_do_build_name(); break; case e_database_method_object::TEXT_SEARCH_DICTIONARY: - $action = c_database_string::TEXT_SEARCH_DICTIONARY . ' ' . $this->name; + $value = c_database_string::TEXT_SEARCH_DICTIONARY . ' ' . $this->p_do_build_name(); break; case e_database_method_object::TEXT_SEARCH_PARSER: - $action = c_database_string::TEXT_SEARCH_PARSER . ' ' . $this->name; + $value = c_database_string::TEXT_SEARCH_PARSER . ' ' . $this->p_do_build_name(); break; case e_database_method_object::TEXT_SEARCH_TEMPLATE: - $action = c_database_string::TEXT_SEARCH_TEMPLATE . ' ' . $this->name; + $value = c_database_string::TEXT_SEARCH_TEMPLATE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::TRANSFORM_FOR: - $action = c_database_string::TRANSFORM_FOR . ' ' . $this->name; + $value = c_database_string::TRANSFORM_FOR . ' ' . $this->p_do_build_name(); if (is_string($this->action_parameter)) { - $action .= ' ' . c_database_string::LANGUAGE . ' ' . $this->action_parameter; + $value .= ' ' . c_database_string::LANGUAGE . ' ' . $this->action_parameter; } else { - unset($action); + unset($value); return new c_base_return_false(); } break; case e_database_method_object::TYPE: - $action = c_database_string::TYPE . ' ' . $this->name; + $value = c_database_string::TYPE . ' ' . $this->p_do_build_name(); break; case e_database_method_object::VIEW: - $action = c_database_string::VIEW . ' ' . $this->name; + $value = c_database_string::VIEW . ' ' . $this->p_do_build_name(); break; default: - unset($action); + unset($value); return new c_base_return_false(); } $this->value = static::p_QUERY_COMMAND; - $this->value .= ' ' . $action; - unset($action); + $this->value .= ' ' . $value; + unset($value); return new c_base_return_true(); } diff --git a/common/database/classes/database_string.php b/common/database/classes/database_string.php index a5b3d3b..ed43c99 100644 --- a/common/database/classes/database_string.php +++ b/common/database/classes/database_string.php @@ -25,6 +25,7 @@ class c_database_string { public const CONVERSION = 'conversion'; public const CREATE = 'create'; 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'; @@ -50,6 +51,8 @@ class c_database_string { public const HANDLER = 'handler'; public const IF_EXISTS = 'if exists'; 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'; @@ -70,6 +73,7 @@ class c_database_string { 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 PROCEDURAL = 'procedural'; public const PUBLIC = 'public'; diff --git a/common/database/enumerations/database_argument_mode.php b/common/database/enumerations/database_argument_mode.php new file mode 100644 index 0000000..d193d96 --- /dev/null +++ b/common/database/enumerations/database_argument_mode.php @@ -0,0 +1,19 @@ +argument_type = NULL; + return new c_base_return_true(); + } + + if (!is_string($argument_type)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'argument_type', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_string($argument_name)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'argument_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + switch($argument_mode) { + case e_database_argument_mode::IN: + case e_database_argument_mode::INOUT: + case e_database_argument_mode::OUT: + case e_database_argument_mode::VARIADIC: + break; + default: + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'argument_mode', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_array($this->argument_type)) { + $this->argument_type = []; + } + + $this->argument_type[] = [ + 'type' => $argument_type, + 'name' => $argument_name, + 'mode' => $argument_mode, + ]; + + return new c_base_return_true(); + } + + /** + * Get the currently assigned sql argument type at the specified index. + * + * @param int|null $index + * (optional) Get the argument type array at the specified index. + * When NULL, all argument type are returned. + * + * @return c_base_return_array|c_base_return_null + * A code representing the argument_type on success. + * NULL is returned if not set (argument_type tablespace is not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_argument_type($index = NULL) { + if (is_null($this->argument_type)) { + return new c_base_return_null(); + } + + if (is_null($this->argument_type['type'])) { + return c_base_return_array::s_new($this->argument_type); + } + else if (isset($this->argument_type[$index]) && is_array($this->argument_type[$index])) { + return c_base_return_int::s_new($this->argument_type[$index]); + } + else { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'argument_type', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_null($error); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_type', ':{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_argument_type() { + if (is_null($this->argument_type)) { + return NULL; + } + + $values = []; + foreach ($this->argument_type as $argument_type) { + $value = ''; + + switch($argument_type['mode']) { + case e_database_argument_mode::IN: + $value = c_database_string::IN; + break; + case e_database_argument_mode::INOUT: + $value = c_database_string::INOUT; + break; + case e_database_argument_mode::OUT: + $value = c_database_string::OUT; + break; + case e_database_argument_mode::VARIADIC: + $value = c_database_string::VARIADIC; + break; + } + + $value .= ' ' . $argument_type['name']; + $value .= ' ' . $argument_type['type']; + + $values[] = $value; + } + unset($argument_type); + unset($value); + + return implode(', ', $values); + } +} diff --git a/common/database/traits/database_depends_on_extension.php b/common/database/traits/database_depends_on_extension.php new file mode 100644 index 0000000..3f5141c --- /dev/null +++ b/common/database/traits/database_depends_on_extension.php @@ -0,0 +1,81 @@ + [':{argument_name}' => 'depends_on_extension', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->depends_on_extension = $depends_on_extension; + return new c_base_return_true(); + } + + /** + * Get the currently assigned schema name to set to. + * + * @return c_base_return_string|c_base_return_null + * An extension name on success. + * NULL is returned if not set (depends on extension is not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_depends_on_extension() { + if (is_null($this->depends_on_extension)) { + return new c_base_return_null(); + } + + if (is_string($this->depends_on_extension)) { + return c_base_return_string::s_new($this->depends_on_extension); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'depends_on_extension', ':{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_depends_on_extension() { + if (is_null($this->depends_on_extension)) { + return NULL; + } + + return c_database_string::DEPENDS_ON_EXTENSION . ' ' . $this->depends_on_extension; + } +} diff --git a/common/database/traits/database_function_action.php b/common/database/traits/database_function_action.php new file mode 100644 index 0000000..9c47935 --- /dev/null +++ b/common/database/traits/database_function_action.php @@ -0,0 +1,162 @@ +function_action = NULL; + return new c_base_return_true(); + } + + switch($function_action) { + case e_database_function_action::CALLED_ON_NULL_INPUT: + case e_database_function_action::COST: + case e_database_function_action::IMMUTABLE: + case e_database_function_action::LEAKPROOF: + case e_database_function_action::NOT_LEAKPROOF: + case e_database_function_action::PARALLEL_RESTRICTED: + case e_database_function_action::PARALLEL_SAFE: + case e_database_function_action::PARALLEL_UNSAFE: + case e_database_function_action::RESET: + case e_database_function_action::RESET_ALL: + case e_database_function_action::RETURNS_NULL_ON_NULL_INPUT: + case e_database_function_action::ROWS: + case e_database_function_action::SECURITY_DEFINER: + case e_database_function_action::SECURITY_INVOKER: + case e_database_function_action::SET_EQUAL: + case e_database_function_action::SET_FROM: + case e_database_function_action::SET_TO: + case e_database_function_action::STABLE: + case e_database_function_action::STRICT: + case e_database_function_action::VOLATILE: + break; + default: + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'function_action', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $action = [ + 'type' => $function_action, + 'parameter_1' => NULL, + 'parameter_2' => NULL, + ]; + + if ($function_action === e_database_function_action::COST || $function_action === e_database_function_action::ROWS) { + if (!is_int($parameter_1)) { + unset($action); + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'parameter_1', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $action['parameter_1'] = $parameter_1; + } + else if ($function_action === e_database_function_action::SET_TO || $function_action === e_database_function_action::SET_EQUAL) { + if (!is_string($parameter_1)) { + unset($action); + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'parameter_1', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_null($parameter_2) && !is_string($parameter_2)) { + unset($action); + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'parameter_2', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $action['parameter_1'] = $parameter_1; + $action['parameter_2'] = $parameter_2; + } + else if ($function_action === e_database_function_action::SET_FROM || $function_action === e_database_function_action::RESET) { + if (!is_string($parameter_1)) { + unset($action); + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'parameter_1', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $action['parameter_1'] = $parameter_1; + } + + $this->function_action = $action; + unset($action); + + return new c_base_return_true(); + } + + /** + * Get the currently assigned sql functionn action. + * + * @return c_base_return_array|c_base_return_null + * An array representing the function action on success. + * NULL is returned if not set (function action tablespace is not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_function_action($index = NULL) { + if (is_null($this->function_action)) { + return new c_base_return_null(); + } + + if (is_array($this->function_action)) { + return c_base_return_array::s_new($this->function_action); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'function_action', ':{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_function_action() { + if (is_null($this->function_action)) { + return NULL; + } + + // @todo + } +} diff --git a/common/database/traits/database_grant.php b/common/database/traits/database_grant.php new file mode 100644 index 0000000..dfcc90e --- /dev/null +++ b/common/database/traits/database_grant.php @@ -0,0 +1,93 @@ +grant = NULL; + return new c_base_return_true(); + } + + if ($grant === e_database_grant::GRANT || $grant === e_database_grant::REVOKE) { + $this->grant = $grant; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'grant', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Get the currently assigned grant code. + * + * @return c_base_return_int|c_base_return_null + * A grant code on success. + * NULL is returned if not set (grant is not to be used). + * NULL with the error bit set is returned on error. + */ + public function get_grant() { + if (is_null($this->grant)) { + return new c_base_return_null(); + } + + if (is_int($this->grant)) { + return c_base_return_int::s_new($this->grant); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'grant', ':{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() { + $value = NULL; + if ($this->grant === e_database_grant::GRANT) { + $value = c_database_string::GRANT; + } + else if ($this->grant === e_database_grant::REVOKE) { + $value = c_database_string::REVOKE; + } + + return $value; + } +} diff --git a/common/database/traits/database_in_schema.php b/common/database/traits/database_in_schema.php index 56c8cbe..b977774 100644 --- a/common/database/traits/database_in_schema.php +++ b/common/database/traits/database_in_schema.php @@ -100,6 +100,6 @@ trait t_database_in_schema { return NULL; } - return c_database_string::IN . ' ' . c_database_string::SCHEMA . ' ' . implode(', ', $this->in_schema); + return c_database_string::IN_SCHEMA . ' ' . implode(', ', $this->in_schema); } } diff --git a/common/database/traits/database_name.php b/common/database/traits/database_name.php index 469df50..2e0b162 100644 --- a/common/database/traits/database_name.php +++ b/common/database/traits/database_name.php @@ -64,4 +64,21 @@ trait t_database_name { $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'name', ':{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_name() { + if (is_null($this->name)) { + return NULL; + } + + return $this->name; + } } diff --git a/common/database/traits/database_on.php b/common/database/traits/database_on.php new file mode 100644 index 0000000..b7b1477 --- /dev/null +++ b/common/database/traits/database_on.php @@ -0,0 +1,111 @@ +on = NULL; + return new c_base_return_true(); + } + + switch ($on) { + case e_database_on::TABLES_TO: + case e_database_on::SEQUENCES: + case e_database_on::FUNCTIONS: + case e_database_on::TYPES: + case e_database_on::SCHEMAS: + $this->on = $on; + return new c_base_return_true(); + default: + break; + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'on', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Get the ON 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_on() { + if (is_null($this->on)) { + return new c_base_return_null(); + } + + return c_base_return_int::s_new($this->on); + } + + /** + * 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_on() { + if (is_null($this->on)) { + return NULL; + } + + $value = NULL; + switch($this->on) { + case e_database_on::TABLES_TO: + $value .= c_database_string::ON_TABLES_TO; + break; + case e_database_on::SEQUENCES: + $value .= c_database_string::ON_SEQUENCES; + break; + case e_database_on::FUNCTIONS: + $value .= c_database_string::ON_FUNCTIONS; + break; + case e_database_on::TYPES: + $value .= c_database_string::ON_TYPES; + break; + case e_database_on::SCHEMAS: + $value .= c_database_string::ON_SCHEMAS; + break; + } + + return $value; + } +} diff --git a/common/database/traits/database_privilege.php b/common/database/traits/database_privilege.php new file mode 100644 index 0000000..b1bc778 --- /dev/null +++ b/common/database/traits/database_privilege.php @@ -0,0 +1,161 @@ +privileges = NULL; + return new c_base_return_true(); + } + + if (is_int($privilege)) { + // no reason to add any privilege once ALL is present. + if ($this->privilege === e_database_privilege::ALL) { + return new c_base_return_true(); + } + + if ($privilege === e_database_privilege::ALL) { + $this->privilege = e_database_privilege::ALL; + } + else { + if (!is_array($this->privilege)) { + $this->privilege = []; + } + + $this->privilege[] = $privilege; + } + + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'privilege', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Get the privileges. + * + * @param int|null $index + * (optional) Get the privilege at the specified index. + * When NULL, all privileges are returned. + * + * @return c_base_return_int|c_base_return_array|c_base_return_null + * An array of privileges or NULL if not defined. + * A single privilege is returned if $index is an integer. + * NULL with the error bit set is returned on error. + */ + public function get_privilege($index = NULL) { + if (is_null($this->privilege)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if ($this->privilege === e_database_privilege::ALL) { + return c_base_return_array::s_new([$this->privilege]); + } + else if (is_array($this->privilege)) { + return c_base_return_array::s_new($this->privilege); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->privilege) && is_int($this->privilege[$index])) { + return clone($this->privilege[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'privilege[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}' => 'privilege', ':{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_privilege() { + if (is_null($this->privilege)) { + return NULL; + } + + if ($this->privilege === e_database_privilege::ALL) { + return c_database_string::ALL; + } + + $privileges = []; + foreach ($this->privilege as $privilege) { + switch ($privilege) { + case e_database_privilege::SELECT: + $privileges[] = c_database_string::SELECT; + break; + case e_database_privilege::INSERT: + $privileges[] = c_database_string::INSERT; + break; + case e_database_privilege::UPDATE: + $privileges[] = c_database_string::UPDATE; + break; + case e_database_privilege::DELETE: + $privileges[] = c_database_string::DELETE; + break; + case e_database_privilege::TRUNCATE: + $privileges[] = c_database_string::TRUNCATE; + break; + case e_database_privilege::REFERENCES: + $privileges[] = c_database_string::REFERENCES; + break; + case e_database_privilege::TRIGGER: + $privileges[] = c_database_string::TRIGGER; + break; + case e_database_privilege::USAGE: + $privileges[] = c_database_string::USAGE; + break; + case e_database_privilege::EXECUTE: + $privileges[] = c_database_string::EXECUTE; + break; + case e_database_privilege::CREATE: + $privileges[] = c_database_string::CREATE; + break; + default: + break; + } + } + + return implode(', ', $privileges); + } +} diff --git a/common/database/traits/database_restrict.php b/common/database/traits/database_restrict.php new file mode 100644 index 0000000..9e670e0 --- /dev/null +++ b/common/database/traits/database_restrict.php @@ -0,0 +1,87 @@ +restrict = NULL; + return new c_base_return_true(); + } + + if (!is_bool($restrict)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'restrict', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->restrict = $restrict; + return new c_base_return_true(); + } + + /** + * Get the currently assigned restrict status. + * + * @return c_base_return_bool|c_base_return_null + * TRUE for RESTRICT or FALSE for not using RESTRICT on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_restrict() { + if (is_null($this->restrict)) { + return new c_base_return_null(); + } + + if (is_bool($this->restrict)) { + return c_base_return_bool::s_new($this->restrict); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'restrict', ':{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_restrict() { + if (is_null($this->restrict)) { + return NULL; + } + + return $this->restrict ? c_database_string::RESTRICT : NULL; + } +} diff --git a/common/database/traits/database_to_role.php b/common/database/traits/database_to_role.php new file mode 100644 index 0000000..93e6e82 --- /dev/null +++ b/common/database/traits/database_to_role.php @@ -0,0 +1,135 @@ +to_role = NULL; + return new c_base_return_true(); + } + + if (is_int($role_type)) { + if ($role_type === e_database_role::NAME && !is_string($role_name) || !is_null($role_name) && !is_string($role_name)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'role_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + else if ($role_type !== e_database_role::GROUP && $role_type !== e_database_role::PUBLIC) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'role_type', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (!is_array($this->to_role)) { + $this->to_role = []; + } + + $this->to_role[] = [ + 'type' => $role_type, + 'name' => $role_name, + ]; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'to_role', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Get the in schema, to roles. + * + * @param int|null $index + * (optional) Get the schema name at the specified index. + * When NULL, all to roles are returned. + * + * @return c_base_return_array|c_base_return_null + * An array of to role arrays or NULL if not defined. + * A single to role array is returned if $index is an integer. + * NULL with the error bit set is returned on error. + */ + public function get_to_role($index = NULL) { + if (is_null($this->to_role)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if (is_array($this->to_role)) { + return c_base_return_array::s_new($this->to_role); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->to_role) && is_array($this->to_role[$index])) { + return c_base_return_array::s_new($this->to_role[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'to_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}' => 'to_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_to_role() { + if (is_null($this->to_role)) { + return NULL; + } + + $values = []; + foreach ($this->to_role as $to_role) { + if ($this->to_role['type'] === e_database_role::PUBLIC) { + $values[] = c_database_string::PUBLIC; + } + else if ($this->to_role['type'] === e_database_role::GROUP) { + $values[] = c_database_string::GROUP; + } + else if ($this->to_role['type'] === e_database_role::NAME) { + $values[] = $this->to_role['name']; + } + } + + return c_database_string::TO . ' ' . implode(', ', $values); + } +}