Forráskód Böngészése

Making getRecords more benign

ahwelp 4 éve
szülő
commit
cbd8d9ef89
1 módosított fájl, 49 hozzáadás és 14 törlés
  1. 49 14
      src/ORM/DBInstance.php

+ 49 - 14
src/ORM/DBInstance.php

@@ -6,7 +6,7 @@ class DBInstance {
 
     private static function addPrefix($sql, $instance = 'default'){
       	$con = Connections::getConnection($instance);
-      	
+
       	//Where is preg_replace when we need it D`:
       	$sql = str_replace("\n", "", $sql);
 		while(strpos($sql, "  ") > -1){
@@ -22,7 +22,7 @@ class DBInstance {
      * Execute
      *
      * This method will execute a SQL on the database
-     * 
+     *
      * @ctag DBInstance::execute($sql);
      * @ctag DBInstance::execute($sql, []);
      *
@@ -39,7 +39,7 @@ class DBInstance {
             $con->exec($sql);
         }
     }
-    
+
     /*
      *
      * @ctag DBInstance::lastInsert();
@@ -63,9 +63,9 @@ class DBInstance {
      *
      * @return pointer A pointer for a foreach loop
      */
-    public static function query($sql, $data = null, $instance = 'default') {        
+    public static function query($sql, $data = null, $instance = 'default') {
         $sql = self::addPrefix($sql, $instance);
-        
+
         if ($data) {
             return self::queryPrepare($sql, $data, $instance);
         } else {
@@ -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);
 
     }
 
@@ -162,8 +164,8 @@ class DBInstance {
      * @return stdClass A object with the record info
      */
     public static function getRecordSql($sql, $data = null, $instance = 'default'){
-        $sql = self::addPrefix($sql, $instance);        
-        
+        $sql = self::addPrefix($sql, $instance);
+
         if ($data) {
             $pointer = self::queryPrepare($sql, $data, $instance);
         } else {
@@ -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 {
             }
         }
 
+        foreach ($limits as $key => $value) {
+            $limits_sql .= "$key $value ";
+        }
+
         $conditions = rtrim($conditions, 'AND ');
-        $sql = "SELECT $select FROM {".$table."} WHERE $conditions";
+        if($conditions == ""){ $conditions = "1 = 1"; $params = false; }
 
-        return self::query($sql , $params, $instance);
+        $sql = "SELECT $select FROM {".$table."} WHERE $conditions $limits_sql";
+
+        return self::query($sql, $params, $instance);
     }
 
     /**
@@ -249,7 +284,7 @@ class DBInstance {
             $elements[ ((array)$point)[$key] ] = $point;
         }
         return $elements;
-    }    
+    }
 
     public static function queryPrepare($sql, $data = Array(), $instance = 'default') {
         $sql = self::addPrefix($sql, $instance);