Skip to content

Commit

Permalink
added support for simple PDO
Browse files Browse the repository at this point in the history
  • Loading branch information
janatjak committed Nov 1, 2018
1 parent 257cff7 commit 5389cfa
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"squizlabs/php_codesniffer": "^3.0",
"tracy/tracy": "^2.4",
"nette/tester": "^2.0",
"phpstan/phpstan": "@dev",
"phpstan/phpstan-shim": "^0.10",
"mockery/mockery": "^1.1"
},
"suggest": {
Expand Down
69 changes: 69 additions & 0 deletions src/Model/Simple/PersistenceLayer/Pdo.php
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 tests/Integration/PersistenceLayer/PdoPersistenceTestCase.phpt
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();

0 comments on commit 5389cfa

Please sign in to comment.