Skip to content

Commit d90f6e6

Browse files
committed
Added migrations and ORMHelper
1 parent ed145ea commit d90f6e6

File tree

15 files changed

+511
-3
lines changed

15 files changed

+511
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/testinglocal

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ User::table()
108108
$query->where("id", "1");
109109
})
110110

111+
// PHP 7.4+
112+
->or(fn ($query) => $query->where("id", "1") )
113+
111114
// Nesting
112115
->or(function($query){
113116
$query->or(function($query){
@@ -126,4 +129,82 @@ User::table()
126129
// Offset (requires a limit to be set)
127130
->offset(0)
128131
->all();
132+
133+
134+
User::table()->each(function(User $entry){
135+
echo $entry->name."\n";
136+
});
137+
138+
```
139+
140+
## Migration
141+
```php
142+
(new Migrator("main"))
143+
->setLogging(true)
144+
->fromFolder("resources/migrations")
145+
->up();
146+
147+
(new Migrator("main"))
148+
->setLogging(true)
149+
->fromFolder("resources/migrations")
150+
->down(/*default val: 1*/);
151+
```
152+
153+
#### resources/migrations/migration_22_0_11_create_users.php
154+
```php
155+
<?php
156+
namespace testinglocal\migrations;
157+
158+
use de\interaapps\ulole\orm\Database;
159+
use de\interaapps\ulole\orm\migration\Blueprint;
160+
use de\interaapps\ulole\orm\migration\Migration;
161+
162+
/**
163+
* CHANGED:
164+
*/
165+
class migration_22_0_11_create_users implements Migration {
166+
public function up(Database $database) {
167+
return $database->create("users", function (Blueprint $blueprint) {
168+
$blueprint->id();
169+
$blueprint->string("name");
170+
$blueprint->string("password");
171+
$blueprint->string("description");
172+
$blueprint->enum("gender", ["FEMALE", "MALE", "OTHER", "DO_NOT_ANSWER"])->default('DO_NOT_ANSWER');
173+
$blueprint->timestamp("created")->currentTimestamp();
174+
});
175+
}
176+
177+
public function down(Database $database) {
178+
return $database->drop("users");
179+
}
180+
}
129181
```
182+
183+
#### resources/migrations/migration_22_0_13_edit_users.php
184+
```php
185+
<?php
186+
namespace testinglocal\migrations;
187+
188+
use de\interaapps\ulole\orm\Database;
189+
use de\interaapps\ulole\orm\migration\Blueprint;
190+
use de\interaapps\ulole\orm\migration\Migration;
191+
192+
/**
193+
* CHANGED:
194+
*/
195+
class migration_22_0_13_edit_users implements Migration {
196+
public function up(Database $database) {
197+
return $database->edit("users", function (Blueprint $blueprint) {
198+
$blueprint->string("name")->default("Johnson");
199+
$blueprint->string("mail");
200+
});
201+
}
202+
203+
public function down(Database $database) {
204+
return $database->edit("users", function(Blueprint $blueprint){
205+
$blueprint->string("name")->nullable()->default(null);
206+
$blueprint->string("mail")->drop();
207+
});
208+
}
209+
}
210+
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/uloleorm",
3-
"version": "2.0",
3+
"version": "2.0.1",
44
"type": "library",
55
"authors": [
66
{

de/interaapps/ulole/orm/Database.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace de\interaapps\ulole\orm;
33

4+
use de\interaapps\ulole\orm\migration\Blueprint;
5+
46
class Database {
57
private $connection;
68

@@ -15,4 +17,46 @@ public function getConnection(){
1517
return $this->connection;
1618
}
1719

20+
public function create($name, $callable, $ifNotExists = false){
21+
$blueprint = new Blueprint();
22+
$callable($blueprint);
23+
$sql = "CREATE TABLE ".($ifNotExists ? "IF NOT EXISTS " : "")."`".$name."` (\n";
24+
$sql .= implode(",\n", $blueprint->getQueries(true));
25+
$sql .= "\n) ENGINE = InnoDB;";
26+
27+
return $this->connection->query($sql);
28+
}
29+
30+
public function edit($name, $callable){
31+
$statement = $this->connection->query("SHOW COLUMNS FROM ".$name.";");
32+
$existingColumns = [];
33+
foreach ($statement->fetchAll(\PDO::FETCH_NUM) as $row) {
34+
array_push($existingColumns, $row[0]);
35+
}
36+
$blueprint = new Blueprint();
37+
$callable($blueprint);
38+
$sql = "ALTER TABLE `".$name."`";
39+
$comma = false;
40+
foreach ($blueprint->getQueries() as $column => $query) {
41+
if ($comma)
42+
$sql .= ", ";
43+
44+
if (in_array($column, $existingColumns))
45+
$sql .= (substr( $query, 0, 4 ) === "DROP" ? "" : "CHANGE `".$column."` ").$query;
46+
else
47+
$sql .= " ADD ".$query;
48+
49+
if (!$comma)
50+
$comma = true;
51+
}
52+
$sql .= ";";
53+
54+
return $this->connection->query($sql);
55+
}
56+
57+
58+
public function drop($name){
59+
return $this->connection->query("DROP TABLE `".$name."`;");
60+
}
61+
1862
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace de\interaapps\ulole\orm;
3+
4+
/**
5+
* Requires ORMModel
6+
*/
7+
trait ORMHelper {
8+
public static function all($limit = null, $offset = null){
9+
$query = static::table();
10+
if ($limit !== null)
11+
$query->limit($limit);
12+
13+
if ($offset !== null)
14+
$query->offset($offset);
15+
return $query->all();
16+
}
17+
18+
public static function get($id) {
19+
$instance = new static();
20+
return static::table()->where($instance->ormInternals_getFieldName('-id'), $id)->get();
21+
}
22+
23+
public static function where($var1, $var2, $var3 = null) {
24+
return static::table()->where($var1, $var2, $var3);
25+
}
26+
27+
public static function like($field, $val) {
28+
return static::table()->like($field, $val);
29+
}
30+
}

de/interaapps/ulole/orm/ORMModel.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public function insert($database = 'main') : bool {
5555
$query .= ')';
5656

5757
$statement = UloleORM::getDatabase($database)->getConnection()->prepare($query);
58-
58+
5959
$result = $statement->execute($values);
6060
$this->{$this->ormInternals_getFieldName('-id')} = UloleORM::getDatabase($database)->getConnection()->lastInsertId();
61+
if ($result)
62+
$this->ormInternals_entryExists = true;
6163
return $result;
6264
}
6365

de/interaapps/ulole/orm/Query.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public function like($field, $like){
4040
return $this->where($field, "LIKE", $like);
4141
}
4242

43+
public function orLike($field, $like){
44+
return $this->orWhere($field, "LIKE", $like);
45+
}
46+
47+
public function search($field, $like){
48+
return $this->where($field, "LIKE", "%".$like."%");
49+
}
50+
4351
public function or(Callable $callable) : Query{
4452
$query = new Query($this->database, $this->model);
4553
$query->temporaryQuery = true;
@@ -90,6 +98,12 @@ public function all() {
9098
return $result;
9199
}
92100

101+
public function each($closure) {
102+
foreach ($this->all() as $entry)
103+
$closure($entry);
104+
return $this;
105+
}
106+
93107
public function count() {
94108
$vars = [];
95109
$query = $this->buildQuery();

de/interaapps/ulole/orm/UloleORM.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ public static function register($name, $model) {
1111
self::$tableNames[$model] = $name;
1212
}
1313

14+
public static function registerIfNot($name, $model) {
15+
if (!isset(self::$tableNames[$model]))
16+
self::$tableNames[$model] = $name;
17+
}
18+
19+
public static function registerMultiple($models = []) {
20+
foreach ($models as $name => $model)
21+
self::$tableNames[$model] = $name;
22+
}
23+
1424
public static function database(string $name, Database $database){
1525
self::$databases[$name] = $database;
1626
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
namespace de\interaapps\ulole\orm\migration;
3+
4+
class Blueprint {
5+
private $columns;
6+
7+
public function __construct() {
8+
$this->columns = [];
9+
}
10+
11+
public function id($name = "id") : Column {
12+
$column = (new Column($name, "INT"))
13+
->ai();
14+
array_push($this->columns, $column);
15+
return $column;
16+
}
17+
18+
public function string($name, $size = null) : Column {
19+
$column = new Column($name, $size === null ? "TEXT" : "VARCHAR");
20+
array_push($this->columns, $column);
21+
return $column;
22+
}
23+
24+
public function int($name, $size = null) : Column {
25+
$column = new Column($name, "INT", $size);
26+
array_push($this->columns, $column);
27+
return $column;
28+
}
29+
30+
public function enum($name, array $set) : Column {
31+
$column = new Column($name, "ENUM", $set);
32+
array_push($this->columns, $column);
33+
return $column;
34+
}
35+
36+
public function timestamp($name) : Column {
37+
$column = new Column($name, "TIMESTAMP");
38+
array_push($this->columns, $column);
39+
return $column;
40+
}
41+
42+
public function getColumns() {
43+
return $this->columns;
44+
}
45+
46+
public function getQueries($addKeys = false){
47+
$columns = [];
48+
$primaryKeys = [];
49+
foreach ($this->columns as $column) {
50+
$columns[$column->name] = $column->generateStructure();
51+
if ($column->primary)
52+
array_push($primaryKeys, "`".($column->renameTo === null ?$column->name : $column->renameTo)."`");
53+
}
54+
if ($addKeys)
55+
$columns[":primary_key"] = "PRIMARY KEY (". implode(",", $primaryKeys) .")";
56+
57+
return $columns;
58+
}
59+
}

0 commit comments

Comments
 (0)