//функция получения данных
function getRecords($table,$settings){
global $DataBase,$ActiveLanguage,$SortLanguage;
//pre variables
$SQL=array();
$query="SELECT ";
$what="*";
$activeLanguage=$ActiveLanguage;
$whereSQL=$orderSQL=$limitSQL=$innerSQL=$sortBy=null;
$showSQL=false;
//get all data
//if(!empty($settings)){
$settings=json_decode(json_encode($settings),false);
//if empty $settings->table => return empty object
if(!empty($table)){
$tableName=$table;
}else{
return null;
}
//get non-standard fields for select
if(isset($settings->what)){
$what=$settings->what;
if($settings->what=="break"){
$what=null;
}
}
//get fields for select
if(isset($settings->as)){
foreach($settings->as as $k=>$v){
$SQL[]="{$k} AS {$v}";
unset($k,$v);
}
}
//get $activeLanguage
if(isset($settings->from) && $settings->from=="admin"){
$activeLanguage=$SortLanguage;
}
//default $settings->where is `Status`='Y'
if(isset($settings->where)){
$whereSQL=" WHERE {$settings->where}";
}
//default order by Position
if(isset($settings->order)){
$order=$settings->order;
$orderSQL=" ORDER BY {$order}";
}
//group records
if(isset($settings->group)){
$orderSQL=" GROUP BY {$settings->group}";
}
//default limit is null
if(isset($settings->limit)){
$limitSQL=" LIMIT {$settings->limit}";
}
//default sort by autoincrement keys
if(isset($settings->sort)){
$sortBy=$settings->sort;
}
//get only one record
if(isset($settings->one) && $settings->one==true){
$isRecord=true;
}
//show SQL
if(isset($settings->show) && $settings->show==true){
$showSQL=true;
}
//get language fields
if(!empty($settings->fields)){
foreach($settings->fields as $field){
$SQL[]="{$field}_{$activeLanguage->Prefix} AS {$field}";
unset($field);
}
}
//add comma
if((!empty($settings->as) || !empty($settings->fields)) && !empty($what)){
$what="*,";
}
//inner join
if(!empty($settings->inner)){
if(count($settings->inner->table)>1){
$innerSQL=array();
foreach($settings->inner->table as $key=>$table){
$innerSQL[]="INNER JOIN {$table} ON {$settings->inner->on[$key]}";
unset($key,$table);
}
$innerSQL=" ".join(" ",$innerSQL);
}else{
$innerSQL=" INNER JOIN {$settings->inner->table} ON {$settings->inner->on}";
}
}
//get SQL
$SQL=join(",",$SQL);
$objectSQL=$query.$what.$SQL." FROM {$tableName}{$innerSQL}{$whereSQL}{$orderSQL}{$limitSQL};";
if($showSQL){
self::preDump($objectSQL);
}
if(!empty($sortBy)){
$allData=$DataBase->Query($objectSQL,true,null,$sortBy);
}else{
$allData=$DataBase->Query($objectSQL,false);
}
return $allData;
}
//функция прочих запросов к БД
function query($table,$settings=array()){
global $DataBase;
$SQL=array();
if(!empty($settings)){
$settings=json_decode(json_encode($settings),false);
//if empty $settings->table => return empty object
if(empty($table)){
return null;
}
//set type to query
if(isset($settings->type)){
if($settings->type=="insert"){
$SQL[]="INSERT INTO";
}elseif($settings->type=="update"){
$SQL[]="UPDATE";
}elseif($settings->type=="delete"){
$SQL[]="DELETE FROM";
}else{
return null;
}
}
//set table for query
$SQL[]=$table;
//get SET param
if(isset($settings->set)){
$SQL[]="SET {$settings->set}";
}
//get WHERE param
if(isset($settings->where)){
$SQL[]="WHERE {$settings->where}";
}
if(!empty($SQL)){
$SQL=join(" ",$SQL);
if(isset($settings->show) && $settings->show==true){
self::preDump($SQL);
}
if($settings->type=="insert"){
return $DataBase->InsertQuery($SQL,null);
}else{
$DataBase->UpdateQuery($SQL);
return true;
}
}else{
return null;
}
}else{
return null;
}
}
//функция начальника для запросов к БД
function Query($Query,$ReturnInArray=true,$Values=array(),$ArrayKey="Id",$ReturnLastId=false,$IsUpdate=false,$ArrayValues=array()){
$QueryResults=$this->Connection->prepare($Query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
$QueryResults->execute($Values);
if($IsUpdate)
return true;
if($ReturnLastId)
return $this->Connection->lastInsertId();
if(!$ReturnInArray){
$QueryResults=$QueryResults->fetch();
foreach($ArrayValues as $Value){
$QueryResults->$Value=array();
unset($Value);
}
unset($ArrayValues);
return $QueryResults;
}else{
$Results=array();
while($Row=$QueryResults->fetch()){
//var_dump($Row);
if(is_null($ArrayKey)){
$Results[]=$Row;
}elseif(is_array($ArrayKey)){
$this->FillArray($Results,$Row,$ArrayKey,true);
}elseif(!empty($ArrayKey))
$Results[$Row->$ArrayKey]=$Row;
else
$Results[]=$Row;
unset($Row);
}
unset($QueryResults);
if(count($ArrayValues)){
foreach($Results as $Key=>$Result){
foreach($ArrayValues as $Value){
$Result->$Value=array();
$Results[$Key]=$Result;
unset($Value);
}
unset($Result);
}
unset($ArrayValues);
}
return $Results;
}
}
$allData=$Kernel->getRecords($GLOBAL->Table,array(
"where"=>"`Type`='Some' AND`Status`='Y'",
"order"=>"`Position`",
"sort"=>"Id"
));
$Kernel->query($GLOBAL->Table,array(
"type"=>"insert",
"set"=>"`Variable1`='{$data1}',`Variable2`={$data2}"
));
$settings=json_decode(json_encode($settings),false);
И да, сторонними библиотеками мы не пользуемся, только велосипеды свои.
$allData=$Kernel->getRecords($GLOBAL->Table,array(
"where"=>"`Type`='Some' AND`Status`='Y'",
"order"=>"`Position`",
"sort"=>"Id"
));
$allData = $pdo
->query("SELECT * FROM $GLOBAL->Table WHERE `Type`='Some' AND `Status`='Y' ORDER BY `Position`")
->fetchAll();
$Kernel->query($GLOBAL->Table,array(
"type"=>"insert",
"set"=>"`Variable1`='{$data1}',`Variable2`={$data2}"
));
$Database->Query("INSERT INTO $GLOBAL->Table SET `Variable1`=?',`Variable2`=?", [$data1, $data2]);
чего не хватает и вообще всей логике написания.
Читал, что использование global ЗЛО
но таков стиль кода моего начальника (а у него опыт 15 лет)
И да, сторонними библиотеками мы не пользуемся, только велосипеды свои.