1
|
|
|
<?php /** ConnectionMicro */ |
2
|
|
|
|
3
|
|
|
namespace Micro\Db; |
4
|
|
|
|
5
|
|
|
use Micro\Base\Exception; |
6
|
|
|
use Micro\Db\Drivers\IDriver; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Threads class file. |
10
|
|
|
* |
11
|
|
|
* @author Oleg Lunegov <[email protected]> |
12
|
|
|
* @link https://github.com/linpax/microphp-framework |
13
|
|
|
* @copyright Copyright (c) 2013 Oleg Lunegov |
14
|
|
|
* @license https://github.com/linpax/microphp-framework/blob/master/LICENSE |
15
|
|
|
* @package Micro |
16
|
|
|
* @subpackage Db |
17
|
|
|
* @version 1.0 |
18
|
|
|
* @since 1.0 |
19
|
|
|
*/ |
20
|
|
|
class Connection implements IConnection |
21
|
|
|
{ |
22
|
|
|
/** @var IDriver $driver */ |
23
|
|
|
private $driver; |
24
|
|
|
|
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Make connection to database with PDO driver |
28
|
|
|
* |
29
|
|
|
* @access public |
30
|
|
|
* |
31
|
|
|
* @param string $dsn DSN connection string |
32
|
|
|
* @param array $config Configuration of connection |
33
|
|
|
* @param array $options Other options |
34
|
|
|
* |
35
|
|
|
* @result void |
36
|
|
|
*/ |
37
|
|
|
public function __construct($dsn, array $config = [], array $options = []) |
38
|
|
|
{ |
39
|
|
|
$this->setDriver($dsn, $config, $options); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get DB driver |
44
|
|
|
* |
45
|
|
|
* @access public |
46
|
|
|
* @return IDriver |
47
|
|
|
*/ |
48
|
|
|
public function getDriver() |
49
|
|
|
{ |
50
|
|
|
return $this->driver; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Set active connection driver |
55
|
|
|
* |
56
|
|
|
* @access public |
57
|
|
|
* |
58
|
|
|
* @param string $dsn DSN connection string |
59
|
|
|
* @param array $config Configuration of connection |
60
|
|
|
* @param array $options Other options |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
* @throws Exception |
64
|
|
|
*/ |
65
|
|
|
public function setDriver($dsn, array $config = [], array $options = []) |
66
|
|
|
{ |
67
|
|
|
$class = '\Micro\Db\Drivers\\'.ucfirst(substr($dsn, 0, strpos($dsn, ':'))).'Driver'; |
68
|
|
|
|
69
|
|
|
if (!class_exists($class)) { |
70
|
|
|
throw new Exception('DB driver `'.$class.'` not supported'); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
unset($this->driver); |
74
|
|
|
|
75
|
|
|
$this->driver = new $class($dsn, $config, $options); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|