diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 6eadb61ff..4825ee7f4 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -421,12 +421,12 @@ class DataMapperAbstract implements DataMapperInterface $id = self::createOwnsOne($propertyName, $property->getValue($obj)); $value = self::parseValue($column['type'], $id); - $query->insert($column['name'])->value($value, $column['type']); + $query->insert($column['name'])->value($value); } elseif (isset(static::$belongsTo[$propertyName])) { $id = self::createBelongsTo($propertyName, $property->getValue($obj)); $value = self::parseValue($column['type'], $id); - $query->insert($column['name'])->value($value, $column['type']); + $query->insert($column['name'])->value($value); } elseif ($column['name'] !== static::$primaryField) { $tValue = $property->getValue($obj); if (\stripos($column['internal'], '/') !== false) { @@ -439,7 +439,7 @@ class DataMapperAbstract implements DataMapperInterface $value = self::parseValue($column['type'], $tValue); - $query->insert($column['name'])->value($value, $column['type']); + $query->insert($column['name'])->value($value); } if (!$isPublic) { @@ -449,7 +449,7 @@ class DataMapperAbstract implements DataMapperInterface // if a table only has a single column = primary key column. This must be done otherwise the query is empty if ($query->getType() === QueryType::NONE) { - $query->insert(static::$primaryField)->value(0, static::$columns[static::$primaryField]['type']); + $query->insert(static::$primaryField)->value(0); } self::$db->con->prepare($query->toSql())->execute(); @@ -490,22 +490,22 @@ class DataMapperAbstract implements DataMapperInterface $id = self::createOwnsOneArray($column['internal'], $property); $value = self::parseValue($column['type'], $id); - $query->insert($column['name'])->value($value, $column['type']); + $query->insert($column['name'])->value($value); } elseif (isset(static::$belongsTo[$path])) { $id = self::createBelongsToArray($column['internal'], $property); $value = self::parseValue($column['type'], $id); - $query->insert($column['name'])->value($value, $column['type']); + $query->insert($column['name'])->value($value); } elseif ($column['internal'] === $path && $column['name'] !== static::$primaryField) { $value = self::parseValue($column['type'], $property); - $query->insert($column['name'])->value($value, $column['type']); + $query->insert($column['name'])->value($value); } } // if a table only has a single column = primary key column. This must be done otherwise the query is empty if ($query->getType() === QueryType::NONE) { - $query->insert(static::$primaryField)->value(0, static::$columns[static::$primaryField]['type']); + $query->insert(static::$primaryField)->value(0); } self::$db->con->prepare($query->toSql())->execute(); @@ -1272,13 +1272,13 @@ class DataMapperAbstract implements DataMapperInterface $value = self::parseValue($column['type'], $id); // todo: should not be done if the id didn't change. but for now don't know if id changed - $query->set([static::$table . '.' . $column['name'] => $value], $column['type']); + $query->set([static::$table . '.' . $column['name'] => $value]); } elseif (isset(static::$belongsTo[$propertyName])) { $id = self::updateBelongsTo($propertyName, $property->getValue($obj), $relations, $depth); $value = self::parseValue($column['type'], $id); // todo: should not be done if the id didn't change. but for now don't know if id changed - $query->set([static::$table . '.' . $column['name'] => $value], $column['type']); + $query->set([static::$table . '.' . $column['name'] => $value]); } elseif ($column['name'] !== static::$primaryField) { $tValue = $property->getValue($obj); if (\stripos($column['internal'], '/') !== false) { @@ -1290,7 +1290,7 @@ class DataMapperAbstract implements DataMapperInterface } $value = self::parseValue($column['type'], $tValue); - $query->set([static::$table . '.' . $column['name'] => $value], $column['type']); + $query->set([static::$table . '.' . $column['name'] => $value]); } if (!$isPublic) { @@ -1340,17 +1340,17 @@ class DataMapperAbstract implements DataMapperInterface $value = self::parseValue($column['type'], $id); // todo: should not be done if the id didn't change. but for now don't know if id changed - $query->set([static::$table . '.' . $column['name'] => $value], $column['type']); + $query->set([static::$table . '.' . $column['name'] => $value]); } elseif (isset(static::$belongsTo[$path])) { $id = self::updateBelongsToArray($column['internal'], $property, $relations, $depth); $value = self::parseValue($column['type'], $id); // todo: should not be done if the id didn't change. but for now don't know if id changed - $query->set([static::$table . '.' . $column['name'] => $value], $column['type']); + $query->set([static::$table . '.' . $column['name'] => $value]); } elseif ($column['name'] !== static::$primaryField) { $value = self::parseValue($column['type'], $property); - $query->set([static::$table . '.' . $column['name'] => $value], $column['type']); + $query->set([static::$table . '.' . $column['name'] => $value]); } } diff --git a/DataStorage/Database/Query/Builder.php b/DataStorage/Database/Query/Builder.php index aa0e55ba3..0604776ba 100644 --- a/DataStorage/Database/Query/Builder.php +++ b/DataStorage/Database/Query/Builder.php @@ -943,17 +943,22 @@ final class Builder extends BuilderAbstract * Values to insert. * * @param mixed $value Values - * @param string $type Data type to insert * * @return Builder * * @since 1.0.0 */ - public function value($value, string $type = 'string') : Builder + public function value($value) : Builder { \end($this->values); - $key = \key($this->values); - $this->values[$key][] = $value; + $key = \key($this->values); + + if (\is_array($value)) { + $this->values[$key] = $value; + } else { + $this->values[$key][] = $value; + } + \reset($this->values); return $this; @@ -970,7 +975,7 @@ final class Builder extends BuilderAbstract */ public function sets(...$sets) : Builder { - $this->sets[] = $sets; + $this->sets[$sets[0]] = $sets[1] ?? null; return $this; } @@ -979,15 +984,14 @@ final class Builder extends BuilderAbstract * Values to insert. * * @param mixed $set Values - * @param string $type Data type to insert * * @return Builder * * @since 1.0.0 */ - public function set($set, string $type = 'string') : Builder + public function set($set) : Builder { - $this->sets[key($set)] = \current($set); + $this->sets[\key($set)] = \current($set); return $this; } diff --git a/Model/Html/Meta.php b/Model/Html/Meta.php index 1d6ccb189..c455541b5 100644 --- a/Model/Html/Meta.php +++ b/Model/Html/Meta.php @@ -240,7 +240,7 @@ class Meta implements RenderableInterface */ public function render() : string { - return (\count($this->keywords) > 0 ? '"' : '') + return (\count($this->keywords) > 0 ? '' : '') . (!empty($this->author) ? '' : '') . (!empty($this->description) ? '' : '') . (!empty($this->charset) ? '' : '') diff --git a/Module/InstallerAbstract.php b/Module/InstallerAbstract.php index b7a8a2418..3567e91ec 100644 --- a/Module/InstallerAbstract.php +++ b/Module/InstallerAbstract.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace phpOMS\Module; +use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\DataStorage\Database\DatabaseType; use phpOMS\DataStorage\Database\Exception\InvalidDatabaseTypeException; use phpOMS\DataStorage\Database\DatabasePool; @@ -45,43 +46,33 @@ class InstallerAbstract */ public static function registerInDatabase(DatabasePool $dbPool, InfoManager $info) : void { - switch ($dbPool->get()->getType()) { - case DatabaseType::MYSQL: - $dbPool->get()->con->beginTransaction(); + $queryModule = new Builder($dbPool->get('insert')); + $queryModule->prefix($dbPool->get('insert')->prefix); + $queryModule->insert('module_id', 'module_theme', 'module_path', 'module_active', 'module_version') + ->into('module') + ->values($info->getInternalName(), 'Default', $info->getDirectory(), 0, $info->getVersion()) + ->execute(); - $sth = $dbPool->get()->con->prepare( - 'INSERT INTO `' . $dbPool->get()->prefix . 'module` (`module_id`, `module_theme`, `module_path`, `module_active`, `module_version`) VALUES - (:internal, :theme, :path, :active, :version);' + $queryLoad = new Builder($dbPool->get('insert')); + $queryLoad->prefix($dbPool->get('insert')->prefix); + $queryLoad->insert('module_load_pid', 'module_load_type', 'module_load_from', 'module_load_for', 'module_load_file') + ->into('module_load'); + + $load = $info->getLoad(); + foreach ($load as $val) { + foreach ($val['pid'] as $pid) { + $queryLoad->values( + sha1(\str_replace('/', '', $pid)), + (int) $val['type'], + $val['from'], + $val['for'], + $val['file'] ); + } + } - $sth->bindValue(':internal', $info->getInternalName(), \PDO::PARAM_INT); - $sth->bindValue(':theme', 'Default', \PDO::PARAM_STR); - $sth->bindValue(':path', $info->getDirectory(), \PDO::PARAM_STR); - $sth->bindValue(':active', 0, \PDO::PARAM_INT); - $sth->bindValue(':version', $info->getVersion(), \PDO::PARAM_STR); - $sth->execute(); - - $sth = $dbPool->get()->con->prepare( - 'INSERT INTO `' . $dbPool->get()->prefix . 'module_load` (`module_load_pid`, `module_load_type`, `module_load_from`, `module_load_for`, `module_load_file`) VALUES - (:pid, :type, :from, :for, :file);' - ); - - $load = $info->getLoad(); - foreach ($load as $val) { - foreach ($val['pid'] as $pid) { - $sth->bindValue(':pid', sha1(\str_replace('/', '', $pid)), \PDO::PARAM_STR); - $sth->bindValue(':type', $val['type'], \PDO::PARAM_INT); - $sth->bindValue(':from', $val['from'], \PDO::PARAM_STR); - $sth->bindValue(':for', $val['for'], \PDO::PARAM_STR); - $sth->bindValue(':file', $val['file'], \PDO::PARAM_STR); - $sth->execute(); - } - } - - $dbPool->get()->con->commit(); - break; - default: - throw new InvalidDatabaseTypeException($dbPool->get()->getType()); + if (!empty($queryLoad->getValues())) { + $queryLoad->execute(); } } diff --git a/Module/StatusAbstract.php b/Module/StatusAbstract.php index 8fcd227d3..2aae6483f 100644 --- a/Module/StatusAbstract.php +++ b/Module/StatusAbstract.php @@ -14,6 +14,7 @@ declare(strict_types=1); namespace phpOMS\Module; +use phpOMS\DataStorage\Database\Query\Builder; use phpOMS\DataStorage\Database\DatabaseType; use phpOMS\DataStorage\Database\DatabasePool; use phpOMS\DataStorage\Database\Exception\InvalidDatabaseTypeException; @@ -72,24 +73,12 @@ class StatusAbstract */ public static function activateInDatabase(DatabasePool $dbPool, InfoManager $info) : void { - switch ($dbPool->get()->getType()) { - case DatabaseType::MYSQL: - $dbPool->get()->con->beginTransaction(); - - $sth = $dbPool->get()->con->prepare( - 'UPDATE `' . $dbPool->get()->prefix . 'module` SET `module_active` = :active WHERE `module_id` = :internal;' - ); - - $sth->bindValue(':internal', $info->getInternalName(), \PDO::PARAM_INT); - $sth->bindValue(':active', 1, \PDO::PARAM_INT); - $sth->execute(); - - $dbPool->get()->con->commit(); - - break; - default: - throw new InvalidDatabaseTypeException($dbPool->get()->getType()); - } + $query = new Builder($dbPool->get('update')); + $query->prefix($dbPool->get('update')->prefix); + $query->update('module') + ->sets('module.module_active', 1) + ->where('module.module_id', '=', $info->getInternalName()) + ->execute(); } /** @@ -135,23 +124,11 @@ class StatusAbstract */ public static function deactivateInDatabase(DatabasePool $dbPool, InfoManager $info) : void { - switch ($dbPool->get()->getType()) { - case DatabaseType::MYSQL: - $dbPool->get()->con->beginTransaction(); - - $sth = $dbPool->get()->con->prepare( - 'UPDATE `' . $dbPool->get()->prefix . 'module` SET `module_active` = :active WHERE `module_id` = :internal;' - ); - - $sth->bindValue(':internal', $info->getInternalName(), \PDO::PARAM_INT); - $sth->bindValue(':active', 0, \PDO::PARAM_INT); - $sth->execute(); - - $dbPool->get()->con->commit(); - - break; - default: - throw new InvalidDatabaseTypeException($dbPool->get()->getType()); - } + $query = new Builder($dbPool->get('update')); + $query->prefix($dbPool->get('update')->prefix); + $query->update('module') + ->sets('module.module_active', 0) + ->where('module.module_id', '=', $info->getInternalName()) + ->execute(); } } diff --git a/tests/Bootstrap.php b/tests/Bootstrap.php index e3363355c..cd4a3efe5 100644 --- a/tests/Bootstrap.php +++ b/tests/Bootstrap.php @@ -120,5 +120,7 @@ $GLOBALS['session'] = $httpSession; $GLOBALS['dbpool'] = new DatabasePool(); $GLOBALS['dbpool']->create('admin', $CONFIG['db']['core']['masters']['admin']); $GLOBALS['dbpool']->create('select', $CONFIG['db']['core']['masters']['select']); +$GLOBALS['dbpool']->create('insert', $CONFIG['db']['core']['masters']['insert']); +$GLOBALS['dbpool']->create('update', $CONFIG['db']['core']['masters']['update']); DataMapperAbstract::setConnection($GLOBALS['dbpool']->get()); diff --git a/tests/DataStorage/Database/Query/BuilderTest.php b/tests/DataStorage/Database/Query/BuilderTest.php index 1f0377a42..ae331c3c3 100644 --- a/tests/DataStorage/Database/Query/BuilderTest.php +++ b/tests/DataStorage/Database/Query/BuilderTest.php @@ -254,11 +254,19 @@ class BuilderTest extends \PHPUnit\Framework\TestCase $sql = 'INSERT INTO `a` VALUES (1, \'test\');'; self::assertEquals($sql, $query->insert()->into('a')->values(1, 'test')->toSql()); + $query = new Builder($this->con); + $sql = 'INSERT INTO `a` VALUES (1, \'test\');'; + self::assertEquals($sql, $query->insert()->into('a')->value([1, 'test'])->toSql()); + $query = new Builder($this->con); $sql = 'INSERT INTO `a` (`test`, `test2`) VALUES (1, \'test\');'; self::assertEquals($sql, $query->insert('test', 'test2')->into('a')->values(1, 'test')->toSql()); self::assertEquals([[1, 'test']], $query->getValues()); + $query = new Builder($this->con); + $sql = 'INSERT INTO `a` (`test`, `test2`) VALUES (1, \'test\'), (2, \'test2\');'; + self::assertEquals($sql, $query->insert('test', 'test2')->into('a')->values(1, 'test')->values(2, 'test2')->toSql()); + $query = new Builder($this->con); $sql = 'INSERT INTO `a` (`test`, `test2`) VALUES (:test, :test2);'; self::assertEquals($sql, $query->insert('test', 'test2')->into('a')->values(':test', ':test2')->toSql()); @@ -281,6 +289,10 @@ class BuilderTest extends \PHPUnit\Framework\TestCase $sql = 'UPDATE `a` SET `a`.`test` = 1, `a`.`test2` = 2 WHERE `a`.`test` = 1;'; self::assertEquals($sql, $query->update('a')->set(['a.test' => 1])->set(['a.test2' => 2])->where('a.test', '=', 1)->toSql()); + $query = new Builder($this->con); + $sql = 'UPDATE `a` SET `a`.`test` = 1, `a`.`test2` = 2 WHERE `a`.`test` = 1;'; + self::assertEquals($sql, $query->update('a')->sets('a.test', 1)->sets('a.test2', 2)->where('a.test', '=', 1)->toSql()); + $query = new Builder($this->con); $sql = 'UPDATE `a` SET `a`.`test` = 1, `a`.`test2` = :test2 WHERE `a`.`test` = :test3;'; self::assertEquals($sql, $query->update('a')->set(['a.test' => 1])->set(['a.test2' => ':test2'])->where('a.test', '=', ':test3')->toSql()); diff --git a/tests/Model/Html/MetaTest.php b/tests/Model/Html/MetaTest.php index 787fb5909..6c4dde037 100644 --- a/tests/Model/Html/MetaTest.php +++ b/tests/Model/Html/MetaTest.php @@ -42,5 +42,21 @@ class MetaTest extends \PHPUnit\Framework\TestCase $meta->setDescription('some description'); self::assertEquals('some description', $meta->getDescription()); + + $meta->setProperty('og:title', 'TestProperty'); + $meta->setItemprop('title', 'TestItemprop'); + $meta->setName('title', 'TestName'); + + self::assertEquals( + '' + . '' + . '' + . '' + . '' + . '' + . '' + . '', + $meta->render() + ); } }