66
77class DB
88{
9+ /**
10+ * @property array $pdoOptions
11+ */
12+ private static $ pdoOptions = [];
13+
14+
15+ /**
16+ * @property array $pdoCharset
17+ */
18+ private static $ pdoCharset = 'utf8mb4 ' ;
19+
920 /**
1021 * @property array $pdoList
1122 */
@@ -32,36 +43,54 @@ private function __construct()
3243 */
3344 public static function connect ($ id , $ driver , $ host , $ database , $ user , $ passwd )
3445 {
35- if (isset (self ::$ pdoList [$ id ])) {
46+ if (isset (self ::$ pdoList [$ id ])) {
3647 throw new \RuntimeException ('The id " ' . $ id . '" is already in use. ' );
3748 }
3849
3950 $ defaultOptions = [
40- PDO ::ATTR_EMULATE_PREPARES => false ,
4151 PDO ::ATTR_DEFAULT_FETCH_MODE => PDO ::FETCH_ASSOC ,
42- PDO ::ATTR_PERSISTENT => true ,
4352 PDO ::ATTR_ERRMODE => PDO ::ERRMODE_EXCEPTION ,
4453 ];
4554
55+ $ options = array_merge (self ::$ pdoOptions , $ defaultOptions );
56+
4657 $ pdo = new PDO (
47- $ driver . ':host= ' . $ host . ';dbname= ' . $ database ,
58+ $ driver . ':host= ' . $ host . ';dbname= ' . $ database . ' ;charset= ' . self :: $ pdoCharset ,
4859 $ user ,
4960 $ passwd ,
50- $ defaultOptions
61+ $ options
5162 );
5263
5364 self ::$ pdoList [$ id ] = $ pdo ;
5465 self ::$ currentPdo = $ pdo ;
5566 }
5667
68+ /**
69+ * @param array $options
70+ */
71+ public static function setPDOOptions ($ options )
72+ {
73+ if (is_array ($ options )) {
74+ self ::$ pdoOptions = $ options ;
75+ }
76+ }
77+
78+ /**
79+ * @param string $charset
80+ */
81+ public static function setPDOCharset ($ charset )
82+ {
83+ self ::$ pdoCharset = $ charset ;
84+ }
85+
5786 /**
5887 * Use a PDO with a specific id
5988 *
6089 * @param string $id
6190 */
6291 public static function useConnection ($ id )
6392 {
64- if (!isset (self ::$ pdoList [$ id ])) {
93+ if (!isset (self ::$ pdoList [$ id ])) {
6594 throw new \RuntimeException ('PDO with ID " ' . $ id . '" does not exist ' );
6695 }
6796
@@ -78,7 +107,7 @@ public static function useConnection($id)
78107 */
79108 public static function PDO ($ id = null )
80109 {
81- if (isset ($ id )) {
110+ if (isset ($ id )) {
82111 self ::useConnection ($ id );
83112 }
84113
0 commit comments