Skip to content

Commit 779d4aa

Browse files
committed
More generic Schema loading
1 parent 6ae854e commit 779d4aa

File tree

5 files changed

+49
-120
lines changed

5 files changed

+49
-120
lines changed

SOB/Cake/scaffolding/generateModels.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,7 @@
22

33
include __DIR__ . '/../../../vendor/autoload.php';
44

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
5+
$schema = new \SOB\Schema();
356

367
echo "Generate Record Models\n\n";
378

SOB/CakeCached/scaffolding/generateModels.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,7 @@
22

33
include __DIR__ . '/../../../vendor/autoload.php';
44

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
5+
$schema = new \SOB\Schema();
356

367
echo "Generate Record Models\n\n";
378

SOB/Eloquent/scaffolding/generateModels.php

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,7 @@
22

33
include __DIR__ . '/../../../vendor/autoload.php';
44

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
5+
$schema = new \SOB\Schema();
356

367
echo "Generate Record Models\n\n";
378

SOB/PHPFUI/scaffolding/generateCRUD.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,14 @@
22

33
include __DIR__ . '/../../../vendor/autoload.php';
44

5-
$pdo = new \PHPFUI\ORM\PDOInstance('sqlite::memory:');
6-
\PHPFUI\ORM::addConnection($pdo);
5+
$schema = new \SOB\Schema();
6+
77
\PHPFUI\ORM::$namespaceRoot = __DIR__ . '/../../..';
88
\PHPFUI\ORM::$recordNamespace = 'SOB\PHPFUI\Record';
99
\PHPFUI\ORM::$tableNamespace = 'SOB\PHPFUI\Table';
1010
\PHPFUI\ORM::$migrationNamespace = 'SOB\PHPFUI\Migration';
1111
\PHPFUI\ORM::$idSuffix = '_id';
1212

13-
$lines = \file(__DIR__ . '/../../../northwind/northwind-schema.sqlite');
14-
15-
$sql = '';
16-
17-
foreach ($lines as $line)
18-
{
19-
20-
// Ignoring comments from the SQL script
21-
if (\str_starts_with((string)$line, '--') || '' == $line)
22-
{
23-
continue;
24-
}
25-
26-
$sql .= $line;
27-
28-
if (\str_ends_with(\trim((string)$line), ';'))
29-
{
30-
\PHPFUI\ORM::execute($sql);
31-
$lastError = \PHPFUI\ORM::getLastError();
32-
33-
if ($lastError)
34-
{
35-
throw new \Exception($lastError . ' SQL: ' . $sql);
36-
}
37-
$sql = '';
38-
}
39-
} // end foreach
40-
4113
echo "Generate Record Models\n\n";
4214

4315
$generator = new \PHPFUI\ORM\Tool\Generate\CRUD();

SOB/Schema.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace SOB;
4+
5+
class Schema
6+
{
7+
8+
private static \PHPFUI\ORM\PDOInstance $pdo;
9+
10+
public function __construct()
11+
{
12+
static::$pdo = new \PHPFUI\ORM\PDOInstance('sqlite::memory:');
13+
\PHPFUI\ORM::addConnection(static::$pdo);
14+
15+
$lines = \file(__DIR__ . '/../northwind/northwind-schema.sqlite');
16+
17+
$sql = '';
18+
19+
foreach ($lines as $line)
20+
{
21+
22+
// Ignoring comments from the SQL script
23+
if (\str_starts_with((string)$line, '--') || '' == $line)
24+
{
25+
continue;
26+
}
27+
28+
$sql .= $line;
29+
30+
if (\str_ends_with(\trim((string)$line), ';'))
31+
{
32+
\PHPFUI\ORM::execute($sql);
33+
$lastError = \PHPFUI\ORM::getLastError();
34+
35+
if ($lastError)
36+
{
37+
throw new \Exception($lastError . ' SQL: ' . $sql);
38+
}
39+
$sql = '';
40+
}
41+
} // end foreach
42+
}
43+
44+
}

0 commit comments

Comments
 (0)