80 lines
1.9 KiB
PHP
80 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Common;
|
|
|
|
abstract class Sql {
|
|
|
|
public static function function($name, ...$arguments)
|
|
{
|
|
return new class($name, ...$arguments) {
|
|
|
|
protected string $as = "";
|
|
|
|
protected string $name;
|
|
|
|
protected array $arguments;
|
|
|
|
public function __construct( $name, ...$arguments) {
|
|
$this->name = $name;
|
|
$this->arguments = $arguments;
|
|
$this->parseArguments();
|
|
}
|
|
|
|
public function __toString() {
|
|
return implode(' ', array_filter([
|
|
"{$this->name}(" . implode(", ", $this->arguments) . ")",
|
|
$this->as ? "AS {$this->as}" : false,
|
|
]));
|
|
}
|
|
|
|
public function as($fieldName) {
|
|
$this->as = $fieldName;
|
|
return $this;
|
|
}
|
|
|
|
protected function parseArguments() {
|
|
foreach($this->arguments as &$item) {
|
|
$item = Sql::escape($item);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
public static function identifier(string $identifier) : object
|
|
{
|
|
return new class($identifier) {
|
|
|
|
protected string $identifier;
|
|
|
|
public function __construct(string $identifier) {
|
|
$this->identifier = $identifier;
|
|
}
|
|
|
|
public function __toString() {
|
|
return $this->identifier;
|
|
}
|
|
};
|
|
}
|
|
|
|
public static function escape($value)
|
|
{
|
|
switch(true) {
|
|
case is_object($value):
|
|
return (string) $value;
|
|
|
|
case is_string($value):
|
|
return "'$value'";
|
|
|
|
case is_null($value):
|
|
return "NULL";
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
public static function parameter($value) : string
|
|
{
|
|
|
|
}
|
|
}
|