Now looks more like Moodle

This commit is contained in:
ahwelp
2019-08-08 19:12:25 +00:00
parent f2eac27b98
commit e48d5a8992
+173
View File
@@ -14,9 +14,19 @@ class DBInstance {
}
$sql = str_replace("{", $con->prefix, $sql);
$sql = str_replace("}", "", $sql);
return $sql;
}
/**
* Execute
*
* This method will execute a SQL on the database
*
* @param String $sql The SQL to be executed
* @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') {
$sql = self::addPrefix($sql, $instance);
if ($data) {
@@ -32,8 +42,20 @@ class DBInstance {
return $con->lastInsertId();
}
/**
* Query
*
* This method will execute a SQL and return a pointer to the resultset
*
* @param String $sql The SQL to be executed
* @param Array $data Pass the info to be replaced on the query_prepare
* @param String $instance Select the connection to execute
*
* @return pointer A pointer for a foreach loop
*/
public static function query($sql, $data = null, $instance = 'default') {
$sql = self::addPrefix($sql, $instance);
if ($data) {
return self::queryPrepare($sql, $data, $instance);
} else {
@@ -42,6 +64,17 @@ class DBInstance {
}
}
/**
* queryOne
*
* This method will execute a SQL and return the first featches register
*
* @param String $sql The SQL to be executed
* @param Array $data Pass the info to be replaced on the query_prepare
* @param String $instance Select the connection to execute
*
* @return stdClass A object with the record info
*/
public static function queryOne($sql, $data = null, $instance = 'default') {
$sql = self::addPrefix($sql, $instance);
if ($data) {
@@ -52,6 +85,146 @@ class DBInstance {
}
}
/**
* getRecord
*
* This method will execute get a register from a 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 getRecord($table, $data = null, $select = Array('*'), $instance = 'default'){
$select = implode(', ', $select);
$conditions = "";
$params = Array();
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;
}
$conditions .= rtrim($inner_conditions, ', ') . ") AND ";
}else{
$conditions .= "$key $cond[0] :$key AND ";
$params[$key] = $cond[1];
}
}else{
$conditions .= "$key = :$key AND ";
$params[$key] = $cond;
}
}
$conditions = rtrim($conditions, 'AND ');
$sql = "SELECT $select FROM {".$table."} WHERE $conditions";
return self::queryOne($sql , $params, $instance)[0];
}
/**
* getRecordSql
*
* This method will get a record from the database using SQL
*
* @param String $sql The SQL to be executed
* @param Array $data Pass the info to be replaced on the query_prepare
* @param String $instance Select the connection to execute
*
* @return stdClass A object with the record info
*/
public static function getRecordSql($sql, $data = null, $instance = 'default'){
$sql = self::addPrefix($sql, $instance);
if ($data) {
$pointer = self::queryPrepare($sql, $data, $instance);
} else {
$con = Connections::getConnection($instance);
return $con->query($sql, \PDO::FETCH_OBJ)->fetch();
}
}
/**
* getRecords
*
* This method will get records from the database
*
* @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 getRecords($table, $data = null, $select = Array('*'), $instance = 'default'){
$select = implode(', ', $select);
$conditions = "";
$params = Array();
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;
}
$conditions .= rtrim($inner_conditions, ', ') . ") AND ";
}else{
$conditions .= "$key $cond[0] :$key AND ";
$params[$key] = $cond[1];
}
}else{
$conditions .= "$key = :$key AND ";
$params[$key] = $cond;
}
}
$conditions = rtrim($conditions, 'AND ');
$sql = "SELECT $select FROM {".$table."} WHERE $conditions";
return self::query($sql , $params, $instance);
}
/**
* getRecordsSql
*
* This method will get records from the database
*
* @param String $sql The SQL to be executed
* @param Array $data Pass the info to be replaced on the query_prepare
* @param String $instance Select the connection to execute
*
* @return Array Array with objects representing the database data
*/
public static function getRecordsSql($sql, $data = null, $instance = 'default'){
$sql = self::addPrefix($sql, $instance);
$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;
}
return $elements;
}
public static function queryPrepare($sql, $data = Array(), $instance = 'default') {
$sql = self::addPrefix($sql, $instance);
$con = Connections::getConnection($instance);