1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Framy Framework |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright Framy |
6
|
|
|
* @Author Marco Bier <[email protected]> |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace app\framework\Component\Database\Schema; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Builder |
13
|
|
|
* Parse the sql query. |
14
|
|
|
* |
15
|
|
|
* @package app\framework\Component\Database\Schema |
16
|
|
|
*/ |
17
|
|
|
class Builder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Builds based on thr Blueprint the sql query. |
21
|
|
|
* |
22
|
|
|
* @param Blueprint $table |
23
|
|
|
* @return string |
24
|
|
|
*/ |
25
|
|
|
public static function createTable(Blueprint $table): string |
26
|
|
|
{ |
27
|
|
|
$query = "CREATE TABLE `".$table->getTable()."`"; |
28
|
|
|
|
29
|
|
|
// go through columns |
30
|
|
|
$query .= " ("; |
31
|
|
|
foreach ($table->getColumns() as $key => $Column) |
32
|
|
|
{ |
33
|
|
|
$query .= "`".$Column->name."` "; |
34
|
|
|
|
35
|
|
|
$query .= $Column->type; |
36
|
|
|
|
37
|
|
|
if(isset($Column->length)) { |
38
|
|
|
$query .= "(".$Column->length; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if(isset($Column->scale) && isset($Column->length)) { |
42
|
|
|
$query .= ", ".$Column->scale.")"; |
43
|
|
|
} else { |
44
|
|
|
if(isset($Column->length)) { |
45
|
|
|
$query .= ")"; |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if($Column->isUnsigned) { |
50
|
|
|
$query .= " UNSIGNED"; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if($Column->notNull) { |
54
|
|
|
$query .= " NOT NULL"; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if($Column->isAutoIncrement) { |
58
|
|
|
$query .= " AUTO_INCREMENT"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if($Column->primaryKey) { |
62
|
|
|
$query .= " PRIMARY KEY"; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if($Column->foreignKey) { |
66
|
|
|
$query .= " FOREIGN KEY"; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if($Column->unique) { |
70
|
|
|
$query .= " UNIQUE"; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if($Column->default != null) { |
74
|
|
|
$default = $Column->default; |
75
|
|
|
if (is_string($default)) { |
76
|
|
|
if (str($default)->startsWith("%")) |
77
|
|
|
$default = str($default)->explode("%")->last(); |
78
|
|
|
else |
79
|
|
|
$default = "'".$default."'"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$query .= " DEFAULT ".$default; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if(count($table->getColumns())-1 != $key) |
86
|
|
|
$query .= ", "; |
87
|
|
|
} |
88
|
|
|
$query .= ");"; |
89
|
|
|
|
90
|
|
|
return $query; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $name |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public static function dropTable(string $name): string |
98
|
|
|
{ |
99
|
|
|
return "DROP TABLE `".$name."`;"; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|