From f5c2a9b6f445e1e1148630fb04be4ca0f915651c Mon Sep 17 00:00:00 2001 From: ahwelp Date: Wed, 23 Mar 2022 22:00:20 +0000 Subject: [PATCH] Identation and new funcionality of inserting record --- src/ORM/DBInstance.php | 159 ++++++++++++++++++++++++++--------------- 1 file changed, 101 insertions(+), 58 deletions(-) diff --git a/src/ORM/DBInstance.php b/src/ORM/DBInstance.php index 954fa43..af47879 100755 --- a/src/ORM/DBInstance.php +++ b/src/ORM/DBInstance.php @@ -2,20 +2,22 @@ namespace ORM; -class DBInstance { +class DBInstance +{ - private static function addPrefix($sql, $instance = 'default'){ - $con = Connections::getConnection($instance); + 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){ - $sql = str_replace(" ", " ", $sql); - } - $sql = str_replace("{", $con->prefix, $sql); - $sql = str_replace("}", "", $sql); + //Where is preg_replace when we need it D`: + $sql = str_replace("\n", "", $sql); + while (strpos($sql, " ") > -1) { + $sql = str_replace(" ", " ", $sql); + } + $sql = str_replace("{", $con->prefix, $sql); + $sql = str_replace("}", "", $sql); - return $sql; + return $sql; } /** @@ -30,7 +32,8 @@ class DBInstance { * @param Array $data Pass the info to be replaced on the query_prepare * @param String $instance Select the connection to execute */ - public static function execute($sql, $data = null, $instance = 'default') { + public static function execute($sql, $data = null, $instance = 'default') + { $sql = self::addPrefix($sql, $instance); if ($data) { self::queryPrepare($sql, $data, $instance); @@ -44,7 +47,9 @@ class DBInstance { * * @ctag DBInstance::lastInsert(); */ - public static function lastInsert($instance = 'default') { + + public static function lastInsert($instance = 'default') + { $con = Connections::getConnection($instance); return $con->lastInsertId(); } @@ -63,7 +68,8 @@ 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) { @@ -80,7 +86,7 @@ class DBInstance { * @ctag DBInstance::queryOne($sql); * @ctag DBInstance::queryOne($sql, []); * - * This method will execute a SQL and return the first featches register + * This method will execute a SQL and return the first featched register * * @param String $sql The SQL to be executed * @param Array $data Pass the info to be replaced on the query_prepare @@ -88,8 +94,10 @@ class DBInstance { * * @return stdClass A object with the record info */ - public static function queryOne($sql, $data = null, $instance = 'default') { + public static function queryOne($sql, $data = null, $instance = 'default') + { $sql = self::addPrefix($sql, $instance); + if ($data) { return self::queryPrepare($sql, $data, $instance); } else { @@ -114,39 +122,40 @@ class DBInstance { * * @return stdClass A object with the record info */ - public static function getRecord($table, $data = Array(), $select = Array('*'), $instance = 'default'){ + public static function getRecord($table, $data = array(), $select = array('*'), $instance = 'default') + { $select = implode(', ', $select); $conditions = ""; - $params = Array(); + $params = array(); - foreach ($data as $key => $cond){ - if(is_array( $cond ) ){ - if(is_array($cond[1])){ + foreach ($data as $key => $cond) { + if (is_array($cond)) { + if (is_array($cond[1])) { $inner_conditions = "$key $cond[0] ("; - foreach ($cond[1] as $in_key => $c){ - $inner_conditions .= ":".$key."_".$in_key.", "; - $params[$key."_".$in_key] = $c; + foreach ($cond[1] as $in_key => $c) { + $inner_conditions .= ":" . $key . "_" . $in_key . ", "; + $params[$key . "_" . $in_key] = $c; } $conditions .= rtrim($inner_conditions, ', ') . ") AND "; - }else{ + } else { $conditions .= "$key $cond[0] :$key AND "; $params[$key] = $cond[1]; } - - }else{ + } else { $conditions .= "$key = :$key AND "; $params[$key] = $cond; } } $conditions = rtrim($conditions, 'AND '); - if($conditions == ""){ $conditions = "1 = 1"; } + if ($conditions == "") { + $conditions = "1 = 1"; + } - $sql = "SELECT $select FROM {".$table."} WHERE $conditions LIMIT 1"; - - return self::queryOne($sql , $params, $instance); + $sql = "SELECT $select FROM {" . $table . "} WHERE $conditions LIMIT 1"; + return self::queryOne($sql, $params, $instance); } /** @@ -163,7 +172,8 @@ class DBInstance { * * @return stdClass A object with the record info */ - public static function getRecordSql($sql, $data = null, $instance = 'default'){ + public static function getRecordSql($sql, $data = null, $instance = 'default') + { $sql = self::addPrefix($sql, $instance); if ($data) { @@ -189,13 +199,14 @@ class DBInstance { * * @return stdClass A object with the record info */ - public static function getRecords($table, $data = null, $select = Array('*'), $limits = 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; + $elements = array(); + foreach ($pointer as $point) { + $key = array_keys((array) $point)[0]; + $elements[((array) $point)[$key]] = $point; } return $elements; } @@ -215,28 +226,28 @@ class DBInstance { * * @return stdClass A object with the record info */ - public static function getRecordsPointer($table, $data = Array(), $select = Array('*'), $limits = Array(), $instance = 'default'){ + public static function getRecordsPointer($table, $data = array(), $select = array('*'), $limits = array(), $instance = 'default') + { $select = implode(', ', $select); $conditions = ""; $limits_sql = ""; - $params = Array(); + $params = array(); - foreach ($data as $key => $cond){ - if(is_array( $cond ) ){ - if(is_array($cond[1])){ + foreach ($data as $key => $cond) { + if (is_array($cond)) { + if (is_array($cond[1])) { $inner_conditions = "$key $cond[0] ("; - foreach ($cond[1] as $in_key => $c){ - $inner_conditions .= ":".$key."_".$in_key.", "; - $params[$key."_".$in_key] = $c; + foreach ($cond[1] as $in_key => $c) { + $inner_conditions .= ":" . $key . "_" . $in_key . ", "; + $params[$key . "_" . $in_key] = $c; } $conditions .= rtrim($inner_conditions, ', ') . ") AND "; - }else{ + } else { $conditions .= "$key $cond[0] :$key AND "; $params[$key] = $cond[1]; } - - }else{ + } else { $conditions .= "$key = :$key AND "; $params[$key] = $cond; } @@ -247,9 +258,12 @@ class DBInstance { } $conditions = rtrim($conditions, 'AND '); - if($conditions == ""){ $conditions = "1 = 1"; $params = false; } + if ($conditions == "") { + $conditions = "1 = 1"; + $params = false; + } - $sql = "SELECT $select FROM {".$table."} WHERE $conditions $limits_sql"; + $sql = "SELECT $select FROM {" . $table . "} WHERE $conditions $limits_sql"; return self::query($sql, $params, $instance); } @@ -268,30 +282,59 @@ class DBInstance { * * @return Array Array with objects representing the database data */ - public static function getRecordsSql($sql, $data = null, $instance = 'default'){ + public static function getRecordsSql($sql, $data = null, $instance = 'default') + { $sql = self::addPrefix($sql, $instance); - $pointer = Array(); + $pointer = array(); if ($data) { $pointer = self::queryPrepare($sql, $data, $instance); } else { $con = Connections::getConnection($instance); $pointer = $con->query($sql, \PDO::FETCH_OBJ); } - $elements = Array(); - foreach ($pointer as $point){ - $key = array_keys((array)$point)[0]; - $elements[ ((array)$point)[$key] ] = $point; + $elements = array(); + foreach ($pointer as $point) { + $key = array_keys((array) $point)[0]; + $elements[((array) $point)[$key]] = $point; } return $elements; } - public static function queryPrepare($sql, $data = Array(), $instance = 'default') { + public static function insertRecord($table, $data = array(), $connection = 'default') + { + + if (is_object($data)) { + $data = (array)$data; + } + + $columns = array_keys($data); + + $first_argument = ''; + $second_argument = ''; + $insert_data = array(); + + foreach ($columns as $column) { + $first_argument .= $column . ', '; + + $insert_data[$column] = $data[$column]; + $second_argument .= ":$column, "; + } + + $first_argument = rtrim($first_argument, ', '); + $second_argument = rtrim($second_argument, ', '); + + $sql = "INSERT INTO {" . $table . "} ($first_argument) VALUES ($second_argument)"; + + DBInstance::execute($sql, $insert_data, $connection); + } + + public static function queryPrepare($sql, $data = array(), $instance = 'default') + { $sql = self::addPrefix($sql, $instance); $con = Connections::getConnection($instance); $statement = $con->prepare($sql); $statement->execute($data); return $statement->fetchAll(\PDO::FETCH_OBJ); } - }