Skip to content

Commit

Permalink
Add code generation into the mix
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 12, 2021
1 parent 53bea8f commit 144d67b
Show file tree
Hide file tree
Showing 43 changed files with 365 additions and 139 deletions.
99 changes: 51 additions & 48 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.4.1@9fd7a7d885b3a216cff8dec9d8c21a132f275224">
<files psalm-version="4.6.2@bca09d74adc704c4eaee36a3c3e9d379e290fc3b">
<file src="src/DataIntegrity.php">
<MissingParamType occurrences="1">
<code>$value</code>
</MissingParamType>
<MixedArgument occurrences="1">
<code>$column_default</code>
</MixedArgument>
<MixedArrayOffset occurrences="5">
<code>$existing_row[$field]</code>
<code>$existing_row[$key]</code>
<code>$new_row[$key]</code>
<code>$new_row[$key]</code>
<code>$new_row[$key]</code>
</MixedArrayOffset>
<MixedAssignment occurrences="4">
<MixedAssignment occurrences="3">
<code>$column_default</code>
<code>$key</code>
<code>$row[$column_name]</code>
<code>$value</code>
</MixedAssignment>
Expand Down Expand Up @@ -162,14 +154,6 @@
</PossiblyNullArgument>
</file>
<file src="src/Parser/SQLLexer.php">
<ArgumentTypeCoercion occurrences="1">
<code>\PREG_SPLIT_DELIM_CAPTURE | \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_OFFSET_CAPTURE</code>
</ArgumentTypeCoercion>
<InvalidArgument occurrences="3">
<code>$tokens</code>
<code>$tokens</code>
<code>$tokens</code>
</InvalidArgument>
<PossiblyNullArrayOffset occurrences="1">
<code>$tokens</code>
</PossiblyNullArrayOffset>
Expand All @@ -178,24 +162,6 @@
<DocblockTypeContradiction occurrences="1">
<code>$next === null</code>
</DocblockTypeContradiction>
<MissingPropertyType occurrences="1">
<code>$cache</code>
</MissingPropertyType>
<MixedArgument occurrences="1">
<code>self::$cache</code>
</MixedArgument>
<MixedArrayAccess occurrences="1">
<code>self::$cache[$sql]</code>
</MixedArrayAccess>
<MixedArrayAssignment occurrences="1">
<code>self::$cache[$sql]</code>
</MixedArrayAssignment>
<MixedInferredReturnType occurrences="1">
<code>SelectQuery|InsertQuery|UpdateQuery|TruncateQuery|DeleteQuery|DropTableQuery|ShowTablesQuery</code>
</MixedInferredReturnType>
<MixedReturnStatement occurrences="1">
<code>self::$cache[$sql]</code>
</MixedReturnStatement>
<MixedReturnTypeCoercion occurrences="2">
<code>$out</code>
<code>array&lt;int, Token&gt;</code>
Expand All @@ -206,11 +172,6 @@
<code>$type</code>
</ArgumentTypeCoercion>
</file>
<file src="src/Parser/TruncateParser.php">
<DocblockTypeContradiction occurrences="1">
<code>$token === null</code>
</DocblockTypeContradiction>
</file>
<file src="src/Parser/UpdateParser.php">
<DocblockTypeContradiction occurrences="1">
<code>$token === null</code>
Expand All @@ -231,9 +192,7 @@
<code>$stmt-&gt;values</code>
<code>$stmt-&gt;values</code>
</MixedArgumentTypeCoercion>
<PossiblyNullArgument occurrences="5">
<code>$conn-&gt;getDatabaseName()</code>
<code>$conn-&gt;getDatabaseName()</code>
<PossiblyNullArgument occurrences="3">
<code>$stmt-&gt;decimals</code>
<code>$stmt-&gt;length</code>
<code>$stmt-&gt;length</code>
Expand Down Expand Up @@ -671,11 +630,20 @@
<code>$values</code>
</MissingConstructor>
</file>
<file src="src/Schema/Column/Blob.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/CharacterColumn.php">
<PossiblyNullPropertyAssignmentValue occurrences="2">
<code>$character_set</code>
<code>$collation</code>
</PossiblyNullPropertyAssignmentValue>
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/DecimalPointColumn.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/Defaultable.php">
<MissingParamType occurrences="1">
Expand All @@ -685,11 +653,36 @@
<code>getDefault</code>
</MissingReturnType>
</file>
<file src="src/Schema/Column/EmptyConstructorTrait.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/HasOptionsTrait.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/IntegerColumn.php">
<MissingParamType occurrences="1">
<code>$mysql_default</code>
</MissingParamType>
</file>
<file src="src/Schema/Column/IntegerColumnTrait.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/LongBlob.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/MediumBlob.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/MySqlDefaultTrait.php">
<MissingParamType occurrences="1">
<code>$mysql_default</code>
Expand All @@ -700,6 +693,16 @@
<code>$mysql_default</code>
</MissingParamType>
</file>
<file src="src/Schema/Column/TextTrait.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/Column/TinyBlob.php">
<MixedOperand occurrences="1">
<code>$this-&gt;getDefault()</code>
</MixedOperand>
</file>
<file src="src/Schema/TableDefinition.php">
<MixedPropertyTypeCoercion occurrences="2">
<code>$autoIncrementOffsets</code>
Expand Down
4 changes: 2 additions & 2 deletions src/DataIntegrity.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function ensureColumnsPresent(
) {
foreach ($table_definition->columns as $column_name => $column) {
$php_type = $column->getPhpType();
$column_nullable = $column->isNullable;
$column_nullable = $column->isNullable();

$column_default = $column instanceof Schema\Column\Defaultable ? $column->getDefault() : null;

Expand Down Expand Up @@ -151,7 +151,7 @@ public static function coerceValueToColumn(
) {
$php_type = $column->getPhpType();

if ($column->isNullable && $value === null) {
if ($column->isNullable() && $value === null) {
return null;
}

Expand Down
1 change: 0 additions & 1 deletion src/Parser/ShowParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function parse()
default:
throw new ParserException("Parser error: expected SHOW TABLES");
}

}

private function parseShowTables(): ShowTablesQuery
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/CreateProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function makeTableDefinition(
foreach ($stmt->fields as $field) {
$definition_columns[$field->name] = $column = self::getDefinitionColumn($field->type);

$column->isNullable = (bool) $field->type->null;
$column->setNullable((bool) $field->type->null);

if ($field->auto_increment && $column instanceof Column\IntegerColumn) {
$column->autoIncrement();
Expand Down
10 changes: 5 additions & 5 deletions src/Processor/Expression/Evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,13 +290,13 @@ public static function combineColumnTypes(array $types) : Column

if ($type_0_null) {
$type = clone $types[1];
$type->isNullable = true;
$type->setNullable(true);
return $type;
}

if ($type_1_null) {
$type = clone $types[0];
$type->isNullable = true;
$type->setNullable(true);
return $type;
}
}
Expand All @@ -311,7 +311,7 @@ public static function combineColumnTypes(array $types) : Column
$non_null_types = [];

foreach ($types as $type) {
if ($type->isNullable) {
if ($type->isNullable()) {
$is_nullable = true;
}

Expand All @@ -326,7 +326,7 @@ public static function combineColumnTypes(array $types) : Column

if (count($non_null_types) === 1) {
$type = clone $non_null_types[0];
$type->isNullable = true;
$type->setNullable(true);
return $type;
}

Expand Down Expand Up @@ -357,7 +357,7 @@ public static function combineColumnTypes(array $types) : Column
}

if ($is_nullable) {
$column->isNullable = true;
$column->setNullable(true);
}

return $column;
Expand Down
8 changes: 4 additions & 4 deletions src/Processor/Expression/FunctionEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static function getColumnSchema(
$column = new Column\BigInt(false, 10);
}

$column->isNullable = true;
$column->setNullable(true);
return $column;

case 'MOD':
Expand All @@ -148,7 +148,7 @@ public static function getColumnSchema(
}

$if = clone $if;
$if->isNullable = false;
$if->setNullable(false);

if ($if->getPhpType() === 'string') {
return $if;
Expand Down Expand Up @@ -209,8 +209,8 @@ public static function getColumnSchema(

$date = new Column\Date();

if ($arg->isNullable) {
$date->isNullable = true;
if ($arg->isNullable()) {
$date->setNullable(true);
}

return $date;
Expand Down
2 changes: 1 addition & 1 deletion src/Processor/JoinProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static function process(
$parts = explode('.%.', $name);
$null_placeholder[$right_table_name . '.%.' . end($parts)] = null;
$column = clone $column;
$column->isNullable = true;
$column->setNullable(true);
$right_result->columns[$name] = $column;
}

Expand Down
4 changes: 2 additions & 2 deletions src/Processor/SelectProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ private static function getSelectSchema(
$use_cache
);

$columns[$expr->name]->isNullable = true;
$columns[$expr->name]->setNullable(true);
}
}
}
Expand Down Expand Up @@ -622,7 +622,7 @@ function ($col) {
if (isset($subquery_result->columns[$column_name])
&& (\get_class($subquery_result->columns[$column_name])
!== \get_class($column)
|| $subquery_result->columns[$column_name]->isNullable !== $column->isNullable)
|| $subquery_result->columns[$column_name]->isNullable() !== $column->isNullable())
) {
$columns[$column_name] = Expression\Evaluator::combineColumnTypes([
$subquery_result->columns[$column_name],
Expand Down
5 changes: 2 additions & 3 deletions src/Processor/ShowIndexProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace Vimeo\MysqlEngine\Processor;


use Vimeo\MysqlEngine\FakePdoInterface;
use Vimeo\MysqlEngine\Query\ShowIndexQuery;
use Vimeo\MysqlEngine\Schema\Column;
Expand Down Expand Up @@ -59,7 +58,7 @@ public static function process(
'Sub_part' => null,
// because PACK_KEYS is not implemented
'Packed' => null,
'Null' => $table_definition->columns[$column]->isNullable ? 'YES' : '',
'Null' => $table_definition->columns[$column]->isNullable() ? 'YES' : '',
// because Index does not have $mode (in the CreateIndex)
'Index_type' => null,
// because DISABLE KEYS is not implemented
Expand All @@ -72,4 +71,4 @@ public static function process(
$result = self::applyWhere($conn, $scope, $stmt->whereClause, new QueryResult($rows, $columns));
return new QueryResult(array_merge($result->rows), $result->columns);
}
}
}
23 changes: 22 additions & 1 deletion src/Schema/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,31 @@ abstract class Column
/**
* @var bool
*/
public $isNullable = true;
private $isNullable = true;

public function isNullable() : bool
{
return $this->isNullable;
}

/**
* @return static
*/
public function setNullable(bool $is_nullable)
{
$this->isNullable = $is_nullable;
return $this;
}

public function getNullablePhp() : string
{
return '->setNullable(' . ($this->isNullable() ? 'true' : 'false') . ')';
}

/**
* @return 'int'|'string'|'float'|'null'
*/
abstract public function getPhpType() : string;

abstract public function getPhpCode() : string;
}
9 changes: 9 additions & 0 deletions src/Schema/Column/Blob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,13 @@ public function __construct()
{
parent::__construct(65535, 'binary', '_bin');
}

public function getPhpCode() : string
{
$default = $this->getDefault() !== null ? '\'' . $this->getDefault() . '\'' : 'null';

return '(new \\' . static::class . '())'
. ($this->hasDefault() ? '->setDefault(' . $default . ')' : '')
. $this->getNullablePhp();
}
}
5 changes: 0 additions & 5 deletions src/Schema/Column/Char.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@
class Char extends CharacterColumn implements StringColumn, Defaultable
{
use MySqlDefaultTrait;

public function __construct(int $max_string_length, ?string $character_set = null, ?string $collation = null)
{
parent::__construct($max_string_length, $character_set, $collation);
}
}
Loading

0 comments on commit 144d67b

Please sign in to comment.