Skip to content

Commit

Permalink
refined adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
k00ni committed Aug 29, 2023
1 parent 794196d commit 12c8eaf
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
9 changes: 6 additions & 3 deletions src/ARC2/Store/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ abstract class AbstractAdapter
protected $queries = [];

/**
* @param array $configuration default is array()
* @param array $configuration Default is array().
*/
public function __construct(array $configuration = [])
{
Expand Down Expand Up @@ -70,6 +70,11 @@ public function getQueries(): array

abstract public function checkRequirements();

/**
* Connect to server.
*
* It returns current object for the connection, such as an instance of \PDO.
*/
abstract public function connect($existingConnection = null);

abstract public function disconnect();
Expand All @@ -90,8 +95,6 @@ abstract public function getDBSName();

abstract public function getLastInsertId();

abstract public function getServerInfo();

abstract public function getErrorMessage();

abstract public function getNumberOfRows($sql);
Expand Down
29 changes: 11 additions & 18 deletions src/ARC2/Store/Adapter/PDOAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace ARC2\Store\Adapter;

use Exception;

/**
* PDO Adapter - Handles database operations using PDO.
*/
Expand Down Expand Up @@ -39,9 +41,9 @@ public function getAffectedRows(): int
}

/**
* Connect to server or storing a given connection.
* Connect to server.
*
* @param EasyDB $existingConnection default is null
* @return \PDO
*/
public function connect($existingConnection = null)
{
Expand Down Expand Up @@ -231,25 +233,16 @@ public function getDBSName()
return $return;
}

public function getServerInfo()
{
return $this->db->getAttribute(\constant('PDO::ATTR_CLIENT_VERSION'));
}

/**
* Returns the version of the database server like 05-00-12.
* Returns the version of the database server like 05.00.12
*/
public function getServerVersion()
public function getServerVersion(): string
{
$res = preg_match(
"/([0-9]+)\.([0-9]+)\.([0-9]+)/",
$this->getServerInfo(),
$matches
);

return 1 == $res
? sprintf('%02d-%02d-%02d', $matches[1], $matches[2], $matches[3])
: '00-00-00';
if ($this->db instanceof \PDO) {
return $this->db->query('select version()')->fetchColumn();
}

throw new Exception('You need to connect to DB server first. Use connect() before this function.');
}

public function getErrorCode()
Expand Down
8 changes: 6 additions & 2 deletions tests/db_adapter_depended/store/ARC2_StoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,10 @@ public function testDump()

public function testEnableFulltextSearch()
{
if (str_starts_with($this->fixture->getDBObject()->getServerVersion(), '5.5.')) {
$this->markTestSkipped('InnoDB does not support fulltext in MySQL 5.5.x');
}

$res1 = $this->fixture->enableFulltextSearch();
$res2 = $this->fixture->disableFulltextSearch();

Expand All @@ -316,10 +320,10 @@ public function testEnableFulltextSearch()
// just check pattern
public function testGetDBVersion()
{
$pattern = '/[0-9]{2}-[0-9]{2}-[0-9]{2}/';
$pattern = '/[0-9]{1,}\.[0-9]{1,}\.[0-9]{2}/';

$result = preg_match($pattern, $this->fixture->getDBVersion(), $match);
$this->assertEquals(1, $result);
$this->assertEquals(1, $result, $this->fixture->getDBVersion());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,10 @@ public function testQuery()

public function testGetServerVersion()
{
// check that server version looks like 05-00-05
$this->assertEquals(1, preg_match('/\d\d-\d\d-\d\d/', $this->fixture->getServerVersion()));
$pattern = '/[0-9]{1,}\.[0-9]{1,}\.[0-9]{2}/';

// check that server version looks like 5.5.21
$this->assertEquals(1, preg_match($pattern, $this->fixture->getServerVersion()));
}

/*
Expand Down

0 comments on commit 12c8eaf

Please sign in to comment.