Skip to content

Commit

Permalink
Charset management
Browse files Browse the repository at this point in the history
  • Loading branch information
bpolaszek committed Oct 10, 2017
1 parent f74e720 commit 2ebe759
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/Model/Adapter/Mysqli/MysqliAdapter.php
Expand Up @@ -57,6 +57,9 @@ public function __construct(mysqli $cnx, CredentialsInterface $credentials, arra
$this->credentials = $credentials;
if (null !== $options) {
$this->options = array_replace($this->getDefaultOptions(), $options);
if ($this->hasOption('charset')) {
$this->cnx->set_charset($this->getOption('charset'));
}
}
}

Expand Down Expand Up @@ -106,6 +109,9 @@ private function reconnect()
} else {
$this->reconnect();
}
if ($this->hasOption('charset')) {
$this->cnx->set_charset($this->getOption('charset'));
}
} catch (Throwable $e) {
$this->reconnectAttempts++;
}
Expand Down Expand Up @@ -235,7 +241,10 @@ private function executeParallel($stmt, array $values = null): PromiseInterface
try {
// Clone connection (Mysqli Asynchronous queries require a different connection to work properly)
$credentials = $this->getCredentials();
$cnx = new mysqli($credentials->getHostname(), $credentials->getUser(), $credentials->getPassword(), $credentials->getDatabase(), $credentials->getPort());
$cnx = self::createLink($credentials);
if ($this->hasOption('charset')) {
$cnx->set_charset($this->getOption('charset'));
}
} catch (mysqli_sql_exception $e) {
throw new AccessDeniedException($e->getMessage(), (int) $e->getCode(), $e);
}
Expand Down
12 changes: 8 additions & 4 deletions src/Model/Adapter/PDO/PDOAdapter.php
Expand Up @@ -109,7 +109,7 @@ private function reconnect()
if (0 !== $this->reconnectAttempts) {
usleep((int) $this->getOption(self::OPT_USLEEP_AFTER_FIRST_ATTEMPT));
}
$this->cnx = self::createLink($this->getCredentials());
$this->cnx = self::createLink($this->getCredentials(), $this->options);
if ($this->isConnected()) {
$this->reconnectAttempts = 0;
} else {
Expand Down Expand Up @@ -242,14 +242,14 @@ public function getDefaultOptions(): array
*/
public static function factory(CredentialsInterface $credentials, array $options = null): self
{
return new static(self::createLink($credentials), $credentials, $options);
return new static(self::createLink($credentials, $options), $credentials, $options);
}

/**
* @param CredentialsInterface $credentials
* @return PDO
*/
private static function createLink(CredentialsInterface $credentials): PDO
private static function createLink(CredentialsInterface $credentials, array $options = null): PDO
{
$dsn = sprintf('%s:', $credentials->getPlatform());
$dsn .= sprintf('host=%s;', $credentials->getHostname());
Expand All @@ -260,7 +260,11 @@ private static function createLink(CredentialsInterface $credentials): PDO
$dsn .= sprintf('dbname=%s;', $credentials->getDatabase());
}
try {
return new PDO($dsn, $credentials->getUser(), $credentials->getPassword(), [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$pdoOptions = [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION];
if (isset($options['charset'])) {
$pdoOptions[PDO::MYSQL_ATTR_INIT_COMMAND] = sprintf('SET NAMES %s', $options['charset']);
}
return new PDO($dsn, $credentials->getUser(), $credentials->getPassword(), $pdoOptions);
} catch (\PDOException $e) {
throw new AccessDeniedException($e->getMessage(), (int) $e->getCode(), $e);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bootstrap.php
Expand Up @@ -38,7 +38,7 @@
}

try {
$link = new PDO(sprintf('mysql:host=%s:%d', $credentials->getHostname(), $credentials->getPort()), $credentials->getUser(), $credentials->getPassword(), [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$link = new PDO(sprintf('mysql:host=%s;port=%d', $credentials->getHostname(), $credentials->getPort()), $credentials->getUser(), $credentials->getPassword(), [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
} catch (PDOException $e) {

print PHP_EOL . PHP_EOL;
Expand Down

0 comments on commit 2ebe759

Please sign in to comment.