Skip to content

Commit

Permalink
[update] - added database function declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
iloElias committed Sep 29, 2024
1 parent 153f28e commit 64e3f26
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,6 @@ foreach ($queries as $query) {
}
```

Sure! Here are the explanations for the available commands that can be included in the `README.md`:

### Commands

Maestro provides several commands to help you manage and synchronize your database schema. Here are the available commands:
Expand Down
10 changes: 10 additions & 0 deletions example/Hr.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@

namespace Maestro\Example;
use Ilias\Maestro\Abstract\Schema;
use Ilias\Maestro\Types\Postgres;

final class Hr extends Schema
{
public User $user;
public AuthCode $authCode;

public function __construct()
{
self::declareFunction(
'generate_four_digit_auth_code',
Postgres::TEXT,
'CREATE OR REPLACE FUNCTION generate_four_digit_auth_code() RETURNS TEXT AS $$ BEGIN RETURN CAST(FLOOR(1000 + RANDOM() * 9000) AS TEXT); END; $$ LANGUAGE plpgsql;'
);
}
}
12 changes: 12 additions & 0 deletions src/Abstract/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

namespace Ilias\Maestro\Abstract;

use Ilias\Maestro\Database\DatabaseFunction;
use Ilias\Maestro\Utils\Utils;

abstract class Schema extends \stdClass
{
use Sanitizable;
private static array $functions = [];
public static function getSchemaName(): string
{
return static::class;
Expand All @@ -27,6 +29,16 @@ public static function getTables(): array
return $tables;
}

public static function declareFunction(string $name, string $returnType, string $sqlDefinition)
{
self::$functions[$name] = new DatabaseFunction($name, $returnType, $sqlDefinition);
}

public static function getFunctions(): array
{
return self::$functions;
}

public static function dumpSchema(): array
{
$tablesMap = [];
Expand Down
22 changes: 22 additions & 0 deletions src/Abstract/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Ilias\Maestro\Abstract;

use Exception;
use Ilias\Maestro\Core\Maestro;
use Ilias\Maestro\Database\Select;
use Ilias\Maestro\Utils\Utils;

Expand Down Expand Up @@ -59,6 +60,27 @@ public static function tableColumns(): array
return $columns;
}

public static function getUniqueColumns(): array
{
return self::tableColumnsProperties(Maestro::DOC_UNIQUE);
}

public static function tableColumnsProperties(string $atDocClause = ''): array
{
$reflection = new \ReflectionClass(static::class);
$properties = $reflection->getProperties(\ReflectionProperty::IS_PUBLIC);
$uniqueColumns = [];

foreach ($properties as $property) {
$docComment = $property->getDocComment();
if ($docComment && strpos($docComment, $atDocClause) !== false) {
$uniqueColumns[] = $property->getName();
}
}

return $uniqueColumns;
}

final public static function tableIdentifier(): array
{
foreach (static::tableColumns() as $name => $type) {
Expand Down
23 changes: 22 additions & 1 deletion src/Core/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,18 @@ public function createDatabase(Database $database, bool $executeOnComplete = tru
$schemasSql = [];
$tablesSql = [];
$constraintsSql = [];
$functionsSql = [];
$schemas = $database::getSchemas();

foreach ($schemas as $schemaClass) {
$schemasSql[] = $this->createSchema($schemaClass);
[$create, $constraints] = $this->createSchemaTables($schemaClass);
$tablesSql = array_merge($tablesSql, $create);
$constraintsSql = array_merge($constraintsSql, ...$constraints);
$functionsSql = array_merge($functionsSql, $this->createSchemaFunctions($schemaClass));
}

$sql = array_merge($schemasSql, $tablesSql, $constraintsSql);
$sql = array_merge($schemasSql, $tablesSql, $constraintsSql, $functionsSql);
if ($executeOnComplete) {
foreach ($sql as $query) {
$this->executeQuery($this->pdo, $query);
Expand Down Expand Up @@ -177,6 +179,25 @@ public function createTable(string $table): string
return $query;
}

/**
* Create functions for a schema.
*
* @param string|Schema $schema
* @return array
*/
public function createSchemaFunctions(string|Schema $schema): array
{
$functionsSql = [];
new $schema();
$functions = $schema::getFunctions();

foreach ($functions as $function) {
$functionsSql[] = $function->getSqlDefinition();
}

return $functionsSql;
}

/**
* Get the column type.
*
Expand Down
32 changes: 32 additions & 0 deletions src/Database/DatabaseFunction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Ilias\Maestro\Database;

class DatabaseFunction
{
private string $name;
private string $returnType;
private string $sqlDefinition;

public function __construct(string $name, string $returnType, string $sqlDefinition)
{
$this->name = $name;
$this->returnType = $returnType;
$this->sqlDefinition = $sqlDefinition;
}

public function getName(): string
{
return $this->name;
}

public function getReturnType(): string
{
return $this->returnType;
}

public function getSqlDefinition(): string
{
return $this->sqlDefinition;
}
}
3 changes: 3 additions & 0 deletions test.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

use Ilias\Maestro\Core\Manager;
use Ilias\Maestro\Types\Timestamp;
use Maestro\Example\MaestroDb;
use Maestro\Example\User;

require_once("./vendor/autoload.php");

$coreDatabase = new Manager();
$agrofastDB = new MaestroDb();
// new User("nickname", "email", "password", true, new Timestamp());

print implode("\n", $coreDatabase->createDatabase($agrofastDB, false)) . "\n";

0 comments on commit 64e3f26

Please sign in to comment.