source: trunk/Packages/Common/ModelDesc.php

Last change on this file was 8, checked in by chronos, 18 months ago
  • Modified: Updated Common package.
  • Modified: Form types made as separate FormManager package.
  • Fixed: PHP 8.1 support.
File size: 6.3 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 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
103class 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
123class 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
146class ModelColumnString extends ModelColumn
147{
148 public ?string $Default;
149
150 function __construct(string $Name)
151 {
152 parent::__construct($Name, ModelColumnType::String);
153 $this->HasDefault = false;
154 $this->Default = null;
155 }
156
157 function GetDefault(): ?string
158 {
159 return '"'.$this->Default.'"';
160 }
161}
162
163class ModelColumnFloat extends ModelColumn
164{
165 public ?float $Default;
166
167 function __construct(string $Name)
168 {
169 parent::__construct($Name, ModelColumnType::Float);
170 $this->HasDefault = false;
171 $this->Default = null;
172 }
173
174 function GetDefault(): ?string
175 {
176 return '"'.$this->Default.'"';
177 }
178}
179
180class ModelColumnText extends ModelColumn
181{
182 public ?string $Default;
183 public int $MaxLength;
184
185 function __construct(string $Name)
186 {
187 parent::__construct($Name, ModelColumnType::Text);
188 $this->HasDefault = false;
189 $this->Default = null;
190 $this->MaxLength = 255;
191 }
192
193 function GetDefault(): ?string
194 {
195 return '"'.$this->Default.'"';
196 }
197}
198
199class ModelColumnInteger extends ModelColumn
200{
201 public ?int $Default;
202
203 function __construct(string $Name)
204 {
205 parent::__construct($Name, ModelColumnType::Integer);
206 $this->HasDefault = false;
207 $this->Default = null;
208 }
209
210 function GetDefault(): ?string
211 {
212 return $this->Default;
213 }
214}
215
216class ModelColumnBigInt extends ModelColumn
217{
218 public ?int $Default;
219
220 function __construct(string $Name)
221 {
222 parent::__construct($Name, ModelColumnType::BigInt);
223 $this->HasDefault = false;
224 $this->Default = null;
225 }
226
227 function GetDefault(): ?string
228 {
229 return $this->Default;
230 }
231}
232
233class ModelColumnDateTime extends ModelColumn
234{
235 public ?DateTime $Default;
236
237 function __construct(string $Name)
238 {
239 parent::__construct($Name, ModelColumnType::DateTime);
240 $this->HasDefault = false;
241 $this->Default = null;
242 }
243
244 function GetDefault(): ?string
245 {
246 return '"'.$this->Default.'"';
247 }
248}
249
250class ModelColumnDate extends ModelColumn
251{
252 public ?DateTime $Default;
253
254 function __construct(string $Name)
255 {
256 parent::__construct($Name, ModelColumnType::Date);
257 $this->HasDefault = false;
258 $this->Default = null;
259 }
260
261 function GetDefault(): ?string
262 {
263 return '"'.$this->Default.'"';
264 }
265}
266
267class ModelColumnReference extends ModelColumn
268{
269 public string $RefTable;
270
271 function __construct(string $Name, string $RefTable)
272 {
273 parent::__construct($Name, ModelColumnType::Reference);
274 $this->RefTable = $RefTable;
275 }
276}
277
278class ModelColumnBoolean extends ModelColumn
279{
280 public ?bool $Default;
281
282 function __construct(string $Name)
283 {
284 parent::__construct($Name, ModelColumnType::Boolean);
285 $this->HasDefault = false;
286 $this->Default = null;
287 }
288
289 function GetDefault(): ?string
290 {
291 return $this->Default;
292 }
293}
294
295class ModelColumnEnum extends ModelColumn
296{
297 public ?bool $Default;
298 public array $States;
299
300 function __construct(string $Name, array $States)
301 {
302 parent::__construct($Name, ModelColumnType::Enum);
303 $this->HasDefault = false;
304 $this->Default = null;
305 $this->States = $States;
306 }
307
308 function GetDefault(): ?string
309 {
310 return $this->Default;
311 }
312}
Note: See TracBrowser for help on using the repository browser.