Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
/vendor/
composer.lock
/tests/coverage.html
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"classmap": ["src/"]
},
"require-dev": {
"nette/tester": "^2.5"
}
"nette/tester": "^2.5",
"phpstan/phpstan": "^2.1"
},
"suggest": {
"ext-sqlite3": "For SQLite driver"
}
}
20 changes: 20 additions & 0 deletions src/Drivers/DateParserTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace CzProject\SqlGenerator\Drivers;

use DateTimeImmutable;
use DateTimeInterface;
use Exception;

trait DateParserTrait {
private function dateFormat(DateTimeInterface|string $value, string $format): string
{
try {
$date = $value instanceof DateTimeInterface ? $value : new DateTimeImmutable($value);
return $date->format($format);
} catch (Exception $e) {
return '';
}
}
}
53 changes: 33 additions & 20 deletions src/Drivers/MysqlDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,31 @@
namespace CzProject\SqlGenerator\Drivers;

use CzProject\SqlGenerator\IDriver;
use DateTimeInterface;


class MysqlDriver implements IDriver
class MysqlDriver implements IDriver
{
public function escapeIdentifier($value)
{
use DateParserTrait;

/**
* @var string[]
*/
private const TRANSACTION = [
'start' => 'BEGIN;',
'commit' => 'COMMIT;',
'rollback' => 'ROLLBACK;',
];

private const LAST_ID = 'LAST_INSERT_ID()';

public function escapeIdentifier(string $value): string
{
// @see http://dev.mysql.com/doc/refman/5.0/en/identifiers.html
// @see http://api.dibiphp.com/2.3.2/source-drivers.DibiMySqlDriver.php.html#307
return '`' . str_replace('`', '``', $value) . '`';
}


public function escapeText($value)
public function escapeText(string $value): string
{
// https://dev.mysql.com/doc/refman/5.5/en/string-literals.html
// http://us3.php.net/manual/en/function.mysql-real-escape-string.php#101248
Expand All @@ -28,27 +40,28 @@ public function escapeText($value)
) . '\'';
}


public function escapeBool($value)
public function escapeBool(bool $value): string
{
return $value ? '1' : '0';
}


public function escapeDate($value)
public function escapeDate(DateTimeInterface|string $value): string
{
if (!($value instanceof \DateTime) && !($value instanceof \DateTimeInterface)) {
$value = new \DateTime($value);
}
return $value->format("'Y-m-d'");
return $this->dateFormat($value, "'Y-m-d'");
}


public function escapeDateTime($value)
public function escapeDateTime(DateTimeInterface|string $value): string
{
if (!($value instanceof \DateTime) && !($value instanceof \DateTimeInterface)) {
$value = new \DateTime($value);
}
return $value->format("'Y-m-d H:i:s'");
return $this->dateFormat($value, "'Y-m-d H:i:s'");
}

public function transaction(string $action): string
{
return self::TRANSACTION[$action];
}

public function lastId(): string
{
return self::LAST_ID;
}
}
65 changes: 65 additions & 0 deletions src/Drivers/SqliteDriver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace CzProject\SqlGenerator\Drivers;

use CzProject\SqlGenerator\IDriver;
use DateTimeInterface;
use SQLite3;

/**
* @see https://sqlite.org/lang_keywords.html
*/
class SqliteDriver implements IDriver
{
use DateParserTrait;

public bool $modifyColumn = false;

/**
* @var string[]
*/
private const TRANSACTION = [
'start' => 'BEGIN TRANSACTION;',
'commit' => 'COMMIT TRANSACTION;',
'rollback' => 'ROLLBACK TRANSACTION;',
];

private const LAST_ID = 'LAST_INSERT_ROWID()';

public function escapeIdentifier(string $value): string
{
return '"'.str_replace('"', '""', $value).'"';
}

public function escapeText(string $value): string
{
return "'".SQLite3::escapeString($value)."'";
}

public function escapeBool(bool $value): string
{
return strval($value ? 1 : 0);
}

public function escapeDate(DateTimeInterface|string $value): string
{
return $this->escapeText($this->dateFormat($value, 'Y-m-d'));
}

public function escapeDateTime(DateTimeInterface|string $value): string
{
return $this->escapeText($this->dateFormat($value, 'Y-m-d H:i:s'));
}

public function transaction(string $action): string
{
return self::TRANSACTION[$action];
}

public function lastId(): string
{
return self::LAST_ID;
}
}
70 changes: 28 additions & 42 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,66 @@

namespace CzProject\SqlGenerator;

use DateTimeInterface;

class Helpers
{
public function __construct()
/**
* @throws StaticClassException
*/
public function __construct()
{
throw new StaticClassException('This is static class.');
}


/**
* @param mixed $value
* @return string
* @throws InvalidArgumentException
* @see https://api.dibiphp.com/3.0/source-Dibi.Translator.php.html#174
*/
public static function formatValue($value, IDriver $driver)
{
/**
* @throws InvalidArgumentException
* @see https://api.dibiphp.com/3.0/source-Dibi.Translator.php.html#174
*/
public static function formatValue(mixed $value, IDriver $driver): string
{
if (is_string($value)) {
return $driver->escapeText($value);

} elseif (is_int($value)) {
return (string) $value;

} elseif (is_float($value)) {
}
elseif (is_int($value)) {
return (string)$value;
}
elseif (is_float($value)) {
return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');

} elseif (is_bool($value)) {
}
elseif (is_bool($value)) {
return $driver->escapeBool($value);

} elseif ($value === NULL) {
}
elseif ($value === NULL) {
return 'NULL';

} elseif ($value instanceof \DateTime || $value instanceof \DateTimeInterface) {
}
elseif ($value instanceof DateTimeInterface) {
return $driver->escapeDateTime($value);
}

throw new InvalidArgumentException("Unsupported value type.");
}


/**
* @param string|TableName $tableName
* @return string
*/
public static function escapeTableName($tableName, IDriver $driver)
{
public static function escapeTableName(TableName|string $tableName, IDriver $driver): string
{
if ($tableName instanceof TableName) {
return $tableName->toString($driver);
}

return $driver->escapeIdentifier($tableName);
}


/**
* @param string|TableName $tableName
* @return string|TableName
*/
public static function createTableName($tableName)
{
public static function createTableName(TableName|string $tableName): TableName|string
{
if (is_string($tableName) && strpos($tableName, '.')) {
return TableName::create($tableName);
}

return $tableName;
}


/**
* @param string $s
* @return string
*/
public static function normalizeNewLines($s)
{
public static function normalizeNewLines(string $s): string
{
return str_replace(["\r\n", "\r"], "\n", $s);
}
}
56 changes: 25 additions & 31 deletions src/IDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,30 @@

namespace CzProject\SqlGenerator;


interface IDriver
use DateTimeInterface;

/**
* A few additional properties can be set to control the driver's capabilities:
* - `public bool $renameTable`
* - `public bool $renameColumn`
* - `public bool $modifyColumn`
*/
interface IDriver
{
/**
* @param string $value
* @return string
*/
function escapeIdentifier($value);

/**
* @param string $value
* @return string
*/
function escapeText($value);

/**
* @param bool $value
* @return string
*/
function escapeBool($value);

/**
* @param string|\DateTime|\DateTimeInterface $value
* @return string
*/
function escapeDate($value);

/**
* @param string|\DateTime|\DateTimeInterface $value
* @return string
*/
function escapeDateTime($value);
public function escapeIdentifier(string $value): string;

public function escapeText(string $value): string;

public function escapeBool(bool $value): string;

public function escapeDate(DateTimeInterface|string $value): string;

public function escapeDateTime(DateTimeInterface|string $value): string;

/**
* @param 'start'|'commit'|'rollback' $action
*/
public function transaction(string $action): string;

public function lastId(): string;
}
6 changes: 1 addition & 5 deletions src/IStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@

namespace CzProject\SqlGenerator;


interface IStatement
{
/**
* @return string
*/
function toSql(IDriver $driver);
public function toSql(IDriver $driver): string;
}
Loading
Loading