| Total Complexity | 22 |
| Total Lines | 219 |
| Duplicated Lines | 0 % |
| Coverage | 6% |
| Changes | 0 | ||
| 1 | <?php |
||
| 25 | class Db |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * 数据库连接实例 |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | protected $instance = []; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Event对象 |
||
| 35 | * @var Event |
||
| 36 | */ |
||
| 37 | protected $event; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * 数据库配置 |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected $config = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * SQL监听 |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | protected $listen = []; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * 查询次数 |
||
| 53 | * @var int |
||
| 54 | */ |
||
| 55 | protected $queryTimes = 0; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * 架构函数 |
||
| 59 | * @param array $config 连接配置 |
||
|
1 ignored issue
–
show
|
|||
| 60 | * @access public |
||
| 61 | */ |
||
| 62 | 1 | public function __construct(array $config = []) |
|
| 63 | { |
||
| 64 | 1 | $this->config = $config; |
|
| 65 | |||
| 66 | 1 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * @param Event $event |
||
|
1 ignored issue
–
show
|
|||
| 70 | * @param Config $config |
||
|
1 ignored issue
–
show
|
|||
| 71 | * @return Db |
||
|
1 ignored issue
–
show
|
|||
| 72 | * @codeCoverageIgnore |
||
| 73 | */ |
||
| 74 | public static function __make(Event $event, Config $config) |
||
|
2 ignored issues
–
show
|
|||
| 75 | { |
||
| 76 | $db = new static($config->get('database')); |
||
| 77 | |||
| 78 | $db->setEvent($event); |
||
| 79 | |||
| 80 | return $db; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * 连接/切换数据库连接 |
||
| 85 | * @access public |
||
| 86 | * @param string|null $name 连接标识 |
||
|
1 ignored issue
–
show
|
|||
| 87 | * @param bool $force 强制重新连接 |
||
|
1 ignored issue
–
show
|
|||
| 88 | * @return Connection |
||
| 89 | */ |
||
| 90 | public function connect(string $name = null, bool $force = false): Connection |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * 使用表达式设置数据 |
||
| 111 | * @access public |
||
| 112 | * @param string $value 表达式 |
||
|
1 ignored issue
–
show
|
|||
| 113 | * @return Raw |
||
| 114 | */ |
||
| 115 | public function raw(string $value): Raw |
||
| 116 | { |
||
| 117 | return new Raw($value); |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 更新查询次数 |
||
| 122 | * @access public |
||
| 123 | * @return void |
||
| 124 | */ |
||
| 125 | public function updateQueryTimes(): void |
||
| 126 | { |
||
| 127 | $this->queryTimes++; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * 重置查询次数 |
||
| 132 | * @access public |
||
| 133 | * @return void |
||
| 134 | */ |
||
| 135 | public function clearQueryTimes(): void |
||
| 136 | { |
||
| 137 | $this->queryTimes = 0; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * 获得查询次数 |
||
| 142 | * @access public |
||
| 143 | * @return integer |
||
| 144 | */ |
||
| 145 | public function getQueryTimes(): int |
||
| 146 | { |
||
| 147 | return $this->queryTimes; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * 创建一个新的查询对象 |
||
| 152 | * @access public |
||
| 153 | * @param string|null $connection 连接配置标识 |
||
|
1 ignored issue
–
show
|
|||
| 154 | * @return Query |
||
| 155 | */ |
||
| 156 | public function buildQuery(string $connection = null): Query |
||
| 157 | { |
||
| 158 | $connection = $this->connect($connection); |
||
| 159 | return $this->newQuery($connection); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * 监听SQL执行 |
||
| 164 | * @access public |
||
| 165 | * @param callable $callback 回调方法 |
||
|
1 ignored issue
–
show
|
|||
| 166 | * @return void |
||
| 167 | */ |
||
| 168 | public function listen(callable $callback): void |
||
| 169 | { |
||
| 170 | $this->listen[] = $callback; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * 获取监听SQL执行 |
||
| 175 | * @access public |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getListen(): array |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * 设置Event对象 |
||
| 185 | * @param Event $event |
||
| 186 | */ |
||
| 187 | public function setEvent(Event $event) |
||
| 188 | { |
||
| 189 | $this->event = $event; |
||
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * 注册回调方法 |
||
| 194 | * @access public |
||
| 195 | * @param string $event 事件名 |
||
|
1 ignored issue
–
show
|
|||
| 196 | * @param callable $callback 回调方法 |
||
|
1 ignored issue
–
show
|
|||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | public function event(string $event, callable $callback): void |
||
| 200 | { |
||
| 201 | if ($this->event) { |
||
| 202 | $this->event->listen('db.' . $event, $callback); |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * 触发事件 |
||
| 208 | * @access public |
||
| 209 | * @param string $event 事件名 |
||
|
1 ignored issue
–
show
|
|||
| 210 | * @param mixed $params 传入参数 |
||
|
1 ignored issue
–
show
|
|||
| 211 | * @param bool $once |
||
|
1 ignored issue
–
show
|
|||
| 212 | * @return mixed |
||
| 213 | */ |
||
| 214 | public function trigger(string $event, $params = null, bool $once = false) |
||
| 215 | { |
||
| 216 | if ($this->event) { |
||
| 217 | return $this->event->trigger('db.' . $event, $params, $once); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * 创建一个新的查询对象 |
||
| 223 | * @access protected |
||
| 224 | * @param Connection $connection 连接对象 |
||
|
1 ignored issue
–
show
|
|||
| 225 | * @return Query |
||
| 226 | */ |
||
| 227 | protected function newQuery(Connection $connection = null): Query |
||
| 228 | { |
||
| 229 | /** @var Query $query */ |
||
| 230 | $connection = $connection ?: $this->connect(); |
||
| 231 | $class = $connection->getQueryClass(); |
||
| 232 | $query = new $class($connection); |
||
| 233 | |||
| 234 | $query->setDb($this); |
||
| 235 | |||
| 236 | return $query; |
||
| 237 | } |
||
| 238 | |||
| 239 | public function __call($method, $args) |
||
| 244 | } |
||
| 245 | } |
||
| 246 |