| Total Complexity | 23 |
| Total Lines | 221 |
| 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 int |
||
| 55 | */ |
||
| 56 | protected $queryTimes = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 架构函数 |
||
| 60 | * @param array $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 61 | * @access public |
||
| 62 | */ |
||
| 63 | public function __construct(array $config = []) |
||
| 64 | { |
||
| 65 | $this->option = $config; |
||
| 66 | |||
| 67 | $this->connect($config); |
||
| 68 | } |
||
| 69 | |||
| 70 | public static function __make(Event $event, Config $config) |
||
|
2 ignored issues
–
show
|
|||
| 71 | { |
||
| 72 | $db = new static($config->get('database')); |
||
| 73 | |||
| 74 | $db->event = $event; |
||
| 75 | $db->config = $config; |
||
| 76 | |||
| 77 | return $db; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * 切换数据库连接 |
||
| 82 | * @access public |
||
| 83 | * @param mixed $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 84 | * @param bool|string $name 连接标识 true 强制重新连接 |
||
|
1 ignored issue
–
show
|
|||
| 85 | * @return $this |
||
| 86 | */ |
||
| 87 | public function connect($config = [], $name = false) |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * 取得数据库连接类实例 |
||
| 95 | * @access public |
||
| 96 | * @param array $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 97 | * @param bool|string $name 连接标识 true 强制重新连接 |
||
|
1 ignored issue
–
show
|
|||
| 98 | * @return Connection |
||
| 99 | */ |
||
| 100 | public function instance(array $config = [], $name = false) |
||
| 101 | { |
||
| 102 | if (false === $name) { |
||
| 103 | $name = md5(serialize($config)); |
||
| 104 | } |
||
| 105 | |||
| 106 | if (true === $name || !isset($this->instance[$name])) { |
||
| 107 | |||
| 108 | if (empty($config['type'])) { |
||
| 109 | throw new InvalidArgumentException('Undefined db type'); |
||
| 110 | } |
||
| 111 | |||
| 112 | if (true === $name) { |
||
| 113 | $name = md5(serialize($config)); |
||
| 114 | } |
||
| 115 | |||
| 116 | $this->instance[$name] = App::factory($config['type'], '\\think\\db\\connector\\', $config); |
||
| 117 | } |
||
| 118 | |||
| 119 | return $this->instance[$name]; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * 使用表达式设置数据 |
||
| 124 | * @access public |
||
| 125 | * @param string $value 表达式 |
||
|
1 ignored issue
–
show
|
|||
| 126 | * @return Raw |
||
| 127 | */ |
||
| 128 | public function raw(string $value): Raw |
||
| 129 | { |
||
| 130 | return new Raw($value); |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * 更新查询次数 |
||
| 135 | * @access public |
||
| 136 | * @return void |
||
| 137 | */ |
||
| 138 | public function updateQueryTimes(): void |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * 获得查询次数 |
||
| 145 | * @access public |
||
| 146 | * @return integer |
||
| 147 | */ |
||
| 148 | public function getQueryTimes(): int |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * 数据库连接参数解析 |
||
| 155 | * @access private |
||
| 156 | * @param mixed $config |
||
|
1 ignored issue
–
show
|
|||
| 157 | * @return array |
||
| 158 | */ |
||
| 159 | private function parseConfig($config): array |
||
| 160 | { |
||
| 161 | if (empty($config)) { |
||
| 162 | $config = $this->option; |
||
| 163 | } elseif (is_string($config)) { |
||
| 164 | // 支持读取配置参数 |
||
| 165 | $config = $this->option[$config] ?? null; |
||
| 166 | } |
||
| 167 | |||
| 168 | return $config; |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * 获取数据库的配置参数 |
||
| 173 | * @access public |
||
| 174 | * @param string $name 参数名称 |
||
|
1 ignored issue
–
show
|
|||
| 175 | * @return mixed |
||
| 176 | */ |
||
| 177 | public function getConfig(string $name = '') |
||
| 178 | { |
||
| 179 | return $name ? ($this->option[$name] ?? null) : $this->option; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * 创建一个新的查询对象 |
||
| 184 | * @access public |
||
| 185 | * @param string|array $connection 连接配置信息 |
||
|
1 ignored issue
–
show
|
|||
| 186 | * @return mixed |
||
| 187 | */ |
||
| 188 | public function buildQuery($connection = []) |
||
| 189 | { |
||
| 190 | return $this->connect($connection)->newQuery(); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * 注册回调方法 |
||
| 195 | * @access public |
||
| 196 | * @param string $event 事件名 |
||
|
1 ignored issue
–
show
|
|||
| 197 | * @param callable $callback 回调方法 |
||
|
1 ignored issue
–
show
|
|||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | public function event(string $event, callable $callback): void |
||
| 201 | { |
||
| 202 | $this->event->listen('db.' . $event, $callback); |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * 触发事件 |
||
| 207 | * @access public |
||
| 208 | * @param string $event 事件名 |
||
|
1 ignored issue
–
show
|
|||
| 209 | * @param mixed $params 传入参数 |
||
|
1 ignored issue
–
show
|
|||
| 210 | * @param bool $once |
||
|
1 ignored issue
–
show
|
|||
| 211 | * @return mixed |
||
| 212 | */ |
||
| 213 | public function trigger(string $event, $params = null, bool $once = false) |
||
| 214 | { |
||
| 215 | return $this->event->trigger('db.' . $event, $params, $once); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * 创建一个新的查询对象 |
||
| 220 | * @access protected |
||
| 221 | * @param Connection $connection 连接对象 |
||
|
1 ignored issue
–
show
|
|||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | protected function newQuery($connection = null) |
||
| 225 | { |
||
| 226 | /** @var Query $query */ |
||
| 227 | $connection = $connection ?: $this->connection; |
||
| 228 | $class = $connection->getQueryClass(); |
||
| 229 | $query = new $class($connection); |
||
| 230 | |||
| 231 | $query->setDb($this); |
||
| 232 | |||
| 233 | return $query; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function __call($method, $args) |
||
| 241 | } |
||
| 242 | } |
||
| 243 |