1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @see https://www.github.com/janhuang |
7
|
|
|
* @see http://www.fast-d.cn/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FastD\Model; |
11
|
|
|
|
12
|
|
|
use Medoo\Medoo; |
13
|
|
|
use PDO; |
14
|
|
|
use PDOException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Database. |
18
|
|
|
*/ |
19
|
|
|
class Database extends Medoo |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var array|null |
23
|
|
|
*/ |
24
|
|
|
protected $config = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var PDO |
28
|
|
|
*/ |
29
|
|
|
public $pdo; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Database constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $options |
35
|
|
|
*/ |
36
|
44 |
|
public function __construct(array $options = null) |
37
|
|
|
{ |
38
|
44 |
|
$this->config = $options; |
39
|
|
|
|
40
|
44 |
|
parent::__construct($this->config); |
41
|
|
|
|
42
|
44 |
|
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
43
|
44 |
|
$this->pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); |
44
|
44 |
|
$this->pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
45
|
44 |
|
$this->pdo->setAttribute(PDO::ATTR_STRINGIFY_FETCHES, false); |
46
|
44 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* reconnect database. |
50
|
|
|
*/ |
51
|
|
|
public function reconnect() |
52
|
|
|
{ |
53
|
|
|
$this->__construct($this->config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $query |
58
|
|
|
* |
59
|
|
|
* @return bool|\PDOStatement |
60
|
|
|
*/ |
61
|
3 |
View Code Duplication |
public function query($query) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
try { |
64
|
3 |
|
return parent::query($query); |
65
|
|
|
} catch (PDOException $e) { |
66
|
|
|
if ('HY000' !== $e->getCode()) { |
67
|
|
|
throw $e; |
68
|
|
|
} |
69
|
|
|
$this->reconnect(); |
70
|
|
|
|
71
|
|
|
return parent::query($query); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $query |
77
|
|
|
* |
78
|
|
|
* @return bool|\PDOStatement |
79
|
|
|
*/ |
80
|
1 |
View Code Duplication |
public function exec($query) |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
try { |
83
|
1 |
|
return parent::exec($query); |
84
|
|
|
} catch (PDOException $e) { |
85
|
|
|
if ('HY000' !== $e->getCode()) { |
86
|
|
|
throw $e; |
87
|
|
|
} |
88
|
|
|
$this->reconnect(); |
89
|
|
|
|
90
|
|
|
return parent::exec($query); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
1 |
|
public function has($table, $join, $where = null) |
95
|
|
|
{ |
96
|
1 |
|
$column = null; |
97
|
|
|
|
98
|
1 |
|
$query = $this->query('SELECT EXISTS('.$this->selectContext($table, $join, $column, $where, 1).')'); |
99
|
|
|
|
100
|
1 |
|
if ($query && intval($query->fetchColumn()) === 1) { |
101
|
1 |
|
return true; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.