source: trunk/Packages/Common/ModelDesc.php

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