| Total Complexity | 30 |
| Total Lines | 268 |
| Duplicated Lines | 0 % |
| Coverage | 4.62% |
| Changes | 0 | ||
| 1 | <?php |
||
| 25 | class Db |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * 当前数据库连接对象 |
||
| 29 | * @var Connection |
||
| 30 | */ |
||
| 31 | protected $connection; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * 数据库连接实例 |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $instance = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Event对象 |
||
| 41 | * @var Event |
||
| 42 | */ |
||
| 43 | protected $event; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * 数据库配置 |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $config = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * SQL监听 |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $listen = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * 查询次数 |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $queryTimes = 0; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * 架构函数 |
||
| 65 | * @param array $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 66 | * @access public |
||
| 67 | */ |
||
| 68 | 1 | public function __construct(array $config = []) |
|
| 69 | { |
||
| 70 | 1 | $this->config = $config; |
|
| 71 | |||
| 72 | 1 | } |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param Event $event |
||
|
1 ignored issue
–
show
|
|||
| 76 | * @param Config $config |
||
|
1 ignored issue
–
show
|
|||
| 77 | * @return Db |
||
|
1 ignored issue
–
show
|
|||
| 78 | * @codeCoverageIgnore |
||
| 79 | */ |
||
| 80 | public static function __make(Event $event, Config $config) |
||
|
2 ignored issues
–
show
|
|||
| 81 | { |
||
| 82 | $db = new static($config->get('database')); |
||
| 83 | |||
| 84 | $db->setEvent($event); |
||
| 85 | |||
| 86 | return $db; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * 切换数据库连接 |
||
| 91 | * @access public |
||
| 92 | * @param mixed $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 93 | * @param bool|string $name 连接标识 true 强制重新连接 |
||
|
1 ignored issue
–
show
|
|||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function connect($config = '', $name = false) |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * 数据库连接参数解析 |
||
| 104 | * @access private |
||
| 105 | * @param mixed $config |
||
|
1 ignored issue
–
show
|
|||
| 106 | * @return array |
||
| 107 | */ |
||
| 108 | private function parseConfig($config): array |
||
| 109 | { |
||
| 110 | $defaultConnector = $this->config['default'] ?? 'mysql'; |
||
| 111 | |||
| 112 | if (empty($config)) { |
||
| 113 | $config = $this->config['connections'][$defaultConnector]; |
||
| 114 | } elseif (is_string($config)) { |
||
| 115 | $config = $this->config['connections'][$config] ?? $this->config['connections'][$defaultConnector]; |
||
| 116 | } |
||
| 117 | |||
| 118 | if (!is_array($config)) { |
||
| 119 | throw new DbException('database config error:' . $config); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $config; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * 取得数据库连接类实例 |
||
| 127 | * @access public |
||
| 128 | * @param array $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 129 | * @param bool|string $name 连接标识 true 强制重新连接 |
||
|
1 ignored issue
–
show
|
|||
| 130 | * @return Connection |
||
| 131 | */ |
||
| 132 | public function instance(array $config = [], $name = false) |
||
| 133 | { |
||
| 134 | if (false === $name) { |
||
| 135 | $name = md5(serialize($config)); |
||
| 136 | } |
||
| 137 | |||
| 138 | if (true === $name || !isset($this->instance[$name])) { |
||
| 139 | |||
| 140 | if (empty($config['type'])) { |
||
| 141 | throw new InvalidArgumentException('Undefined db type'); |
||
| 142 | } |
||
| 143 | |||
| 144 | if (true === $name) { |
||
| 145 | $name = md5(serialize($config)); |
||
| 146 | } |
||
| 147 | |||
| 148 | $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config); |
||
| 149 | } |
||
| 150 | |||
| 151 | return $this->instance[$name]; |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * 使用表达式设置数据 |
||
| 156 | * @access public |
||
| 157 | * @param string $value 表达式 |
||
|
1 ignored issue
–
show
|
|||
| 158 | * @return Raw |
||
| 159 | */ |
||
| 160 | public function raw(string $value): Raw |
||
| 161 | { |
||
| 162 | return new Raw($value); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * 更新查询次数 |
||
| 167 | * @access public |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | public function updateQueryTimes(): void |
||
| 171 | { |
||
| 172 | $this->queryTimes++; |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * 重置查询次数 |
||
| 177 | * @access public |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public function clearQueryTimes(): void |
||
| 181 | { |
||
| 182 | $this->queryTimes = 0; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * 获得查询次数 |
||
| 187 | * @access public |
||
| 188 | * @return integer |
||
| 189 | */ |
||
| 190 | public function getQueryTimes(): int |
||
| 191 | { |
||
| 192 | return $this->queryTimes; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * 创建一个新的查询对象 |
||
| 197 | * @access public |
||
| 198 | * @param string|array $connection 连接配置信息 |
||
|
1 ignored issue
–
show
|
|||
| 199 | * @return mixed |
||
| 200 | */ |
||
| 201 | public function buildQuery($connection = []) |
||
| 202 | { |
||
| 203 | $connection = $this->instance($this->parseConfig($connection)); |
||
| 204 | return $this->newQuery($connection); |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * 监听SQL执行 |
||
| 209 | * @access public |
||
| 210 | * @param callable $callback 回调方法 |
||
|
1 ignored issue
–
show
|
|||
| 211 | * @return void |
||
| 212 | */ |
||
| 213 | public function listen(callable $callback): void |
||
| 214 | { |
||
| 215 | $this->listen[] = $callback; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * 获取监听SQL执行 |
||
| 220 | * @access public |
||
| 221 | * @return array |
||
| 222 | */ |
||
| 223 | public function getListen(): array |
||
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * 设置Event对象 |
||
| 230 | * @param Event $event |
||
| 231 | */ |
||
| 232 | public function setEvent(Event $event) |
||
| 233 | { |
||
| 234 | $this->event = $event; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * 注册回调方法 |
||
| 239 | * @access public |
||
| 240 | * @param string $event 事件名 |
||
|
1 ignored issue
–
show
|
|||
| 241 | * @param callable $callback 回调方法 |
||
|
1 ignored issue
–
show
|
|||
| 242 | * @return void |
||
| 243 | */ |
||
| 244 | public function event(string $event, callable $callback): void |
||
| 245 | { |
||
| 246 | if ($this->event) { |
||
| 247 | $this->event->listen('db.' . $event, $callback); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | /** |
||
| 252 | * 触发事件 |
||
| 253 | * @access public |
||
| 254 | * @param string $event 事件名 |
||
|
1 ignored issue
–
show
|
|||
| 255 | * @param mixed $params 传入参数 |
||
|
1 ignored issue
–
show
|
|||
| 256 | * @param bool $once |
||
|
1 ignored issue
–
show
|
|||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | public function trigger(string $event, $params = null, bool $once = false) |
||
| 260 | { |
||
| 261 | if ($this->event) { |
||
| 262 | return $this->event->trigger('db.' . $event, $params, $once); |
||
| 263 | } |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * 创建一个新的查询对象 |
||
| 268 | * @access protected |
||
| 269 | * @param Connection $connection 连接对象 |
||
|
1 ignored issue
–
show
|
|||
| 270 | * @return mixed |
||
| 271 | */ |
||
| 272 | protected function newQuery($connection = null) |
||
| 273 | { |
||
| 274 | /** @var Query $query */ |
||
| 275 | if (is_null($connection) && !$this->connection) { |
||
| 276 | $this->connect($this->config); |
||
| 277 | } |
||
| 278 | |||
| 279 | $connection = $connection ?: $this->connection; |
||
| 280 | $class = $connection->getQueryClass(); |
||
| 281 | $query = new $class($connection); |
||
| 282 | |||
| 283 | $query->setDb($this); |
||
| 284 | |||
| 285 | return $query; |
||
| 286 | } |
||
| 287 | |||
| 288 | public function __call($method, $args) |
||
| 293 | } |
||
| 294 | } |
||
| 295 |