From: Kevin Day Date: Mon, 3 Dec 2018 04:58:47 +0000 (-0600) Subject: Progress: continue development on database abstraction X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=ec95e49f86624fb356b81a6e695beb1d13745222;p=koopa Progress: continue development on database abstraction --- diff --git a/common/database/classes/database_alter_foreign_table.php b/common/database/classes/database_alter_foreign_table.php index 64854ff..7f5dd01 100644 --- a/common/database/classes/database_alter_foreign_table.php +++ b/common/database/classes/database_alter_foreign_table.php @@ -12,8 +12,12 @@ require_once('common/database/classes/database_query.php'); require_once('common/database/traits/database_action.php'); require_once('common/database/traits/database_name.php'); -require_once('common/database/traits/database_rename_column_to.php'); +require_once('common/database/traits/database_options.php'); +require_once('common/database/traits/database_owner_to.php'); +require_once('common/database/traits/database_rename_column.php'); require_once('common/database/traits/database_rename_to.php'); +require_once('common/database/traits/database_set_schema.php'); +require_once('common/database/traits/database_set_with_oids.php'); /** * The class for building and returning a Postgresql ALTER FOREIGN TABLE query string. @@ -21,49 +25,35 @@ require_once('common/database/traits/database_rename_to.php'); * @see: https://www.postgresql.org/docs/current/static/sql-alterforeigntable.html */ class c_database_alter_foreign_table extends c_database_query { - protected const pr_QUERY_COMMAND = 'alter foreign table'; use t_database_action; use t_database_name; - use t_database_rename_column_to; + use t_database_options; + use t_database_owner_to; + use t_database_rename_column; use t_database_rename_to; + use t_database_set_schema; + use t_database_set_with_oids; + + protected const pr_QUERY_COMMAND = 'alter foreign table'; protected $if_exists; protected $include_descendents; // The '*' following 'name' protected $only; -// sub-forms: -// ADD COLUMN -// DROP COLUMN -// SET DATA TYPE -// SET DEFAULT -// SET NOT NULL -// SET STATISTICS -// SET -// RESET -// SET STORAGE -// ADD ... -// VALIDATE CONSTRAINT -// DROP CONSTRAINT -// DISABLE/ENABLE -// SET WITH OIDS -// SET WITHOUT OIDS -// INHERIT -// NO INHERIT -// OWNER -// OPTIONS -// RENAME -// SET SCHEMA - /** * Class constructor. */ public function __construct() { parent::__construct(); - $this->action = NULL; - $this->name = NULL; - $this->rename_column_to = NULL; - $this->rename_to = NULL; + $this->action = NULL; + $this->name = NULL; + $this->options = NULL; + $this->owner_to = NULL; + $this->rename_column = NULL; + $this->rename_to = NULL; + $this->set_schema = NULL; + $this->set_with_oids = NULL; $this->if_exists = NULL; $this->include_descendents = NULL; @@ -78,8 +68,12 @@ class c_database_alter_foreign_table extends c_database_query { unset($this->action); unset($this->name); - unset($this->rename_column_to); + unset($this->options); + unset($this->owner_to); + unset($this->rename_column); unset($this->rename_to); + unset($this->set_schema); + unset($this->set_with_oids); unset($this->if_exists); unset($this->include_descendents); @@ -108,6 +102,84 @@ class c_database_alter_foreign_table extends c_database_query { } /** + * Assigns IF EXISTS. + * + * @param bool|null $if_exists + * Set to TRUE to enable IF EXISTS, FALSE to disable. + * 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_if_exists($if_exists) { + if (is_null($if_exists)) { + $this->if_exists = NULL; + return new c_base_return_true(); + } + + if (is_bool($if_exists)) { + $this->if_exists = $if_exists; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'if_exists', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Assigns wildcard '*' after the table name. + * + * @param bool|null $include_decendents + * Set to TRUE to enable wildcard '*', FALSE to disable. + * 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_include_decendents($include_decendents) { + if (is_null($include_decendents)) { + $this->include_decendents = NULL; + return new c_base_return_true(); + } + + if (is_bool($include_decendents)) { + $this->include_decendents = $include_decendents; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'include_decendents', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Assigns ONLY. + * + * @param bool|null $only + * Set to TRUE to enable ONLY, FALSE to disable. + * 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_only($only) { + if (is_null($only)) { + $this->only = NULL; + return new c_base_return_true(); + } + + if (is_bool($only)) { + $this->only = $only; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'only', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** * Implements do_build(). */ public function do_build() { @@ -120,11 +192,70 @@ class c_database_alter_foreign_table extends c_database_query { $action = ' ' . c_database_string::IF_EXISTS; } - if ($this->only) { - $action = ' ' . c_database_string::ONLY; + if (is_array($this->rename_column)) { + if ($this->only) { + $action .= is_null($action) ? '' : ' '; + $action .= c_database_string::ONLY; + } + + $action .= ' ' . $this->p_do_build_rename_column(); + } + else if (is_array($this->rename_to)) { + $action .= is_null($action) ? '' : ' '; + $action .= $this->p_do_build_rename_to(); } + else if (is_array($this->set_schema)) { + $action .= is_null($action) ? '' : ' '; + $action .= $this->p_do_build_set_schema(); + } + else { + if ($this->only) { + $action .= is_null($action) ? '' : ' '; + $action .= c_database_string::ONLY; + } + + $action .= is_null($action) ? '' : ' '; + // @todo - // @todo +// sub-forms: +// ADD COLUMN +// DROP COLUMN +// SET DATA TYPE +// SET DEFAULT +// SET NOT NULL +// SET STATISTICS +// SET +// RESET +// SET STORAGE +// ADD ... +// VALIDATE CONSTRAINT +// DROP CONSTRAINT +// DISABLE/ENABLE +// SET WITH OIDS +// SET WITHOUT OIDS +// INHERIT +// NO INHERIT +// OWNER +// OPTIONS +// RENAME +// SET SCHEMA + else if (is_bool($this->set_with_oids)) { + $action .= $this->p_do_build_set_with_oids(); + } + else if (is_array($this->inherit)) { + $action .= $this->p_do_build_inherit(); + } + else if (is_array($this->owner_to)) { + $action .= $this->p_do_build_owner_to(); + } + else if (is_array($this->options)) { + $action .= $this->p_do_build_options(); + } + else { + unset($action); + return new c_base_return_false(); + } + } $this->value = static::pr_QUERY_COMMAND; $this->value .= ' ' . $this->name; @@ -133,7 +264,7 @@ class c_database_alter_foreign_table extends c_database_query { $this->value .' *'; } - $this->value .= $action; + $this->value .= ' ' . $action; unset($action); return new c_base_return_true(); diff --git a/common/database/classes/database_string.php b/common/database/classes/database_string.php index f61b9a8..b1dc6a0 100644 --- a/common/database/classes/database_string.php +++ b/common/database/classes/database_string.php @@ -45,11 +45,13 @@ class c_database_string { public const HANDLER = 'handler'; public const IF_EXISTS = 'if exists'; public const IN = 'in'; + public const INHERIT = 'inherit'; public const INSERT = 'insert'; public const IS_TEMPLATE = 'is_template'; public const LANGUAGE = 'language'; public const MATERIALIZED_VIEW = 'materialized view'; public const NO_HANDLER = 'no handler'; + public const NO_INHERIT = 'no inherit'; public const NO_VALIDATOR = 'no validator'; public const NOT_NULL = 'not null'; public const NOT_VALID = 'not valid'; @@ -84,6 +86,8 @@ class c_database_string { public const SET_DEFAULT = 'set default'; public const SET_SCHEMA = 'set schema'; public const SET_TABLESPACE = 'set tablespace'; + public const SET_WITH_OIDS = 'set with oids'; + public const SET_WITHOUT_OIDS = 'set without oids'; public const TABLE = 'table'; public const TEXT_SEARCH_CONFIGURATION = 'text search configuration'; public const TEXT_SEARCH_DICTIONARY = 'text search dictionary'; diff --git a/common/database/traits/database_group_by.php b/common/database/traits/database_group_by.php index 2ecfd4f..ec2c05b 100644 --- a/common/database/traits/database_group_by.php +++ b/common/database/traits/database_group_by.php @@ -78,6 +78,6 @@ trait t_database_group_by { * NULL is returned if there is nothing to process or there is an error. */ protected function p_do_build_group_by() { - //return c_database_string::GROUP_BY . ' (' . $this->group_by . ')'; + return c_database_string::GROUP_BY . $this->group_by; } } diff --git a/common/database/traits/database_inherit.php b/common/database/traits/database_inherit.php new file mode 100644 index 0000000..3ebdc18 --- /dev/null +++ b/common/database/traits/database_inherit.php @@ -0,0 +1,111 @@ +name = NULL; + return new c_base_return_true(); + } + + if (!is_string($name)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'inherit', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->inherit = [ + 'name' => $name, + 'inherit' => $inherit, + ]; + return new c_base_return_true(); + } + + /** + * Get the currently assigned inherit status. + * + * @return c_base_return_bool|c_base_return_null + * TRUE for INHERIT or FALSE for NO INHERIT on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_inherit() { + if (is_null($this->inherit)) { + return new c_base_return_null(); + } + + if (is_bool($this->inherit['inherit'])) { + return c_base_return_bool::s_new($this->inherit['inherit']); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'inherit[inherit]', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Get the currently assigned name to inherit from. + * + * @return c_base_return_string|c_base_return_null + * A name on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_inherit_name() { + if (is_null($this->inherit)) { + return new c_base_return_null(); + } + + if (is_string($this->inherit['name'])) { + return c_base_return_string::s_new($this->inherit['name']); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'inherit[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_inherit() { + $value = $this->inherit['inherit'] ? c_database_string::INHERIT : c_database_string::NO_INHERIT; + $value .= ' ' . $this->inherit['name']; + return $value; + } +} diff --git a/common/database/traits/database_set_with_oids.php b/common/database/traits/database_set_with_oids.php new file mode 100644 index 0000000..29008c8 --- /dev/null +++ b/common/database/traits/database_set_with_oids.php @@ -0,0 +1,82 @@ +set_with_oids = NULL; + return new c_base_return_true(); + } + + if (!is_bool($set_with_oids)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'set_with_oids', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + $this->set_with_oids = $set_with_oids; + return new c_base_return_true(); + } + + /** + * Get the currently assigned set with oids status. + * + * @return c_base_return_bool|c_base_return_null + * TRUE for SET WITH OIDS or FALSE for SET WITHOUT OIDS on success. + * NULL is returned if not set. + * NULL with the error bit set is returned on error. + */ + public function get_set_with_oids() { + if (is_null($this->set_with_oids)) { + return new c_base_return_null(); + } + + if (is_bool($this->set_with_oids)) { + return c_base_return_bool::s_new($this->set_with_oids); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'set_with_oids', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Perform the common build process for this trait. + * + * As an internal trait method, the caller is expected to perform any appropriate validation. + * + * @return string|null + * A string is returned on success. + * NULL is returned if there is nothing to process or there is an error. + */ + protected function p_do_build_set_with_oids() { + return $this->set_with_oids ? c_database_string::SET_WITH_OIDS : c_database_string::SET_WITHOUT_OIDS; + } +}