-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
/* | ||
* This file is part of the some package. | ||
* (c) Jakub Janata <jakubjanata@gmail.com> | ||
* For the full copyright and license information, please view the LICENSE file. | ||
*/ | ||
|
||
namespace XcoreCMS\InlineEditing\Model\Simple\PersistenceLayer; | ||
|
||
use PDO as XPDO; | ||
|
||
/** | ||
* @author Jakub Janata <jakubjanata@gmail.com> | ||
*/ | ||
class Pdo extends AbstractPersistenceLayer | ||
{ | ||
/** @var XPDO */ | ||
private $pdo; | ||
|
||
/** | ||
* @param string $tableName | ||
* @param XPDO $pdo | ||
*/ | ||
public function __construct(string $tableName, XPDO $pdo) | ||
{ | ||
parent::__construct($tableName); | ||
$this->pdo = $pdo; | ||
} | ||
|
||
/** | ||
* @param string $sql | ||
* @param array $args | ||
* @return array | ||
*/ | ||
protected function getKeyPairResult(string $sql, array $args): array | ||
{ | ||
$stmt = $this->pdo->prepare($sql); | ||
$stmt->bindValue(1, $args[0]); | ||
$stmt->bindValue(2, $args[1]); | ||
$stmt->execute(); | ||
|
||
return $stmt->fetchAll(XPDO::FETCH_KEY_PAIR) ?: []; | ||
} | ||
|
||
/** | ||
* @param string $sql | ||
* @param array $args | ||
* @return bool | ||
*/ | ||
protected function updateOrInsertRecord(string $sql, array $args): bool | ||
{ | ||
$stmt = $this->pdo->prepare($sql); | ||
$stmt->bindValue(1, $args[0]); | ||
$stmt->bindValue(2, $args[1]); | ||
$stmt->bindValue(3, $args[2]); | ||
$stmt->bindValue(4, $args[3]); | ||
return $stmt->execute(); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function getDriverName(): string | ||
{ | ||
return $this->pdo->getAttribute(\PDO::ATTR_DRIVER_NAME); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
tests/Integration/PersistenceLayer/PdoPersistenceTestCase.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace XcoreCMS\InlineEditing\Tests\Integration\PersistenceLayer; | ||
|
||
use Tester\Environment; | ||
use XcoreCMS\InlineEditing\Model\Simple\PersistenceLayer\Pdo; | ||
|
||
require __DIR__ . '/../../bootstrap.php'; | ||
|
||
/** | ||
* @author Jakub Janata <jakubjanata@gmail.com> | ||
* @dataProvider ../../databases.ini | ||
*/ | ||
class PdoPersistenceTestCase extends BasePersistenceTestCase | ||
{ | ||
/** | ||
* | ||
*/ | ||
protected function initPersistentLayer(): void | ||
{ | ||
$params = Environment::loadData(); | ||
$params['driver'] = 'pdo'; | ||
|
||
$pdo = new \PDO($params['dsn'], $params['user'], $params['password']); | ||
$this->persistentLayer = new Pdo('inline_content', $pdo); | ||
} | ||
} | ||
|
||
(new PdoPersistenceTestCase)->run(); |