55use PDO ;
66use SplFileInfo ;
77use Throwable ;
8+ use Uspdev \Cache \Cache ;
89use Uspdev \Replicado \Replicado as Config ;
910
1011class DB
@@ -15,8 +16,10 @@ class DB
1516 */
1617 private static $ instance ;
1718
18- private function __construct () {}
19- private function __clone () {}
19+ private function __construct ()
20+ {}
21+ private function __clone ()
22+ {}
2023
2124 /**
2225 * Retorna uma instância do pdo - cria ou reaproveita se for o caso
@@ -26,12 +29,12 @@ private function __clone() {}
2629 protected static function getInstance ()
2730 {
2831 $ config = Config::getInstance ();
29- if (!self ::$ instance ) {
32+ if (!SELF ::$ instance ) {
3033 try {
3134 $ dsn = "dblib:host= {$ config ->host }: {$ config ->port };dbname= {$ config ->database }" ;
32- // self ::$instance = new PDO($dsn, $config->username, $config->password, [PDO::ATTR_TIMEOUT => 10]);
33- self ::$ instance = new PDO ($ dsn , $ config ->username , $ config ->password );
34- self ::$ instance ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
35+ // SELF ::$instance = new PDO($dsn, $config->username, $config->password, [PDO::ATTR_TIMEOUT => 10]);
36+ SELF ::$ instance = new PDO ($ dsn , $ config ->username , $ config ->password );
37+ SELF ::$ instance ->setAttribute (PDO ::ATTR_ERRMODE , PDO ::ERRMODE_EXCEPTION );
3538 } catch (Throwable $ t ) {
3639 $ config ->log ('Erro na conexão ' , $ t ->getMessage ());
3740 if ($ config ->debug ) {
@@ -41,7 +44,7 @@ protected static function getInstance()
4144 }
4245 }
4346 }
44- return self ::$ instance ;
47+ return SELF ::$ instance ;
4548 }
4649
4750 /**
@@ -53,7 +56,13 @@ protected static function getInstance()
5356 */
5457 public static function fetch (string $ query , array $ param = [])
5558 {
56- return self ::overrideFetch ('fetch ' , $ query , $ param );
59+ $ config = Config::getInstance ();
60+ if ($ config ->usarCache ) {
61+ $ cache = $ config ->getCacheInstance ();
62+ return $ cache ->getCached ('Uspdev\Replicado\DB::overrideFetch ' , ['fetch ' , $ query , $ param ]);
63+ } else {
64+ return SELF ::overrideFetch ('fetch ' , $ query , $ param );
65+ }
5766 }
5867
5968 /**
@@ -65,7 +74,13 @@ public static function fetch(string $query, array $param = [])
6574 */
6675 public static function fetchAll (string $ query , array $ param = [])
6776 {
68- return self ::overrideFetch ('fetchAll ' , $ query , $ param );
77+ $ config = Config::getInstance ();
78+ if ($ config ->usarCache ) {
79+ $ cache = $ config ->getCacheInstance ();
80+ return $ cache ->getCached ('Uspdev\Replicado\DB::overrideFetch ' , ['fetchAll ' , $ query , $ param ]);
81+ } else {
82+ return SELF ::overrideFetch ('fetchAll ' , $ query , $ param );
83+ }
6984 }
7085
7186 /**
@@ -82,12 +97,10 @@ public static function fetchAll(string $query, array $param = [])
8297 public static function overrideFetch (string $ fetchType , string $ query , array $ param = [])
8398 {
8499 $ config = Config::getInstance ();
85- $ query = self ::automaticReplaces ($ query );
86- $ stmt = self ::getInstance ()->prepare ($ query );
100+ $ query = SELF ::automaticReplaces ($ query );
101+ $ stmt = SELF ::getInstance ()->prepare ($ query );
87102 foreach ($ param as $ campo => $ valor ) {
88- $ valor = $ config ->sybase
89- ? mb_convert_encoding ($ valor , 'ISO-8859-1 ' , 'UTF-8 ' )
90- : $ valor ;
103+ $ valor = $ config ->sybase ? utf8_decode ($ valor ) : $ valor ;
91104 $ stmt ->bindValue (": $ campo " , $ valor );
92105 if ($ config ->debugLevel >= 2 ) {
93106 $ queryLog = str_replace (": $ campo " , $ valor , $ queryLog ?? $ query );
@@ -136,9 +149,10 @@ public static function overrideFetch(string $fetchType, string $query, array $pa
136149 *
137150 * @return bool
138151 */
139- public static function test ()
152+ public static function test ($ config = [] )
140153 {
141- return self ::getInstance () instanceof PDO ;
154+ $ config = Config::getInstance ($ config );
155+ return SELF ::getInstance () ? true : false ;
142156 }
143157
144158 /**
0 commit comments