From: Kevin Day Date: Wed, 1 Aug 2018 03:54:41 +0000 (-0500) Subject: Progress: continue adding query helper classes and traits X-Git-Url: https://git.kevux.org/?a=commitdiff_plain;h=aedf60357c1c241b1542832fe747f8d579641940;p=koopa Progress: continue adding query helper classes and traits --- diff --git a/common/base/classes/base_query.php b/common/base/classes/base_query.php index 4a0916f..b67b550 100644 --- a/common/base/classes/base_query.php +++ b/common/base/classes/base_query.php @@ -224,8 +224,9 @@ abstract class c_base_query extends c_base_return_string implements i_base_query * Implements do_reset(). */ public function do_reset() { - $this->value = ''; + $this->value = NULL; $this->placeholders = []; + return new c_base_return_true(); } } @@ -236,13 +237,15 @@ abstract class c_base_query extends c_base_return_string implements i_base_query * SQL queries may be passed to other SQL queries, marking them as sub-queries. * This will most commonly be added to other expressions. */ -class c_base_query_argument_query implements i_base_query_argument { +class c_base_query_argument_query extends c_base_return_string implements i_base_query_argument { protected $query; /** * Class constructor. */ public function __construct() { + parent::__construct(); + $this->query = NULL; } @@ -251,6 +254,8 @@ class c_base_query_argument_query implements i_base_query_argument { */ public function __destruct() { unset($this->query); + + parent::__destruct(); } /** @@ -340,14 +345,26 @@ class c_base_query_argument_query implements i_base_query_argument { } unset($built); - return c_base_return_string::s_new($this->query->get_value_exact()); + $this->value = $this->query->get_value_exact(); + + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->query = NULL; + + return new c_base_return_true(); } } /** * Provide an SQL expression argument. */ -class c_base_query_argument_expression implements i_base_query_argument { +class c_base_query_argument_expression extends c_base_return_string implements i_base_query_argument { protected const pr_QUERY_AS = 'as'; protected $expression; @@ -357,6 +374,8 @@ class c_base_query_argument_expression implements i_base_query_argument { * Class constructor. */ public function __construct() { + parent::__construct(); + $this->expression = NULL; $this->alias = NULL; } @@ -367,6 +386,8 @@ class c_base_query_argument_expression implements i_base_query_argument { public function __destruct() { unset($this->expression); unset($this->alias); + + parent::__destruct(); } /** @@ -493,16 +514,26 @@ class c_base_query_argument_expression implements i_base_query_argument { */ public function do_build_argument() { if (!is_string($this->expression)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'expression', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); - return c_base_return_error::s_null($error); + return new c_base_return_false(); } - $built = $this->expression; + $this->value = $this->expression; if (!is_string($this->alias)) { - $built .= ' ' . static::pr_QUERY_AS . ' ' . $this->alias; + $this->value .= ' ' . static::pr_QUERY_AS . ' ' . $this->alias; } - return c_base_return_string::s_new($built); + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->expression = NULL; + $this->alias = NULL; + + return new c_base_return_true(); } } @@ -614,14 +645,25 @@ class c_base_query_argument_expression_column extends c_base_query_argument_expr return new c_base_return_false(); } - if (is_string($this->column) && $built instanceof c_base_return_string) { - $new_built = $this->column . '.' . $built->get_value_exact(); + if (is_string($this->column) && $built instanceof c_base_return_true) { + $this->value = $this->column . '.' . $this->value; unset($built); return c_base_return_string::s_new($new_built); } + unset($built); + + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->column = NULL; - return $built; + return new c_base_return_true(); } } @@ -631,7 +673,7 @@ class c_base_query_argument_expression_column extends c_base_query_argument_expr * This is essentially an aggregate_signature without the wildcard and ORDER BY support. * The ORDER BY support can then utilize this base without having its own wildcard and ORDER BY. */ -class c_base_query_argument_aggregate_signature_base implements i_base_query_argument { +class c_base_query_argument_aggregate_signature_base extends c_base_return_string implements i_base_query_argument { public const QUERY_ARGUMENT_MODE_NONE = 0; public const QUERY_ARGUMENT_MODE_IN = 1; public const QUERY_ARGUMENT_MODE_VARIADIC = 2; @@ -644,6 +686,8 @@ class c_base_query_argument_aggregate_signature_base implements i_base_query_arg * Class constructor. */ public function __construct() { + parent::__construct(); + $this->argument_mode = NULL; $this->argument_name = NULL; $this->argument_type = NULL; @@ -656,6 +700,8 @@ class c_base_query_argument_aggregate_signature_base implements i_base_query_arg unset($this->argument_mode); unset($this->argument_name); unset($this->argument_type); + + parent::__destruct(); } /** @@ -771,7 +817,7 @@ class c_base_query_argument_aggregate_signature_base implements i_base_query_arg } if (is_int($this->argument_mode)) { - return $this->argument_mode; + return c_base_return_int::s_new($this->argument_mode); } $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_mode', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); @@ -792,7 +838,7 @@ class c_base_query_argument_aggregate_signature_base implements i_base_query_arg } if (is_string($this->argument_name)) { - return $this->argument_name; + return c_base_return_string::s_new($this->argument_name); } $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); @@ -813,7 +859,7 @@ class c_base_query_argument_aggregate_signature_base implements i_base_query_arg } if (is_string($this->argument_type)) { - return $this->argument_type; + return c_base_return_string::s_new($this->argument_type); } $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_type}' => 'argument_type', ':{function_type}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); @@ -825,27 +871,38 @@ class c_base_query_argument_aggregate_signature_base implements i_base_query_arg */ public function do_build_argument() { if (!is_string($this->argument_type)) { - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_typr', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); - return c_base_return_error::s_null($error); + return new c_base_return_false(); } - $built = ''; + $this->value = ''; if ($this->argument_mode === static::QUERY_MODE_IN) { - $built = static::pr_QUERY_MODE_IN; + $this->value = static::pr_QUERY_MODE_IN; } - elseif ($this->argument_mode === static::QUERY_MODE_VARIADIC) { - $built = static::pr_QUERY_MODE_VARIADIC; + else if ($this->argument_mode === static::QUERY_MODE_VARIADIC) { + $this->value = static::pr_QUERY_MODE_VARIADIC; } if (is_string($this->argument_name)) { - $built .= ' ' . $this->argument_name; + $this->value .= ' ' . $this->argument_name; } if (is_string($this->argument_type)) { - $built .= ' ' . $this->argument_type; + $this->value .= ' ' . $this->argument_type; } - return c_base_return_string::s_new($built); + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->argument_mode = NULL; + $this->argument_name = NULL; + $this->argument_type = NULL; + + return new c_base_return_true(); } } @@ -951,39 +1008,48 @@ class c_base_query_argument_aggregate_signature extends c_base_query_argument_ag */ public function do_build_argument() { if ($this->argument_all === TRUE) { - $built = '*'; + $this->value = '*'; } else { if (!is_string($this->argument_type)) { - $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); + return new c_base_return_false(); } - $built = ''; + $this->value = ''; if ($this->argument_mode === static::QUERY_MODE_IN) { - $built .= c_base_query_string::IN; + $this->value .= c_base_query_string::IN; } - elseif ($this->argument_mode === static::QUERY_MODE_VARIADIC) { - $built .= c_base_query_string::VARIADIC; + else if ($this->argument_mode === static::QUERY_MODE_VARIADIC) { + $this->value .= c_base_query_string::VARIADIC; } if (is_string($this->argument_name)) { - $built .= ' ' . $this->argument_name; + $this->value .= ' ' . $this->argument_name; } if (is_string($this->argument_type)) { - $built .= ' ' . $this->argument_type; + $this->value .= ' ' . $this->argument_type; } } - return c_base_return_string::s_new($built); + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->argument_all = NULL; + + return new c_base_return_true(); } } /** * Provide an SQL argument database option. */ -class c_base_query_argument_database_option extends i_base_query_argument { +class c_base_query_argument_database_option extends c_base_return_string implements i_base_query_argument { protected $argument_allow_connection; protected $argument_connection_limit; protected $argument_is_template; @@ -1156,23 +1222,23 @@ class c_base_query_argument_database_option extends i_base_query_argument { } /** - * Get the assigned SQL argument allow connection. + * Get the assigned SQL argument is template. * * @return bool|null * TRUE if enabled, FALSE otherwise. * NULL is returned if not assigned. * NULL with the error bit set is returned on error. */ - public function get_argument_allow_connection() { - if (is_null($this->argument_allow_connection)) { + public function get_argument_is_template() { + if (is_null($this->argument_is_template)) { return new c_base_return_null(); } - if (is_bool($this->argument_allow_connection)) { - return c_base_return_bool::s_new($this->argument_allow_connection); + if (is_bool($this->argument_is_template)) { + return c_base_return_bool::s_new($this->argument_is_template); } - $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_allow_connection', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_is_template', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); return c_base_return_error::s_null($error); } @@ -1180,38 +1246,239 @@ class c_base_query_argument_database_option extends i_base_query_argument { * Implements do_build_argument(). */ public function do_build_argument() { - $built = NULL; + $this->value = NULL; if (is_bool($this->argument_allow_connection)) { - $built .= ' ' c_base_query_string::ALLOW_CONNECTION; + $this->value .= ' ' . c_base_query_string::ALLOW_CONNECTION; if ($this->argument_connection_limit) { - $built .= ' ' . c_base_query_string::TRUE; + $this->value .= ' ' . c_base_query_string::TRUE; } else { - $built .= ' ' . c_base_query_string::FALSE; + $this->value .= ' ' . c_base_query_string::FALSE; } } if (is_int($this->argument_connection_limit)) { - $built .= ' ' . c_base_query_string::CONNECTION_LIMIT . ' ' . $this->argument_connection_limit; + $this->value .= ' ' . c_base_query_string::CONNECTION_LIMIT . ' ' . $this->argument_connection_limit; } if (is_bool($this->argument_is_template)) { - $built .= ' ' . c_base_query_string::IS_TEMPLATE; + $this->value .= ' ' . c_base_query_string::IS_TEMPLATE; if ($this->argument_is_template) { - $built .= ' ' . c_base_query_string::TRUE; + $this->value .= ' ' . c_base_query_string::TRUE; } else { - $built .= ' ' . c_base_query_string::FALSE; + $this->value .= ' ' . c_base_query_string::FALSE; } } - if (is_null($built)) { - $built = ''; + if (is_null($this->value)) { + return new c_base_return_false(); + } + + $this->value = c_base_query_string::WITH . ' ' . $this->value; + + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->argument_allow_connection = NULL; + $this->argument_connection_limit = NULL; + $this->argument_is_template = NULL; + + return new c_base_return_true(); + } +} + +/** + * Provide an SQL role name base structure. + */ +class c_base_query_argument_role_name extends c_base_return_string implements i_base_query_argument { + public const QUERY_ARGUMENT_MODE_NONE = 0; + public const QUERY_ARGUMENT_MODE_PUBLIC = 1; + public const QUERY_ARGUMENT_MODE_NAME = 2; + public const QUERY_ARGUMENT_MODE_GROUP = 3; + + protected $argument_mode; + protected $argument_name; + + /** + * Class constructor. + */ + public function __construct() { + parent::__construct(); + + $this->argument_mode = NULL; + $this->argument_name = NULL; + } + + /** + * Class destructor. + */ + public function __destruct() { + unset($this->argument_mode); + unset($this->argument_name); + + parent::__destruct(); + } + + /** + * @see: t_base_return_value::p_s_new() + */ + public static function s_new($value) { + return self::p_s_new($value, __CLASS__); + } + + /** + * @see: t_base_return_value::p_s_value() + */ + public static function s_value($return) { + return self::p_s_value($return, __CLASS__); + } + + /** + * @see: t_base_return_value_exact::p_s_value_exact() + */ + public static function s_value_exact($return) { + return self::p_s_value_exact($return, __CLASS__, ''); + } + + /** + * Set the SQL argument mode. + * + * @param int|null $argument_mode + * The argument mode to assign. + * Set to NULL to remove the assigned mode. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + * FALSE with the error bit set is returned on error. + */ + public function set_argument_mode($argument_mode) { + if (is_null($argument_mode)) { + $this->argument_mode = NULL; + return new c_base_return_true(); + } + + if (is_int($argument_mode)) { + $this->argument_mode = $argument_mode; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_mode}' => 'argument_mode', ':{function_mode}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Set the SQL argument name. + * + * @param string|null $argument_name + * The argument name to assign. + * Set to NULL to remove the assigned name. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + * FALSE with the error bit set is returned on error. + */ + public function set_argument_name($argument_name) { + if (is_null($argument_name)) { + $this->argument_name = NULL; + return new c_base_return_true(); + } + + if (is_string($argument_name)) { + $this->argument_name = $argument_name; + return new c_base_return_true(); + } + + $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); + } + + /** + * Get the assigned SQL argument mode. + * + * @return int|null + * A valid query argument mode. + * NULL is returned if not assigned. + * NULL with the error bit set is returned on error. + */ + public function get_argument_mode() { + if (is_null($this->argument_mode)) { + return new c_base_return_null(); + } + + if (is_string($this->argument_mode)) { + return c_base_return_int::s_new($this->argument_mode); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_mode}' => 'argument_mode', ':{function_mode}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Get the assigned SQL argument name. + * + * @return string|null + * A valid query argument name. + * NULL is returned if not assigned. + * NULL with the error bit set is returned on error. + */ + public function get_argument_name() { + if (is_null($this->argument_name)) { + return new c_base_return_null(); + } + + if (is_string($this->argument_name)) { + return c_base_return_string::s_new($this->argument_name); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'argument_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } + + /** + * Implements do_build_argument(). + */ + public function do_build_argument() { + if (!is_string($this->argument_type)) { + return new c_base_return_false(); + } + + $this->value = ''; + if ($this->argument_mode === static::QUERY_ARGUMENT_MODE_PUBLIC) { + $this->value .= ' ' . c_base_query_string::PUBLIC; + } + else if ($this->argument_mode === static::QUERY_ARGUMENT_MODE_NAME) { + if (is_string($this->argument_name)) { + $this->value .= ' ' . $this->argument_name; + } + } + else if ($this->argument_mode === static::QUERY_ARGUMENT_MODE_GROUP) { + $this->value .= ' ' . c_base_query_string::GROUP; + + if (is_string($this->argument_name)) { + $this->value .= ' ' . $this->argument_name; + } } else { - $built = c_base_query_string::WITH . ' ' . $built; + return new c_base_return_false(); } - return c_base_return_string::s_new($built); + return new c_base_return_true(); + } + + /** + * Implements do_build_argument(). + */ + public function do_reset_argument() { + $this->value = NULL; + $this->argument_mode = NULL; + $this->argument_name = NULL; + + return new c_base_return_true(); } } diff --git a/common/base/classes/base_query_alter_aggregate.php b/common/base/classes/base_query_alter_aggregate.php index 27df082..a25fd43 100644 --- a/common/base/classes/base_query_alter_aggregate.php +++ b/common/base/classes/base_query_alter_aggregate.php @@ -90,7 +90,7 @@ class c_base_query_alter_aggregate extends c_base_query { * When NULL, this will remove all aggregate signatures regardless of the $append parameter. * @param bool $append * (optional) When TRUE, the aggregate signatures will be appended. - * When FALSE, any existing aggregate signatures will be cleared with this value assigned. + * When FALSE, any existing aggregate signatures will be cleared before appending the aggregate signature. * * @return c_base_return_status * TRUE on success, FALSE otherwise. @@ -135,7 +135,7 @@ class c_base_query_alter_aggregate extends c_base_query { * When NULL, this will remove all modes regardless of the $append parameter. * @param bool $append * (optional) When TRUE, the argument mode will be appended. - * When FALSE, any existing modes will be cleared with this value assigned. + * When FALSE, any existing aggregate signatures will be cleared before appending the aggregate signature. * * @return c_base_return_status * TRUE on success, FALSE otherwise. @@ -178,7 +178,7 @@ class c_base_query_alter_aggregate extends c_base_query { * (optional) Get the argument signature at the specified index. * When NULL, all argument signatures are returned. * - * @return c_base_query_argument_aggregate_signature|c_base_return_null + * @return c_base_query_argument_aggregate_signature|c_base_return_array|c_base_return_null * An array of aggregate signatures or NULL if not defined. * A single aggregate signature is returned if $index is an integer. * NULL with the error bit set is returned on error. @@ -190,7 +190,7 @@ class c_base_query_alter_aggregate extends c_base_query { if (is_null($index)) { if (is_array($this->aggregate_signatures)) { - return $this->aggregate_signatures; + return c_base_return_array::s_new($this->aggregate_signatures); } } else { @@ -213,7 +213,7 @@ class c_base_query_alter_aggregate extends c_base_query { * (optional) Get the argument signature at the specified index. * When NULL, all argument signatures are returned. * - * @return c_base_query_argument_aggregate_signature|c_base_return_null + * @return c_base_query_argument_aggregate_signature|c_base_return_array|c_base_return_null * An array of order by aggregate signatures or NULL if not defined. * A single order by aggregate signature is returned if $index is an integer. * NULL with the error bit set is returned on error. @@ -225,7 +225,7 @@ class c_base_query_alter_aggregate extends c_base_query { if (is_null($index)) { if (is_array($this->order_by_signatures)) { - return $this->order_by_signatures; + return c_base_return_array::s_new($this->order_by_signatures); } } else { @@ -268,11 +268,8 @@ class c_base_query_alter_aggregate extends c_base_query { $aggregate_signatures .= ', '; } - $signature = $aggregate_signature->do_build_argument(); - if ($signature instanceof c_base_return_string) { - $aggregate_signatures .= $signature->get_value_exact(); - } - unset($signature); + $aggregate_signature->do_build_argument(); + $aggregate_signatures .= $aggregate_signature->get_value_exact(); } } unset($aggregate_signature); @@ -288,11 +285,8 @@ class c_base_query_alter_aggregate extends c_base_query { $order_by_signatures .= ', '; } - $signature = $order_by_signature->do_build_argument(); - if ($signature instanceof c_base_return_string) { - $aggregate_signatures .= $signature->get_value_exact(); - } - unset($signature); + $order_by_signature->do_build_argument(); + $aggregate_signatures .= $order_by_signature->get_value_exact(); } } unset($order_by_signature); @@ -300,7 +294,7 @@ class c_base_query_alter_aggregate extends c_base_query { if (is_string($aggregate_signatures)) { $aggregate_signatures = ' (' . $aggregate_signatures; if (is_string($order_by_signatures)) { - $order_by_signatures = ' ' . c_base_query_string::ORDER_BY . ' ' . $order_by_signatures . ''; + $aggregate_signatures = ' ' . c_base_query_string::ORDER_BY . ' ' . $order_by_signatures . ''; } $aggregate_signatures .= ')'; } diff --git a/common/base/classes/base_query_alter_coalation.php b/common/base/classes/base_query_alter_coalation.php index a41a165..253998d 100644 --- a/common/base/classes/base_query_alter_coalation.php +++ b/common/base/classes/base_query_alter_coalation.php @@ -133,7 +133,9 @@ class c_base_query_alter_coalation extends c_base_query { $this->value .= ' ' . $this->query_name; if (is_bool($this->refresh_version)) { - $this->value .= ' ' . c_base_query_string::REFRESH_VERSION; + if ($this->refresh_version) { + $this->value .= ' ' . c_base_query_string::REFRESH_VERSION; + } } elseif (is_string($this->query_rename_to)) { $this->value .= ' ' . $this->pr_QUERY_RENAME_TO . ' (' . $this->query_rename_to . ')'; diff --git a/common/base/classes/base_query_alter_database.php b/common/base/classes/base_query_alter_database.php index 4338dc6..99eee7f 100644 --- a/common/base/classes/base_query_alter_database.php +++ b/common/base/classes/base_query_alter_database.php @@ -94,7 +94,6 @@ class c_base_query_alter_database extends c_base_query { * @param c_base_query_argument_database_option|null $option * The database options to use. * Set to NULL to disable. - * When NULL, this will remove all modes regardless of the $append parameter. * * @return c_base_return_status * TRUE on success, FALSE otherwise. @@ -148,11 +147,8 @@ class c_base_query_alter_database extends c_base_query { $this->value .= ' ' . $this->query_name; if ($this->option instanceof c_base_query_argument_database_option) { - $built = $this->option->do_build_argument(); - if ($built instanceof c_base_return_string) { - $this->value .= ' ' . $built->get_value_exact(); - } - unset($built); + $this->option->do_build_argument(); + $this->value .= ' ' . $this->option->get_value_exact(); } elseif (is_string($this->query_rename_to)) { $this->value .= ' ' . c_base_query_string::RENAME_TO . ' (' . $this->query_rename_to . ')'; diff --git a/common/base/classes/base_query_alter_default_privileges.php b/common/base/classes/base_query_alter_default_privileges.php index 0140213..04eaf5f 100644 --- a/common/base/classes/base_query_alter_default_privileges.php +++ b/common/base/classes/base_query_alter_default_privileges.php @@ -14,19 +14,66 @@ require_once('common/base/traits/base_query.php'); /** * The class for building and returning a Postgresql ALTER DEFAULT PRIVILEGES query string. * - * When no argument mode is specified, then a wildcard * is auto-provided for the aggregate_signature parameter. - * - * @see: https://www.postgresql.org/docs/current/static/sql-alteraggregate.html + * @see: https://www.postgresql.org/docs/current/static/sql-alterdefaultpriveleges.html */ -class c_base_query_alter_coalation extends c_base_query { +class c_base_query_alter_default_priveleges extends c_base_query { + public const ACTION_NONE = 0; + public const ACTION_GRANT = 1; + public const ACTION_REVOKE = 2; + + public const OPTION_NONE = 0; + public const OPTION_CASCADE = 1; + public const OPTION_RESTRICT = 2; + + public const ON_NONE = 0; + public const ON_TABLES_TO = 1; + public const ON_SEQUENCES = 2; + public const ON_FUNCTIONS = 3; + public const ON_TYPES = 4; + public const ON_SCHEMAS = 5; + + public const PRIVILEGE_NONE = 0; + public const PRIVILEGE_SELECT = 1; + public const PRIVILEGE_INSERT = 2; + public const PRIVILEGE_UPDATE = 3; + public const PRIVILEGE_DELETE = 4; + public const PRIVILEGE_TRUNCATE = 5; + public const PRIVILEGE_REFERENCES = 6; + public const PRIVILEGE_TRIGGER = 7; + public const PRIVILEGE_USAGE = 8; + public const PRIVILEGE_EXECUTE = 9; + public const PRIVILEGE_CREATE = 10; + public const PRIVILEGE_ALL = 11; + + use t_base_query_in_schema; + protected const pr_QUERY_COMMAND = 'alter default privileges'; + protected $abbreviated; + protected $action; + protected $option; + protected $option_grant; + protected $on; + protected $privileges; + protected $role_names; /** * Class constructor. */ public function __construct() { parent::__construct(); + + $this->query_in_schema = NULL; + $this->for_targets = NULL; + + $this->abbreviated = NULL; + $this->action = NULL; + $this->action_scope = NULL; + $this->option = NULL; + $this->option_grant = NULL; + $this->on = NULL; + $this->privileges = NULL; + $this->role_names = NULL; } /** @@ -34,6 +81,15 @@ class c_base_query_alter_coalation extends c_base_query { */ public function __destruct() { parent::__destruct(); + + unset($this->query_in_schema); + + unset($this->abbreviated); + unset($this->action); + unset($this->option); + unset($this->option_grant); + unset($this->privileges); + unset($this->role_names); } /** @@ -58,12 +114,565 @@ class c_base_query_alter_coalation extends c_base_query { } /** + * 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 for role targets regardless of the $append parameter. + * @param bool $append + * (optional) When TRUE, the for role target will be appended. + * When FALSE, any existing for role targets will be cleared before appending the for role target. + * + * @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, $append = TRUE) { + if (is_null($target)) { + $this->target = NULL; + return new c_base_return_true(); + } + + if (is_string($target)) { + if ($append) { + if (!is_array($this->for_role_targets)) { + $this->for_role_targets = []; + } + + $this->for_role_targets[] = $target; + } + else { + $this->for_role_targets = [$target]; + } + + return new c_base_return_true(); + } + elseif ($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); + } + + /** + * Assigns this SQL to be a GRANT/REVOKE operation. + * + * @param int|null $action + * Whether or not to use GRANT or REVOKE 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_action($action) { + if (is_null($grant)) { + $this->action = NULL; + return new c_base_return_true(); + } + + if ($action === static::ACTION_GRANT || $action === static::ACTION_REVOKE) { + $this->action = $action; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'action', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Assigns this SQL to be a CASCADE/RESTRICT operation. + * + * @param int|null $option + * Whether or not to use CASCADE or 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_option($option) { + if (is_null($option)) { + $this->option = NULL; + return new c_base_return_true(); + } + + if ($option === static::OPTION_CASCADE || $option === static::OPTION_RESTRICT) { + $this->option = $option; + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'option', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * 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 + * 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 static::ON_TABLES_TO: + case static::ON_SEQUENCES: + case static::ON_FUNCTIONS: + case static::ON_TYPES: + case static::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 privileges regardless of the $append parameter. + * @param bool $append + * (optional) When TRUE, the aggregate signatures will be appended. + * When FALSE, any existing aggregate signatures will be cleared before appending the aggregate signature. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + * FALSE with error bit set is returned on error. + */ + public function set_privilege($privilege, $append = TRUE) { + if (is_null($privilege)) { + $this->privileges = NULL; + return new c_base_return_true(); + } + + if (!is_bool($append)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (is_int($privilege)) { + if ($append) { + if (!is_array($this->privilege)) { + $this->privileges = []; + } + + $this->privileges[] = $privilege; + } + else { + $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_base_query_argument_role_name|null $role_name + * The role name to use. + * Set to NULL to disable. + * When NULL, this will remove all role names regardless of the $append parameter. + * @param bool $append + * (optional) When TRUE, the role name will be appended. + * When FALSE, any existing role names will be cleared before appending the role name. + * + * @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, $append = TRUE) { + if (is_null($role_name)) { + $this->role_names = NULL; + return new c_base_return_true(); + } + + if (!is_bool($append)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if ($role_name instanceof c_base_query_argument_role_name) { + if ($append) { + if (!is_array($this->role_names)) { + $this->role_names = []; + } + + $this->role_names[] = $role_name; + } + else { + $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_base_query_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_bse_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_base_query_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 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 GRANT/REVOKE status. + * + * @return c_base_return_int|c_base_return_null + * Integer representing the action is returned on success. + * NULL is returned if undefined. + * FALSE with error bit set is returned on error. + */ + protected function get_action() { + if (is_null($this->action)) { + return new c_base_return_null(); + } + + return c_base_return_int::s_new($this->action); + } + + /** + * Get the CASCADE/RESTRICT status. + * + * @return c_base_return_int|c_base_return_null + * An integer representing the CASCADE/RESTRICT option. + * NULL is returned if undefined. + * FALSE with error bit set is returned on error. + */ + protected function get_option() { + if (is_null($this->option)) { + return new c_base_return_null(); + } + + return c_base_return_int::s_new($this->option); + } + + /** + * 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 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->aggregate_signatures); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->aggregate_signatures) && $this->aggregate_signatures[$index] instanceof c_base_query_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() { - $this->value = NULL; + if (is_null($this->grant) || !(is_array($this->role_names) && !empty($this->role_names))) { + return new c_base_return_false(); + } + + $this->value = static::pr_QUERY_COMMAND; + + // [ FOR ROLE target_role [, ... ] ] + if (is_array($this->for_role_targets) && !empty($this->for_role_targets)) { + $this->value .= ' ' . c_base_query_string::FOR . ' ' . c_base_query_string::ROLE; + + $names = NULL; + foreach ($this->for_role_targets as $schema_name) { + $names .= ', ' . $schema_name; + } + + $this->value .= ltrim($names, ','); + unset($names); + } + + // [ IN SCHEMA schema_name [, ... ] ] + if (is_array($this->query_in_schema) && !empty($this->query_in_schema)) { + $this->value .= ' ' . c_base_query_string::IN . ' ' . c_base_query_string::SCHEMA; + + $names = NULL; + foreach ($this->query_in_schema as $schema_name) { + $names .= ', ' . $schema_name; + } + + $this->value .= ltrim($names, ','); + unset($names); + } + + if ($this->action === static::ACTION_GRANT) { + $this->value .= ' ' . c_base_query_string::GRANT; + } + else if ($this->action === static::ACTION_REVOKE) { + $this->value .= ' ' . c_base_query_string::REVOKE; + + if ($this->option_grant) { + $this->value .= ' ' . c_base_query_string::GRANT_OPTION_FOR; + } + } + + // [ { SELECT | INSERT | UPDATE | DELETE | TRUNCATE | REFERENCES | TRIGGER | USAGE | EXECUTE | CREATE } [, ...] | ALL ] + $privileges = ''; + foreach ($this->privileges as $privilege) { + $privileges .= ', '; + switch ($privilege) { + case static::PRIVILEGE_SELECT: + $privileges .= c_base_query_string::SELECT; + break; + case static::PRIVILEGE_INSERT: + $privileges .= c_base_query_string::INSERT; + break; + case static::PRIVILEGE_UPDATE: + $privileges .= c_base_query_string::UPDATE; + break; + case static::PRIVILEGE_DELETE: + $privileges .= c_base_query_string::DELETE; + break; + case static::PRIVILEGE_TRUNCATE: + $privileges .= c_base_query_string::TRUNCATE; + break; + case static::PRIVILEGE_REFERENCES: + $privileges .= c_base_query_string::REFERENCES; + break; + case static::PRIVILEGE_TRIGGER: + $privileges .= c_base_query_string::TRIGGER; + break; + case static::PRIVILEGE_USAGE: + $privileges .= c_base_query_string::USAGE; + break; + case static::PRIVILEGE_EXECUTE: + $privileges .= c_base_query_string::EXECUTE; + break; + case static::PRIVILEGE_CREATE: + $privileges .= c_base_query_string::CREATE; + break; + case static::PRIVILEGE_ALL: + $privileges .= c_base_query_string::ALL; + break; + default: + break; + } + } + + $this->value .= ltrim($privileges, ','); + + // ON ... + switch($this->on) { + case static::ON_TABLES_TO: + $this->value .= ' ' . c_base_query_string::ON_TABLES_TO; + break; + case static::ON_SEQUENCES: + $this->value .= ' ' . c_base_query_string::ON_SEQUENCES; + break; + case static::ON_FUNCTIONS: + $this->value .= ' ' . c_base_query_string::ON_FUNCTIONS; + break; + case static::ON_TYPES: + $this->value .= ' ' . c_base_query_string::ON_TYPES; + break; + case static::ON_SCHEMAS: + $this->value .= ' ' . c_base_query_string::ON_SCHEMAS; + break; + } + + // [ TO | FROM ] ... role names ... + if ($this->action === static::ACTION_GRANT) { + $this->value .= ' ' . c_base_query_string::TO; + } + else if ($this->action === static::ACTION_REVOKE) { + $this->value .= ' ' . c_base_query_string::FROM; + } + + foreach ($this->role_names as $role_name) { + if (!($role_name instanceof c_base_query_argument_role_name)) { + continue; + } + + $role_name->do_build_argument(); + $this->value .= ' ' . $role_name->get_value_exact(); + } - // @todo + if ($this->action === static::ACTION_GRANT) { + // [ WITH GRANT OPTION ] + if ($this->option_grant) { + $this->value .= ' ' . c_base_query_string::WITH_GRANT_OPTION; + } + } + else if ($this->action === static::ACTION_REVOKE) { + // [ CASCADE | RESTRICT ] + if ($this->option === static::OPTION_CASCADE) { + $this->value .= ' ' . c_base_query_string::CASCADE; + } + else if ($this->option === static::OPTION_RESTRICT) { + $this->value .= ' ' . c_base_query_string::RESTRICT; + } + } return new c_base_return_true(); } diff --git a/common/base/classes/base_query_code.php b/common/base/classes/base_query_code.php index 3a7d1ca..e9a03ad 100644 --- a/common/base/classes/base_query_code.php +++ b/common/base/classes/base_query_code.php @@ -10,7 +10,7 @@ namespace n_koopa; /** * Codes associated with postgresql user/session information. */ -static class c_base_query_code_user { +class c_base_query_code_user { public const NONE = 0; public const CURRENT = 1; public const SESSION = 2; @@ -20,7 +20,7 @@ static class c_base_query_code_user { /** * Codes associated with GROUP BY, ORDER BY, and related queries. */ -static class c_base_query_code_direction { +class c_base_query_code_direction { public const NONE = 0; public const ASCEND = 1; public const DESCEND = 2; @@ -29,7 +29,7 @@ static class c_base_query_code_direction { /** * Codes associated with SET and related queries. */ -static class c_base_query_code_set { +class c_base_query_code_set { public const NONE = 0; public const TO = 1; public const EQUAL = 2; @@ -39,7 +39,7 @@ static class c_base_query_code_set { /** * Codes associated with RESET and related queries. */ -static class c_base_query_code_reset { +class c_base_query_code_reset { public const NONE = 0; public const ALL = 1; public const PARAMETER = 2; diff --git a/common/base/classes/base_query_string.php b/common/base/classes/base_query_string.php index 77d1b34..0034734 100644 --- a/common/base/classes/base_query_string.php +++ b/common/base/classes/base_query_string.php @@ -10,11 +10,15 @@ namespace n_koopa; /** * A collection of strings used for generating SQL. */ -static class c_base_query_string { +class c_base_query_string { public const OWNER_TO = 'owner to'; public const USER_CURRENT = 'current_user'; public const USER_SESSION = 'session_user'; + public const PUBLIC = 'public'; + public const ROLE = 'role'; + public const GROUP = 'group'; + public const REFRESH_VERSION = 'refresh version'; public const SET_SCHEMA = 'set schema'; @@ -26,6 +30,8 @@ static class c_base_query_string { public const IN = 'in'; public const WITH = 'with'; public const TO = 'to'; + public const FOR = 'for'; + public const ALL = 'all'; public const VARIADIC = 'variadic'; @@ -36,6 +42,29 @@ static class c_base_query_string { public const ASCEND = 'asc'; public const DESCEND = 'desc'; + public const GRANT = 'grant'; + public const REVOKE = 'revoke'; + + public const WITH_GRANT_OPTION = 'with grant option'; + public const GRANT_OPTION_FOR = 'grant option for'; + + public const ON_TABLES_TO = 'on tables to'; + public const ON_SEQUENCES = 'on sequences'; + public const ON_FUNCTIONS = 'on functons'; + public const ON_TYPES = 'on types'; + public const ON_SCHEMAS = 'on schemas'; + + public const SELECT = 'select'; + public const INSERT = 'insert'; + public const UPDATE = 'update'; + public const DELETE = 'delete'; + public const TRUNCATE = 'truncate'; + public const REFERENCES = 'references'; + public const TRIGGER = 'trigger'; + public const USAGE = 'usage'; + public const EXECUTE = 'execute'; + public const CREATE = 'create'; + public const TRUE = 'true'; public const FALSE = 'false'; @@ -45,4 +74,7 @@ static class c_base_query_string { public const SET_TABLESPACE = 'set tablespace'; public const FROM_CURRENT = 'from current'; + + public const CASCADE = 'cascade'; + public const RESTRICT = 'restrict'; } diff --git a/common/base/interfaces/base_query.php b/common/base/interfaces/base_query.php index b214bdf..628b380 100644 --- a/common/base/interfaces/base_query.php +++ b/common/base/interfaces/base_query.php @@ -16,13 +16,14 @@ interface i_base_query { * Build the Postgresql query string. * * @return c_base_return_status - * TRUE on success, FALSE otherwise. + * TRUE on success. + * FALSE without error bit set is returned if there is nothing to build. * FALSE with the error bit set is returned on error. */ public function do_build(); /** - * Reset all values in this class. + * Reset all query values in this class. * * @return c_base_return_status * TRUE on success, FALSE otherwise. @@ -37,12 +38,21 @@ interface i_base_query { interface i_base_query_argument { /** - * Return a processed argument. + * Build the Postgresql query argument. * - * @return c_base_return_string|c_base_return_false - * An SQL string representing the built argument. + * @return c_base_return_bool + * TRUE is returned on success. * FALSE without error bit set is returned if there is nothing to build. * FALSE with the error bit set is returned on error. */ public function do_build_argument(); + + /** + * Reset all argument values in this class. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + * FALSE with the error bit set is returned on error. + */ + public function do_reset_argument(); } diff --git a/common/base/traits/base_query.php b/common/base/traits/base_query.php index eab14a1..c6eb14e 100644 --- a/common/base/traits/base_query.php +++ b/common/base/traits/base_query.php @@ -657,3 +657,90 @@ trait t_base_query_reset { return c_base_return_error::s_null($error); } } + +/** + * Provide the sql IN SCHEMA functionality. + */ +trait t_base_query_in_schema { + protected $query_in_schema; + + /** + * Set the in schema, schema names. + * + * @param string|null $schema_name + * The schema name to use. + * Set to NULL to disable. + * When NULL, this will remove all schema names regardless of the $append parameter. + * @param bool $append + * (optional) When TRUE, the schema name will be appended. + * When FALSE, any existing schema names will be cleared before appending the schema name. + * + * @return c_base_return_status + * TRUE on success, FALSE otherwise. + * FALSE with error bit set is returned on error. + */ + public function set_query_in_schema($schema_name, $append = TRUE) { + if (is_null($schema_name)) { + $this->query_in_schema = NULL; + return new c_base_return_true(); + } + + if (!is_bool($append)) { + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'append', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + if (is_string($schema_name)) { + if ($append) { + if (!is_array($this->query_in_schema)) { + $this->query_in_schema = []; + } + + $this->query_in_schema[] = $schema_name; + } + else { + $this->query_in_schema = [$schema_name]; + } + + return new c_base_return_true(); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{argument_name}' => 'schema_name', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_ARGUMENT); + return c_base_return_error::s_false($error); + } + + /** + * Get the in schema, schema names. + * + * @param int|null $index + * (optional) Get the schema name at the specified index. + * When NULL, all schema names are returned. + * + * @return c_base_return_string|c_base_return_array|c_base_return_null + * An array of schema names or NULL if not defined. + * A single schema name is returned if $index is an integer. + * NULL with the error bit set is returned on error. + */ + public function get_query_in_schema($index = NULL) { + if (is_null($this->query_in_schema)) { + return new c_base_return_null(); + } + + if (is_null($index)) { + if (is_array($this->query_in_schema)) { + return c_bse_return_array::s_new($this->query_in_schema); + } + } + else { + if (is_int($index) && array_key_exists($index, $this->query_in_schema) && is_string($this->query_in_schema[$index])) { + return c_base_return_string::s_new($this->query_in_schema[$index]); + } + + $error = c_base_error::s_log(NULL, ['arguments' => [':{variable_name}' => 'query_in_schema[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}' => 'query_in_schema', ':{function_name}' => __CLASS__ . '->' . __FUNCTION__]], i_base_error_messages::INVALID_VARIABLE); + return c_base_return_error::s_null($error); + } +}