Skip to content

Commit 1acc19d

Browse files
committed
feat: nullable fields on post and put and improved performances
Added nullable fields on post and put Improved performances
1 parent 66e486a commit 1acc19d

File tree

1 file changed

+35
-13
lines changed

1 file changed

+35
-13
lines changed

includes/classes/API.php

+35-13
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public static function run()
6767
*
6868
* @param null $db
6969
*
70-
* @return object
70+
* @return \PDO
7171
*/
7272
public static function getConnection($db = null)
7373
{
@@ -307,7 +307,9 @@ public function setDatabase($db = null)
307307
*
308308
* @param string $db the database slug
309309
*
310-
* @return object the PDO object
310+
* @return \PDO
311+
*
312+
* @todo support port #s and test on each database
311313
*/
312314
public function &connect($db = null)
313315
{
@@ -420,7 +422,7 @@ private function documentation($query, $db = null)
420422
{
421423
$final_result = array();
422424

423-
$key = md5(json_encode($query) . $this->getDatabase($db)->name . '_docs');
425+
$key = crc32(json_encode($query) . $this->getDatabase($db)->name . '_docs');
424426

425427
if ($cache = $this->getCache($key)) {
426428
return $cache;
@@ -520,7 +522,7 @@ private function documentation($query, $db = null)
520522
*/
521523
private function get($query, $db = null)
522524
{
523-
$key = md5(json_encode($query) . $this->getDatabase($db)->name);
525+
$key = crc32(json_encode($query) . $this->getDatabase($db)->name);
524526

525527
if ($cache = $this->getCache($key)) {
526528
return $cache;
@@ -655,6 +657,7 @@ private function get($query, $db = null)
655657
$select_columns = implode(', ', $standard_columns);
656658
}
657659
}
660+
// Prefix table before column
658661
} elseif (!empty($query['prefix'])) {
659662
$prefix_columns = array();
660663
foreach ($select_tables as $table) {
@@ -678,7 +681,7 @@ private function get($query, $db = null)
678681
$sql = $where['sql'] . ' AND ' . $restriction;
679682
$where_values = $where['values'];
680683
} elseif (!empty($restriction)) {
681-
$sql .= ' WHERE ' . $restriction . $this->hooks->apply_filters('get_where_' . strtolower($query['table']), '');
684+
$sql .= ' WHERE ' . $restriction . $this->hooks->apply_filters('get_where_' . strtolower($query['table']), '', $table);
682685
}
683686

684687
// build ORDER query
@@ -796,7 +799,7 @@ private function get($query, $db = null)
796799
foreach ($where_values as $key => $value) {
797800
$type = self::detectPDOType($value);
798801
$key = ':' . $key;
799-
$sql_compiled = preg_replace('/(' . preg_quote($key) . ")([,]|\s|$|\))/i", "'" . $value . "'$2", $sql_compiled);
802+
$sql_compiled = preg_replace('/(' . preg_quote($key, '/') . ")([,]|\s|$|\))/i", "'" . $value . "'$2", $sql_compiled);
800803
$sth->bindValue($key, $value, $type);
801804
}
802805
}
@@ -806,7 +809,7 @@ private function get($query, $db = null)
806809
foreach ($join_values as $key => $value) {
807810
$type = self::detectPDOType($value);
808811
$key = ':' . $key;
809-
$sql_compiled = preg_replace('/(' . preg_quote($key) . ")([,]|\s|$|\))/i", "'" . $value . "'$2", $sql_compiled);
812+
$sql_compiled = preg_replace('/(' . preg_quote($key, '/') . ")([,]|\s|$|\))/i", "'" . $value . "'$2", $sql_compiled);
810813
$sth->bindValue($key, $value, $type);
811814
}
812815
}
@@ -849,7 +852,7 @@ private function post($query, $db = null)
849852
$dbh = &$this->connect($db);
850853

851854
// check values
852-
if (!empty($query['insert']) && !is_array($query['insert']) || count($query['insert']) < 1) {
855+
if ((!empty($query['insert']) && !is_array($query['insert'])) || count($query['insert']) < 1) {
853856
Response::error('Invalid insert values', 400);
854857
}
855858

@@ -1050,6 +1053,9 @@ private function patch($query, $db = null)
10501053
if (is_array($value)) {
10511054
$value = serialize($value);
10521055
}
1056+
if (is_string($value) && strtolower($value) == 'null') {
1057+
$value = null;
1058+
}
10531059
$key = ':' . $key;
10541060
$sql_compiled = self::debugCompileSQL($sql_compiled, $key, $value);
10551061
if (empty($value)) {
@@ -1162,6 +1168,9 @@ private function executeInsert($table, $columns, $dbh)
11621168
if (is_array($value)) {
11631169
$value = serialize($value);
11641170
}
1171+
if (is_string($value) && strtolower($value) == 'null') {
1172+
$value = null;
1173+
}
11651174
$column = ':' . $column;
11661175
$sth->bindValue($column, $value);
11671176
$sql_compiled = self::debugCompileSQL($sql_compiled, $column, $value);
@@ -1355,7 +1364,7 @@ public function tableExists($query_table, $db = null)
13551364
*/
13561365
public function getTables($db = null)
13571366
{
1358-
$key = md5($this->getDatabase($db)->name . '_tables');
1367+
$key = crc32($this->getDatabase($db)->name . '_tables');
13591368

13601369
if ($cache = $this->getCache($key)) {
13611370
return $cache;
@@ -1446,7 +1455,7 @@ public function getColumns($table, $db = null)
14461455
return false;
14471456
}
14481457

1449-
$key = md5($this->getDatabase($db)->name . '.' . $table . '_columns');
1458+
$key = crc32($this->getDatabase($db)->name . '.' . $table . '_columns');
14501459

14511460
if ($cache = $this->getCache($key)) {
14521461
return $cache;
@@ -1593,7 +1602,19 @@ private function parseWhere($main_table, $where, $sql)
15931602
$where_sql[] = "{$table}.{$column} NOT IN (" . implode(', ', $where_not_in) . ')';
15941603
}
15951604
}
1596-
} elseif (!is_array($value_condition) && is_int($condition) || $condition === '=') { // Check equal array cases
1605+
} elseif ($condition === '=' && is_array($value_condition)) { // Check equal array cases
1606+
foreach ($value_condition as $value) {
1607+
$_value_split = explode('.', $value, 2);
1608+
if (count($_value_split) > 1 && $this->checkColumn(@$_value_split[1], @$_value_split[0])) {
1609+
$index_value = $_value_split[0] . '.' . $_value_split[1];
1610+
} else {
1611+
$index_key = self::indexValue($prefix, $column, $where_values);
1612+
$index_value = ' :' . $index_key;
1613+
$where_values[$index_key] = $value;
1614+
}
1615+
$where_in[] = $index_value;
1616+
}
1617+
} elseif (!is_array($value_condition) && is_int($condition)) { // Check equal array cases
15971618
$_value_split = explode('.', $value_condition, 2);
15981619
if (count($_value_split) > 1 && $this->checkColumn(@$_value_split[1], @$_value_split[0])) {
15991620
$index_value = $_value_split[0] . '.' . $_value_split[1];
@@ -1756,7 +1777,7 @@ private static function detectPDOType($value)
17561777
*/
17571778
private static function debugCompileSQL($string, $key, $value)
17581779
{
1759-
$string = preg_replace('/' . $key . "([,]|\s|$|\))/i", "'" . $value . "'$1", $string);
1780+
$string = preg_replace('/' . $key . "([,]|\s|$|\))/i", (is_null($value) ? 'NULL$1' : "'" . $value . "'$1"), $string);
17601781

17611782
return $string;
17621783
}
@@ -1801,7 +1822,8 @@ private function sanitizeResults($results)
18011822
foreach ($results as $key => $result) {
18021823
// Sanitize encoding
18031824
foreach ($result as $column => $value) {
1804-
$results[$key]->$column = utf8_encode($value);
1825+
// Remind: change LBL_CHARSET in include/language/[iso_lang]_[iso_lang].lang.php
1826+
$results[$key]->$column = (mb_detect_encoding($value) == 'UTF-8') ? $value : utf8_encode($value);
18051827
}
18061828
}
18071829

0 commit comments

Comments
 (0)