Files
kiri-core/Database/Condition/Condition.php
T

84 lines
1.5 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 12:38:32 +08:00
namespace Database\Condition;
2021-03-26 19:18:43 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 12:38:32 +08:00
use Snowflake\Abstracts\BaseObject;
use Snowflake\Core\Str;
/**
* Class Condition
* @package Database\Condition
*/
abstract class Condition extends BaseObject
{
2021-03-26 19:18:43 +08:00
protected string $column = '';
protected string $opera = '=';
2021-02-28 00:43:59 +08:00
2021-03-26 19:18:43 +08:00
/** @var array|mixed */
protected $value;
2021-02-28 00:43:59 +08:00
2021-03-26 19:18:43 +08:00
const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp'];
2021-02-28 00:43:59 +08:00
2021-03-26 19:18:43 +08:00
protected array $attributes = [];
2021-02-28 00:43:59 +08:00
2021-03-26 19:18:43 +08:00
abstract public function builder();
2021-02-28 00:43:59 +08:00
2021-03-26 19:18:43 +08:00
/**
* @param string $column
*/
public function setColumn(string $column): void
{
$this->column = $column;
}
2021-02-28 00:43:59 +08:00
2021-03-26 19:18:43 +08:00
/**
* @param string $opera
*/
public function setOpera(string $opera): void
{
$this->opera = $opera;
}
2021-02-28 00:43:59 +08:00
2021-03-04 15:51:53 +08:00
/**
* @param $params
*/
2021-03-26 19:18:43 +08:00
public function setValue($params): void
{
if (is_array($params)) {
$values = [];
foreach ($params as $item => $value) {
$values[$item] = is_numeric($value) ? $value : '\'' . $value . '\'';
}
$this->value = $values;
} else {
$this->value = $this->checkIsSqlString($params);
}
}
/**
* @param $params
* @return int|string
*/
#[Pure] private function checkIsSqlString($params): int|string
{
if (is_numeric($params)) {
return $params;
}
2021-03-26 19:25:49 +08:00
$check = ltrim($params, '(');
2021-03-26 19:25:10 +08:00
$check = strtolower(substr($check, 0, 6));
2021-03-26 19:18:43 +08:00
if (in_array($check, ['update', 'select', 'insert', 'delete'])) {
2021-03-26 19:41:03 +08:00
return $params;
2021-03-26 19:18:43 +08:00
} else {
return sprintf('\'%s\'', $params);
}
}
2020-08-31 12:38:32 +08:00
}