88
99namespace Inhere \Library \Components ;
1010
11+ use Inhere \Exceptions \UnknownMethodException ;
1112use Inhere \Library \Helpers \DsnHelper ;
1213use Inhere \Library \Traits \LiteConfigTrait ;
1314use Inhere \Library \Traits \LiteEventTrait ;
@@ -48,6 +49,18 @@ class DatabaseClient
4849 /** @var string */
4950 protected $ prefixPlaceholder = '{@pfx} ' ;
5051
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+
5164 /**
5265 * All of the queries run against the connection.
5366 * @var array
@@ -80,6 +93,7 @@ class DatabaseClient
8093
8194 'tablePrefix ' => '' ,
8295
96+ 'debug ' => false ,
8397 // retry times.
8498 'retry ' => 0 ,
8599 ];
@@ -97,13 +111,39 @@ class DatabaseClient
97111 PDO ::ATTR_EMULATE_PREPARES => false ,
98112 ];
99113
114+ /**
115+ * @param array $config
116+ * @return static
117+ */
118+ public static function make (array $ config = [])
119+ {
120+ return new static ($ config );
121+ }
122+
100123 /**
101124 * @param array $config
102125 */
103126 public function __construct (array $ config = [])
104127 {
128+ if (!class_exists (\PDO ::class, false )) {
129+ throw new \RuntimeException ("The php extension 'redis' is required. " );
130+ }
131+
105132 $ this ->setConfig ($ config );
106133
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+
107147 $ this ->initQuoteNameChar ($ this ->config ['driver ' ]);
108148 }
109149
@@ -116,18 +156,14 @@ public function connect()
116156 return $ this ;
117157 }
118158
119- $ this ->config ['options ' ] = array_merge ($ this ->config ['options ' ], static ::$ pdoOptions );
120-
121159 $ config = $ this ->config ;
122160 $ retry = (int ) $ config ['retry ' ];
123161 $ retry = ($ retry > 0 && $ retry <= 5 ) ? $ retry : 0 ;
124- $ options = is_array ($ config ['options ' ]) ? $ config ['options ' ] : [];
125162 $ dsn = DsnHelper::getDsn ($ config );
126163
127164 do {
128165 try {
129- $ pdo = new PDO ($ dsn , $ config ['user ' ], $ config ['password ' ], $ options );
130-
166+ $ this ->pdo = new PDO ($ dsn , $ config ['user ' ], $ config ['password ' ], $ config ['options ' ]);
131167 break ;
132168 } catch (\PDOException $ e ) {
133169 if ($ retry <= 0 ) {
@@ -139,7 +175,6 @@ public function connect()
139175 usleep (50000 );
140176 } while ($ retry >= 0 );
141177
142- $ this ->pdo = $ pdo ;
143178 $ this ->log ('connect to DB server ' , ['config ' => $ config ], 'connect ' );
144179 $ this ->fire (self ::CONNECT , [$ this ]);
145180
@@ -154,10 +189,10 @@ public function reconnect()
154189
155190 /**
156191 * disconnect
157- * @return bool
158192 */
159193 public function disconnect ()
160194 {
195+ $ this ->fire (self ::DISCONNECT , [$ this ]);
161196 $ this ->pdo = null ;
162197 }
163198
@@ -224,27 +259,27 @@ public function __call($name, array $arguments)
224259 /**
225260 * Run a select statement, fetch one
226261 * @param string $from
227- * @param array|string $wheres
262+ * @param array|string|int $wheres
228263 * @param string|array $select
229264 * @param array $options
230265 * @return array
231266 */
232267 public function find (string $ from , $ wheres = 1 , $ select = '* ' , array $ options = [])
233268 {
234-
269+ return [];
235270 }
236271
237272 /**
238273 * Run a select statement, fetch all
239274 * @param string $from
240- * @param array|string $wheres
275+ * @param array|string|int $wheres
241276 * @param string|array $select
242277 * @param array $options
243278 * @return array
244279 */
245280 public function findAll (string $ from , $ wheres = 1 , $ select = '* ' , array $ options = [])
246281 {
247- # code...
282+ return [];
248283 }
249284
250285 /**
@@ -319,11 +354,13 @@ public function count(string $table, $wheres)
319354 * $db->exists();
320355 * // SQL: select exists(select * from `table` where (`phone` = 152xxx)) as `exists`;
321356 * ```
357+ * @param $statement
358+ * @param array $bindings
322359 * @return int
323360 */
324361 public function exists ($ statement , array $ bindings = [])
325362 {
326- $ sql = sprintf ('SELECT EXISTS(%s) AS `exists` ' , $ sql );
363+ $ sql = sprintf ('SELECT EXISTS(%s) AS `exists` ' , $ statement );
327364
328365 $ result = $ this ->fetchObject ($ sql , $ bindings );
329366
@@ -747,8 +784,6 @@ public function ping()
747784 /**
748785 * handle where condition
749786 * @param array|string|\Closure $wheres
750- * @param RecordModel|string $model the model class name, is a string
751- * @param Query $query
752787 * @example
753788 * ```
754789 * ...
@@ -1066,6 +1101,16 @@ public static function getAvailableDrivers()
10661101 return PDO ::getAvailableDrivers ();
10671102 }
10681103
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+
10691114 /**
10701115 * @param PDOStatement $sth
10711116 * @return $this
@@ -1079,6 +1124,19 @@ public function freeResource($sth = null)
10791124 return $ this ;
10801125 }
10811126
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+
10821140 /**
10831141 * Get the name of the connected database.
10841142 * @return string
@@ -1172,4 +1230,5 @@ public function isConnected(): bool
11721230 {
11731231 return (bool ) $ this ->pdo ;
11741232 }
1233+
11751234}
0 commit comments