433 lines
14 KiB
PHP
Executable File
433 lines
14 KiB
PHP
Executable File
<?php
|
|
|
|
namespace modules\gemba\admin\model;
|
|
|
|
use ErrorException;
|
|
use Exception;
|
|
use PDO;
|
|
use PDOException;
|
|
|
|
/**
|
|
*
|
|
*/
|
|
trait DbBuilderHelper
|
|
{
|
|
|
|
public $enabledDebugMode = false;
|
|
|
|
|
|
public function dbBuilderUpdate($table, $column = [], $where = [])
|
|
{
|
|
try {
|
|
|
|
$sql = "UPDATE $table SET ";
|
|
$ctr = count($column);
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$cKey = $column[$i]['key'];
|
|
$sql .= "`$cKey` = :$cKey";
|
|
$next = $i + 1;
|
|
if ($next < $ctr) {
|
|
$sql .= ', ';
|
|
}
|
|
}
|
|
|
|
$ctrWhere = count($where);
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$exp = isset($where[$i]['exp']) ? $where[$i]['exp'] : ' AND ';
|
|
$opr = isset($where[$i]['opr']) ? $where[$i]['opr'] : ' = ';
|
|
$wKey = $where[$i]['key'];
|
|
if ($i == 0) {
|
|
$sql .= " WHERE `$wKey` $opr :$wKey";
|
|
continue;
|
|
}
|
|
$sql .= "$exp `$wKey` $opr :$wKey";
|
|
}
|
|
|
|
$sql = str_replace(' ', ' ', $sql);
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$bind = isset($column[$i]['bind']) ? $column[$i]['bind'] : PDO::PARAM_STR;
|
|
$cKey = $column[$i]['key'];
|
|
$stmt->bindValue(":$cKey", $column[$i]['val'], $bind);
|
|
}
|
|
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$bind = isset($where[$i]['bind']) ? $where[$i]['bind'] : PDO::PARAM_STR;
|
|
$wKey = $where[$i]['key'];
|
|
$stmt->bindValue(":$wKey", $where[$i]['val'], $bind);
|
|
}
|
|
|
|
$this->checkDebugMode($stmt);
|
|
|
|
$stmt->execute();
|
|
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderUpdate : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
} catch (ErrorException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderUpdate : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
|
|
public function dbBuilderInsert($table, $column = [], $insertId = false)
|
|
{
|
|
|
|
try {
|
|
|
|
$sql = "INSERT INTO $table ";
|
|
$ctr = count($column);
|
|
$columnKey = '';
|
|
$binderKey = '';
|
|
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$cKey = $column[$i]['key'];
|
|
$next = $i + 1;
|
|
|
|
if ($i === 0) {
|
|
$columnKey .= '(';
|
|
$binderKey .= '(';
|
|
}
|
|
|
|
$columnKey .= "`$cKey`";
|
|
$binderKey .= ":$cKey";
|
|
|
|
if ($next < $ctr) {
|
|
$columnKey .= ', ';
|
|
$binderKey .= ', ';
|
|
}
|
|
|
|
if ($next === $ctr) {
|
|
$columnKey .= ')';
|
|
$binderKey .= ')';
|
|
}
|
|
}
|
|
|
|
$sql .= "$columnKey VALUES $binderKey";
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
|
|
for ($i = 0; $i < $ctr; $i++) {
|
|
$bind = isset($column[$i]['bind']) ? $column[$i]['bind'] : PDO::PARAM_STR;
|
|
$cKey = $column[$i]['key'];
|
|
$stmt->bindValue(":$cKey", $column[$i]['val'], $bind);
|
|
}
|
|
|
|
$this->checkDebugMode($stmt);
|
|
|
|
$stmt->execute();
|
|
|
|
if ($insertId) {
|
|
return $this->registry->db->lastInsertId();
|
|
}
|
|
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderInsert : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
} catch (ErrorException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderInsert : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function dbBuilderDelete($table, $columnWhere, $id, $bind = PDO::PARAM_INT)
|
|
{
|
|
try {
|
|
|
|
$sql = "DELETE FROM $table WHERE $columnWhere = :id";
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
$stmt->bindValue(':id', $id, $bind);
|
|
|
|
$this->checkDebugMode($stmt);
|
|
|
|
$stmt->execute();
|
|
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderDelete : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
} catch (ErrorException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderDelete : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function dbBuilderDeleteMultiWhere($table, $where = [])
|
|
{
|
|
try {
|
|
|
|
$sql = "DELETE FROM $table WHERE [[rw]] ";
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
|
|
$ctrWhere = count($where);
|
|
$whereColumn = '';
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$exp = isset($where[$i]['exp']) ? $where[$i]['exp'] : ' AND ';
|
|
$opr = isset($where[$i]['opr']) ? $where[$i]['opr'] : ' = ';
|
|
|
|
$wKey = $where[$i]['key'];
|
|
if ($i == 0) {
|
|
$whereColumn .= "`$wKey` $opr :$wKey";
|
|
continue;
|
|
}
|
|
$whereColumn .= "$exp `$wKey` $opr :$wKey";
|
|
}
|
|
|
|
$sql = str_replace('[[rw]]', $whereColumn, $sql);
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$bind = isset($where[$i]['bind']) ? $where[$i]['bind'] : PDO::PARAM_INT;
|
|
$wKey = $where[$i]['key'];
|
|
$stmt->bindValue(":$wKey", $where[$i]['val'], $bind);
|
|
}
|
|
|
|
$this->checkDebugMode($stmt);
|
|
|
|
$stmt->execute();
|
|
|
|
return true;
|
|
} catch (PDOException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderDeleteMultiWhere : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
} catch (ErrorException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderDeleteMultiWhere : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public function dbBuilderSelect($table, $column = ['*'], $where = [])
|
|
{
|
|
try {
|
|
$sql = "SELECT [[rc]] FROM $table WHERE [[rw]]";
|
|
|
|
$ctrColumn = count($column);
|
|
$keyColumn = '';
|
|
for ($i = 0; $i < $ctrColumn; $i++) {
|
|
$cKey = $column[$i];
|
|
$next = $i + 1;
|
|
$keyColumn .= $cKey === '*' ? $cKey : "`$cKey`";
|
|
if ($next < $ctrColumn) {
|
|
$keyColumn .= ', ';
|
|
}
|
|
}
|
|
|
|
$sql = str_replace('[[rc]]', $keyColumn, $sql);
|
|
|
|
$ctrWhere = count($where);
|
|
$whereColumn = '';
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$exp = isset($where[$i]['exp']) ? $where[$i]['exp'] : ' AND ';
|
|
$opr = isset($where[$i]['opr']) ? $where[$i]['opr'] : ' = ';
|
|
$wKey = $where[$i]['key'];
|
|
if ($i == 0) {
|
|
$whereColumn .= "`$wKey` $opr :$wKey";
|
|
continue;
|
|
}
|
|
$whereColumn .= "$exp `$wKey` $opr :$wKey";
|
|
}
|
|
|
|
if (count($where) === 0) {
|
|
$sql = str_replace('WHERE', '', $sql);
|
|
}
|
|
|
|
$sql = str_replace('[[rw]]', $whereColumn, $sql);
|
|
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$bind = isset($where[$i]['bind']) ? $where[$i]['bind'] : PDO::PARAM_STR;
|
|
$wKey = $where[$i]['key'];
|
|
$stmt->bindValue(":$wKey", $where[$i]['val'], $bind);
|
|
}
|
|
|
|
$this->checkDebugMode($stmt);
|
|
|
|
$stmt->execute();
|
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOexception $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderSelect : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return array();
|
|
} catch (ErrorException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderSelect : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine());
|
|
return array();
|
|
}
|
|
}
|
|
|
|
private function checkDebugMode($stmt)
|
|
{
|
|
if ($this->enabledDebugMode) {
|
|
$stmt->debugDumpParams();
|
|
die();
|
|
}
|
|
}
|
|
|
|
public function dbBuilderSelectJoin($table, $column = ['*'], $where = [],$order="")
|
|
{
|
|
try {
|
|
$sql = "SELECT [[rc]] FROM $table WHERE [[rw]] $order";
|
|
|
|
|
|
|
|
$ctrColumn = count($column);
|
|
$keyColumn = '';
|
|
for ($i = 0; $i < $ctrColumn; $i++) {
|
|
$cKey = $column[$i];
|
|
$next = $i + 1;
|
|
$keyColumn .= $cKey === '*' ? $cKey : "$cKey";
|
|
if ($next < $ctrColumn) {
|
|
$keyColumn .= ', ';
|
|
}
|
|
}
|
|
|
|
$sql = str_replace('[[rc]]', $keyColumn, $sql);
|
|
|
|
$ctrWhere = count($where);
|
|
$whereColumn = '';
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$startSymbol = isset($where[$i]['start_symbol']) ? $where[$i]['start_symbol'] : '';
|
|
$endSymbol = isset($where[$i]['end_symbol']) ? $where[$i]['end_symbol'] : '';
|
|
$exp = isset($where[$i]['exp']) ? $where[$i]['exp'] : ' AND ';
|
|
$opr = isset($where[$i]['opr']) ? $where[$i]['opr'] : ' = ';
|
|
$wField = isset($where[$i]['field'])?$where[$i]['field']:$where[$i]['key'];
|
|
$wVal = isset($where[$i]['val'])?$where[$i]['val']:'';
|
|
$wKey = isset($where[$i]['key'])?$where[$i]['key']:'';
|
|
if ($i == 0) {
|
|
if($wKey == ''){
|
|
$whereColumn .= "$startSymbol :$wField $opr '$wVal' $endSymbol";
|
|
}else{
|
|
if($opr == 'in'){
|
|
$whereColumn .= "$startSymbol $wKey $opr ('". implode("','", $wVal) . "') $endSymbol";
|
|
}else if($opr == 'IS NULL'){
|
|
$whereColumn .= "$wKey $opr";
|
|
}else if($opr == 'between'){
|
|
$start = isset($where[$i]['start'])?$where[$i]['start']:'';
|
|
$end = isset($where[$i]['end'])?$where[$i]['end']:'';
|
|
|
|
$whereColumn .= "$wKey $opr '".$start."' AND '".$end."'";
|
|
}else{
|
|
$whereColumn .= "$startSymbol $wKey $opr :$wField $endSymbol";
|
|
}
|
|
}
|
|
continue;
|
|
}
|
|
|
|
if($wKey == ''){
|
|
$whereColumn .= " $exp $startSymbol :$wField $opr '$wVal' $endSymbol";
|
|
}else{
|
|
if($opr == 'in'){
|
|
$whereColumn .= " $exp $startSymbol $wKey $opr ('". implode("','", $wVal) . "') $endSymbol";
|
|
}else if($opr == 'IS NULL'){
|
|
$whereColumn .= "$exp $wKey $opr";
|
|
}else if($opr == 'between'){
|
|
$start = isset($where[$i]['start'])?$where[$i]['start']:'';
|
|
$end = isset($where[$i]['end'])?$where[$i]['end']:'';
|
|
|
|
$whereColumn .= "$exp $wKey $opr '".$start."' AND '".$end."'";
|
|
}else{
|
|
$whereColumn .= " $exp $startSymbol $wKey $opr :$wField $endSymbol";
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
|
|
if (count($where) === 0) {
|
|
$sql = str_replace('WHERE', '', $sql);
|
|
}
|
|
|
|
$sql = str_replace('[[rw]]', $whereColumn, $sql);
|
|
|
|
$stmt = $this->registry->db->prepare($sql);
|
|
|
|
for ($i = 0; $i < $ctrWhere; $i++) {
|
|
$bind = isset($where[$i]['bind']) ? $where[$i]['bind'] : PDO::PARAM_STR;
|
|
$wField = isset($where[$i]['field'])?$where[$i]['field']:$where[$i]['key'];
|
|
$opr = isset($where[$i]['opr']) ? $where[$i]['opr'] : ' = ';
|
|
$wKey = isset($where[$i]['key'])?$where[$i]['key']:'';
|
|
if($wKey != ''){
|
|
if($opr == 'LIKE'){
|
|
$stmt->bindValue(":$wField", '%'.$where[$i]['val'].'%', $bind);
|
|
}else if($opr == 'in'){
|
|
}else if($opr == 'IS NULL'){
|
|
}else if($opr == 'between'){
|
|
}else{
|
|
$stmt->bindValue(":$wField", $where[$i]['val'], $bind);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
// var_dump($stmt);
|
|
// die();
|
|
|
|
$this->checkDebugMode($stmt);
|
|
|
|
$stmt->execute();
|
|
|
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (PDOexception $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderSelect : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine().' , table : '
|
|
.$table );
|
|
return array();
|
|
} catch (ErrorException $e) {
|
|
$this->registry
|
|
->log
|
|
->error('CompleteRegisterHelper / action : dbBuilderSelect : '
|
|
. $e->getMessage() . ', Line: '
|
|
. $e->getLine().' , table : '
|
|
.$table);
|
|
return array();
|
|
}
|
|
}
|
|
|
|
}
|