Skip to content

Commit e8ba4dd

Browse files
author
SergeyGrishin
committed
-
1 parent 731165f commit e8ba4dd

File tree

7 files changed

+125
-70
lines changed

7 files changed

+125
-70
lines changed

.gitignore

Lines changed: 0 additions & 5 deletions
This file was deleted.

lib/orm/DataBase/Field.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace orm\DataBase;
44

55

6+
use orm\DataBase\fields\Number;
67
use orm\DataBase\fields\DateTime;
7-
use orm\DataBase\fields\ForeignKey;
88
use orm\DataBase\fields\PrimaryKey;
9+
use orm\DataBase\fields\ForeignKey;
910
use orm\DataBase\fields\StringField;
1011

1112

@@ -18,6 +19,23 @@
1819
class Field
1920
{
2021

22+
/**
23+
* @param string $type type of number field (int, float, double)
24+
* @param int $size size of field
25+
* @param string $attribute attribute of field (binary, unsigned)
26+
* @param bool $auto_increment, auto_increment flag
27+
* @return \orm\DataBase\fields\Number
28+
*/
29+
public static function number($type = "int", $size = 10, $attribute = "", $auto_increment = false)
30+
{
31+
$obj = new Number();
32+
$obj->type = $type;
33+
$obj->size = $size;
34+
$obj->attribute = $attribute;
35+
$obj->auto_increment = $auto_increment;
36+
return $obj;
37+
}
38+
2139
/**
2240
* describe PRIMARY KEY with AUTO_INCREMENT
2341
* @return PrimaryKey

lib/orm/DataBase/Table.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,17 @@ public function initTable()
4545
public function migrate()
4646
{
4747
try {
48-
(new QueryExecutor(PdoAdapter::getInstance()
49-
->getPdoObject()->prepare(
50-
(new Migration($this->table_fields, $this->table_name))
51-
->buildMigrationSqlCode()), []))
52-
->executeSql();
48+
$generator = $this->getQueryGeneratorInstance();
49+
$create_database_query = $generator->createDataBase(QueryMemento::createInstance()->getStorage()["dbname"]);
50+
$create_table_query = $generator->createTable($this->table_name, $this->table_fields);
51+
52+
print_r($create_table_query);
53+
54+
// (new QueryExecutor(PdoAdapter::getInstance()
55+
// ->getPdoObject()->prepare(
56+
// (new Migration($this->table_fields, $this->table_name))
57+
// ->buildMigrationSqlCode()), []))
58+
// ->executeSql();
5359
} catch (\PDOException $e) {
5460
throw new MigrationException($e->getMessage());
5561
}

lib/orm/DataBase/fields/Number.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/**
4+
* @TODO: think about merging functional of this class and class PrimaryKey.
5+
* @TODO: Create universal class and make 2 children
6+
*/
7+
8+
9+
namespace orm\DataBase\fields;
10+
11+
12+
/**
13+
* Class Number
14+
* @package lib\orm\DataBase\fields
15+
*/
16+
class Number
17+
{
18+
19+
/**
20+
* @var
21+
*/
22+
public $type;
23+
/**
24+
* @var
25+
*/
26+
public $size;
27+
/**
28+
* @var
29+
*/
30+
public $attribute;
31+
/**
32+
* @var
33+
*/
34+
public $auto_increment;
35+
36+
}

lib/orm/Migrations/Migration.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

lib/orm/Query/MysqlQueryGenerator.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
namespace orm\Query;
44

55

6+
use orm\DataBase\fields\DateTime;
7+
use orm\DataBase\fields\ForeignKey;
8+
use orm\DataBase\fields\PrimaryKey;
9+
use orm\DataBase\fields\StringField;
10+
use orm\Exceptions\MigrationException;
11+
612
class MysqlQueryGenerator implements QueryGeneratorInterface
713
{
814

@@ -46,4 +52,41 @@ public function selectAll($table)
4652
{
4753
return $this->pdo->prepare("select * from {$table}");
4854
}
55+
56+
public function createDataBase($dbname)
57+
{
58+
return $this->pdo->prepare("create database if not exists `{$dbname}` default character set utf8 collate" .
59+
"utf8_general_ci;\nuse `{$dbname}`;\n");
60+
}
61+
62+
public function createTable($table, $fields)
63+
{
64+
$query = "set foreign_key_checks = 0;\n";
65+
$foreign_keys = "";
66+
$query .= "drop table if exists `{$table}`; create table `{$table}` (\n";
67+
foreach ($fields as $key => $field) {
68+
if ($field instanceof PrimaryKey) {
69+
$query .= "`{$key}` {$field->type}({$field->size}) not null primary key auto_increment,\n";
70+
} elseif ($field instanceof ForeignKey) {
71+
$query .= "`{$key}` int not null,\n";
72+
$table = new $field->table();
73+
$reflection = new \ReflectionClass($table);
74+
$property = $reflection->getProperty("table_name");
75+
$property->setAccessible(true);
76+
$foreign_keys .= "alter table `{$table}` add constraint `{$key}_fk` foreign key (`{$key}`)" .
77+
" references `{$property->getValue($table)}` (`{$field->field}`) on delete " .
78+
"{$field->on_delete} on update {$field->on_update};\n";
79+
$property->setAccessible(false);
80+
} elseif ($field instanceof StringField) {
81+
$query .= "`{$key}` {$field->type}({$field->size}) not null,\n";
82+
} elseif ($field instanceof DateTime) {
83+
$query .= "`{$key}` {$field->type} not null,\n";
84+
} else {
85+
throw new MigrationException("Migration Exception");
86+
}
87+
}
88+
return $this->pdo->prepare(substr($query, 0, -2) . "\n) engine=InnoDB default charset=utf8;\n" .
89+
$foreign_keys . "set foreign_key_checks = 1;\n");
90+
}
91+
4992
}

lib/orm/Query/QueryGeneratorInterface.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22

3+
34
namespace orm\Query;
45

56

67
/**
7-
* Interface QueryGeneratorInterface. Base API for all *QueryGenerator classes
8+
* Interface QueryGeneratorInterface
89
* @package orm\Query
910
*/
1011
interface QueryGeneratorInterface
@@ -41,4 +42,18 @@ public function selectByKeys($table, $keys);
4142
*/
4243
public function selectAll($table);
4344

45+
/**
46+
* Signature of createDataBase query generator.
47+
* @param $dbname string, name of database
48+
* @return \PDOStatement, prepared query
49+
*/
50+
public function createDataBase($dbname);
51+
52+
/**
53+
* @param $table string, name of table
54+
* @param $fields array of objects \DataBase\fields\* with structure of fields of table
55+
* @return \PDOStatement, prepared query
56+
*/
57+
public function createTable($table, $fields);
58+
4459
}

0 commit comments

Comments
 (0)