1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* SugarORM\Schema manages the creation of database table. |
4
|
|
|
* |
5
|
|
|
* @package Ibonly\SugarORM\Schema |
6
|
|
|
* @author Ibraheem ADENIYI <[email protected]> |
7
|
|
|
* @license MIT <https://opensource.org/licenses/MIT> |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Ibonly\PotatoORM; |
11
|
|
|
|
12
|
|
|
use PDOException; |
13
|
|
|
use Ibonly\PotatoORM\DatabaseQuery; |
14
|
|
|
use Ibonly\PotatoORM\SchemaInterface; |
15
|
|
|
|
16
|
|
|
class Schema extends DatabaseQuery implements SchemaInterface |
17
|
|
|
{ |
18
|
|
|
//Inject the inflector trait |
19
|
|
|
use Inflector; |
20
|
|
|
|
21
|
|
|
protected $fieldDescription = []; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* field(arguments) contains the sql field statement |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
public function field($type, $fieldName, $length=NULL) |
29
|
|
|
{ |
30
|
|
|
if($length === null){ |
31
|
|
|
$this->fieldDescription[] = $type ." ".$fieldName; |
32
|
|
|
}else |
33
|
|
|
{ |
34
|
|
|
$this->fieldDescription[] = $type ." ".$fieldName." ".$length; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* buildQuery(argument): Builds the CREATE query |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function buildQuery($tablename) |
45
|
|
|
{ |
46
|
|
|
$pluralTableName = self::pluralize($tablename); |
47
|
|
|
$query = "CREATE TABLE IF NOT EXISTS {$pluralTableName} (".PHP_EOL; |
48
|
|
|
|
49
|
|
|
$callback = function($fieldName) use (&$query) |
50
|
|
|
{ |
51
|
|
|
$fields = explode(" ", $fieldName); |
52
|
|
|
|
53
|
|
|
$constrain = $fields[0]; |
54
|
|
|
if(count($fields) == 2) |
55
|
|
|
{ |
56
|
|
|
$query .= $this->$constrain($fields[1], 20) .", ".PHP_EOL; |
57
|
|
|
}else |
58
|
|
|
{ |
59
|
|
|
$query .= $this->$constrain($fields[1], $fields[2]) .", ".PHP_EOL; |
60
|
|
|
} |
61
|
|
|
}; |
62
|
|
|
array_walk($this->fieldDescription, $callback); |
63
|
|
|
$query .= ');)'; |
64
|
|
|
|
65
|
|
|
return $query; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* SanitizeQuery(argument) Removes the unwanted character in the build |
70
|
|
|
* and completes the statement |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function sanitizeQuery($query) |
75
|
|
|
{ |
76
|
|
|
$q = substr_replace($this->buildQuery($query), "", -6); |
77
|
|
|
$q .= ");"; |
78
|
|
|
return $q; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* createTable(argument) Execute the CREATE query |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function createTable($tablename, $connection = NULL) |
87
|
|
|
{ |
88
|
|
|
$connection = DatabaseQuery::connect(); |
89
|
|
|
try |
90
|
|
|
{ |
91
|
|
|
$sqlQuery = self::sanitizeQuery($tablename); |
92
|
|
|
$query = $connection->prepare($sqlQuery); |
93
|
|
|
if($query->execute()) |
94
|
|
|
{ |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
}catch(PDOException $e){ |
98
|
|
|
return $e->getMessage(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* increments(argument) |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function increments($value) |
108
|
|
|
{ |
109
|
|
|
return $value." int(11) NOT NULL AUTO_INCREMENT"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* strings(arguments) |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function strings($value, $length) |
118
|
|
|
{ |
119
|
|
|
return $value ." varchar (".$length.") NOT NULL"; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* text(argument) |
124
|
|
|
* |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
public function text($value) |
128
|
|
|
{ |
129
|
|
|
return $value." text NOT NULL"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* increments(argument) |
134
|
|
|
* |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function integer($value, $length) |
139
|
|
|
{ |
140
|
|
|
return $value." int(".$length.") NOT NULL"; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* increments(argument) |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
|
|
public function primaryKey($value) |
149
|
|
|
{ |
150
|
|
|
return "PRIMARY KEY ({$value})"; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* unique(argument) |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function unique($value) |
159
|
|
|
{ |
160
|
|
|
return "UNIQUE KEY {$value} ({$value})"; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* foreignKey(argument) |
165
|
|
|
* |
166
|
|
|
* @return string |
167
|
|
|
*/ |
168
|
|
|
public function foreignKey($value, $length) |
169
|
|
|
{ |
170
|
|
|
$r = explode("-", $length); |
171
|
|
|
|
172
|
|
|
return "FOREIGN KEY ({$value}) REFERENCES ".$r[0]."(".$r[1].")"; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* dateTime description] |
177
|
|
|
* |
178
|
|
|
* @param [type] $value [description] |
|
|
|
|
179
|
|
|
* @param [type] $type [description] |
|
|
|
|
180
|
|
|
* @return [type] [description] |
|
|
|
|
181
|
|
|
*/ |
182
|
|
|
public function dateTime($value, $type = NULL) |
183
|
|
|
{ |
184
|
|
|
$apend = ""; |
|
|
|
|
185
|
|
|
switch ($type) { |
186
|
|
|
case 'time': |
187
|
|
|
$apend = 'time'; |
188
|
|
|
break; |
189
|
|
|
case 'timestamp': |
190
|
|
|
$apend = 'timestamp'; |
191
|
|
|
break; |
192
|
|
|
case 'date': |
193
|
|
|
$apend = 'date'; |
194
|
|
|
break; |
195
|
|
|
case 'datetime': |
196
|
|
|
$apend = 'datetime'; |
197
|
|
|
break; |
198
|
|
|
case 'year': |
199
|
|
|
$apend = 'year(4)'; |
200
|
|
|
break; |
201
|
|
|
default: |
202
|
|
|
$apend = 'timestamp'; |
203
|
|
|
break; |
204
|
|
|
} |
205
|
|
|
return $value . " " . $apend . " NOT NULL"; |
206
|
|
|
} |
207
|
|
|
} |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.