6
6
* 2021年03月12日 -> Added rel="canonical|prev|next" to <a> pagination tags
7
7
* 2021年03月12日 -> Added a lot of comments
8
8
* 2021年06月05日 -> Added transformWith private method for insert and update methods using the $additional array. Added formatMonney public method to work with monetary values. Changed getPageNow to getCurrentPage. Added search method.
9
- *
9
+ * 2021年08月17日 -> Made a few improvements within base core functions
10
10
*/
11
11
12
12
class db
@@ -73,12 +73,12 @@ private static function getInstance()
73
73
self ::useConnection ($ connectionName );
74
74
}
75
75
try {
76
- $ connection = new PDO ('mysql:host= ' . self ::$ connections [self ::$ connectionName ]['HOST ' ] . ";dbname= " . self ::$ connections [self ::$ connectionName ]['NAME ' ] . ";charset=utf8; " , self ::$ connections [self ::$ connectionName ]['USER ' ], self ::$ connections [self ::$ connectionName ]['PASSWORD ' ]);
77
- if ($ connection ) {
78
- $ connection ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , false );
76
+ $ instance = new PDO ('mysql:host= ' . self ::$ connections [self ::$ connectionName ]['HOST ' ] . ";dbname= " . self ::$ connections [self ::$ connectionName ]['NAME ' ] . ";charset=utf8; " , self ::$ connections [self ::$ connectionName ]['USER ' ], self ::$ connections [self ::$ connectionName ]['PASSWORD ' ]);
77
+ if ($ instance ) {
78
+ $ instance ->setAttribute (PDO ::ATTR_EMULATE_PREPARES , false );
79
79
//$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
80
80
self ::updateTotalRequests ();
81
- return $ connection ;
81
+ return $ instance ;
82
82
} else {
83
83
return false ;
84
84
}
@@ -93,7 +93,7 @@ public static function addConnection($connectionName, $connectionCredentials)
93
93
{
94
94
if (!array_key_exists ($ connectionName , self ::$ connections )) {
95
95
self ::$ connections [$ connectionName ] = $ connectionCredentials ;
96
- return new static ();
96
+ return new static (new stdClass );
97
97
}
98
98
}
99
99
@@ -102,7 +102,7 @@ public static function useConnection($connectionName)
102
102
{
103
103
if (array_key_exists ($ connectionName , self ::$ connections )) {
104
104
self ::$ connectionName = $ connectionName ;
105
- return new static ();
105
+ return new static (new stdClass );
106
106
}
107
107
}
108
108
@@ -133,9 +133,12 @@ private static function encapsulate($mixed)
133
133
if (is_string ($ mixed )) {
134
134
$ key = md5 ($ mixed );
135
135
if (!array_key_exists ($ key , self ::$ object )) {
136
- $ instance = self ::getInstance ();
137
- $ object = new dbObject ($ instance ->query ($ mixed ), array ("key " => $ key ));
138
- self ::$ object [$ key ] = $ object ;
136
+ $ instance = new dbObject (self ::getInstance ()->query ($ mixed ), array ("key " => $ key ));
137
+ self ::$ object [$ key ] = $ instance ;
138
+ }
139
+ if (self ::$ object [$ key ]->extra ["rows " ] === self ::$ object [$ key ]->extra ["totalEntries " ]) {
140
+ unset(self ::$ object [$ key ]);
141
+ return false ;
139
142
}
140
143
return self ::$ object [$ key ];
141
144
}
@@ -158,14 +161,10 @@ public static function fetch($mixed, $simple = false)
158
161
}
159
162
}
160
163
$ mixed = self ::encapsulate ($ mixed );
161
- if (!isset ($ mixed ->extra ['rows ' ])) {
162
- $ mixed ->extra ['rows ' ] = 0 ;
163
- }
164
- $ mixed ->extra ['rows ' ]++;
165
- return $ mixed ->getInstance ()->fetch (PDO ::FETCH_ASSOC );
164
+ return ($ mixed ? $ mixed ->getData () : $ mixed );
166
165
}
167
166
if ($ mixed instanceof dbObject) {
168
- return $ mixed ->getInstance ()-> fetch ( PDO :: FETCH_ASSOC );
167
+ return $ mixed ->getData ( );
169
168
}
170
169
if ($ mixed instanceof PDOStatement) {
171
170
return $ mixed ->fetch (PDO ::FETCH_ASSOC );
@@ -176,12 +175,8 @@ public static function fetch($mixed, $simple = false)
176
175
public static function fetchAll ($ mixed )
177
176
{
178
177
$ mixed = self ::encapsulate ($ mixed );
179
- if (is_string ($ mixed )) {
180
- $ mixed = self ::encapsulate ($ mixed );
181
- return $ mixed ->getInstance ()->fetchAll (PDO ::FETCH_ASSOC );
182
- }
183
178
if ($ mixed instanceof dbObject) {
184
- return $ mixed-> getInstance ()-> fetchAll ( PDO :: FETCH_ASSOC ) ;
179
+ return $ mixed ? $ mixed -> getdata ( 1 ) : $ mixed ;
185
180
}
186
181
if ($ mixed instanceof PDOStatement) {
187
182
return $ mixed ->fetchAll (PDO ::FETCH_ASSOC );
@@ -191,16 +186,14 @@ public static function fetchAll($mixed)
191
186
/** It counts the total rows that $mixed have */
192
187
public static function count ($ mixed )
193
188
{
194
- $ object = self ::encapsulate ($ mixed );
195
- return $ object ->extra ['totalEntries ' ];
189
+ return self ::encapsulate ($ mixed )->extra ['totalEntries ' ];
196
190
}
197
191
198
192
/** Checks if the given $query returns null. If it returns null or 0, the function return true (is empty) */
199
193
public static function empty ($ query )
200
194
{
201
195
if (is_string ($ query )) {
202
- $ object = self ::query ($ query );
203
- return ($ object ->getInstance ()->rowCount () == 0 );
196
+ return (self ::query ($ query )->getInstance ()->rowCount () == 0 );
204
197
}
205
198
if ($ query instanceof dbObject) {
206
199
return ($ query ->getInstance ()->rowCount () == 1 );
@@ -217,9 +210,9 @@ public static function query($mixed)
217
210
public static function pagedQuery ($ mixed , $ limit , $ page = false , $ words = false )
218
211
{
219
212
if (is_string ($ mixed )) {
220
- $ object = self ::query ($ mixed );
221
- $ object ->extra ['limit ' ] = $ limit ;
222
- self ::setPaginationWords ($ object , $ words );
213
+ $ instance = self ::query ($ mixed );
214
+ $ instance ->extra ['limit ' ] = $ limit ;
215
+ self ::setPaginationWords ($ instance , $ words );
223
216
if ($ page == false ) {
224
217
$ page = self ::getCurrentPage ();
225
218
}
@@ -228,8 +221,8 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
228
221
}
229
222
if ($ mixed instanceof dbObject) {
230
223
$ totalRows = $ mixed ->extra ['totalEntries ' ];
231
- $ object = new dbObject ($ mixed ->getInstance (), array ("limit " => $ limit , 'totalEntries ' => $ totalRows ));
232
- self ::setPaginationWords ($ object , $ words );
224
+ $ instance = new dbObject ($ mixed ->getInstance (), array ("limit " => $ limit , 'totalEntries ' => $ totalRows ));
225
+ self ::setPaginationWords ($ instance , $ words );
233
226
$ page = self ::getCurrentPage ();
234
227
if ($ totalRows > $ limit ) {
235
228
$ newObject = self ::query ($ mixed ->getInstance ()->queryString . " LIMIT " . (($ limit * $ page ) - $ limit ) . ", " . $ limit );
@@ -260,7 +253,7 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
260
253
public static function setLanguage ($ language )
261
254
{
262
255
self ::$ language = $ language ;
263
- return new static ();
256
+ return new static (new stdClass );
264
257
}
265
258
266
259
/** It defines the words that the pagination HTML will have */
@@ -366,11 +359,10 @@ public static function page($echo = true, $class = "")
366
359
if ($ pageCount == $ pageNow ) {
367
360
if (self ::$ friendlyURL ) {
368
361
$ parts [$ keyPart ] = self ::$ friendlyURL ->gerarLink ($ words ['url ' ], $ pageNow );
369
- $ buttons [] = str_replace (array ("{rel} " , "{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , "javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
370
362
} else {
371
363
$ _GET [$ words ['url ' ]] = $ pageNow ;
372
- $ buttons [] = str_replace (array ("{rel} " , "{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , "javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
373
364
}
365
+ $ buttons [] = str_replace (array ("{rel} " , "{target} " , '{text|number} ' , '{active} ' , '{disabled} ' ), array ("" , "javascript:void(0) " , $ pageNow , 'active ' , 'disabled ' ), $ htmlButton );
374
366
} else {
375
367
if ($ pageCount <= $ pageNow && ($ pageCount >= $ pageNow - 4 && ($ pageNow == $ totalPages || $ pageNow + 1 == $ totalPages || $ pageNow + 2 == $ totalPages ) || ($ pageCount >= $ pageNow - 2 )) && $ pageCount > 0 && count ($ buttons ) < 5 && ($ pageCount == $ pageNow - 1 || $ pageCount == $ pageNow - 2 || count ($ buttons ) <= 5 )) {
376
368
if (self ::$ friendlyURL ) {
@@ -452,7 +444,7 @@ public static function setCollation($collation, $show = false, $execute = false)
452
444
public static function setFriendlyURL ($ FriendlyURLInstance )
453
445
{
454
446
self ::$ friendlyURL = $ FriendlyURLInstance ;
455
- return new static ();
447
+ return new static (new stdClass );
456
448
}
457
449
458
450
/** It will retrieve the collumns of the given $table */
@@ -514,15 +506,15 @@ private static function fixDataCollumns($data, $table, &$newData = array(), $mod
514
506
}
515
507
516
508
/** It will prepare a given $sql to the given $object */
517
- public static function prepare ($ object , $ sql )
509
+ public static function prepare ($ instance , $ sql )
518
510
{
519
- return $ object ->prepare ($ sql );
511
+ return $ instance ->prepare ($ sql );
520
512
}
521
513
522
514
/** It will set the given $value on the specific $key, inside the $object. Object is a PDO Statement */
523
- public static function set (&$ object , $ key , $ value )
515
+ public static function set (&$ instance , $ key , $ value )
524
516
{
525
- $ object ->bindValue (": " . $ key , $ value );
517
+ $ instance ->bindValue (": " . $ key , $ value );
526
518
}
527
519
528
520
/** It executes any given $sql */
@@ -560,8 +552,8 @@ public static function insert($data, $table, $additional = array())
560
552
self ::fixDataCollumns ($ data , $ table , $ newData );
561
553
$ array_keys = array_keys ($ newData );
562
554
$ sql = "INSERT INTO $ table ( " . implode (", " , $ array_keys ) . ") VALUES (: " . implode (", : " , $ array_keys ) . "); " ;
563
- $ object = self ::getInstance ();
564
- $ stmnt = self ::prepare ($ object , $ sql );
555
+ $ instance = self ::getInstance ();
556
+ $ stmnt = self ::prepare ($ instance , $ sql );
565
557
if (!empty ($ additional )) {
566
558
self ::transformWith ($ newData , $ additional );
567
559
}
@@ -570,8 +562,8 @@ public static function insert($data, $table, $additional = array())
570
562
}
571
563
if ($ stmnt ->execute ()) {
572
564
self ::updateTotalRequests ();
573
- self ::$ id = $ object ->lastInsertId ();
574
- unset($ stmnt , $ object );
565
+ self ::$ id = $ instance ->lastInsertId ();
566
+ unset($ stmnt , $ instance );
575
567
return true ;
576
568
} else {
577
569
return false ;
@@ -599,8 +591,8 @@ public static function update($data, $table, $rules = array(), $additional = arr
599
591
};
600
592
}
601
593
$ sql = "UPDATE $ table SET " . implode (", " , $ collumns ) . (!empty ($ rules ) ? " WHERE " . implode (" AND " , $ newRules ) : "" ) . "; " ;
602
- $ object = self ::getInstance ();
603
- $ stmnt = self ::prepare ($ object , $ sql );
594
+ $ instance = self ::getInstance ();
595
+ $ stmnt = self ::prepare ($ instance , $ sql );
604
596
if (!empty ($ additional )) {
605
597
self ::transformWith ($ newData , $ additional );
606
598
}
@@ -630,8 +622,8 @@ public static function delete($table, $rules = array())
630
622
};
631
623
}
632
624
$ sql = "DELETE FROM $ table " . (empty ($ newRules ) ? "" : "WHERE " . implode (" AND " , $ newRules )) . "; " ;
633
- $ object = self ::getInstance ();
634
- $ stmnt = self ::prepare ($ object , $ sql );
625
+ $ instance = self ::getInstance ();
626
+ $ stmnt = self ::prepare ($ instance , $ sql );
635
627
if (!empty ($ rules )) {
636
628
foreach ($ rules as $ key => $ value ) {
637
629
self ::set ($ stmnt , "rule_ " . $ key , $ value );
@@ -696,7 +688,7 @@ public static function search($what, $where)
696
688
}
697
689
698
690
/** It will normalize a string to be accepted on URL addresses */
699
- public function URLNormalize ($ string )
691
+ public static function URLNormalize ($ string )
700
692
{
701
693
$ string = preg_replace ('/[áàãâä]/ui ' , 'a ' , $ string );
702
694
$ string = preg_replace ('/[éèêë]/ui ' , 'e ' , $ string );
@@ -716,15 +708,20 @@ class dbObject
716
708
/** It is the DB instance */
717
709
protected static $ instance = null ;
718
710
711
+ private $ data = array ();
712
+
719
713
/** Array with extra info */
720
- public $ extra = null ;
714
+ public $ extra = array () ;
721
715
722
716
/** It already sets a number of info on $extra */
723
717
public function __construct ($ instance , $ extra = array ())
724
718
{
725
719
$ this ->setInstance ($ instance );
726
720
$ this ->extra = $ extra ;
721
+ $ this ->extra ['rows ' ] = -1 ;
727
722
$ this ->extra ['totalEntries ' ] = $ instance ->rowCount ();
723
+ $ this ->extra ['query ' ] = $ instance ->queryString ;
724
+ $ this ->data = $ instance ->fetchAll (PDO ::FETCH_ASSOC );
728
725
return $ this ;
729
726
}
730
727
@@ -739,4 +736,14 @@ public static function getInstance()
739
736
{
740
737
return self ::$ instance ;
741
738
}
739
+
740
+ /** Returns current data */
741
+ public function getData ($ all = false )
742
+ {
743
+ if ($ all ) {
744
+ return $ this ->data ;
745
+ }
746
+ $ this ->extra ["rows " ]++;
747
+ return isset ($ this ->data [$ this ->extra ["rows " ]]) ? $ this ->data [$ this ->extra ["rows " ]] : false ;
748
+ }
742
749
}
0 commit comments