10
10
* 2021年11月16日 -> Fixed Fetch method when same SQL is called more than once
11
11
* 2022年01月22日 -> Fixed Fetch method when same SQL is called in a simple way. Also added a way to RETRIEVE data if same SQL is sent again
12
12
* 2022年03月30日 -> Made some refactor in try/catch methods. Changed explanations to methods
13
+ * 2022年04月04日 -> Turned DB Objet into non-static object. This way is much more easier to run multiple queries while fetching more data
13
14
*/
14
15
15
16
class db
16
17
{
17
- /** This will define if errors will be displayed */
18
- private $ debug = false ;
19
-
20
18
/** This stores the current $connectionName */
21
19
protected static $ connectionName = null ;
22
20
@@ -63,11 +61,14 @@ class db
63
61
/**
64
62
* @return object $instance Returns the database instance
65
63
*/
66
- private static function getInstance ()
64
+ private static function getInstance ($ key = false )
67
65
{
68
66
if (self ::$ dbInit == null ) {
69
67
self ::$ dbInit = microtime ();
70
68
}
69
+ if ($ key && isset (self ::$ object [$ key ])) {
70
+ return self ::$ object [$ key ]->getInstance ();
71
+ }
71
72
if (is_null (self ::$ connectionName )) {
72
73
global $ DB_HOST , $ DB_USER , $ DB_PASSWORD , $ DB_NAME ;
73
74
$ connectionName = "default " ;
@@ -84,7 +85,6 @@ private static function getInstance()
84
85
$ instance = new PDO ('mysql:host= ' . self ::$ connections [self ::$ connectionName ]['HOST ' ] . ";dbname= " . self ::$ connections [self ::$ connectionName ]['NAME ' ] . "; " , self ::$ connections [self ::$ connectionName ]['USER ' ], self ::$ connections [self ::$ connectionName ]['PASSWORD ' ]);
85
86
if ($ instance ) {
86
87
$ instance ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , false );
87
- //$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
88
88
self ::updateTotalRequests ();
89
89
return $ instance ;
90
90
} else {
@@ -175,10 +175,6 @@ private static function encapsulate($mixed, $simple = false)
175
175
unset(self ::$ object [$ key ]);
176
176
return false ;
177
177
}
178
- /*if (self::$object[$key]->extra["rows"] + 1 == self::$object[$key]->extra["totalEntries"] && self::$object[$key]->extra["totalEntries"] <= 0) {
179
- unset(self::$object[$key]);
180
- return false;
181
- }*/
182
178
return self ::$ object [$ key ];
183
179
}
184
180
if ($ mixed instanceof dbObject) {
@@ -205,7 +201,6 @@ public static function fetch($mixed, $simple = false)
205
201
}
206
202
$ mixed = self ::encapsulate ($ mixed , $ simple );
207
203
return is_bool ($ mixed ) ? $ mixed : $ mixed ->getData ();
208
- //return ($mixed ? $mixed->getData() : $mixed);
209
204
}
210
205
if ($ mixed instanceof dbObject) {
211
206
return $ mixed ->getData ();
@@ -647,7 +642,7 @@ public static function prepare($instance, $sql)
647
642
*/
648
643
public static function set (&$ instance , $ key , $ value )
649
644
{
650
- $ instance ->bindValue (": " . $ key , $ value );
645
+ $ instance ->bindValue (": " . $ key , is_array ( $ value ) ? json_encode ( $ value ) : $ value );
651
646
}
652
647
653
648
/**
@@ -711,11 +706,14 @@ public static function insert($data, $table, $additional = array())
711
706
self ::set ($ stmnt , $ key , $ value );
712
707
}
713
708
try {
714
- $ stmnt ->execute ();
715
- self ::updateTotalRequests ();
716
- self ::$ id = $ instance ->lastInsertId ();
717
- unset($ stmnt , $ instance );
718
- return true ;
709
+ if (is_object ($ stmnt )) {
710
+ $ stmnt ->execute ();
711
+ self ::updateTotalRequests ();
712
+ self ::$ id = $ instance ->lastInsertId ();
713
+ unset($ stmnt , $ instance );
714
+ return true ;
715
+ }
716
+ return false ;
719
717
} catch (Error $ e ) {
720
718
throw ("An error ocurred: " . $ e );
721
719
return false ;
@@ -766,9 +764,12 @@ public static function update($data, $table, $rules = array(), $additional = arr
766
764
}
767
765
}
768
766
try {
769
- $ stmnt ->execute ();
770
- self ::updateTotalRequests ();
771
- return true ;
767
+ if (is_object ($ stmnt )) {
768
+ $ stmnt ->execute ();
769
+ self ::updateTotalRequests ();
770
+ return true ;
771
+ }
772
+ return false ;
772
773
} catch (Error $ e ) {
773
774
throw ("An error has occured: " . $ e );
774
775
return false ;
@@ -797,7 +798,10 @@ public static function delete($table, $rules = array())
797
798
}
798
799
}
799
800
try {
800
- return $ stmnt ->execute ();
801
+ if (is_object ($ stmnt )) {
802
+ return $ stmnt ->execute ();
803
+ }
804
+ return false ;
801
805
} catch (Error $ e ) {
802
806
throw ("An error has occured: " . $ e );
803
807
return false ;
@@ -884,7 +888,7 @@ public static function URLNormalize($string)
884
888
class dbObject
885
889
{
886
890
/** It is the DB instance */
887
- protected static $ instance = null ;
891
+ protected $ instance = null ;
888
892
889
893
private $ data = array ();
890
894
@@ -908,17 +912,17 @@ public function __construct($instance, $extra = array())
908
912
/**
909
913
* @param object Set as reference to DB Class to work with
910
914
*/
911
- private static function setInstance ($ instance )
915
+ private function setInstance ($ instance )
912
916
{
913
- self :: $ instance = $ instance ;
917
+ $ this -> instance = $ instance ;
914
918
}
915
919
916
920
/**
917
921
* @return object Returns DB Class intance reference
918
922
*/
919
- public static function getInstance ()
923
+ public function getInstance ()
920
924
{
921
- return self :: $ instance ;
925
+ return $ this -> instance ;
922
926
}
923
927
924
928
/**
0 commit comments