1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mattbit\MysqlCompat\BridgeComponents; |
4
|
|
|
|
5
|
|
|
use PDO; |
6
|
|
|
use Mattbit\MysqlCompat\MysqlConstants; |
7
|
|
|
use Mattbit\MysqlCompat\Exception\NotSupportedException; |
8
|
|
|
|
9
|
|
|
trait ManageConnections |
10
|
|
|
{ |
11
|
|
|
public function clientEncoding(Connection $linkIdentifier = null) |
12
|
|
|
{ |
13
|
|
|
$connection = $this->manager->getOpenConnectionOrFail($linkIdentifier); |
|
|
|
|
14
|
|
|
|
15
|
|
|
return $connection->getCharset(); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function close(Connection $linkIdentifier = null) |
19
|
|
|
{ |
20
|
|
|
return $this->manager->disconnect($linkIdentifier); |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function connect($server = null, $username = null, $password = null, $newLink = false, $clientFlags = 0) |
24
|
|
|
{ |
25
|
|
|
if ($server === null) $server = ini_get("mysql.default_host"); |
26
|
|
|
if ($username === null) $username = ini_get("mysql.default_user"); |
27
|
|
|
if ($password === null) $password = ini_get("mysql.default_password"); |
28
|
|
|
|
29
|
|
|
$options = $this->parseClientFlags($clientFlags); |
30
|
|
|
|
31
|
|
|
return $this->manager->connect("mysql:host={$server};", $username, $password, $options, $newLink); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function parseClientFlags($clientFlags) |
35
|
|
|
{ |
36
|
|
|
$options = []; |
37
|
|
|
|
38
|
|
|
if ($clientFlags & MysqlConstants::CLIENT_COMPRESS) { |
39
|
|
|
$options[PDO::MYSQL_ATTR_COMPRESS] = 1; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($clientFlags & MysqlConstants::CLIENT_IGNORE_SPACE) { |
43
|
|
|
$options[PDO::MYSQL_ATTR_IGNORE_SPACE] = 1; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if ($clientFlags & MysqlConstants::CLIENT_SSL) { |
47
|
|
|
throw new NotSupportedException("SSL is not supported. You must create the PDO instance manually."); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($clientFlags & MysqlConstants::CLIENT_INTERACTIVE) { |
51
|
|
|
throw new NotSupportedException("Interactive client is not supported by PDO."); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $options; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
public function pconnect() |
59
|
|
|
{ |
60
|
|
|
// @todo |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: