Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a5d9f65

Browse files
bug: Problem in fixDataCollumns method due to DB Object update
1 parent e0f6861 commit a5d9f65

File tree

2 files changed

+79
-50
lines changed

2 files changed

+79
-50
lines changed

‎db.class.php

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* 2021年03月12日 -> Added rel="canonical|prev|next" to <a> pagination tags
77
* 2021年03月12日 -> Added a lot of comments
88
* 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
1010
*/
1111

1212
class db
@@ -73,12 +73,12 @@ private static function getInstance()
7373
self::useConnection($connectionName);
7474
}
7575
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);
7979
//$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
8080
self::updateTotalRequests();
81-
return $connection;
81+
return $instance;
8282
} else {
8383
return false;
8484
}
@@ -93,7 +93,7 @@ public static function addConnection($connectionName, $connectionCredentials)
9393
{
9494
if (!array_key_exists($connectionName, self::$connections)) {
9595
self::$connections[$connectionName] = $connectionCredentials;
96-
return new static();
96+
return new static(newstdClass);
9797
}
9898
}
9999

@@ -102,7 +102,7 @@ public static function useConnection($connectionName)
102102
{
103103
if (array_key_exists($connectionName, self::$connections)) {
104104
self::$connectionName = $connectionName;
105-
return new static();
105+
return new static(newstdClass);
106106
}
107107
}
108108

@@ -133,9 +133,12 @@ private static function encapsulate($mixed)
133133
if (is_string($mixed)) {
134134
$key = md5($mixed);
135135
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;
139142
}
140143
return self::$object[$key];
141144
}
@@ -158,14 +161,10 @@ public static function fetch($mixed, $simple = false)
158161
}
159162
}
160163
$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);
166165
}
167166
if ($mixed instanceof dbObject) {
168-
return $mixed->getInstance()->fetch(PDO::FETCH_ASSOC);
167+
return $mixed->getData();
169168
}
170169
if ($mixed instanceof PDOStatement) {
171170
return $mixed->fetch(PDO::FETCH_ASSOC);
@@ -176,12 +175,8 @@ public static function fetch($mixed, $simple = false)
176175
public static function fetchAll($mixed)
177176
{
178177
$mixed = self::encapsulate($mixed);
179-
if (is_string($mixed)) {
180-
$mixed = self::encapsulate($mixed);
181-
return $mixed->getInstance()->fetchAll(PDO::FETCH_ASSOC);
182-
}
183178
if ($mixed instanceof dbObject) {
184-
return $mixed->getInstance()->fetchAll(PDO::FETCH_ASSOC);
179+
return $mixed ? $mixed->getdata(1) : $mixed;
185180
}
186181
if ($mixed instanceof PDOStatement) {
187182
return $mixed->fetchAll(PDO::FETCH_ASSOC);
@@ -191,16 +186,14 @@ public static function fetchAll($mixed)
191186
/** It counts the total rows that $mixed have */
192187
public static function count($mixed)
193188
{
194-
$object = self::encapsulate($mixed);
195-
return $object->extra['totalEntries'];
189+
return self::encapsulate($mixed)->extra['totalEntries'];
196190
}
197191

198192
/** Checks if the given $query returns null. If it returns null or 0, the function return true (is empty) */
199193
public static function empty($query)
200194
{
201195
if (is_string($query)) {
202-
$object = self::query($query);
203-
return ($object->getInstance()->rowCount() == 0);
196+
return (self::query($query)->getInstance()->rowCount() == 0);
204197
}
205198
if ($query instanceof dbObject) {
206199
return ($query->getInstance()->rowCount() == 1);
@@ -217,9 +210,9 @@ public static function query($mixed)
217210
public static function pagedQuery($mixed, $limit, $page = false, $words = false)
218211
{
219212
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);
223216
if ($page == false) {
224217
$page = self::getCurrentPage();
225218
}
@@ -228,8 +221,8 @@ public static function pagedQuery($mixed, $limit, $page = false, $words = false)
228221
}
229222
if ($mixed instanceof dbObject) {
230223
$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);
233226
$page = self::getCurrentPage();
234227
if ($totalRows > $limit) {
235228
$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)
260253
public static function setLanguage($language)
261254
{
262255
self::$language = $language;
263-
return new static();
256+
return new static(newstdClass);
264257
}
265258

266259
/** It defines the words that the pagination HTML will have */
@@ -366,11 +359,10 @@ public static function page($echo = true, $class = "")
366359
if ($pageCount == $pageNow) {
367360
if (self::$friendlyURL) {
368361
$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);
370362
} else {
371363
$_GET[$words['url']] = $pageNow;
372-
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", "javascript:void(0)", $pageNow, 'active', 'disabled'), $htmlButton);
373364
}
365+
$buttons[] = str_replace(array("{rel}", "{target}", '{text|number}', '{active}', '{disabled}'), array("", "javascript:void(0)", $pageNow, 'active', 'disabled'), $htmlButton);
374366
} else {
375367
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)) {
376368
if (self::$friendlyURL) {
@@ -452,7 +444,7 @@ public static function setCollation($collation, $show = false, $execute = false)
452444
public static function setFriendlyURL($FriendlyURLInstance)
453445
{
454446
self::$friendlyURL = $FriendlyURLInstance;
455-
return new static();
447+
return new static(newstdClass);
456448
}
457449

458450
/** It will retrieve the collumns of the given $table */
@@ -514,15 +506,15 @@ private static function fixDataCollumns($data, $table, &$newData = array(), $mod
514506
}
515507

516508
/** It will prepare a given $sql to the given $object */
517-
public static function prepare($object, $sql)
509+
public static function prepare($instance, $sql)
518510
{
519-
return $object->prepare($sql);
511+
return $instance->prepare($sql);
520512
}
521513

522514
/** 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)
524516
{
525-
$object->bindValue(":" . $key, $value);
517+
$instance->bindValue(":" . $key, $value);
526518
}
527519

528520
/** It executes any given $sql */
@@ -560,8 +552,8 @@ public static function insert($data, $table, $additional = array())
560552
self::fixDataCollumns($data, $table, $newData);
561553
$array_keys = array_keys($newData);
562554
$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);
565557
if (!empty($additional)) {
566558
self::transformWith($newData, $additional);
567559
}
@@ -570,8 +562,8 @@ public static function insert($data, $table, $additional = array())
570562
}
571563
if ($stmnt->execute()) {
572564
self::updateTotalRequests();
573-
self::$id = $object->lastInsertId();
574-
unset($stmnt, $object);
565+
self::$id = $instance->lastInsertId();
566+
unset($stmnt, $instance);
575567
return true;
576568
} else {
577569
return false;
@@ -599,8 +591,8 @@ public static function update($data, $table, $rules = array(), $additional = arr
599591
};
600592
}
601593
$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);
604596
if (!empty($additional)) {
605597
self::transformWith($newData, $additional);
606598
}
@@ -630,8 +622,8 @@ public static function delete($table, $rules = array())
630622
};
631623
}
632624
$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);
635627
if (!empty($rules)) {
636628
foreach ($rules as $key => $value) {
637629
self::set($stmnt, "rule_" . $key, $value);
@@ -696,7 +688,7 @@ public static function search($what, $where)
696688
}
697689

698690
/** It will normalize a string to be accepted on URL addresses */
699-
public function URLNormalize($string)
691+
public staticfunction URLNormalize($string)
700692
{
701693
$string = preg_replace('/[áàãâä]/ui', 'a', $string);
702694
$string = preg_replace('/[éèêë]/ui', 'e', $string);
@@ -716,15 +708,20 @@ class dbObject
716708
/** It is the DB instance */
717709
protected static $instance = null;
718710

711+
private $data = array();
712+
719713
/** Array with extra info */
720-
public $extra = null;
714+
public $extra = array();
721715

722716
/** It already sets a number of info on $extra */
723717
public function __construct($instance, $extra = array())
724718
{
725719
$this->setInstance($instance);
726720
$this->extra = $extra;
721+
$this->extra['rows'] = -1;
727722
$this->extra['totalEntries'] = $instance->rowCount();
723+
$this->extra['query'] = $instance->queryString;
724+
$this->data = $instance->fetchAll(PDO::FETCH_ASSOC);
728725
return $this;
729726
}
730727

@@ -739,4 +736,14 @@ public static function getInstance()
739736
{
740737
return self::$instance;
741738
}
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+
}
742749
}

‎index.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,35 @@
11
<?php
22

3+
// needs a BIG refactor
4+
5+
36
include("db.class.php");
47

58
db::addConnection('default', array("HOST" => "localhost", "USER" => "root", "PASSWORD" => "", "NAME" => "test"));
69

7-
$resultado = db::search("teste comum verdadeiro falso com amor", "emails");
10+
$query = db::pagedQuery("select * from emails", 10);
11+
12+
$count = 0;
13+
14+
while ($dado = db::fetch($query)) {
15+
16+
$count++;
17+
while ($dado2 = db::pagedQuery("select * from monney", 5)) {
18+
$count++;
19+
echo "<pre>";
20+
print_r($dado2);
21+
echo "</pre>";
22+
if ($count == 4) {
23+
break;
24+
}
25+
}
826

9-
while ($dado = db::fetch($resultado)) {
1027
echo "<pre>";
1128
print_r($dado);
1229
echo "</pre>";
30+
if ($count == 4) {
31+
break;
32+
}
1333
}
34+
35+
db::page();

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /