1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace suda\application\database; |
5
|
|
|
|
6
|
|
|
use suda\framework\Config; |
7
|
|
|
use suda\database\DataSource; |
8
|
|
|
use suda\application\ApplicationModule; |
9
|
|
|
use suda\database\exception\SQLException; |
10
|
|
|
use suda\database\connection\observer\Observer; |
11
|
|
|
use suda\application\Resource as ApplicationResource; |
12
|
|
|
use suda\application\database\creator\MySQLTableCreator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Database |
16
|
|
|
* @package suda\application\database |
17
|
|
|
*/ |
18
|
|
|
class Database |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* 应用引用 |
22
|
|
|
* |
23
|
|
|
* @var ApplicationModule |
24
|
|
|
*/ |
25
|
|
|
protected static $application; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var DataSource[] |
29
|
|
|
*/ |
30
|
|
|
protected static $dataSource = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 从应用创建表 |
34
|
|
|
* |
35
|
|
|
* @param ApplicationModule $application |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
|
|
public static function loadApplication(ApplicationModule $application) |
39
|
|
|
{ |
40
|
|
|
static::$application = $application; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Get 应用引用 |
45
|
|
|
* |
46
|
|
|
* @return ApplicationModule |
47
|
|
|
*/ |
48
|
|
|
public static function application() |
49
|
|
|
{ |
50
|
|
|
return static::$application; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 获取默认的数据源 |
55
|
|
|
* @return DataSource |
56
|
|
|
* @throws SQLException |
57
|
|
|
*/ |
58
|
|
|
public static function getDefaultDataSource():DataSource |
59
|
|
|
{ |
60
|
|
|
return static::getDataSource('default'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $name |
65
|
|
|
* @return DataSource |
66
|
|
|
* @throws SQLException |
67
|
|
|
*/ |
68
|
|
|
public static function getDataSource(string $name) |
69
|
|
|
{ |
70
|
|
|
return static::getDataSourceFrom(static::$application->getResource(), $name); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param ApplicationResource $resource |
75
|
|
|
* @param string $groupName |
76
|
|
|
* @return DataSource |
77
|
|
|
* @throws SQLException |
78
|
|
|
*/ |
79
|
|
|
public static function getDataSourceFrom(ApplicationResource $resource, string $groupName) |
80
|
|
|
{ |
81
|
|
|
if (array_key_exists($groupName, static::$dataSource)) { |
82
|
|
|
return static::$dataSource[$groupName]; |
83
|
|
|
} |
84
|
|
|
$group = $groupName === 'default' ? '': '-'. $groupName; |
85
|
|
|
$dataSourceConfigPath = $resource->getConfigResourcePath('config/data-source'.$group); |
86
|
|
|
$dataSource = new DataSource; |
87
|
|
|
if ($dataSourceConfigPath !== null) { |
88
|
|
|
$observer = new DebugObserver(static::$application->debug()); |
89
|
|
|
$dataSourceConfig = Config::loadConfig($dataSourceConfigPath); |
90
|
|
|
foreach ($dataSourceConfig as $name => $config) { |
91
|
|
|
$enable = $config['enable']; |
92
|
|
|
if ($enable) { |
93
|
|
|
static::applyDataSource( |
94
|
|
|
$dataSource, |
95
|
|
|
$observer, |
96
|
|
|
$name, |
97
|
|
|
$config['type'] ?? 'mysql', |
98
|
|
|
$config['mode'] ?? '', |
99
|
|
|
$config |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
return static::$dataSource[$groupName] = $dataSource; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @param DataSource $source |
110
|
|
|
* @param Observer $observer |
111
|
|
|
* @param string $name |
112
|
|
|
* @param string $type |
113
|
|
|
* @param string $mode |
114
|
|
|
* @param array $config |
115
|
|
|
* @throws SQLException |
116
|
|
|
*/ |
117
|
|
|
protected static function applyDataSource( |
118
|
|
|
DataSource $source, |
119
|
|
|
Observer $observer, |
120
|
|
|
string $name, |
121
|
|
|
string $type, |
122
|
|
|
string $mode, |
123
|
|
|
array $config |
124
|
|
|
) { |
125
|
|
|
$mode = strtolower($mode); |
126
|
|
|
$data = DataSource::new($type, $config, $name); |
127
|
|
|
$data->setObserver($observer); |
128
|
|
|
if (strlen($mode) > 0) { |
129
|
|
|
if (strpos($mode, 'read') !== false || strpos($mode, 'slave') !== false) { |
130
|
|
|
$source->addRead($data); |
131
|
|
|
} |
132
|
|
|
if (strpos($mode, 'write') !== false) { |
133
|
|
|
$source->addWrite($data); |
134
|
|
|
} |
135
|
|
|
if (strpos($mode, 'master') !== false) { |
136
|
|
|
$source->add($data); |
137
|
|
|
} |
138
|
|
|
} else { |
139
|
|
|
$source->add($data); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|