Skip to content

Commit 0240ab6

Browse files
committed
Adding in progress stuff
1 parent 108e686 commit 0240ab6

File tree

247 files changed

+72034
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

247 files changed

+72034
-0
lines changed

inProgress/Composite/Tests.php

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace SOB\Composite;
4+
5+
/**
6+
* This won't work because the attribute hard codes the DB connection to the table definition. Unbelievable shit!
7+
*/
8+
class Tests extends \SOB\Test
9+
{
10+
private Doctrine\DBAL\Connection $connection;
11+
12+
public function closeConnection() : void
13+
{
14+
$this->pdo = null;
15+
}
16+
17+
/**
18+
* class must delete one record with id=$id
19+
*/
20+
public function delete(int $id) : bool
21+
{
22+
$table = new \SOB\Composite\Record\EmployeeTable();
23+
$employee = $table->findOne($id);
24+
25+
if (! $employee)
26+
{
27+
return false;
28+
}
29+
30+
$table->delete($employee);
31+
32+
return true;
33+
}
34+
35+
/**
36+
* Initialize Responsibilities:
37+
*
38+
* * Initialize the orm
39+
* * open the database
40+
* * initialize the database schema
41+
*/
42+
public function init(\SOB\Configuration $config) : static
43+
{
44+
$connectionParams = [
45+
'dbname' => $config->getDatabase(),
46+
'user' => $config->getUser(),
47+
'password' => $config->getPassword(),
48+
'host' => $config->getHost(),
49+
'driver' => $config->getDriver(),
50+
];
51+
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams);
52+
53+
if (\str_contains($config->getDriver(), 'sqlite'))
54+
{
55+
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sqlite');
56+
\fclose(\fopen($config->getNamespace() . '.sqlite', 'w'));
57+
}
58+
else
59+
{
60+
$lines = \file(__DIR__ . '/../../northwind/northwind-schema.sql');
61+
}
62+
63+
$sql = '';
64+
foreach ($lines as $line)
65+
{
66+
// Ignoring comments from the SQL script
67+
if (\str_starts_with((string)$line, '--') || '' == $line)
68+
{
69+
continue;
70+
}
71+
72+
$sql .= $line;
73+
74+
if (\str_ends_with(\trim((string)$line), ';'))
75+
{
76+
$this->connection->executeStatement($sql);
77+
$sql = '';
78+
}
79+
} // end foreach
80+
81+
return $this;
82+
}
83+
84+
/**
85+
* class must insert one record with id=$id
86+
*
87+
* @return int $id inserted
88+
*/
89+
public function insert(int $id) : int
90+
{
91+
$employee = new \SOB\Composite\Record\Employee();
92+
$employee->id = $id;
93+
$employee->company = "Company {$id}";
94+
$employee->last_name = "Last {$id}";
95+
$employee->first_name = "First {$id}";
96+
97+
$table = new \SOB\Composite\Record\EmployeeTable();
98+
$table->save($employee);
99+
100+
return $id;
101+
}
102+
103+
/**
104+
* class must read and return one record with id=$id or null on no matching record
105+
*/
106+
public function read(int $id) : ?object
107+
{
108+
$table = new \SOB\Composite\Record\EmployeeTable();
109+
$employee = $table->findOne($id);
110+
111+
if (! $employee)
112+
{
113+
return null;
114+
}
115+
116+
return $employee;
117+
}
118+
119+
/**
120+
* class must update one record with id=$id to have $to in the data
121+
*/
122+
public function update(int $id, int $to) : bool
123+
{
124+
$table = new \SOB\Composite\Record\EmployeeTable();
125+
$employee = $table->findOne($id);
126+
127+
if (! $employee)
128+
{
129+
return false;
130+
}
131+
132+
$employee->last_name = "Updated {$to}";
133+
$table->save($employee);
134+
135+
return true;
136+
}
137+
}

inProgress/Composite/User.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
namespace App\Composite;
3+
4+
use Composite\DB\Attributes\{Table, PrimaryKey};
5+
use Composite\Entity\AbstractEntity;
6+
7+
#[Table(connection: 'mysql', name: 'Users')]
8+
class User extends AbstractEntity
9+
{
10+
#[PrimaryKey(autoIncrement: true)]
11+
public readonly int $id;
12+
13+
public function __construct(
14+
public string $name,
15+
public int $age,
16+
public float $microtime,
17+
public \DateTimeImmutable $created_at = new \DateTimeImmutable(),
18+
) {}
19+
}

inProgress/Composite/UsersTable.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Composite;
4+
5+
use Composite\DB\AbstractTable;
6+
use Composite\DB\TableConfig;
7+
8+
class UsersTable extends AbstractTable
9+
{
10+
protected function getConfig(): TableConfig
11+
{
12+
return TableConfig::fromEntitySchema(User::schema());
13+
}
14+
15+
public function findOne(int $id): ?User
16+
{
17+
return $this->_findByPk($id);
18+
}
19+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
return [
4+
'mysql' => [
5+
'driver' => 'pdo_mysql',
6+
'dbname' => $_ENV['MYSQL_DB_NAME'],
7+
'user' => $_ENV['MYSQL_USERNAME'],
8+
'password' => $_ENV['MYSQL_PASSWORD'],
9+
'host' => $_ENV['MYSQL_HOST'],
10+
'port' => $_ENV['MYSQL_HOST_PORT'],
11+
],
12+
];
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
<?php
2+
3+
include __DIR__ . '/../../../vendor/autoload.php';
4+
5+
$pdo = new \PHPFUI\ORM\PDOInstance('sqlite::memory:');
6+
\PHPFUI\ORM::addConnection($pdo);
7+
8+
$lines = \file(__DIR__ . '/../../../northwind/northwind-schema.sqlite');
9+
10+
$sql = '';
11+
12+
foreach ($lines as $line)
13+
{
14+
15+
// Ignoring comments from the SQL script
16+
if (\str_starts_with((string)$line, '--') || '' == $line)
17+
{
18+
continue;
19+
}
20+
21+
$sql .= $line;
22+
23+
if (\str_ends_with(\trim((string)$line), ';'))
24+
{
25+
\PHPFUI\ORM::execute($sql);
26+
$lastError = \PHPFUI\ORM::getLastError();
27+
28+
if ($lastError)
29+
{
30+
throw new \Exception($lastError . ' SQL: ' . $sql);
31+
}
32+
$sql = '';
33+
}
34+
} // end foreach
35+
36+
echo "Generate Record Models\n\n";
37+
38+
$tables = \PHPFUI\ORM::getTables();
39+
40+
if (! \count($tables))
41+
{
42+
echo "No tables found. Check your database configuration settings.\n";
43+
44+
exit;
45+
}
46+
47+
foreach ($tables as $table)
48+
{
49+
\generate($table);
50+
echo "{$table}\n";
51+
}
52+
53+
function generate(string $table) : void
54+
{
55+
$template = \file_get_contents(__DIR__ . '/model.template');
56+
$parameters['~TABLE~'] = $table;
57+
$class = \PHPFUI\ORM::getBaseClassName($table);
58+
$parameters['~CLASS~'] = $class;
59+
$fields = \PHPFUI\ORM::describeTable($table);
60+
$autoIncrement = false;
61+
$commentedFields = [];
62+
63+
$docBlock = '';
64+
65+
foreach ($fields as $field)
66+
{
67+
if ($field->autoIncrement)
68+
{
69+
$autoIncrement = true;
70+
}
71+
72+
if ($field->primaryKey)
73+
{
74+
$parameters['~PRIMARY_KEY~'] = $field->name;
75+
}
76+
$comment = \getComment($field, $commentedFields);
77+
78+
if ($comment)
79+
{
80+
$docBlock .= " * @property {$comment}\n";
81+
}
82+
}
83+
84+
$parameters['~AUTO_INCREMENT~'] = $autoIncrement ? 'true' : 'false';
85+
$parameters['~DOC_BLOCK~'] = $docBlock;
86+
87+
$fileName = __DIR__ . '/../Model/' . $class . '.php';
88+
89+
foreach ($parameters as $key => $value)
90+
{
91+
$template = \str_replace($key, $value, $template);
92+
}
93+
94+
\file_put_contents($fileName, $template);
95+
}
96+
97+
/**
98+
* @param array<string,true> &$commentedFields
99+
*/
100+
function getComment(\PHPFUI\ORM\Schema\Field $field, array &$commentedFields) : ?string
101+
{
102+
$fieldName = $field->name;
103+
104+
if (isset($commentedFields[$fieldName]))
105+
{
106+
return null;
107+
}
108+
$commentedFields[$fieldName] = true;
109+
$mySQLType = \str_replace("'", '', $field->type);
110+
$phpType = \getPHPType($mySQLType);
111+
$null = $field->nullable ? '?' : '';
112+
113+
$block = $null . $phpType . ' $' . $fieldName . ' MySQL type ' . $mySQLType;
114+
$commentedFields[$fieldName] = true;
115+
116+
return $block;
117+
}
118+
119+
function getPHPType(string $type) : string
120+
{
121+
$start = \strpos($type, '(');
122+
123+
if (false !== $start)
124+
{
125+
$type = \substr($type, 0, $start);
126+
}
127+
128+
static $types = [
129+
'integer' => 'int',
130+
'int' => 'int',
131+
'int unsigned' => 'int',
132+
'smallint' => 'int',
133+
'tinyint' => 'int',
134+
'mediumint' => 'int',
135+
'bigint' => 'int',
136+
'decimal' => 'float',
137+
'numeric' => 'float',
138+
'float' => 'float',
139+
'double' => 'float',
140+
'bit' => 'bool',
141+
'year' => 'int',
142+
];
143+
144+
return $types[\strtolower($type)] ?? 'string';
145+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
<?php declare(strict_types=1);
3+
namespace SOB\Composite\Model;
4+
5+
use Composite\DB\Attributes\{Table, PrimaryKey};
6+
use Composite\Entity\AbstractEntity;
7+
use Composite\DB\AbstractTable;
8+
use Composite\DB\TableConfig;
9+
10+
/**
11+
* This code is automatically generated. See SOB\Composite\scaffolding\generateModels.php. Do not update by hand.
12+
*
13+
~DOC_BLOCK~ */
14+
#[Table(connection: 'mysql', name: '~TABLE~')]
15+
class ~CLASS~ extends AbstractEntity
16+
{
17+
#[PrimaryKey(autoIncrement: ~AUTO_INCREMENT~)]
18+
public readonly int $~PRIMARY_KEY~;
19+
20+
public function __construct(
21+
~PARAMETERS~
22+
) {}
23+
}
24+
25+
class ~CLASS~Table extends AbstractTable
26+
{
27+
protected function getConfig(): TableConfig
28+
{
29+
return TableConfig::fromEntitySchema(~CLASS~::schema());
30+
}
31+
32+
public function findOne(int $id): ?~CLASS~
33+
{
34+
return $this->_findByPk($id);
35+
}
36+
}
37+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace SOB\Nextras\Model;
4+
5+
use Nextras\Orm\Entity\Entity;
6+
7+
abstract class AbstractEntity extends Entity
8+
{
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace SOB\Nextras\Model;
4+
5+
abstract class AbstractFacade
6+
{
7+
}

0 commit comments

Comments
 (0)