151 lines
3.9 KiB
PHP
151 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace Ulmus\Adapter;
|
|
|
|
use Ulmus\Common\PdoObject;
|
|
|
|
use Ulmus\Entity\Sqlite\Table;
|
|
use Ulmus\Exception\AdapterConfigurationException;
|
|
use Ulmus\Migration\FieldDefinition;
|
|
use Ulmus\{ Repository, QueryBuilder };
|
|
|
|
class SQLite implements AdapterInterface {
|
|
use DefaultAdapterTrait;
|
|
|
|
const DSN_PREFIX = "sqlite";
|
|
|
|
public string $path;
|
|
|
|
public array $pragma;
|
|
|
|
public function __construct(
|
|
? string $path = null,
|
|
? array $pragma = null
|
|
) {
|
|
if ($path !== null) {
|
|
$this->path = $path;
|
|
}
|
|
|
|
if ($pragma !== null) {
|
|
$this->pragma = $pragma;
|
|
}
|
|
}
|
|
|
|
public function connect() : PdoObject
|
|
{
|
|
try {
|
|
$pdo = new PdoObject($this->buildDataSourceName(), null, null);
|
|
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
|
|
$pdo->setAttribute(\PDO::ATTR_DEFAULT_FETCH_MODE, \PDO::FETCH_ASSOC);
|
|
}
|
|
catch(PDOException $ex){
|
|
throw $ex;
|
|
}
|
|
|
|
return $pdo;
|
|
}
|
|
|
|
public function buildDataSourceName() : string
|
|
{
|
|
$parts[] = $this->path;
|
|
|
|
return static::DSN_PREFIX . ":" . implode(';', $parts);
|
|
}
|
|
|
|
public function setup(array $configuration) : void
|
|
{
|
|
$this->path = $configuration['path'] ?? "";
|
|
$this->pragma = $configuration['pragma'] ?? [];
|
|
}
|
|
|
|
# https://sqlite.org/lang_keywords.html
|
|
public function escapeIdentifier(string $segment, int $type) : string
|
|
{
|
|
switch($type) {
|
|
case static::IDENTIFIER_DATABASE:
|
|
case static::IDENTIFIER_TABLE:
|
|
case static::IDENTIFIER_FIELD:
|
|
return '"' . trim(str_replace('"', '""', $segment), '"') . '"';
|
|
|
|
case static::IDENTIFIER_VALUE:
|
|
return "'$segment'";
|
|
}
|
|
}
|
|
|
|
public function defaultEngine(): ? string
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public function databaseName() : string
|
|
{
|
|
$base = basename($this->path);
|
|
|
|
return substr($base, 0, strrpos($base, '.') ?: strlen($base));
|
|
}
|
|
|
|
public function schemaTable(string $databaseName, string $tableName) /* : ? object */
|
|
{
|
|
return Table::repository()->loadOneFromField(Table::field('tableName'), $tableName);
|
|
}
|
|
|
|
public function mapFieldType(FieldDefinition $field, bool $typeOnly = false) : string
|
|
{
|
|
$type = $field->type;
|
|
|
|
$length = $field->length;
|
|
|
|
if ( is_a($type, Entity\Field\Date::class, true) || is_a($type, Entity\Field\Time::class, true) || is_a($type, \DateTime::class, true) ) {
|
|
$type = "TEXT";
|
|
$length = strlen((string) $type);
|
|
}
|
|
else {
|
|
switch($type) {
|
|
case "bool":
|
|
$check = sprintf("CHECK (%s IN (0, 1))", $field->getColumnName());
|
|
|
|
case "bigint":
|
|
case "int":
|
|
$type = "INTEGER";
|
|
break;
|
|
|
|
case "array":
|
|
case "string":
|
|
$type = "TEXT";
|
|
$length = null;
|
|
break;
|
|
|
|
case "float":
|
|
$type = "REAL";
|
|
break;
|
|
|
|
default:
|
|
$type = "BLOB";
|
|
break;
|
|
}
|
|
}
|
|
|
|
return $typeOnly ? $type : $type . ( $length ? "($length" . ( $precision ? ",$precision" : "" ) . ")" : "" );
|
|
}
|
|
|
|
public function tableSyntax() : array
|
|
{
|
|
return [
|
|
'ai' => "AUTOINCREMENT",
|
|
'pk' => "PRIMARY KEY",
|
|
'unsigned' => "",
|
|
];
|
|
}
|
|
|
|
public function repositoryClass() : string
|
|
{
|
|
return Repository\SqliteRepository::class;
|
|
}
|
|
|
|
public function queryBuilderClass() : string
|
|
{
|
|
return QueryBuilder\SqliteQueryBuilder::class;
|
|
}
|
|
}
|