Skip to content
Merged
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
111 changes: 66 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ console app or you can also do this programmatically through the `PSX\Sql\Genera

use PSX\Sql\Generator\Generator;

$connection = null; // a doctrine DBAL connection
$connection = null; // a doctrine DBAL connection
$target = __DIR__;

$generator = new Generator($connection, 'Acme\\Table');
Expand All @@ -44,12 +44,15 @@ The following are basic examples how you can work with a generated table class.
use PSX\Sql\Condition;
use PSX\Sql\OrderBy;
use PSX\Sql\TableManager;
use PSX\Sql\Tests\Generator\SqlTableTestTable;
use PSX\Sql\Tests\Generator\SqlTableTestColumn;
use PSX\Sql\Tests\Generator\SqlTableTestRow;

$connection = null; // a doctrine DBAL connection
$tableManager = new TableManager($connection);

/** @var \PSX\Sql\Tests\Generator\SqlTableTestTable $table */
$table = $tableManager->getTable(\PSX\Sql\Tests\Generator\SqlTableTestTable::class);
/** @var SqlTableTestTable $table */
$table = $tableManager->getTable(SqlTableTestTable::class);

// returns by default 16 entries from the table ordered by the primary column descending
$table->findAll();
Expand All @@ -58,7 +61,7 @@ $table->findAll();
$table->findAll(startIndex: 0, count: 12);

// orders the entries after the column "id" descending
$table->findAll(startIndex: 0, count: 12, sortBy: 'id', sortOrder: OrderBy::DESC);
$table->findAll(startIndex: 0, count: 12, sortBy: SqlTableTestColumn::ID, sortOrder: OrderBy::DESC);

// returns all rows which match the specified title
$table->findByTitle('foo%');
Expand All @@ -70,7 +73,7 @@ $table->find(1);
$table->getCount();

// creates a new row
$row = new \PSX\Sql\Tests\Generator\SqlTableTestRow();
$row = new SqlTableTestRow();
$row->setTitle('foo');
$table->create($row);

Expand Down Expand Up @@ -103,41 +106,41 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
public const COLUMN_ID = 'id';
public const COLUMN_TITLE = 'title';
public const COLUMN_DATE = 'date';
public function getName() : string
public function getName(): string
{
return self::NAME;
}
public function getColumns() : array
public function getColumns(): array
{
return array(self::COLUMN_ID => 0x3020000a, self::COLUMN_TITLE => 0xa00020, self::COLUMN_DATE => 0x800000);
return [self::COLUMN_ID => 0x3020000a, self::COLUMN_TITLE => 0xa00020, self::COLUMN_DATE => 0x800000];
}
/**
* @return array<\PSX\Sql\Tests\Generator\SqlTableTestRow>
* @throws \PSX\Sql\Exception\QueryException
*/
public function findAll(?\PSX\Sql\Condition $condition = null, ?int $startIndex = null, ?int $count = null, ?string $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null) : array
public function findAll(?\PSX\Sql\Condition $condition = null, ?int $startIndex = null, ?int $count = null, ?\PSX\Sql\Tests\Generator\SqlTableTestColumn $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null): array
{
return $this->doFindAll($condition, $startIndex, $count, $sortBy, $sortOrder);
}
/**
* @return array<\PSX\Sql\Tests\Generator\SqlTableTestRow>
* @throws \PSX\Sql\Exception\QueryException
*/
public function findBy(\PSX\Sql\Condition $condition, ?int $startIndex = null, ?int $count = null, ?string $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null) : array
public function findBy(\PSX\Sql\Condition $condition, ?int $startIndex = null, ?int $count = null, ?\PSX\Sql\Tests\Generator\SqlTableTestColumn $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null): array
{
return $this->doFindBy($condition, $startIndex, $count, $sortBy, $sortOrder);
}
/**
* @throws \PSX\Sql\Exception\QueryException
*/
public function findOneBy(\PSX\Sql\Condition $condition) : ?\PSX\Sql\Tests\Generator\SqlTableTestRow
public function findOneBy(\PSX\Sql\Condition $condition): ?\PSX\Sql\Tests\Generator\SqlTableTestRow
{
return $this->doFindOneBy($condition);
}
/**
* @throws \PSX\Sql\Exception\QueryException
*/
public function find(int $id) : ?\PSX\Sql\Tests\Generator\SqlTableTestRow
public function find(int $id): ?\PSX\Sql\Tests\Generator\SqlTableTestRow
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('id', $id);
Expand All @@ -147,7 +150,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
* @return array<\PSX\Sql\Tests\Generator\SqlTableTestRow>
* @throws \PSX\Sql\Exception\QueryException
*/
public function findById(int $value, ?int $startIndex = null, ?int $count = null, ?string $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null) : array
public function findById(int $value, ?int $startIndex = null, ?int $count = null, ?\PSX\Sql\Tests\Generator\SqlTableTestColumn $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null): array
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('id', $value);
Expand All @@ -156,7 +159,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\QueryException
*/
public function findOneById(int $value) : ?\PSX\Sql\Tests\Generator\SqlTableTestRow
public function findOneById(int $value): ?\PSX\Sql\Tests\Generator\SqlTableTestRow
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('id', $value);
Expand All @@ -165,7 +168,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function updateById(int $value, \PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function updateById(int $value, \PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('id', $value);
Expand All @@ -174,7 +177,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function deleteById(int $value) : int
public function deleteById(int $value): int
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('id', $value);
Expand All @@ -184,7 +187,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
* @return array<\PSX\Sql\Tests\Generator\SqlTableTestRow>
* @throws \PSX\Sql\Exception\QueryException
*/
public function findByTitle(string $value, ?int $startIndex = null, ?int $count = null, ?string $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null) : array
public function findByTitle(string $value, ?int $startIndex = null, ?int $count = null, ?\PSX\Sql\Tests\Generator\SqlTableTestColumn $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null): array
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->like('title', $value);
Expand All @@ -193,7 +196,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\QueryException
*/
public function findOneByTitle(string $value) : ?\PSX\Sql\Tests\Generator\SqlTableTestRow
public function findOneByTitle(string $value): ?\PSX\Sql\Tests\Generator\SqlTableTestRow
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->like('title', $value);
Expand All @@ -202,7 +205,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function updateByTitle(string $value, \PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function updateByTitle(string $value, \PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->like('title', $value);
Expand All @@ -211,7 +214,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function deleteByTitle(string $value) : int
public function deleteByTitle(string $value): int
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->like('title', $value);
Expand All @@ -221,7 +224,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
* @return array<\PSX\Sql\Tests\Generator\SqlTableTestRow>
* @throws \PSX\Sql\Exception\QueryException
*/
public function findByDate(\PSX\DateTime\LocalDateTime $value, ?int $startIndex = null, ?int $count = null, ?string $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null) : array
public function findByDate(\PSX\DateTime\LocalDateTime $value, ?int $startIndex = null, ?int $count = null, ?\PSX\Sql\Tests\Generator\SqlTableTestColumn $sortBy = null, ?\PSX\Sql\OrderBy $sortOrder = null): array
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('date', $value);
Expand All @@ -230,7 +233,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\QueryException
*/
public function findOneByDate(\PSX\DateTime\LocalDateTime $value) : ?\PSX\Sql\Tests\Generator\SqlTableTestRow
public function findOneByDate(\PSX\DateTime\LocalDateTime $value): ?\PSX\Sql\Tests\Generator\SqlTableTestRow
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('date', $value);
Expand All @@ -239,7 +242,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function updateByDate(\PSX\DateTime\LocalDateTime $value, \PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function updateByDate(\PSX\DateTime\LocalDateTime $value, \PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('date', $value);
Expand All @@ -248,7 +251,7 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function deleteByDate(\PSX\DateTime\LocalDateTime $value) : int
public function deleteByDate(\PSX\DateTime\LocalDateTime $value): int
{
$condition = \PSX\Sql\Condition::withAnd();
$condition->equals('date', $value);
Expand All @@ -257,42 +260,42 @@ class SqlTableTestTable extends \PSX\Sql\TableAbstract
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function create(\PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function create(\PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
return $this->doCreate($record->toRecord());
}
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function update(\PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function update(\PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
return $this->doUpdate($record->toRecord());
}
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function updateBy(\PSX\Sql\Condition $condition, \PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function updateBy(\PSX\Sql\Condition $condition, \PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
return $this->doUpdateBy($condition, $record->toRecord());
}
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function delete(\PSX\Sql\Tests\Generator\SqlTableTestRow $record) : int
public function delete(\PSX\Sql\Tests\Generator\SqlTableTestRow $record): int
{
return $this->doDelete($record->toRecord());
}
/**
* @throws \PSX\Sql\Exception\ManipulationException
*/
public function deleteBy(\PSX\Sql\Condition $condition) : int
public function deleteBy(\PSX\Sql\Condition $condition): int
{
return $this->doDeleteBy($condition);
}
/**
* @param array<string, mixed> $row
*/
protected function newRecord(array $row) : \PSX\Sql\Tests\Generator\SqlTableTestRow
protected function newRecord(array $row): \PSX\Sql\Tests\Generator\SqlTableTestRow
{
return \PSX\Sql\Tests\Generator\SqlTableTestRow::from($row);
}
Expand All @@ -313,49 +316,67 @@ class SqlTableTestRow implements \JsonSerializable, \PSX\Record\RecordableInterf
private ?int $id = null;
private ?string $title = null;
private ?\PSX\DateTime\LocalDateTime $date = null;
public function setId(int $id) : void
public function setId(int $id): void
{
$this->id = $id;
}
public function getId() : int
public function getId(): int
{
return $this->id;
return $this->id ?? throw new \PSX\Sql\Exception\NoValueAvailable('No value for required column "id" was provided');
}
public function setTitle(string $title) : void
public function setTitle(string $title): void
{
$this->title = $title;
}
public function getTitle() : string
public function getTitle(): string
{
return $this->title;
return $this->title ?? throw new \PSX\Sql\Exception\NoValueAvailable('No value for required column "title" was provided');
}
public function setDate(\PSX\DateTime\LocalDateTime $date) : void
public function setDate(\PSX\DateTime\LocalDateTime $date): void
{
$this->date = $date;
}
public function getDate() : \PSX\DateTime\LocalDateTime
public function getDate(): \PSX\DateTime\LocalDateTime
{
return $this->date;
return $this->date ?? throw new \PSX\Sql\Exception\NoValueAvailable('No value for required column "date" was provided');
}
public function toRecord() : \PSX\Record\RecordInterface
public function toRecord(): \PSX\Record\RecordInterface
{
/** @var \PSX\Record\Record<mixed> $record */
$record = new \PSX\Record\Record();
$record->put('id', $this->id);
$record->put('title', $this->title);
$record->put('date', $this->date);
return $record;
}
public function jsonSerialize() : object
public function jsonSerialize(): object
{
return (object) $this->toRecord()->getAll();
}
public static function from(array|\ArrayAccess $data) : self
public static function from(array|\ArrayAccess $data): self
{
$row = new self();
$row->id = $data['id'] ?? null;
$row->title = $data['title'] ?? null;
$row->date = isset($data['date']) ? \PSX\DateTime\LocalDateTime::from($data['date']) : null;
$row->id = isset($data['id']) && is_int($data['id']) ? $data['id'] : null;
$row->title = isset($data['title']) && is_string($data['title']) ? $data['title'] : null;
$row->date = isset($data['date']) && $data['date'] instanceof \DateTimeInterface ? \PSX\DateTime\LocalDateTime::from($data['date']) : null;
return $row;
}
}
```

## Column

The following is an example of a generated table column.

```php
<?php

namespace PSX\Sql\Tests\Generator;

enum SqlTableTestColumn : string implements \PSX\Sql\ColumnInterface
{
case ID = \PSX\Sql\Tests\Generator\SqlTableTestTable::COLUMN_ID;
case TITLE = \PSX\Sql\Tests\Generator\SqlTableTestTable::COLUMN_TITLE;
case DATE = \PSX\Sql\Tests\Generator\SqlTableTestTable::COLUMN_DATE;
}
```
32 changes: 32 additions & 0 deletions src/ColumnInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* PSX is an open source PHP framework to develop RESTful APIs.
* For the current version and information visit <https://phpsx.org>
*
* Copyright 2010-2023 Christoph Kappestein <christoph.kappestein@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace PSX\Sql;

/**
* Represents a column
*
* @author Christoph Kappestein <christoph.kappestein@gmail.com>
* @license http://www.apache.org/licenses/LICENSE-2.0
* @link https://phpsx.org
*/
interface ColumnInterface extends \BackedEnum
{
}
Loading