-
Notifications
You must be signed in to change notification settings - Fork 2
/
Driver.php
94 lines (76 loc) · 2.16 KB
/
Driver.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
87
88
89
90
91
92
93
94
<?php
namespace dizews\dbConsole;
use yii\base\Component;
use yii\base\Object;
abstract class Driver extends Object implements DriverInterface
{
public $clientPath;
public $host = '127.0.0.1';
protected $dsn = [];
/**
* @param Component $connection instance of database connection
* @param array $config
*/
public function __construct(Component $connection, $config = [])
{
$this->dsn = array_fill_keys(['user', 'password', 'host', 'port', 'dbname'], null);
$this->dsn = $this->parseDsn($connection->dsn);
parent::__construct($config);
}
/**
* @inheritdoc
*/
public function combineParams($params)
{
$programParams = '';
if ($this->getPasswordParamName()) {
$programParams = $this->getPasswordParamName() .'='. $this->getPasswordValue();
}
foreach ($params as $key => $value) {
if ($key == '') {
$programParams .= ' '. $value;
} else {
$programParams .= " --{$key}={$value}";
}
}
return $programParams;
}
public function getPasswordValue()
{
return isset($this->dsn['password']) ? $this->dsn['password'] : '';
}
public function getPasswordParamName()
{
return '';
}
/**
* get name of database driver
*
* @param $connection instance of database connection
* @return string
*/
public static function driverName($connection)
{
if ($connection->hasProperty('driverName')) {
$driverName = $connection->driverName;
} elseif (($pos = strpos($connection->dsn, ':')) !== false) {
$driverName = strtolower(substr($connection->dsn, 0, $pos));
}
return $driverName;
}
protected function parseDsn($dsn)
{
$result = [];
$params = explode(':', $dsn, 2);
$params = explode(';', $params[1]);
foreach ($params as $v) {
list($name, $value) = explode('=', $v);
$result[$name] = $value;
}
return $result;
}
public function getEnv()
{
return null;
}
}