Skip to content

Commit c4e2738

Browse files
committed
update
1 parent e2be486 commit c4e2738

File tree

8 files changed

+146
-67
lines changed

8 files changed

+146
-67
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ These are all the configuration options and their default value between brackets
5858
- "password": Password of the user connecting to the database (no default)
5959
- "database": Database the connecting is made to (no default)
6060
- "tables": Comma separated list of tables to publish (defaults to 'all')
61-
- "initcommand": Command to execute on init connection.
61+
- "command": SQL command to execute on initializing the database connection (none)
6262
- "mapping": Comma separated list of table/column mappings (no mappping)
6363
- "middlewares": List of middlewares to load (`cors`)
6464
- "controllers": List of controllers to load (`records,geojson,openapi,status`)

api.include.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5555,6 +5555,7 @@ class GenericDB
55555555
private $address;
55565556
private $port;
55575557
private $database;
5558+
private $command;
55585559
private $tables;
55595560
private $mapping;
55605561
private $username;
@@ -5585,22 +5586,30 @@ private function getCommands(): array
55855586
{
55865587
switch ($this->driver) {
55875588
case 'mysql':
5588-
return [
5589+
$commands = [
55895590
'SET SESSION sql_warnings=1;',
55905591
'SET NAMES utf8mb4;',
55915592
'SET SESSION sql_mode = "ANSI,TRADITIONAL";',
55925593
];
5594+
break;
55935595
case 'pgsql':
5594-
return [
5596+
$commands = [
55955597
"SET NAMES 'UTF8';",
55965598
];
5599+
break;
55975600
case 'sqlsrv':
5598-
return [];
5601+
$commands = [];
5602+
break;
55995603
case 'sqlite':
5600-
return [
5604+
$commands = [
56015605
'PRAGMA foreign_keys = on;',
56025606
];
5607+
break;
56035608
}
5609+
if ($this->command != '') {
5610+
$commands[] = $this->command;
5611+
}
5612+
return $commands;
56045613
}
56055614

56065615
private function getOptions(): array
@@ -5647,20 +5656,21 @@ private function initPdo(): bool
56475656
return $result;
56485657
}
56495658

5650-
public function __construct(string $driver, string $address, int $port, string $database, array $tables, array $mapping, string $username, string $password)
5659+
public function __construct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password)
56515660
{
56525661
$this->driver = $driver;
56535662
$this->address = $address;
56545663
$this->port = $port;
56555664
$this->database = $database;
5665+
$this->command = $command;
56565666
$this->tables = $tables;
56575667
$this->mapping = $mapping;
56585668
$this->username = $username;
56595669
$this->password = $password;
56605670
$this->initPdo();
56615671
}
56625672

5663-
public function reconstruct(string $driver, string $address, int $port, string $database, array $tables, array $mapping, string $username, string $password): bool
5673+
public function reconstruct(string $driver, string $address, int $port, string $database, string $command, array $tables, array $mapping, string $username, string $password): bool
56645674
{
56655675
if ($driver) {
56665676
$this->driver = $driver;
@@ -5674,6 +5684,9 @@ public function reconstruct(string $driver, string $address, int $port, string $
56745684
if ($database) {
56755685
$this->database = $database;
56765686
}
5687+
if ($command) {
5688+
$this->command = $command;
5689+
}
56775690
if ($tables) {
56785691
$this->tables = $tables;
56795692
}
@@ -8832,6 +8845,15 @@ private function getDatabase(): string
88328845
return '';
88338846
}
88348847

8848+
private function getCommand(): string
8849+
{
8850+
$commandHandler = $this->getProperty('commandHandler', '');
8851+
if ($commandHandler) {
8852+
return call_user_func($commandHandler);
8853+
}
8854+
return '';
8855+
}
8856+
88358857
private function getTables(): array
88368858
{
88378859
$tablesHandler = $this->getProperty('tablesHandler', '');
@@ -8874,12 +8896,13 @@ public function process(ServerRequestInterface $request, RequestHandlerInterface
88748896
$address = $this->getAddress();
88758897
$port = $this->getPort();
88768898
$database = $this->getDatabase();
8899+
$command = $this->getCommand();
88778900
$tables = $this->getTables();
88788901
$mapping = $this->getMapping();
88798902
$username = $this->getUsername();
88808903
$password = $this->getPassword();
8881-
if ($driver || $address || $port || $database || $tables || $mapping || $username || $password) {
8882-
$this->db->reconstruct($driver, $address, $port, $database, $tables, $mapping, $username, $password);
8904+
if ($driver || $address || $port || $database || $command || $tables || $mapping || $username || $password) {
8905+
$this->db->reconstruct($driver, $address, $port, $database, $command, $tables, $mapping, $username, $password);
88838906
}
88848907
return $next->handle($request);
88858908
}
@@ -11529,6 +11552,7 @@ public function __construct(Config $config)
1152911552
$config->getAddress(),
1153011553
$config->getPort(),
1153111554
$config->getDatabase(),
11555+
$config->getCommand(),
1153211556
$config->getTables(),
1153311557
$config->getMapping(),
1153411558
$config->getUsername(),
@@ -11601,7 +11625,7 @@ public function __construct(Config $config)
1160111625
case 'json':
1160211626
new JsonMiddleware($router, $responder, $properties);
1160311627
break;
11604-
}
11628+
}
1160511629
}
1160611630
foreach ($config->getControllers() as $controller) {
1160711631
switch ($controller) {
@@ -11627,7 +11651,7 @@ public function __construct(Config $config)
1162711651
break;
1162811652
case 'status':
1162911653
new StatusController($router, $responder, $cache, $db);
11630-
break;
11654+
break;
1163111655
}
1163211656
}
1163311657
foreach ($config->getCustomControllers() as $className) {
@@ -11714,6 +11738,7 @@ class Config
1171411738
'username' => '',
1171511739
'password' => '',
1171611740
'database' => '',
11741+
'command' => '',
1171711742
'tables' => '',
1171811743
'mapping' => '',
1171911744
'middlewares' => 'cors,errors',
@@ -11781,15 +11806,15 @@ private function applyEnvironmentVariables(array $values, string $prefix = 'PHP_
1178111806
$suffix = strtoupper(preg_replace('/(?<!^)[A-Z]/', '_$0', str_replace('.', '_', $key)));
1178211807
$newPrefix = $prefix . "_" . $suffix;
1178311808
if (is_array($value)) {
11784-
$newPrefix = str_replace('PHP_CRUD_API_MIDDLEWARES_','PHP_CRUD_API_',$newPrefix);
11809+
$newPrefix = str_replace('PHP_CRUD_API_MIDDLEWARES_', 'PHP_CRUD_API_', $newPrefix);
1178511810
$result[$key] = $this->applyEnvironmentVariables($value, $newPrefix);
1178611811
} else {
1178711812
$result[$key] = getenv($newPrefix, true) ?: $value;
1178811813
}
1178911814
}
1179011815
return $result;
1179111816
}
11792-
11817+
1179311818
public function __construct(array $values)
1179411819
{
1179511820
$driver = $this->getDefaultDriver($values);
@@ -11860,15 +11885,22 @@ public function getDatabase(): string
1186011885
return $this->values['database'];
1186111886
}
1186211887

11888+
public function getCommand(): string
11889+
{
11890+
return $this->values['command'];
11891+
}
11892+
1186311893
public function getTables(): array
1186411894
{
1186511895
return array_filter(array_map('trim', explode(',', $this->values['tables'])));
1186611896
}
1186711897

1186811898
public function getMapping(): array
1186911899
{
11870-
$mapping = array_map(function($v){ return explode('=', $v); }, array_filter(array_map('trim', explode(',', $this->values['mapping']))));
11871-
return array_combine(array_column($mapping,0),array_column($mapping,1));
11900+
$mapping = array_map(function ($v) {
11901+
return explode('=', $v);
11902+
}, array_filter(array_map('trim', explode(',', $this->values['mapping']))));
11903+
return array_combine(array_column($mapping, 0), array_column($mapping, 1));
1187211904
}
1187311905

1187411906
public function getMiddlewares(): array

0 commit comments

Comments
 (0)