diff --git a/README.md b/README.md index e75d301..4db43d0 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ This repository contains an index (see below) of all the approved powers within - **final class Insert** | [Details](src/524eb8f6-38d4-47dc-92ad-98b94e099ac0) | [Code](src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.php) | [Settings](src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/settings.json) | [524eb8f6-38d4-47dc-92ad-98b94e099ac0](src/524eb8f6-38d4-47dc-92ad-98b94e099ac0) - **final class Load** | [Details](src/06f8eada-d59b-441c-b287-0aea1793da5a) | [Code](src/06f8eada-d59b-441c-b287-0aea1793da5a/code.php) | [Settings](src/06f8eada-d59b-441c-b287-0aea1793da5a/settings.json) | [06f8eada-d59b-441c-b287-0aea1793da5a](src/06f8eada-d59b-441c-b287-0aea1793da5a) + - **final class Update** | [Details](src/cce56585-58b0-4f72-a92c-e2635ea52d83) | [Code](src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.php) | [Settings](src/cce56585-58b0-4f72-a92c-e2635ea52d83/settings.json) | [cce56585-58b0-4f72-a92c-e2635ea52d83](src/cce56585-58b0-4f72-a92c-e2635ea52d83) - **Namespace**: [VDM\Joomla\Componentbuilder\Interfaces](#vdm-joomla-componentbuilder-interfaces) - **interface ModelInterface** | [Details](src/8aef58c1-3f70-4bd4-b9e4-3f29fcd41cff) | [Code](src/8aef58c1-3f70-4bd4-b9e4-3f29fcd41cff/code.php) | [Settings](src/8aef58c1-3f70-4bd4-b9e4-3f29fcd41cff/settings.json) | [8aef58c1-3f70-4bd4-b9e4-3f29fcd41cff](src/8aef58c1-3f70-4bd4-b9e4-3f29fcd41cff) @@ -305,6 +306,13 @@ namespace VDM\Joomla\Componentbuilder\Database #Olive { # one() : bool # query() : ?object } + + class Update << (F,LightGreen) >> #Green { + + rows() : bool + + items() : bool + + row() : bool + + item() : bool + } } diff --git a/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.php b/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.php index b3c5465..7e149d4 100644 --- a/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.php +++ b/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.php @@ -181,12 +181,6 @@ final class Insert extends Database implements InsertInterface **/ protected function insert(array &$data, string $table, array $columns, bool $isArray): bool { - // get a query object - $query = $this->db->getQuery(true); - - // set the query targets - $query->insert($this->db->quoteName($this->getTable($table)))->columns($this->db->quoteName(array_keys($columns))); - // set joomla default columns $add_created = false; $add_version = false; @@ -200,24 +194,30 @@ final class Insert extends Database implements InsertInterface if (!isset($columns['created'])) { - $columns['created'] = '_|-v-|_'; + $columns['created'] = ' (o_O) '; $add_created = true; } if (!isset($columns['version'])) { - $columns['version'] = '_|-v-|_'; + $columns['version'] = ' (o_O) '; $add_version = true; } if (!isset($columns['version'])) { - $columns['published'] = '_|-v-|_'; + $columns['published'] = ' (o_O) '; $add_published = true; } - // the _|-v-|_ prevents an empty value from being loaded + // the (o_O) prevents an empty value from being loaded } + // get a query object + $query = $this->db->getQuery(true); + + // set the query targets + $query->insert($this->db->quoteName($this->getTable($table)))->columns($this->db->quoteName(array_keys($columns))); + // limiting factor on the amount of rows to insert before we reset the query $limit = 300; @@ -244,7 +244,7 @@ final class Insert extends Database implements InsertInterface $row = []; foreach ($columns as $column => $key) { - if ('_|-v-|_' !== $key) + if (' (o_O) ' !== $key) { $row[] = ($isArray && isset($value[$key])) ? $this->quote($value[$key]) : ((!$isArray && isset($value->{$key})) ? $this->quote($value->{$key}) : ''); @@ -285,7 +285,6 @@ final class Insert extends Database implements InsertInterface $this->defaults(); return true; - } - + } } diff --git a/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.power b/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.power index 7ad3def..932de0d 100644 --- a/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.power +++ b/src/524eb8f6-38d4-47dc-92ad-98b94e099ac0/code.power @@ -154,12 +154,6 @@ **/ protected function insert(array &$data, string $table, array $columns, bool $isArray): bool { - // get a query object - $query = $this->db->getQuery(true); - - // set the query targets - $query->insert($this->db->quoteName($this->getTable($table)))->columns($this->db->quoteName(array_keys($columns))); - // set joomla default columns $add_created = false; $add_version = false; @@ -173,24 +167,30 @@ if (!isset($columns['created'])) { - $columns['created'] = '_|-v-|_'; + $columns['created'] = ' (o_O) '; $add_created = true; } if (!isset($columns['version'])) { - $columns['version'] = '_|-v-|_'; + $columns['version'] = ' (o_O) '; $add_version = true; } if (!isset($columns['version'])) { - $columns['published'] = '_|-v-|_'; + $columns['published'] = ' (o_O) '; $add_published = true; } - // the _|-v-|_ prevents an empty value from being loaded + // the (o_O) prevents an empty value from being loaded } + // get a query object + $query = $this->db->getQuery(true); + + // set the query targets + $query->insert($this->db->quoteName($this->getTable($table)))->columns($this->db->quoteName(array_keys($columns))); + // limiting factor on the amount of rows to insert before we reset the query $limit = 300; @@ -217,7 +217,7 @@ $row = []; foreach ($columns as $column => $key) { - if ('_|-v-|_' !== $key) + if (' (o_O) ' !== $key) { $row[] = ($isArray && isset($value[$key])) ? $this->quote($value[$key]) : ((!$isArray && isset($value->{$key})) ? $this->quote($value->{$key}) : ''); @@ -258,4 +258,4 @@ $this->defaults(); return true; - } + } \ No newline at end of file diff --git a/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.php b/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.php index 1f338db..e219473 100644 --- a/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.php +++ b/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.php @@ -185,7 +185,8 @@ abstract class GuidHelper } } } - return false; + + return null; } /** diff --git a/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.power b/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.power index 938ba00..c7b091b 100644 --- a/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.power +++ b/src/9c513baf-b279-43fd-ae29-a585c8cbc4f0/code.power @@ -160,7 +160,8 @@ } } } - return false; + + return null; } /** diff --git a/src/cce56585-58b0-4f72-a92c-e2635ea52d83/README.md b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/README.md new file mode 100644 index 0000000..1ef394a --- /dev/null +++ b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/README.md @@ -0,0 +1,83 @@ +``` +██████╗ ██████╗ ██╗ ██╗███████╗██████╗ +██╔══██╗██╔═══██╗██║ ██║██╔════╝██╔══██╗ +██████╔╝██║ ██║██║ █╗ ██║█████╗ ██████╔╝ +██╔═══╝ ██║ ██║██║███╗██║██╔══╝ ██╔══██╗ +██║ ╚██████╔╝╚███╔███╔╝███████╗██║ ██║ +╚═╝ ╚═════╝ ╚══╝╚══╝ ╚══════╝╚═╝ ╚═╝ +``` +# final class Update (Details) +> namespace: **VDM\Joomla\Componentbuilder\Database** +```uml +@startuml +class Update << (F,LightGreen) >> #Green { + + rows(array $data, string $key, ...) : bool + + items(array $data, string $key, ...) : bool + + row(array $data, string $key, ...) : bool + + item(object $data, string $key, ...) : bool +} + +note right of Update::rows + Update rows in the database (with remapping and filtering columns option) + + since: 3.2.0 + return: bool + + arguments: + array $data + string $key + string $table + array $columns = [] +end note + +note right of Update::items + Update items in the database (with remapping and filtering columns option) + + since: 3.2.0 + return: bool + + arguments: + array $data + string $key + string $table + array $columns = [] +end note + +note right of Update::row + Update row in the database + + since: 3.2.0 + return: bool + + arguments: + array $data + string $key + string $table +end note + +note right of Update::item + Update item in the database + + since: 3.2.0 + return: bool + + arguments: + object $data + string $key + string $table +end note + +@enduml +``` + +--- +``` + ██╗ ██████╗██████╗ + ██║██╔════╝██╔══██╗ + ██║██║ ██████╔╝ +██ ██║██║ ██╔══██╗ +╚█████╔╝╚██████╗██████╔╝ + ╚════╝ ╚═════╝╚═════╝ +``` +> Build with [Joomla Component Builder](https://git.vdm.dev/joomla/Component-Builder) + diff --git a/src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.php b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.php new file mode 100644 index 0000000..8cce8f4 --- /dev/null +++ b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.php @@ -0,0 +1,189 @@ + + * @git Joomla Component Builder + * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved. + * @license GNU General Public License version 2 or later; see LICENSE.txt + */ + +namespace VDM\Joomla\Componentbuilder\Database; + + +use VDM\Joomla\Componentbuilder\Interfaces\UpdateInterface; +use VDM\Joomla\Componentbuilder\Abstraction\Database; + + +/** + * Database Update Class + * + * @since 3.2.0 + */ +final class Update extends Database implements UpdateInterface +{ + /** + * Update rows in the database (with remapping and filtering columns option) + * + * @param array $data Dataset to update in database [array of arrays (key => value)] + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being updated + * @param array $columns Data columns for remapping and filtering + * + * @return bool + * @since 3.2.0 + **/ + public function rows(array $data, string $key, string $table, array $columns = []): bool + { + // set the update columns + if ($data === [] || strlen($key) == 0) + { + return false; + } + + // set the update values + foreach ($data as $values) + { + if ($columns !== []) + { + // load only what is part of the columns set + $row = []; + foreach ($columns as $column => $key_) + { + if (isset($values[$key_])) + { + $row[$column] = $values[$key_]; + } + } + + // update the row + $this->row($row, $key, $table); + } + else + { + // update the row + $this->row((array) $values, $key, $table); + } + } + + return true; + } + + /** + * Update items in the database (with remapping and filtering columns option) + * + * @param array $data Data to updated in database (array of objects) + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being update + * @param array $columns Data columns for remapping and filtering + * + * @return bool + * @since 3.2.0 + **/ + public function items(array $data, string $key, string $table, array $columns = []): bool + { + // set the update columns + if ($data === [] || strlen($key) == 0) + { + return false; + } + + // set the update values + foreach ($data as $nr => $values) + { + if ($columns !== []) + { + // load only what is part of the columns set + $row = []; + foreach ($columns as $column => $key_) + { + if (isset($values->{$key_})) + { + $row[$column] = $values->{$key_}; + } + } + + // update the row + $this->row($row, $key, $table); + } + else + { + // update the row + $this->row((array) $values, $key, $table); + } + } + + return true; + } + + /** + * Update row in the database + * + * @param array $data Dataset to update in database (key => value) + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being updated + * + * @return bool + * @since 3.2.0 + **/ + public function row(array $data, string $key, string $table): bool + { + // set the update columns + if ($data === [] || strlen($key) == 0) + { + return false; + } + + // get a query object + $query = $this->db->getQuery(true); + + // set the query targets + $query->update($this->db->quoteName($this->getTable($table))); + + // set the update values + $key_ = null; + foreach ($data as $column => $value) + { + if ($column === $key) + { + $key_ = $value; + } + else + { + $query->set($this->db->quoteName($column) . ' = ' . $this->quote($value)); + } + } + + // add the key condition + if ($key_ !== null) + { + $query->where($this->db->quoteName($key) . ' = ' . $this->quote($key_)); + + // execute the final query + $this->db->setQuery($query); + + return $this->db->execute(); + } + + return false; + } + + /** + * Update item in the database + * + * @param object $data Dataset to update in database (key => value) + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being updated + * + * @return bool + * @since 3.2.0 + **/ + public function item(object $data, string $key, string $table): bool + { + // convert to an array + return $this->row((array) get_object_vars($data), $key, $table); + } + +} + diff --git a/src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.power b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.power new file mode 100644 index 0000000..fa55f3c --- /dev/null +++ b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/code.power @@ -0,0 +1,161 @@ + /** + * Update rows in the database (with remapping and filtering columns option) + * + * @param array $data Dataset to update in database [array of arrays (key => value)] + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being updated + * @param array $columns Data columns for remapping and filtering + * + * @return bool + * @since 3.2.0 + **/ + public function rows(array $data, string $key, string $table, array $columns = []): bool + { + // set the update columns + if ($data === [] || strlen($key) == 0) + { + return false; + } + + // set the update values + foreach ($data as $values) + { + if ($columns !== []) + { + // load only what is part of the columns set + $row = []; + foreach ($columns as $column => $key_) + { + if (isset($values[$key_])) + { + $row[$column] = $values[$key_]; + } + } + + // update the row + $this->row($row, $key, $table); + } + else + { + // update the row + $this->row((array) $values, $key, $table); + } + } + + return true; + } + + /** + * Update items in the database (with remapping and filtering columns option) + * + * @param array $data Data to updated in database (array of objects) + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being update + * @param array $columns Data columns for remapping and filtering + * + * @return bool + * @since 3.2.0 + **/ + public function items(array $data, string $key, string $table, array $columns = []): bool + { + // set the update columns + if ($data === [] || strlen($key) == 0) + { + return false; + } + + // set the update values + foreach ($data as $nr => $values) + { + if ($columns !== []) + { + // load only what is part of the columns set + $row = []; + foreach ($columns as $column => $key_) + { + if (isset($values->{$key_})) + { + $row[$column] = $values->{$key_}; + } + } + + // update the row + $this->row($row, $key, $table); + } + else + { + // update the row + $this->row((array) $values, $key, $table); + } + } + + return true; + } + + /** + * Update row in the database + * + * @param array $data Dataset to update in database (key => value) + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being updated + * + * @return bool + * @since 3.2.0 + **/ + public function row(array $data, string $key, string $table): bool + { + // set the update columns + if ($data === [] || strlen($key) == 0) + { + return false; + } + + // get a query object + $query = $this->db->getQuery(true); + + // set the query targets + $query->update($this->db->quoteName($this->getTable($table))); + + // set the update values + $key_ = null; + foreach ($data as $column => $value) + { + if ($column === $key) + { + $key_ = $value; + } + else + { + $query->set($this->db->quoteName($column) . ' = ' . $this->quote($value)); + } + } + + // add the key condition + if ($key_ !== null) + { + $query->where($this->db->quoteName($key) . ' = ' . $this->quote($key_)); + + // execute the final query + $this->db->setQuery($query); + + return $this->db->execute(); + } + + return false; + } + + /** + * Update item in the database + * + * @param object $data Dataset to update in database (key => value) + * @param string $key Dataset key column to use in updating the values in the Database + * @param string $table The table where the data is being updated + * + * @return bool + * @since 3.2.0 + **/ + public function item(object $data, string $key, string $table): bool + { + // convert to an array + return $this->row((array) get_object_vars($data), $key, $table); + } diff --git a/src/cce56585-58b0-4f72-a92c-e2635ea52d83/settings.json b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/settings.json new file mode 100644 index 0000000..d783690 --- /dev/null +++ b/src/cce56585-58b0-4f72-a92c-e2635ea52d83/settings.json @@ -0,0 +1,20 @@ +{ + "add_head": "0", + "add_licensing_template": "2", + "extends": "6cbef8f8-4813-48e3-b05a-65e1aea95171", + "guid": "cce56585-58b0-4f72-a92c-e2635ea52d83", + "implements": [ + "7179fde6-1e51-4b51-8545-7ca18f74a0f4" + ], + "load_selection": null, + "name": "Update", + "power_version": "1.0.0", + "system_name": "JCB.Database.Update", + "type": "final class", + "use_selection": null, + "namespace": "VDM\\Joomla\\Componentbuilder.Database.Update", + "description": "Database Update Class\r\n\r\n@since 3.2.0", + "licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 4th September, 2022\r\n * @author Llewellyn van der Merwe \r\n * @git Joomla Component Builder \r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n", + "head": "", + "composer": "" +} \ No newline at end of file diff --git a/super-powers.json b/super-powers.json index b998d6a..95e7e55 100644 --- a/super-powers.json +++ b/super-powers.json @@ -209,6 +209,16 @@ "path": "src\/584747d1-3a86-453d-b7a3-a2219de8d777", "guid": "584747d1-3a86-453d-b7a3-a2219de8d777" }, + "cce56585-58b0-4f72-a92c-e2635ea52d83": { + "name": "Update", + "type": "final class", + "namespace": "VDM\\Joomla\\Componentbuilder\\Database", + "code": "src\/cce56585-58b0-4f72-a92c-e2635ea52d83\/code.php", + "power": "src\/cce56585-58b0-4f72-a92c-e2635ea52d83\/code.power", + "settings": "src\/cce56585-58b0-4f72-a92c-e2635ea52d83\/settings.json", + "path": "src\/cce56585-58b0-4f72-a92c-e2635ea52d83", + "guid": "cce56585-58b0-4f72-a92c-e2635ea52d83" + }, "b84e947d-2a13-44f7-ac0f-3902ae55ae0c": { "name": "BaseRegistry", "type": "abstract class",