194 lines
4.7 KiB
PHP
194 lines
4.7 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
Module : /DB/SQL.php
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : February 2nd, 2007.
|
|
Last Modified : August 21st, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Webapp\DB;
|
|
|
|
use \flight\Engine;
|
|
use \flight\Session;
|
|
use \flight\database\SimplePdo;
|
|
use Webapp\Web\Param;
|
|
|
|
/**
|
|
* Class SQL
|
|
*
|
|
* Database CRUD dengan data dari GET/POST.
|
|
*
|
|
* @license MIT, https://www.3fonia.com
|
|
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
|
*
|
|
* @package Webapp\DB
|
|
*/
|
|
|
|
final class SQL {
|
|
private string $_ttable;
|
|
private array $_afield = [];
|
|
private array $_awhere = [];
|
|
protected Engine $app;
|
|
protected SimplePdo $db;
|
|
protected array $webuser;
|
|
|
|
/**
|
|
* @param Engine $app
|
|
*/
|
|
public function __construct(Engine $app){
|
|
$this->app = $app;
|
|
$this->db = $app->db();
|
|
$this->webuser = $app->session()->get('WEB_USER', ['UserID'=>0, 'UNIX'=>0]);
|
|
}
|
|
|
|
//todo: deprecated
|
|
public function SafeSQL($ttext = "") {
|
|
return $ttext;
|
|
}
|
|
|
|
public function set_table(string $table = ""): void {
|
|
$this->_ttable = $table;
|
|
}
|
|
|
|
public function add_param(string $param = "", mixed $value = null, bool $nomor = false): void {
|
|
$this->_afield[] = [
|
|
'field' => $param,
|
|
'value' => is_null($value) ? Param::Get($param) : $value,
|
|
'numbe' => $nomor,
|
|
];
|
|
}
|
|
|
|
public function add_param_date(string $param = "", string|null $value = null): void {
|
|
$this->_afield[] = [
|
|
'field' => $param,
|
|
'value' => tgl_sql2str(is_null($value) ? Param::Get($param) : $value),
|
|
'numbe' => false,
|
|
];
|
|
}
|
|
|
|
public function add_param_stamp(string $param = "DateStamp"): void {
|
|
$this->_afield[] = [
|
|
'field' => $param,
|
|
'value' => 'NOW()',
|
|
'numbe' => true,
|
|
];
|
|
}
|
|
|
|
public function add_param_unix(): void {
|
|
$this->_afield[] = [
|
|
'field' => 'Unix',
|
|
'value' => $this->webuser['UNIX'],
|
|
'numbe' => false,
|
|
];
|
|
}
|
|
|
|
public function add_param_userid(string $param = "UserID"): void {
|
|
$this->_afield[] = [
|
|
'field' => $param,
|
|
'value' => $this->webuser['UserID'],
|
|
'numbe' => false,
|
|
];
|
|
}
|
|
|
|
public function add_where_unix(): void {
|
|
$this->_awhere[] = [
|
|
'field' => 'Unix',
|
|
'value' => $this->webuser['UNIX'],
|
|
'numbe' => false,
|
|
];
|
|
}
|
|
|
|
public function add_where_date(string $field = "", string|null $value = null): void {
|
|
$this->_awhere[] = [
|
|
'field' => $field,
|
|
'value' => (is_null($value)) ? '0000-00-00' : tgl_sql2str($value),
|
|
'numbe' => false,
|
|
];
|
|
}
|
|
|
|
public function add_where(string $field = "", mixed $value = null, bool $nomor = false): void {
|
|
$this->_awhere[] = [
|
|
'field' => $field,
|
|
'value' => is_null($value) ? Param::Get($field) : $value,
|
|
'numbe' => $nomor,
|
|
];
|
|
}
|
|
|
|
public function clear_param(): void {
|
|
$this->_afield = [];
|
|
}
|
|
|
|
public function clear_where(): void {
|
|
$this->_awhere = [];
|
|
}
|
|
|
|
public function InsertDB(): void {
|
|
$__afield = [];
|
|
$__aplace = [];
|
|
$__aexec = [];
|
|
foreach ($this->_afield as $col) {
|
|
$__afield[] = '`'.$col['field'].'`';
|
|
if ($col['numbe'] === true) {
|
|
$__aplace[] = $col['value'];
|
|
} else {
|
|
$__aplace[] = '?';
|
|
$__aexec[] = $col['value'];
|
|
}
|
|
}
|
|
$tsql = 'INSERT INTO '.$this->_ttable.' ('. implode(',', $__afield) .') VALUES ('. implode(',', $__aplace) .')';
|
|
$this->db->runQuery($tsql, $__aexec);
|
|
}
|
|
|
|
public function UpdateDB(): void {
|
|
$__aplace = [];
|
|
$__awhere = [];
|
|
$__aexec = [];
|
|
foreach ($this->_afield as $col) {
|
|
if ($col['numbe'] === true) {
|
|
$__aplace[] = '`'.$col['field'].'`='. $col['value'];
|
|
} else {
|
|
$__aplace[] = '`'.$col['field'].'`=?';
|
|
$__aexec[] = $col['value'];
|
|
}
|
|
}
|
|
foreach ($this->_awhere as $col) {
|
|
if ($col['numbe'] === true) {
|
|
$__awhere[] = '`'.$col['field'].'`='. $col['value'];
|
|
} else {
|
|
$__awhere[] = '`'.$col['field'].'`=?';
|
|
$__aexec[] = $col['value'];
|
|
}
|
|
}
|
|
$tsql = 'UPDATE '.$this->_ttable.' SET '. implode(',', $__aplace) .' WHERE '. implode(' AND ', $__awhere);
|
|
$this->db->runQuery($tsql, $__aexec);
|
|
}
|
|
|
|
public function DeleteDB(): void {
|
|
$__awhere = [];
|
|
$__aexec = [];
|
|
foreach ($this->_awhere as $col) {
|
|
if ($col['numbe'] === true) {
|
|
$__awhere[] = '`'.$col['field'].'`='. $col['value'];
|
|
} else {
|
|
$__awhere[] = '`'.$col['field'].'`=?';
|
|
$__aexec[] = $col['value'];
|
|
}
|
|
}
|
|
$tsql = 'DELETE FROM '.$this->_ttable.' WHERE '. implode(' AND ', $__awhere);
|
|
$this->db->runQuery($tsql, $__aexec);
|
|
}
|
|
|
|
public function LastInsertID(): int {
|
|
return $this->db->lastInsertId();
|
|
}
|
|
}
|
|
|
|
?>
|