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