-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathFakePdo.php
86 lines (69 loc) · 1.69 KB
/
FakePdo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
namespace Vimeo\MysqlEngine;
class FakePdo extends \PDO
{
/**
* @var Server
*/
private $server;
/**
* @var ?\PDO
*/
private $real = null;
/**
* @var scalar
*/
public $lastInsertId;
/**
* @var bool
*/
public $stringifyResult = true;
/**
* @var ?string
* @readonly
*/
public $databaseName = null;
public function __construct(string $dsn, string $username = '', string $passwd = '', array $options = [])
{
//$this->real = new \PDO($dsn, $username, $passwd, $options);
$dsn = \Nyholm\Dsn\DsnParser::parse($dsn);
$host = $dsn->getHost();
if (preg_match('/dbname=([a-zA-Z0-9_]+);/', $host, $matches)) {
$this->databaseName = $matches[1];
}
$this->server = Server::getOrCreate('primary');
}
public function setAttribute($key, $value)
{
if ($key === \PDO::ATTR_EMULATE_PREPARES) {
$this->stringifyResult = (bool) $value;
}
if ($this->real && $key !== \PDO::ATTR_STATEMENT_CLASS) {
$this->real->setAttribute($key, $value);
}
}
public function getServer()
{
return $this->server;
}
public function prepare(string $sql)
{
return new FakePdoStatement($this, $sql, $this->real);
}
public function lastInsertId()
{
return $this->lastInsertId;
}
public function beginTransaction()
{
Server::snapshot('transaction');
}
public function commit()
{
return Server::deleteSnapshot('transaction');
}
public function rollback()
{
Server::restoreSnapshot('transaction');
}
}