1 | <?php
|
---|
2 |
|
---|
3 | class ModelDesc
|
---|
4 | {
|
---|
5 | public string $Name;
|
---|
6 | public array $Columns;
|
---|
7 | public array $Indices;
|
---|
8 | public string $PrimaryKey;
|
---|
9 | public bool $Memory;
|
---|
10 | public $DefaultValuesMethod;
|
---|
11 |
|
---|
12 | function __construct(string $Name)
|
---|
13 | {
|
---|
14 | $this->Name = $Name;
|
---|
15 | $this->Columns = array();
|
---|
16 | $this->Indices = array();
|
---|
17 | $this->PrimaryKey = 'Id';
|
---|
18 | $this->Memory = false;
|
---|
19 | $this->DefaultValuesMethod = null;
|
---|
20 | }
|
---|
21 |
|
---|
22 | function AddString(string $Name): ModelColumnString
|
---|
23 | {
|
---|
24 | $Result = new ModelColumnString($Name);
|
---|
25 | $this->Columns[] = $Result;
|
---|
26 | return $Result;
|
---|
27 | }
|
---|
28 |
|
---|
29 | function AddFloat(string $Name): ModelColumnFloat
|
---|
30 | {
|
---|
31 | $Result = new ModelColumnFloat($Name);
|
---|
32 | $this->Columns[] = $Result;
|
---|
33 | return $Result;
|
---|
34 | }
|
---|
35 |
|
---|
36 | function AddText(string $Name): ModelColumnText
|
---|
37 | {
|
---|
38 | $Result = new ModelColumnText($Name);
|
---|
39 | $this->Columns[] = $Result;
|
---|
40 | return $Result;
|
---|
41 | }
|
---|
42 |
|
---|
43 | function AddInteger(string $Name): ModelColumnInteger
|
---|
44 | {
|
---|
45 | $Result = new ModelColumnInteger($Name);
|
---|
46 | $this->Columns[] = $Result;
|
---|
47 | return $Result;
|
---|
48 | }
|
---|
49 |
|
---|
50 | function AddBigInt(string $Name): ModelColumnBigInt
|
---|
51 | {
|
---|
52 | $Result = new ModelColumnBigInt($Name);
|
---|
53 | $this->Columns[] = $Result;
|
---|
54 | return $Result;
|
---|
55 | }
|
---|
56 |
|
---|
57 | function AddDateTime(string $Name): ModelColumnDateTime
|
---|
58 | {
|
---|
59 | $Result = new ModelColumnDateTime($Name);
|
---|
60 | $this->Columns[] = $Result;
|
---|
61 | return $Result;
|
---|
62 | }
|
---|
63 |
|
---|
64 | function AddDate(string $Name): ModelColumnDate
|
---|
65 | {
|
---|
66 | $Result = new ModelColumnDate($Name);
|
---|
67 | $this->Columns[] = $Result;
|
---|
68 | return $Result;
|
---|
69 | }
|
---|
70 |
|
---|
71 | function AddBoolean(string $Name): ModelColumnBoolean
|
---|
72 | {
|
---|
73 | $Result = new ModelColumnBoolean($Name);
|
---|
74 | $this->Columns[] = $Result;
|
---|
75 | return $Result;
|
---|
76 | }
|
---|
77 |
|
---|
78 | function AddReference(string $Name, string $RefTable, bool $Nullable = true): ModelColumnReference
|
---|
79 | {
|
---|
80 | $Result = new ModelColumnReference($Name, $RefTable);
|
---|
81 | $this->Columns[] = $Result;
|
---|
82 | $Result->Nullable = $Nullable;
|
---|
83 | return $Result;
|
---|
84 | }
|
---|
85 |
|
---|
86 | function AddEnum(string $Name, array $States): ModelColumnEnum
|
---|
87 | {
|
---|
88 | $Result = new ModelColumnEnum($Name, $States);
|
---|
89 | $this->Columns[] = $Result;
|
---|
90 | return $Result;
|
---|
91 | }
|
---|
92 |
|
---|
93 | function AddChangeAction(): void
|
---|
94 | {
|
---|
95 | $Column = $this->AddEnum('ChangeAction', array('add', 'modify', 'remove'));
|
---|
96 | $Column->Nullable = true;
|
---|
97 | $Column = $this->AddDateTime('ChangeTime');
|
---|
98 | $Column->Nullable = true;
|
---|
99 | $this->AddInteger('ChangeReplaceId');
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | class ModelColumnType
|
---|
104 | {
|
---|
105 | const Integer = 0;
|
---|
106 | const String = 1;
|
---|
107 | const Float = 2;
|
---|
108 | const Text = 3;
|
---|
109 | const DateTime = 4;
|
---|
110 | const Reference = 5;
|
---|
111 | const Boolean = 6;
|
---|
112 | const Date = 7;
|
---|
113 | const Enum = 8;
|
---|
114 | const BigInt = 9;
|
---|
115 |
|
---|
116 | static function GetName(int $Index)
|
---|
117 | {
|
---|
118 | $Text = array('Integer', 'String', 'Float', 'Text', 'DateTime', 'Reference', 'Boolean', 'Date', 'Enum', 'BigInt');
|
---|
119 | return $Text[$Index];
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | class ModelColumn
|
---|
124 | {
|
---|
125 | public string $Name;
|
---|
126 | public int $Type; // ModelColumnType
|
---|
127 | public bool $Nullable;
|
---|
128 | public bool $Unique;
|
---|
129 | public bool $HasDefault;
|
---|
130 |
|
---|
131 | function __construct(string $Name, int $Type, bool $Nullable = false, bool $Unique = false)
|
---|
132 | {
|
---|
133 | $this->Name = $Name;
|
---|
134 | $this->Type = $Type;
|
---|
135 | $this->Nullable = $Nullable;
|
---|
136 | $this->Unique = $Unique;
|
---|
137 | $this->HasDefault = false;
|
---|
138 | }
|
---|
139 |
|
---|
140 | function GetDefault(): ?string
|
---|
141 | {
|
---|
142 | return null;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | class ModelColumnString extends ModelColumn
|
---|
147 | {
|
---|
148 | public ?string $Default;
|
---|
149 | public int $MaxLength;
|
---|
150 |
|
---|
151 | function __construct(string $Name)
|
---|
152 | {
|
---|
153 | parent::__construct($Name, ModelColumnType::String);
|
---|
154 | $this->HasDefault = false;
|
---|
155 | $this->Default = null;
|
---|
156 | }
|
---|
157 |
|
---|
158 | function GetDefault(): ?string
|
---|
159 | {
|
---|
160 | return '"'.$this->Default.'"';
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | class ModelColumnFloat extends ModelColumn
|
---|
165 | {
|
---|
166 | public ?float $Default;
|
---|
167 |
|
---|
168 | function __construct(string $Name)
|
---|
169 | {
|
---|
170 | parent::__construct($Name, ModelColumnType::Float);
|
---|
171 | $this->HasDefault = false;
|
---|
172 | $this->Default = null;
|
---|
173 | }
|
---|
174 |
|
---|
175 | function GetDefault(): ?string
|
---|
176 | {
|
---|
177 | return '"'.$this->Default.'"';
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | class ModelColumnText extends ModelColumn
|
---|
182 | {
|
---|
183 | public ?string $Default;
|
---|
184 | public int $MaxLength;
|
---|
185 |
|
---|
186 | function __construct(string $Name)
|
---|
187 | {
|
---|
188 | parent::__construct($Name, ModelColumnType::Text);
|
---|
189 | $this->HasDefault = false;
|
---|
190 | $this->Default = null;
|
---|
191 | $this->MaxLength = 255;
|
---|
192 | }
|
---|
193 |
|
---|
194 | function GetDefault(): ?string
|
---|
195 | {
|
---|
196 | return '"'.$this->Default.'"';
|
---|
197 | }
|
---|
198 | }
|
---|
199 |
|
---|
200 | class ModelColumnInteger extends ModelColumn
|
---|
201 | {
|
---|
202 | public ?int $Default;
|
---|
203 |
|
---|
204 | function __construct(string $Name)
|
---|
205 | {
|
---|
206 | parent::__construct($Name, ModelColumnType::Integer);
|
---|
207 | $this->HasDefault = false;
|
---|
208 | $this->Default = null;
|
---|
209 | }
|
---|
210 |
|
---|
211 | function GetDefault(): ?string
|
---|
212 | {
|
---|
213 | return $this->Default;
|
---|
214 | }
|
---|
215 | }
|
---|
216 |
|
---|
217 | class ModelColumnBigInt extends ModelColumn
|
---|
218 | {
|
---|
219 | public ?int $Default;
|
---|
220 |
|
---|
221 | function __construct(string $Name)
|
---|
222 | {
|
---|
223 | parent::__construct($Name, ModelColumnType::BigInt);
|
---|
224 | $this->HasDefault = false;
|
---|
225 | $this->Default = null;
|
---|
226 | }
|
---|
227 |
|
---|
228 | function GetDefault(): ?string
|
---|
229 | {
|
---|
230 | return $this->Default;
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | class ModelColumnDateTime extends ModelColumn
|
---|
235 | {
|
---|
236 | public ?DateTime $Default;
|
---|
237 |
|
---|
238 | function __construct(string $Name)
|
---|
239 | {
|
---|
240 | parent::__construct($Name, ModelColumnType::DateTime);
|
---|
241 | $this->HasDefault = false;
|
---|
242 | $this->Default = null;
|
---|
243 | }
|
---|
244 |
|
---|
245 | function GetDefault(): ?string
|
---|
246 | {
|
---|
247 | return '"'.$this->Default.'"';
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | class ModelColumnDate extends ModelColumn
|
---|
252 | {
|
---|
253 | public ?DateTime $Default;
|
---|
254 |
|
---|
255 | function __construct(string $Name)
|
---|
256 | {
|
---|
257 | parent::__construct($Name, ModelColumnType::Date);
|
---|
258 | $this->HasDefault = false;
|
---|
259 | $this->Default = null;
|
---|
260 | }
|
---|
261 |
|
---|
262 | function GetDefault(): ?string
|
---|
263 | {
|
---|
264 | return '"'.$this->Default.'"';
|
---|
265 | }
|
---|
266 | }
|
---|
267 |
|
---|
268 | class ModelColumnReference extends ModelColumn
|
---|
269 | {
|
---|
270 | public string $RefTable;
|
---|
271 |
|
---|
272 | function __construct(string $Name, string $RefTable)
|
---|
273 | {
|
---|
274 | parent::__construct($Name, ModelColumnType::Reference);
|
---|
275 | $this->RefTable = $RefTable;
|
---|
276 | }
|
---|
277 | }
|
---|
278 |
|
---|
279 | class ModelColumnBoolean extends ModelColumn
|
---|
280 | {
|
---|
281 | public ?bool $Default;
|
---|
282 |
|
---|
283 | function __construct(string $Name)
|
---|
284 | {
|
---|
285 | parent::__construct($Name, ModelColumnType::Boolean);
|
---|
286 | $this->HasDefault = false;
|
---|
287 | $this->Default = null;
|
---|
288 | }
|
---|
289 |
|
---|
290 | function GetDefault(): ?string
|
---|
291 | {
|
---|
292 | return $this->Default;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | class ModelColumnEnum extends ModelColumn
|
---|
297 | {
|
---|
298 | public ?bool $Default;
|
---|
299 | public array $States;
|
---|
300 |
|
---|
301 | function __construct(string $Name, array $States)
|
---|
302 | {
|
---|
303 | parent::__construct($Name, ModelColumnType::Enum);
|
---|
304 | $this->HasDefault = false;
|
---|
305 | $this->Default = null;
|
---|
306 | $this->States = $States;
|
---|
307 | }
|
---|
308 |
|
---|
309 | function GetDefault(): ?string
|
---|
310 | {
|
---|
311 | return $this->Default;
|
---|
312 | }
|
---|
313 | }
|
---|