Skip to content

Commit

Permalink
Update CodeGen Templates
Browse files Browse the repository at this point in the history
  • Loading branch information
byjg committed Sep 17, 2024
1 parent cef8d9c commit 6759bae
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 107 deletions.
10 changes: 8 additions & 2 deletions builder/Scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ public function runCodeGenerator(array $arguments)

$tableDefinition = $dbDriver->getIterator("EXPLAIN " . strtolower($table))->toArray();
$tableIndexes = $dbDriver->getIterator("SHOW INDEX FROM " . strtolower($table))->toArray();
$autoIncrement = false;

// Convert DB Types to PHP Types
foreach ($tableDefinition as $key => $field) {
Expand All @@ -230,6 +231,10 @@ public function runCodeGenerator(array $arguments)
return strtoupper($matches[1]);
}, $field['field']);

if ($field['extra'] == 'auto_increment') {
$autoIncrement = true;
}

switch ($type) {
case 'int':
case 'tinyint':
Expand Down Expand Up @@ -292,15 +297,15 @@ public function runCodeGenerator(array $arguments)
}
}

// Create an array with non nullable fields but primary keys
// Create an array with non-nullable fields but primary keys
$nonNullableFields = [];
foreach ($tableDefinition as $field) {
if ($field['null'] == 'NO' && $field['key'] != 'PRI') {
$nonNullableFields[] = $field["property"];
}
}

// Create an array with non nullable fields but primary keys
// Create an array with non-nullable fields but primary keys
foreach ($tableIndexes as $key => $field) {
$tableIndexes[$key]['camelColumnName'] = preg_replace_callback('/_(.?)/', function($match) {
return strtoupper($match[1]);
Expand All @@ -309,6 +314,7 @@ public function runCodeGenerator(array $arguments)

$data = [
'namespace' => 'RestReferenceArchitecture',
'autoIncrement' => $autoIncrement ? 'yes' : 'no',
'restTag' => ucwords(explode('_', strtolower($table))[0]),
'restPath' => str_replace('_', '/', strtolower($table)),
'className' => preg_replace_callback('/(?:^|_)(.?)/', function($match) {
Expand Down
22 changes: 14 additions & 8 deletions src/Model/Dummy.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,49 @@
<?php

namespace RestReferenceArchitecture\Model;

use ByJG\MicroOrm\Attributes\FieldAttribute;
use ByJG\MicroOrm\Attributes\TableAttribute;
use OpenApi\Attributes as OA;

/**
* Class Dummy
* @package RestReferenceArchitecture\Model
*/
#[OA\Schema(required: ["id", "field"], type: "object", xml: new OA\Xml(name: "Dummy"))]
#[TableAttribute("dummy")]
class Dummy
{

/**
* @var int|null
*/
#[OA\Property(type: "integer", format: "int32")]
protected ?int $id = null;
#[FieldAttribute(primaryKey: true, fieldName: "id")]
protected int|null $id = null;

/**
* @var string|null
*/
#[OA\Property(type: "string", format: "string")]
protected ?string $field = null;
#[FieldAttribute(fieldName: "field")]
protected string|null $field = null;



/**
* @return int|null
*/
public function getId(): ?int
public function getId(): int|null
{
return $this->id;
}

/**
* @param int|null $id
* @return Dummy
* @return $this
*/
public function setId(?int $id): Dummy
public function setId(int|null $id): static
{
$this->id = $id;
return $this;
Expand All @@ -46,16 +52,16 @@ public function setId(?int $id): Dummy
/**
* @return string|null
*/
public function getField(): ?string
public function getField(): string|null
{
return $this->field;
}

/**
* @param string|null $field
* @return Dummy
* @return $this
*/
public function setField(?string $field): Dummy
public function setField(string|null $field): static
{
$this->field = $field;
return $this;
Expand Down
37 changes: 23 additions & 14 deletions src/Model/DummyHex.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
<?php

namespace RestReferenceArchitecture\Model;

use ByJG\MicroOrm\Attributes\FieldAttribute;
use ByJG\MicroOrm\Attributes\FieldUuidAttribute;
use ByJG\MicroOrm\Attributes\TableMySqlUuidPKAttribute;
use ByJG\MicroOrm\Literal\HexUuidLiteral;
use OpenApi\Attributes as OA;

/**
* Class DummyHex
* @package RestReferenceArchitecture\Model
*/
#[OA\Schema(required: ["id", "field"], type: "object", xml: new OA\Xml(name: "DummyHex"))]
#[TableMySqlUuidPKAttribute("dummyhex")]
class DummyHex
{

/**
* @var string|null
*/
#[OA\Property(type: "string", format: "string")]
protected ?string $id = null;
#[FieldUuidAttribute(primaryKey: true, fieldName: "id")]
protected string|HexUuidLiteral|null $id = null;

/**
* @var string|null
*/
#[OA\Property(type: "string", format: "string", nullable: true)]
protected ?string $uuid = null;
#[FieldAttribute(fieldName: "uuid", syncWithDb: false)]
protected string|null $uuid = null;

/**
* @var string|null
*/
#[OA\Property(type: "string", format: "string")]
protected ?string $field = null;
#[FieldAttribute(fieldName: "field")]
protected string|null $field = null;



/**
* @return string|null
* @return string|HexUuidLiteral|null
*/
public function getId(): ?string
public function getId(): string|HexUuidLiteral|null
{
return $this->id;
}

/**
* @param string|null $id
* @return DummyHex
* @param string|HexUuidLiteral|null $id
* @return $this
*/
public function setId(?string $id): DummyHex
public function setId(string|HexUuidLiteral|null $id): static
{
$this->id = $id;
return $this;
Expand All @@ -52,16 +61,16 @@ public function setId(?string $id): DummyHex
/**
* @return string|null
*/
public function getUuid(): ?string
public function getUuid(): string|null
{
return $this->uuid;
}

/**
* @param string|null $uuid
* @return DummyHex
* @return $this
*/
public function setUuid(?string $uuid): DummyHex
public function setUuid(string|null $uuid): static
{
$this->uuid = $uuid;
return $this;
Expand All @@ -70,16 +79,16 @@ public function setUuid(?string $uuid): DummyHex
/**
* @return string|null
*/
public function getField(): ?string
public function getField(): string|null
{
return $this->field;
}

/**
* @param string|null $field
* @return DummyHex
* @return $this
*/
public function setField(?string $field): DummyHex
public function setField(string|null $field): static
{
$this->field = $field;
return $this;
Expand Down
29 changes: 6 additions & 23 deletions src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use ByJG\Config\Exception\KeyNotFoundException;
use ByJG\MicroOrm\Exception\OrmBeforeInvalidException;
use ByJG\MicroOrm\Exception\OrmInvalidFieldsException;
use ByJG\MicroOrm\Exception\RepositoryReadOnlyException;
use ByJG\MicroOrm\Exception\UpdateConstraintException;
use ByJG\MicroOrm\FieldMapping;
use ByJG\MicroOrm\Literal\HexUuidLiteral;
use ByJG\MicroOrm\Literal\Literal;
Expand All @@ -36,7 +38,7 @@ abstract class BaseRepository
*/
public function get($itemId)
{
return $this->repository->get($this->prepareUuidQuery($itemId));
return $this->repository->get(HexUuidLiteral::create($itemId));
}

public function getMapper()
Expand All @@ -55,28 +57,6 @@ public function getByQuery($query)
return $this->repository->getByQuery($query);
}

protected function prepareUuidQuery($itemId)
{
$result = [];
foreach ((array)$itemId as $item) {
if ($item instanceof Literal) {
$result[] = $item;
continue;
}
$hydratedItem = preg_replace('/[^0-9A-F\-]/', '', $item);
if (preg_match("/^\w{8}-?\w{4}-?\w{4}-?\w{4}-?\w{12}$/", $hydratedItem)) {
$result[] = new HexUuidLiteral($hydratedItem);
} else {
$result[] = $item;
}
}

if (count($result) == 1) {
return $result[0];
}
return $result;
}

/**
* @param int|null $page
* @param int $size
Expand Down Expand Up @@ -208,11 +188,14 @@ protected function setClosureFixBinaryUUID(?Mapper $mapper, $binPropertyName = '

/**
* @param $model
* @param UpdateConstraint|null $updateConstraint
* @return mixed
* @throws InvalidArgumentException
* @throws OrmBeforeInvalidException
* @throws OrmInvalidFieldsException
* @throws \ByJG\MicroOrm\Exception\InvalidArgumentException
* @throws RepositoryReadOnlyException
* @throws UpdateConstraintException
*/
public function save($model, ?UpdateConstraint $updateConstraint = null)
{
Expand Down
21 changes: 5 additions & 16 deletions src/Repository/DummyHexRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace RestReferenceArchitecture\Repository;

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\MicroOrm\FieldMapping;
use ByJG\MicroOrm\Mapper;
use ByJG\MicroOrm\MapperClosure;
use ByJG\MicroOrm\Exception\OrmModelInvalidException;
use ByJG\MicroOrm\Repository;
use ReflectionException;
use RestReferenceArchitecture\Model\DummyHex;

class DummyHexRepository extends BaseRepository
Expand All @@ -15,22 +14,12 @@ class DummyHexRepository extends BaseRepository
* DummyHexRepository constructor.
*
* @param DbDriverInterface $dbDriver
*
* @throws OrmModelInvalidException
* @throws ReflectionException
*/
public function __construct(DbDriverInterface $dbDriver)
{
$mapper = new Mapper(
DummyHex::class,
'dummyhex',
'id'
);
$mapper->withPrimaryKeySeedFunction(BaseRepository::getClosureNewUUID());


$this->setClosureFixBinaryUUID($mapper);
$mapper->addFieldMapping(FieldMapping::create('uuid')->withFieldName('uuid')->withUpdateFunction(MapperClosure::readonly()));

$this->repository = new Repository($dbDriver, $mapper);
$this->repository = new Repository($dbDriver, DummyHex::class);
}


Expand Down
20 changes: 5 additions & 15 deletions src/Repository/DummyRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace RestReferenceArchitecture\Repository;

use ByJG\AnyDataset\Db\DbDriverInterface;
use ByJG\MicroOrm\Mapper;
use ByJG\MicroOrm\Exception\OrmModelInvalidException;
use ByJG\MicroOrm\Query;
use ByJG\MicroOrm\Repository;
use ReflectionException;
use RestReferenceArchitecture\Model\Dummy;

class DummyRepository extends BaseRepository
Expand All @@ -14,23 +15,12 @@ class DummyRepository extends BaseRepository
* DummyRepository constructor.
*
* @param DbDriverInterface $dbDriver
*
* @throws OrmModelInvalidException
* @throws ReflectionException
*/
public function __construct(DbDriverInterface $dbDriver)
{
$mapper = new Mapper(
Dummy::class,
'dummy',
'id'
);
// $mapper->withPrimaryKeySeedFunction(BaseRepository::getClosureNewUUID());


// Table UUID Definition
// $this->setClosureFixBinaryUUID($mapper);


$this->repository = new Repository($dbDriver, $mapper);
$this->repository = new Repository($dbDriver, Dummy::class);
}


Expand Down
Loading

0 comments on commit 6759bae

Please sign in to comment.