|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Kotori.php |
|
4
|
|
|
* |
|
5
|
|
|
* A Tiny Model-View-Controller PHP Framework |
|
6
|
|
|
* |
|
7
|
|
|
* This content is released under the Apache 2 License |
|
8
|
|
|
* |
|
9
|
|
|
* Copyright (c) 2015-2017 Kotori Technology. All rights reserved. |
|
10
|
|
|
* |
|
11
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
12
|
|
|
* you may not use this file except in compliance with the License. |
|
13
|
|
|
* You may obtain a copy of the License at |
|
14
|
|
|
* |
|
15
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
|
16
|
|
|
* |
|
17
|
|
|
* Unless required by applicable law or agreed to in writing, software |
|
18
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
19
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
20
|
|
|
* See the License for the specific language governing permissions and |
|
21
|
|
|
* limitations under the License. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Database Class |
|
26
|
|
|
* |
|
27
|
|
|
* @package Kotori |
|
28
|
|
|
* @subpackage Core |
|
29
|
|
|
* @author Kokororin |
|
30
|
|
|
* @link https://kotori.love |
|
31
|
|
|
*/ |
|
32
|
|
|
namespace Kotori\Core; |
|
33
|
|
|
|
|
34
|
|
|
use Exception; |
|
35
|
|
|
use Kotori\Debug\Hook; |
|
36
|
|
|
use Kotori\Exception\DatabaseException; |
|
37
|
|
|
use Medoo\Medoo; |
|
38
|
|
|
use PDOException; |
|
39
|
|
|
|
|
40
|
|
|
class Database extends Medoo |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* SQL queries |
|
44
|
|
|
* |
|
45
|
|
|
* @var array |
|
46
|
|
|
*/ |
|
47
|
|
|
public static $queries = []; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Instance Handle |
|
51
|
|
|
* |
|
52
|
|
|
* @var array |
|
53
|
|
|
*/ |
|
54
|
|
|
protected static $instance; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Disable Clone |
|
58
|
|
|
* |
|
59
|
|
|
* @return boolean |
|
60
|
|
|
*/ |
|
61
|
|
|
public function __clone() |
|
62
|
|
|
{ |
|
63
|
|
|
return false; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get singleton |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $key |
|
70
|
|
|
* @return object |
|
71
|
|
|
* |
|
72
|
|
|
* @throws \Kotori\Exception\DatabaseException |
|
73
|
|
|
*/ |
|
74
|
2 |
|
public static function getInstance($key = null) |
|
75
|
|
|
{ |
|
76
|
2 |
|
$config = Container::get('config')->get('db'); |
|
77
|
2 |
|
if (!is_array($config) || count($config) == 0) { |
|
78
|
2 |
|
return null; |
|
79
|
|
|
} elseif ($key == null) { |
|
|
|
|
|
|
80
|
|
|
$dbKeys = array_keys($config); |
|
81
|
|
|
if (isset($dbKeys[0])) { |
|
82
|
|
|
$key = $dbKeys[0]; |
|
83
|
|
|
} else { |
|
84
|
|
|
return null; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
Container::get('config')->set('selected_db_key', $key); |
|
89
|
|
|
|
|
90
|
|
|
if (!isset(self::$instance[$key])) { |
|
91
|
|
|
try { |
|
92
|
|
|
self::$instance[$key] = new self($config[$key]); |
|
93
|
|
|
} catch (PDOException $e) { |
|
94
|
|
|
throw new DatabaseException($e); |
|
95
|
|
|
} catch (Exception $e) { |
|
96
|
|
|
throw new DatabaseException($e); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return self::$instance[$key]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Class constructor |
|
105
|
|
|
* |
|
106
|
|
|
* Initialize Database. |
|
107
|
|
|
*/ |
|
108
|
|
|
public function __construct(array $options = []) |
|
109
|
|
|
{ |
|
110
|
|
|
parent::__construct([ |
|
111
|
|
|
'database_type' => $options['type'], |
|
112
|
|
|
'database_name' => $options['name'], |
|
113
|
|
|
'server' => $options['host'], |
|
114
|
|
|
'username' => $options['user'], |
|
115
|
|
|
'password' => $options['pwd'], |
|
116
|
|
|
'charset' => $options['charset'], |
|
117
|
|
|
'port' => $options['port'], |
|
118
|
|
|
]); |
|
119
|
|
|
Hook::listen(__CLASS__); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* medoo::query() |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $query |
|
126
|
|
|
* @param array $map |
|
127
|
|
|
* @return \PDOStatement |
|
128
|
|
|
*/ |
|
129
|
|
View Code Duplication |
public function query($query, $map = []) |
|
|
|
|
|
|
130
|
|
|
{ |
|
131
|
|
|
$statement = parent::exec($query, $map); |
|
|
|
|
|
|
132
|
|
|
$lastSQL = parent::last(); |
|
|
|
|
|
|
133
|
|
|
Container::get('logger')->info($lastSQL); |
|
134
|
|
|
array_push(self::$queries, $lastSQL); |
|
135
|
|
|
return $statement; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* medoo:exec() |
|
140
|
|
|
* |
|
141
|
|
|
* @param string $query |
|
142
|
|
|
* @param array $map |
|
143
|
|
|
* @return \PDOStatement |
|
144
|
|
|
*/ |
|
145
|
4 |
View Code Duplication |
public function exec($query, $map = []) |
|
|
|
|
|
|
146
|
|
|
{ |
|
147
|
4 |
|
$statement = parent::exec($query, $map); |
|
|
|
|
|
|
148
|
4 |
|
$lastSQL = parent::last(); |
|
|
|
|
|
|
149
|
4 |
|
Container::get('logger')->info($lastSQL); |
|
150
|
4 |
|
array_push(self::$queries, $lastSQL); |
|
151
|
4 |
|
return $statement; |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|