1 | <?php |
||
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) |
||
111 | |||
112 | /** |
||
113 | * strings(arguments) |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function strings($value, $length) |
||
121 | |||
122 | /** |
||
123 | * text(argument) |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function text($value) |
||
131 | |||
132 | /** |
||
133 | * increments(argument) |
||
134 | * |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function integer($value, $length) |
||
142 | |||
143 | /** |
||
144 | * increments(argument) |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | public function primaryKey($value) |
||
152 | |||
153 | /** |
||
154 | * unique(argument) |
||
155 | * |
||
156 | * @return string |
||
157 | */ |
||
158 | public function unique($value) |
||
162 | |||
163 | /** |
||
164 | * foreignKey(argument) |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function foreignKey($value, $length) |
||
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) |
||
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.