8
8
9
9
namespace Inhere \Library \Components ;
10
10
11
+ use Inhere \Exceptions \UnknownMethodException ;
11
12
use Inhere \Library \Helpers \DsnHelper ;
12
13
use Inhere \Library \Traits \LiteConfigTrait ;
13
14
use Inhere \Library \Traits \LiteEventTrait ;
@@ -48,6 +49,18 @@ class DatabaseClient
48
49
/** @var string */
49
50
protected $ prefixPlaceholder = '{@pfx} ' ;
50
51
52
+ /** @var string */
53
+ protected $ quoteNamePrefix = '" ' ;
54
+
55
+ /** @var string */
56
+ protected $ quoteNameSuffix = '" ' ;
57
+
58
+ /** @var string */
59
+ protected $ quoteNameEscapeChar = '" ' ;
60
+
61
+ /** @var string */
62
+ protected $ quoteNameEscapeReplace = '"" ' ;
63
+
51
64
/**
52
65
* All of the queries run against the connection.
53
66
* @var array
@@ -80,6 +93,7 @@ class DatabaseClient
80
93
81
94
'tablePrefix ' => '' ,
82
95
96
+ 'debug ' => false ,
83
97
// retry times.
84
98
'retry ' => 0 ,
85
99
];
@@ -97,13 +111,39 @@ class DatabaseClient
97
111
PDO ::ATTR_EMULATE_PREPARES => false ,
98
112
];
99
113
114
+ /**
115
+ * @param array $config
116
+ * @return static
117
+ */
118
+ public static function make (array $ config = [])
119
+ {
120
+ return new static ($ config );
121
+ }
122
+
100
123
/**
101
124
* @param array $config
102
125
*/
103
126
public function __construct (array $ config = [])
104
127
{
128
+ if (!class_exists (\PDO ::class, false )) {
129
+ throw new \RuntimeException ("The php extension 'redis' is required. " );
130
+ }
131
+
105
132
$ this ->setConfig ($ config );
106
133
134
+ // init something...
135
+ $ this ->debug = (bool )$ this ->config ['debug ' ];
136
+ $ this ->tablePrefix = $ this ->config ['tablePrefix ' ];
137
+ $ this ->databaseName = $ this ->config ['database ' ];
138
+
139
+ $ retry = (int ) $ this ->config ['retry ' ];
140
+ $ this ->config ['retry ' ] = ($ retry > 0 && $ retry <= 5 ) ? $ retry : 0 ;
141
+ $ this ->config ['options ' ] = static ::$ pdoOptions + $ this ->config ['options ' ];
142
+
143
+ if (!self ::isSupported ($ this ->config ['driver ' ])) {
144
+ throw new \RuntimeException ("The system is not support driver: {$ this ->config ['driver ' ]}" );
145
+ }
146
+
107
147
$ this ->initQuoteNameChar ($ this ->config ['driver ' ]);
108
148
}
109
149
@@ -116,18 +156,14 @@ public function connect()
116
156
return $ this ;
117
157
}
118
158
119
- $ this ->config ['options ' ] = array_merge ($ this ->config ['options ' ], static ::$ pdoOptions );
120
-
121
159
$ config = $ this ->config ;
122
160
$ retry = (int ) $ config ['retry ' ];
123
161
$ retry = ($ retry > 0 && $ retry <= 5 ) ? $ retry : 0 ;
124
- $ options = is_array ($ config ['options ' ]) ? $ config ['options ' ] : [];
125
162
$ dsn = DsnHelper::getDsn ($ config );
126
163
127
164
do {
128
165
try {
129
- $ pdo = new PDO ($ dsn , $ config ['user ' ], $ config ['password ' ], $ options );
130
-
166
+ $ this ->pdo = new PDO ($ dsn , $ config ['user ' ], $ config ['password ' ], $ config ['options ' ]);
131
167
break ;
132
168
} catch (\PDOException $ e ) {
133
169
if ($ retry <= 0 ) {
@@ -139,7 +175,6 @@ public function connect()
139
175
usleep (50000 );
140
176
} while ($ retry >= 0 );
141
177
142
- $ this ->pdo = $ pdo ;
143
178
$ this ->log ('connect to DB server ' , ['config ' => $ config ], 'connect ' );
144
179
$ this ->fire (self ::CONNECT , [$ this ]);
145
180
@@ -154,10 +189,10 @@ public function reconnect()
154
189
155
190
/**
156
191
* disconnect
157
- * @return bool
158
192
*/
159
193
public function disconnect ()
160
194
{
195
+ $ this ->fire (self ::DISCONNECT , [$ this ]);
161
196
$ this ->pdo = null ;
162
197
}
163
198
@@ -224,27 +259,27 @@ public function __call($name, array $arguments)
224
259
/**
225
260
* Run a select statement, fetch one
226
261
* @param string $from
227
- * @param array|string $wheres
262
+ * @param array|string|int $wheres
228
263
* @param string|array $select
229
264
* @param array $options
230
265
* @return array
231
266
*/
232
267
public function find (string $ from , $ wheres = 1 , $ select = '* ' , array $ options = [])
233
268
{
234
-
269
+ return [];
235
270
}
236
271
237
272
/**
238
273
* Run a select statement, fetch all
239
274
* @param string $from
240
- * @param array|string $wheres
275
+ * @param array|string|int $wheres
241
276
* @param string|array $select
242
277
* @param array $options
243
278
* @return array
244
279
*/
245
280
public function findAll (string $ from , $ wheres = 1 , $ select = '* ' , array $ options = [])
246
281
{
247
- # code...
282
+ return [];
248
283
}
249
284
250
285
/**
@@ -319,11 +354,13 @@ public function count(string $table, $wheres)
319
354
* $db->exists();
320
355
* // SQL: select exists(select * from `table` where (`phone` = 152xxx)) as `exists`;
321
356
* ```
357
+ * @param $statement
358
+ * @param array $bindings
322
359
* @return int
323
360
*/
324
361
public function exists ($ statement , array $ bindings = [])
325
362
{
326
- $ sql = sprintf ('SELECT EXISTS(%s) AS `exists` ' , $ sql );
363
+ $ sql = sprintf ('SELECT EXISTS(%s) AS `exists` ' , $ statement );
327
364
328
365
$ result = $ this ->fetchObject ($ sql , $ bindings );
329
366
@@ -747,8 +784,6 @@ public function ping()
747
784
/**
748
785
* handle where condition
749
786
* @param array|string|\Closure $wheres
750
- * @param RecordModel|string $model the model class name, is a string
751
- * @param Query $query
752
787
* @example
753
788
* ```
754
789
* ...
@@ -1066,6 +1101,16 @@ public static function getAvailableDrivers()
1066
1101
return PDO ::getAvailableDrivers ();
1067
1102
}
1068
1103
1104
+ /**
1105
+ * Is this driver supported.
1106
+ * @param string $driver
1107
+ * @return bool
1108
+ */
1109
+ public static function isSupported (string $ driver )
1110
+ {
1111
+ return in_array ($ driver , \PDO ::getAvailableDrivers (), true );
1112
+ }
1113
+
1069
1114
/**
1070
1115
* @param PDOStatement $sth
1071
1116
* @return $this
@@ -1079,6 +1124,19 @@ public function freeResource($sth = null)
1079
1124
return $ this ;
1080
1125
}
1081
1126
1127
+ /**************************************************************************
1128
+ * getter/setter methods
1129
+ *************************************************************************/
1130
+
1131
+ /**
1132
+ * Get the name of the driver.
1133
+ * @return string
1134
+ */
1135
+ public function getDriverName ()
1136
+ {
1137
+ return $ this ->config ['driver ' ];
1138
+ }
1139
+
1082
1140
/**
1083
1141
* Get the name of the connected database.
1084
1142
* @return string
@@ -1172,4 +1230,5 @@ public function isConnected(): bool
1172
1230
{
1173
1231
return (bool ) $ this ->pdo ;
1174
1232
}
1233
+
1175
1234
}
0 commit comments