Making getRecords more benign

This commit is contained in:
2021-07-12 22:05:00 -03:00
parent 5b6c3ee627
commit cbd8d9ef89
+42 -7
View File
@@ -114,7 +114,7 @@ class DBInstance {
*
* @return stdClass A object with the record info
*/
public static function getRecord($table, $data = null, $select = Array('*'), $instance = 'default'){
public static function getRecord($table, $data = Array(), $select = Array('*'), $instance = 'default'){
$select = implode(', ', $select);
$conditions = "";
@@ -141,9 +141,11 @@ class DBInstance {
}
$conditions = rtrim($conditions, 'AND ');
$sql = "SELECT $select FROM {".$table."} WHERE $conditions";
if($conditions == ""){ $conditions = "1 = 1"; }
return self::queryOne($sql , $params, $instance)[0];
$sql = "SELECT $select FROM {".$table."} WHERE $conditions LIMIT 1";
return self::queryOne($sql , $params, $instance);
}
@@ -187,10 +189,37 @@ class DBInstance {
*
* @return stdClass A object with the record info
*/
public static function getRecords($table, $data = null, $select = Array('*'), $instance = 'default'){
public static function getRecords($table, $data = null, $select = Array('*'), $limits = Array(), $instance = 'default'){
$pointer = self::getRecordsPointer($table, $data, $select, $limits, $instance);
$elements = Array();
foreach ($pointer as $point){
$key = array_keys((array)$point)[0];
$elements[ ((array)$point)[$key] ] = $point;
}
return $elements;
}
/**
* getRecordsPointer
*
* This method will get records from the database
*
* @ctag DBInstance::getRecordsPointer($table);
* @ctag DBInstance::getRecordsPointer($table, []);
*
* @param String $table The table to be searched
* @param Array $data Pass the info to be replaced on the query_prepare
* @param Array $select Info to be put on the SELECT part of the query
* @param String $instance Select the connection to execute
*
* @return stdClass A object with the record info
*/
public static function getRecordsPointer($table, $data = Array(), $select = Array('*'), $limits = Array(), $instance = 'default'){
$select = implode(', ', $select);
$conditions = "";
$limits_sql = "";
$params = Array();
foreach ($data as $key => $cond){
@@ -213,10 +242,16 @@ class DBInstance {
}
}
$conditions = rtrim($conditions, 'AND ');
$sql = "SELECT $select FROM {".$table."} WHERE $conditions";
foreach ($limits as $key => $value) {
$limits_sql .= "$key $value ";
}
return self::query($sql , $params, $instance);
$conditions = rtrim($conditions, 'AND ');
if($conditions == ""){ $conditions = "1 = 1"; $params = false; }
$sql = "SELECT $select FROM {".$table."} WHERE $conditions $limits_sql";
return self::query($sql, $params, $instance);
}
/**