Skip to content

Commit

Permalink
charset configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
doganoo committed May 14, 2020
1 parent 492be88 commit 2a61a57
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"ext-fileinfo": "*",
"ext-iconv": "*",
"ext-mbstring": "*",
"ext-json": "*"
"ext-json": "*",
"ext-pdo": "*"
},
"require-dev": {
"phpunit/phpunit": "^6"
Expand Down
28 changes: 18 additions & 10 deletions src/Storage/PDOConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
namespace doganoo\PHPUtil\Storage;

use doganoo\PHPUtil\Exception\InvalidCredentialsException;
use PDO;
use PDOStatement;

/**
* Class MySQLConnector
Expand All @@ -36,9 +38,9 @@ class PDOConnector implements IStorageConnector {

/** @var array $credentials */
private $credentials = null;
/** @var \PDO $mysqli */
/** @var PDO $mysqli */
private $pdo = null;
/** @var \PDOStatement $statement */
/** @var PDOStatement $statement */
private $statement = null;
/** @var bool $transactionExists */
private $transactionExists = false;
Expand All @@ -64,6 +66,7 @@ public function getSchema(): ?string {

/**
* starts a database transaction
*
* @return bool
*/
public function startTransaction(): bool {
Expand All @@ -77,6 +80,7 @@ public function startTransaction(): bool {

/**
* commits a started database transaction
*
* @return bool
*/
public function commit(): bool {
Expand All @@ -90,6 +94,7 @@ public function commit(): bool {

/**
* rolls a started transaction back
*
* @return bool
*/
public function rollback(): bool {
Expand All @@ -113,12 +118,13 @@ public function connect(): bool {
}
$host = $this->credentials["servername"];
$db = $this->credentials["dbname"];
$dsn = "mysql:host=$host;dbname=$db;charset=utf8";
$this->pdo = new \PDO($dsn,
$charSet = $this->credentials["charset"] ?? 'utf8';
$dsn = "mysql:host=$host;dbname=$db;charset=$charSet";
$this->pdo = new PDO($dsn,
$this->credentials["username"],
$this->credentials["password"]
);
$this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $this->pdo !== null;
}

Expand Down Expand Up @@ -147,9 +153,10 @@ private function hasMinimumCredentials(): bool {
* prepares a SQL statement
*
* @param string $sql
* @return null|\PDOStatement
*
* @return null|PDOStatement
*/
public function prepare(string $sql): ?\PDOStatement {
public function prepare(string $sql): ?PDOStatement {
$statement = $this->getConnection()->prepare($sql);
if ($statement === false) {
return null;
Expand All @@ -161,7 +168,7 @@ public function prepare(string $sql): ?\PDOStatement {
/**
* returns the connection
*
* @return \PDO|null
* @return PDO|null
*/
public function getConnection() {
if (!$this->hasMinimumCredentials()) {
Expand Down Expand Up @@ -203,6 +210,7 @@ public function disconnect(): bool {

/**
* @param null $name
*
* @return string
*/
public function getLastInsertId($name = null) {
Expand All @@ -218,8 +226,8 @@ public function getLastInsertId($name = null) {
* @param null $length
* @param null $driverOptions
*/
public function bindParam(string $param, $value, $dataType = \PDO::PARAM_STR, $length = null, $driverOptions = null) {
public function bindParam(string $param, $value, $dataType = PDO::PARAM_STR, $length = null, $driverOptions = null) {
$this->statement->bindParam($param, $value, $dataType, $length, $driverOptions);
}

}
}

0 comments on commit 2a61a57

Please sign in to comment.