Skip to content

Commit 166591e

Browse files
committed
Adding Composite to inProgress instead of branch
1 parent 30fd341 commit 166591e

File tree

6 files changed

+180
-23
lines changed

6 files changed

+180
-23
lines changed

inProgress/Composite/Employee.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace SOB\Composite;
4+
5+
use Composite\DB\Attributes\Table;
6+
use Composite\DB\Attributes\PrimaryKey;
7+
use Composite\Sync\Attributes\Index;
8+
use Composite\Entity\AbstractEntity;
9+
10+
#[Table(connection: 'sqlite', name: 'employee')]
11+
#[Index(columns: ['city'], name: 'employee_city')]
12+
#[Index(columns: ['company'], name: 'employee_company')]
13+
#[Index(columns: ['first_name'], name: 'employee_first_name')]
14+
#[Index(columns: ['last_name'], name: 'employee_last_name')]
15+
#[Index(columns: ['zip_postal_code'], name: 'employee_zip_postal_code')]
16+
#[Index(columns: ['state_province'], name: 'employee_state_province')]
17+
class Employee extends AbstractEntity
18+
{
19+
#[PrimaryKey(autoIncrement: true)]
20+
public int $employee_id;
21+
22+
public function __construct(
23+
public ?string $company = null,
24+
public ?string $last_name = null,
25+
public ?string $first_name = null,
26+
public ?string $email_address = null,
27+
public ?string $job_title = null,
28+
public ?string $business_phone = null,
29+
public ?string $home_phone = null,
30+
public ?string $mobile_phone = null,
31+
public ?string $fax_number = null,
32+
public ?string $address = null,
33+
public ?string $city = null,
34+
public ?string $state_province = null,
35+
public ?string $zip_postal_code = null,
36+
public ?string $country_region = null,
37+
public ?string $web_page = null,
38+
public ?string $notes = null,
39+
public ?string $attachments = null,
40+
) {}
41+
}

inProgress/Composite/UsersTable.php renamed to inProgress/Composite/EmployeeTable.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
<?php declare(strict_types=1);
22

3-
namespace App\Composite;
3+
namespace SOB\Composite;
44

55
use Composite\DB\AbstractTable;
66
use Composite\DB\TableConfig;
77

8-
class UsersTable extends AbstractTable
8+
class EmployeeTable extends AbstractTable
99
{
1010
protected function getConfig(): TableConfig
1111
{
12-
return TableConfig::fromEntitySchema(User::schema());
12+
return TableConfig::fromEntitySchema(Employee::schema());
1313
}
1414

15-
public function findOne(int $id): ?User
15+
public function findOne(int $id): ?Employee
1616
{
1717
return $this->_findByPk($id);
1818
}

inProgress/Composite/TestsOld.php

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

inProgress/Composite/User.php

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
include '../../../vendor/autoload.php';
4+
5+
use Composite\Sync\Commands;
6+
use Symfony\Component\Console\Application;
7+
8+
$dir = __DIR__ . '/config.php';
9+
putenv('CONNECTIONS_CONFIG_FILE=' . $dir);
10+
11+
$app = new Application();
12+
$app->addCommands([
13+
new Commands\GenerateEntityCommand(), //to initialize $dbManager see example above
14+
new Commands\GenerateTableCommand(),
15+
]);
16+
$app->run();
Binary file not shown.

0 commit comments

Comments
 (0)