审查视图

lib/DbPool.php 6.9 KB
1  
邓超 authored
1 2
<?php
1  
邓超 authored
3 4 5 6 7 8 9 10 11
namespace Lib;

/**
 * db 池
 * @author:dc
 * @time 2023/2/13 15:03
 * Class DbPool
 * @package Lib
 */
1  
邓超 authored
12 13 14
class DbPool {

    /**
1  
邓超 authored
15
     * @var \Lib\DbPool[]
1  
邓超 authored
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
     */
    static $instance = [];


    /**
     * @var \PDO
     */
    private $client;

    /**
     * @return mixed
     * @author:dc
     * @time 2023/2/13 9:12
     */
    public function getClient(){
1  
邓超 authored
31 32 33
        if(!$this->client){
            $this->connect();
        }
1  
邓超 authored
34 35 36 37 38 39
        return $this->client;
    }


    public function __construct()
    {
1  
邓超 authored
40 41 42 43 44 45 46 47 48
        $this->connect();
    }

    /**
     * 连接sql
     * @author:dc
     * @time 2023/3/23 9:14
     */
    public function connect(){
1  
邓超 authored
49 50 51 52 53 54 55 56
        try {
            $this->client = new \PDO(
                'mysql:charset=utf8mb4;dbname='.DB_DATABASE.';host='.DB_HOST.';port='.DB_PORT,
                DB_USER,
                DB_PASSWORD,
                [
                    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
                    \PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8mb4'",
1  
邓超 authored
57
                    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
1  
邓超 authored
58 59 60
                ]
            );
        }catch (\PDOException $e){
1  
邓超 authored
61
            logs($e->getMessage());
1  
邓超 authored
62 63 64 65
        }
    }

1  
邓超 authored
66
    /**
1  
邓超 authored
67 68 69
     * 查询
     * @param string|array $sql
     * @return false|\PDOStatement
1  
邓超 authored
70
     * @author:dc
1  
邓超 authored
71
     * @time 2023/2/17 10:01
1  
邓超 authored
72
     */
1  
邓超 authored
73 74 75 76 77 78
    private function query(string|array $sql){
        if(is_array($sql)){
            list($sql,$params) = $sql;
        }else{
            $params = null;
        }
1  
邓超 authored
79 80 81 82 83 84

        if(APP_DEBUG) {
            $timer = microtime(true);
        }

1  
邓超 authored
85 86 87 88 89 90 91
        try {
            $query = $this->getClient()->prepare($sql);
            $ret = $query->execute($params);
        }catch (\PDOException $e){
            logs($e->getMessage().$e->getTraceAsString());
            $ret = false;
        }
1  
邓超 authored
92 93

1  
邓超 authored
94
        if(APP_DEBUG){
1  
邓超 authored
95 96
            $timer2  = microtime(true);
1  
邓超 authored
97
            // todo:: 记录日志,生产请注释
1  
邓超 authored
98
            $sql = '['.substr(($timer2-$timer)*1000,0,6).'ms] '.$sql;
1  
邓超 authored
99 100
            logs(
                $params ? [$sql,$params] : $sql,
1  
邓超 authored
101
                LOG_PATH.'/'.date('Y-m-d').'.sql.log'
1  
邓超 authored
102 103 104
            );
        }
1  
邓超 authored
105
        if($ret){
1  
邓超 authored
106 107 108 109
            return $query;
        }

        return false;
1  
邓超 authored
110
    }
1  
邓超 authored
111
1  
邓超 authored
112
1  
邓超 authored
113
    /**
1  
邓超 authored
114 115 116 117 118 119
     * 更新数据
     * @param string $table
     * @param array $data
     * @param string $where
     * @param bool $timeauto
     * @return int
1  
邓超 authored
120
     * @author:dc
1  
邓超 authored
121
     * @time 2023/2/17 14:03
1  
邓超 authored
122
     */
1  
邓超 authored
123 124 125 126 127 128 129
    public function update(string $table, array $data, string $where, $timeauto = true):int {

        if($timeauto){
            $data['updated_at'] = empty($data['updated_at']) ? date('Y-m-d H:i:s') : $data['updated_at'];
        }

        $sql = "update `{$table}` set ".dbUpdate($data). " where ".$where;
1  
邓超 authored
130
1  
邓超 authored
131
        $data = $this->getData($data);
1  
邓超 authored
132
1  
邓超 authored
133 134 135 136 137
        $query = $this->query([$sql,$data]);
        if($query){
            return $query->rowCount();
        }
        return 0;
1  
邓超 authored
138 139
    }
1  
邓超 authored
140 141

    /**
1  
邓超 authored
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
     * 在更新/插入时处理数据
     * @param $data
     * @return mixed
     * @author:dc
     * @time 2023/2/18 14:50
     */
    private function getData($data){
        // 如果存储的值是数组,就json一次
        foreach ($data as $k=>$datum){
            if(is_array($datum)){
                $data[$k] = json_encode($datum,JSON_UNESCAPED_UNICODE);
            }elseif ($datum === null){
                $data[$k] = '';
            }
        }
        return $data;
    }


    /**
1  
邓超 authored
162 163 164 165 166
     * 插入数据
     * @param string $table
     * @param array $data
     * @param bool $timeauto
     * @return int
1  
邓超 authored
167
     * @author:dc
1  
邓超 authored
168
     * @time 2023/2/17 14:04
1  
邓超 authored
169
     */
1  
邓超 authored
170
    public function insert(string $table, array $data, $timeauto = true):int {
1  
邓超 authored
171
1  
邓超 authored
172 173 174
        if($timeauto){
            $data['created_at'] = empty($data['created_at']) ? date('Y-m-d H:i:s') : $data['created_at'];
        }
1  
邓超 authored
175
1  
邓超 authored
176
        $sql = "insert into `{$table}` set ".dbUpdate($data);
1  
邓超 authored
177
1  
邓超 authored
178
        $data = $this->getData($data);
1  
邓超 authored
179
1  
邓超 authored
180
        $query = $this->query([$sql,$data]);
1  
邓超 authored
181
1  
邓超 authored
182
        if($query){
1  
邓超 authored
183
            return $this->getClient()->lastInsertId();
1  
邓超 authored
184 185 186
        }

        return 0;
1  
邓超 authored
187 188
    }
1  
邓超 authored
189
    /**
1  
邓超 authored
190
     * 删除语句 软删
1  
邓超 authored
191 192
     * @param string $table
     * @param array $where
1  
邓超 authored
193
     * @param null $upFiled
1  
邓超 authored
194 195
     * @return int
     * @author:dc
1  
邓超 authored
196
     * @time 2023/4/11 14:47
1  
邓超 authored
197
     */
1  
邓超 authored
198 199 200 201 202 203
    public function delete(string $table, array $where,$upFiled=null){

        if($upFiled){
            return $this->update($table,[$upFiled === true ? 'deleted_at' : $upFiled =>time()],$where);
        }
1  
邓超 authored
204 205 206 207 208 209 210 211 212 213 214
        $sql = "delete from `{$table}` where ".dbUpdate($where);

        $query = $this->query($sql);

        if($query){
            return $query->rowCount();
        }

        return 0;
    }
1  
邓超 authored
215
1  
邓超 authored
216 217 218 219 220 221 222
    /**
     * 统计数量
     * @param string $sql
     * @return int
     * @author:dc
     * @time 2023/2/14 16:19
     */
1  
邓超 authored
223 224 225
    public function count(string|array $sql):int{
        $query = $this->query($sql);
        if($query){
1  
邓超 authored
226 227 228 229 230
            return $query->fetch(\PDO::FETCH_COLUMN);
        }
        return 0;
    }
1  
邓超 authored
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    /**
     * 某个值
     * @param string|array $sql
     * @return mixed|null
     * @author:dc
     * @time 2023/2/17 11:03
     */
    public function value(string|array $sql){
        $query = $this->query($sql);
        if($query){
            return $query->fetch(\PDO::FETCH_COLUMN);
        }
        return null;
    }
1  
邓超 authored
246
1  
邓超 authored
247 248 249 250 251 252 253 254 255
    /**
     * 查询一条数据
     * @param string|array $sql
     * @return mixed|null
     * @author:dc
     * @time 2023/2/13 14:54
     */
    public function first(string|array $sql){
1  
邓超 authored
256
        $query = $this->query($sql);
1  
邓超 authored
257
1  
邓超 authored
258
        if($query){
1  
邓超 authored
259 260
            return $query->fetch();
        }
1  
邓超 authored
261
1  
邓超 authored
262
        return null;
1  
邓超 authored
263 264
    }
1  
邓超 authored
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    /**
     * 查询列表
     * @param string|array $sql
     * @return mixed|null
     * @author:dc
     * @time 2023/2/13 14:54
     */
    public function all(string|array $sql){

        $query = $this->query($sql);

        if($query){
            return $query->fetchAll();
        }

        return null;
    }
1  
邓超 authored
283
1  
邓超 authored
284 285 286 287 288 289
    /**
     * 事务开启
     * @author:dc
     * @time 2023/2/17 11:35
     */
    public function transaction(){
1  
邓超 authored
290
        $this->getClient()->beginTransaction();
1  
邓超 authored
291
    }
1  
邓超 authored
292
1  
邓超 authored
293 294 295 296 297 298
    /**
     * 事务回滚
     * @author:dc
     * @time 2023/2/17 11:35
     */
    public function rollBack(){
1  
邓超 authored
299
        $this->getClient()->rollBack();
1  
邓超 authored
300 301 302 303 304 305 306 307
    }

    /**
     * 事务提交
     * @author:dc
     * @time 2023/2/17 11:35
     */
    public function commit(){
1  
邓超 authored
308
        $this->getClient()->commit();
1  
邓超 authored
309 310 311
    }

1  
邓超 authored
312
1  
邓超 authored
313 314 315 316 317 318
    /**
     * @param $cid
     * @return DbPool
     * @author:dc
     * @time 2023/2/13 9:39
     */
1  
邓超 authored
319
    public static function instance($cid=0){
1  
邓超 authored
320
        if(empty(static::$instance[$cid])){
1  
邓超 authored
321
            static::$instance[$cid] = new DbPool();
1  
邓超 authored
322 323 324 325 326
        }
        return static::$instance[$cid];
    }

1  
邓超 authored
327 328 329 330
    /**
     * 结束
     */
    public function __destruct(){
1  
邓超 authored
331
        $this->close();
1  
邓超 authored
332 333 334
    }

1  
邓超 authored
335 336 337 338
    public function close(){
        $this->client = null;
    }
1  
邓超 authored
339 340

1  
邓超 authored
341
}