Skip to content

Commit 2e433d9

Browse files
author
Marco Cesarato
committed
Fixed bug without prefix on cloumn extraction selected over join requests, Fixed some possible bugs
1 parent 79d300d commit 2e433d9

2 files changed

Lines changed: 32 additions & 29 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# PHP Database Web API
22
![](cover.png)
33

4-
**Version:** 0.6.124 beta
4+
**Version:** 0.6.125 beta
55

66
**Github:** https://github.com/marcocesarato/Database-Web-API
77

includes/classes/API.php

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -296,11 +296,11 @@ public function setDatabase($db = null) {
296296
public function &connect($db = null) {
297297

298298
// check for existing connection
299-
if(empty($db) && !empty($this->db->name) && isset($this->connections[$this->db->name])) {
299+
if(empty($db) && !empty($this->db->name) && !empty($this->connections[$this->db->name])) {
300300
$db = $this->db->name;
301301

302302
return $this->connections[$db];
303-
} else if(!empty($db) && is_string($db) && isset($this->connections[$db])) {
303+
} else if(!empty($db) && is_string($db) && !empty($this->connections[$db])) {
304304
return $this->connections[$db];
305305
}
306306

@@ -522,7 +522,7 @@ private function get($query, $db = null) {
522522
}
523523

524524
// check WHERE
525-
if(isset($query['where']) && is_array($query['where'])) {
525+
if(!empty($query['where']) && is_array($query['where'])) {
526526
foreach($query['where'] as $column => $value) {
527527
$column_table = $query['table'];
528528
$_split = explode('.', $column, 2);
@@ -537,7 +537,7 @@ private function get($query, $db = null) {
537537
}
538538

539539
// check id
540-
if(isset($query['id']) && !empty($query['id'])) {
540+
if(!empty($query['id'])) {
541541
$query["where"][$this->getFirstColumn($query['table'], $db)] = $query['id'];
542542
}
543543

@@ -554,7 +554,7 @@ private function get($query, $db = null) {
554554

555555
// build JOIN query
556556
$join_sql = "";
557-
if(isset($query['join']) && is_array($query['join']) && !empty($query['join'])) {
557+
if(!empty($query['join']) && is_array($query['join'])) {
558558

559559
$methods_available = array('INNER', 'LEFT', 'RIGHT');
560560

@@ -624,22 +624,25 @@ private function get($query, $db = null) {
624624
$join_sql .= "{$join_value_table}.{$join_value_column}";
625625
}
626626
}
627-
if(!empty($query['prefix']) && count($select_tables) > 1) {
627+
if(count($select_tables) > 1) {
628+
$standard_columns = array();
628629
$prefix_columns = array();
629630
foreach($select_tables as $table) {
630631
$columns = $this->getColumns($table, $db);
631632
foreach($columns as $column) {
632633
if($this->checkColumn($column, $table, $db)) {
634+
$standard_columns[] = "{$table}.{$column} AS {$column}";
633635
$prefix_columns[] = "{$table}.{$column} AS {$table}__{$column}";
634636
}
635637
}
636638
}
637-
$select_columns = implode(', ', $prefix_columns);
639+
if(!empty($query['prefix'])) {
640+
$select_columns = implode(', ', $prefix_columns);
641+
} else {
642+
$select_columns = implode(', ', $standard_columns);
643+
}
638644
}
639-
}
640-
641-
// Prefix table before column
642-
if(!empty($query['prefix'])) {
645+
} else if(!empty($query['prefix'])) {
643646
$prefix_columns = array();
644647
foreach($select_tables as $table) {
645648
$columns = $this->getColumns($table, $db);
@@ -656,7 +659,7 @@ private function get($query, $db = null) {
656659

657660
// build WHERE query
658661
$restriction = $this->auth->permissionSQL($query['table'], 'READ');
659-
if(isset($query['where']) && is_array($query['where'])) {
662+
if(!empty($query['where']) && is_array($query['where'])) {
660663
$where = $this->parseWhere($query['table'], $query['where'], $sql);
661664
$sql = $where["sql"] . ' AND ' . $restriction;
662665
$where_values = $where["values"];
@@ -773,7 +776,7 @@ private function get($query, $db = null) {
773776
$sth = $dbh->prepare($sql);
774777

775778
// bind WHERE values
776-
if(isset($where_values) && count($where_values) > 0) {
779+
if(!empty($where_values) && count($where_values) > 0) {
777780
foreach($where_values as $key => $value) {
778781
$type = self::detectPDOType($value);
779782
$key = ':' . $key;
@@ -783,7 +786,7 @@ private function get($query, $db = null) {
783786
}
784787

785788
// bind JOIN values
786-
if(isset($join_values) && count($join_values) > 0) {
789+
if(!empty($join_values) && count($join_values) > 0) {
787790
foreach($join_values as $key => $value) {
788791
$type = self::detectPDOType($value);
789792
$key = ':' . $key;
@@ -909,7 +912,7 @@ private function put($query, $db = null) {
909912
}
910913

911914
foreach($u as $update) {
912-
if(isset($update['where']) && is_array($update['where'])) {
915+
if(!empty($update['where']) && is_array($update['where'])) {
913916
foreach($update['where'] as $column => $value) {
914917
if(!$this->checkColumn($column, $table)) {
915918
Response::error('Invalid where condition ' . $column, 404);
@@ -971,11 +974,11 @@ private function patch($query, $db = null) {
971974

972975
foreach($update as $values) {
973976

974-
if(!isset($values['where']) || !is_array($values['where']) || count($values['where']) < 1) {
977+
if(empty($values['where']) || !is_array($values['where']) || count($values['where']) < 1) {
975978
Response::error('Invalid conditions', 400);
976979
}
977980

978-
if(!isset($values['values']) || !is_array($values['values']) || count($values['values']) < 1) {
981+
if(empty($values['values']) || !is_array($values['values']) || count($values['values']) < 1) {
979982
Response::error('Invalid values', 400);
980983
}
981984

@@ -1042,7 +1045,7 @@ private function patch($query, $db = null) {
10421045
}
10431046

10441047
// bind WHERE values
1045-
if(isset($where_values) && count($where_values) > 0) {
1048+
if(!empty($where_values) && count($where_values) > 0) {
10461049
foreach($where_values as $key => $value) {
10471050
$key = ':' . $key;
10481051
$sql_compiled = self::debugCompileSQL($sql_compiled, $key, $value);
@@ -1083,15 +1086,15 @@ private function delete($query, $db = null) {
10831086
}
10841087

10851088
// check ID
1086-
if(isset($query['id']) && !empty($query['id'])) {
1089+
if(!empty($query['id']) && !empty($query['id'])) {
10871090
$query["where"][$this->getFirstColumn($query['table'])] = $query['id'];
10881091
}
10891092

10901093
$sql = 'DELETE FROM ' . $query['table'];
10911094

10921095
// build WHERE query
10931096
$restriction = $this->auth->permissionSQL($query['table'], 'DELETE');
1094-
if(isset($query['where']) && is_array($query['where'])) {
1097+
if(!empty($query['where']) && is_array($query['where'])) {
10951098
$where = $this->parseWhere($query['table'], $query['where'], $sql);
10961099
$sql = $where["sql"] . ' AND ' . $restriction;
10971100
$where_values = $where["values"];
@@ -1103,7 +1106,7 @@ private function delete($query, $db = null) {
11031106
$sql_compiled = $sql;
11041107

11051108
// bind WHERE values
1106-
if(isset($where_values) && count($where_values) > 0) {
1109+
if(!empty($where_values) && count($where_values) > 0) {
11071110
foreach($where_values as $key => $value) {
11081111
$type = self::detectPDOType($value);
11091112
$key = ':' . $key;
@@ -1167,7 +1170,7 @@ public function render($data) {
11671170
ob_clean();
11681171
$default_format = Request::method() == 'GET' ? "html" : "json";
11691172
$data = $this->hooks->apply_filters('render', $data, $this->query, Request::method());
1170-
$renderer = 'render' . ucfirst(strtolower(isset($this->query['format']) ? $this->query['format'] : $default_format));
1173+
$renderer = 'render' . ucfirst(strtolower(!empty($this->query['format']) ? $this->query['format'] : $default_format));
11711174
$this->$renderer($data);
11721175
die();
11731176
}
@@ -1579,9 +1582,9 @@ private function parseUpdateQuery($query) {
15791582
$query = $this->reformatUpdateQuery($query);
15801583
$first_col = $this->getFirstColumn($query['table']);
15811584
// Check id
1582-
if(isset($query['table']) && !empty($query['table']) && isset($query['id']) && !empty($query['id'])) {
1585+
if(!empty($query['table']) && !empty($query['id'])) {
15831586
// Check WHERE
1584-
if(isset($query['where']) && is_array($query['where'])) {
1587+
if(!empty($query['where']) && is_array($query['where'])) {
15851588
foreach($query['where'] as $column => $value) {
15861589
$column_table = $query['table'];
15871590
$_split = explode('.', $column, 2);
@@ -1601,12 +1604,12 @@ private function parseUpdateQuery($query) {
16011604
$query['update'][$query['table']][$key]['values'] = $query['update'];
16021605
$query['update'][$query['table']][$key]['where'][$first_col] = $query['id'];
16031606
}
1604-
} elseif(!isset($query['update']) && !is_array($query['update']) && count($query['update']) < 1) { // Check values
1607+
} elseif(empty($query['update']) && !is_array($query['update']) && count($query['update']) < 1) { // Check values
16051608
Response::error('Invalid values', 400);
16061609
} else {
16071610
foreach($query['update'] as $table => $u) {
16081611
foreach($u as $update) {
1609-
if(isset($update['where']) && is_array($update['where'])) {
1612+
if(!empty($update['where']) && is_array($update['where'])) {
16101613
foreach($update['where'] as $column => $value) {
16111614
if(!$this->checkColumn($column, $table)) {
16121615
Response::error('Invalid where condition ' . $column, 404);
@@ -1745,7 +1748,7 @@ private function sanitizeResults($results) {
17451748
private function getCache($key) {
17461749

17471750
if(!extension_loaded('apc') || (ini_get('apc.enabled') != 1)) {
1748-
if(isset($this->cache[$key])) {
1751+
if(!empty($this->cache[$key])) {
17491752
return $this->cache[$key];
17501753
}
17511754
} else {
@@ -1765,7 +1768,7 @@ private function getCache($key) {
17651768
private function setCache($key, $value, $ttl = null) {
17661769

17671770
if($ttl == null) {
1768-
$ttl = (isset($this->db->ttl)) ? $this->db->ttl : $this->ttl;
1771+
$ttl = (!empty($this->db->ttl)) ? $this->db->ttl : $this->ttl;
17691772
}
17701773

17711774
$key = 'api_' . $key;

0 commit comments

Comments
 (0)