Changeset 889 for trunk/Packages/Common
- Timestamp:
- Dec 22, 2020, 11:34:19 PM (4 years ago)
- Location:
- trunk/Packages/Common
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Packages/Common/AppModule.php
r887 r889 184 184 $this->Models[get_class($Model)] = $Model; 185 185 } 186 187 function InstallModel(ModelDesc $ModelDesc) 188 { 189 $Query = 'CREATE TABLE IF NOT EXISTS `'.$ModelDesc->Name.'` ('."\n"; 190 $Query = ' `'.$ModelDesc->PrimaryKey.'` int(11) NOT NULL AUTO_INCREMENT,'."\n"; 191 foreach ($ModelDesc->Columns as $Column) 192 { 193 $Query .= " `".$Column->Name."` "; 194 if ($Column->Type == ModelColumnType::Integer) $Query .= 'int(11)'; 195 else if ($Column->Type == ModelColumnType::String) $Query .= 'varchar(255)'; 196 else if ($Column->Type == ModelColumnType::Float) $Query .= 'varchar(255)'; 197 else if ($Column->Type == ModelColumnType::Text) $Query .= 'text'; 198 else if ($Column->Type == ModelColumnType::DateTime) $Query .= 'datetime'; 199 else if ($Column->Type == ModelColumnType::Reference) $Query .= 'int(11)'; 200 201 if ($Column->Nullable) $Query .= ''; 202 else $Query .= ' NOT NULL'; 203 204 $Query .= ' COLLATE utf8_general_ci'; 205 206 if ($Column->HasDefault) 207 { 208 if ($Column->Default == null) 209 $Query .= ' DEFAULT NULL'; 210 $Query .= ' DEFAULT '.$Column->GetDefault(); 211 } 212 $Query .= ",\n"; 213 } 214 $Query .= ' PRIMARY KEY (`'.$ModelDesc->PrimaryKey.'`),'; 215 foreach ($ModelDesc->Columns as $Column) 216 { 217 if ($Column->Type == ModelColumnType::Reference) 218 $Query .= ' KEY `'.$Column->Name.'` (`'.$Column->Name.'`)'."\n"; 219 else if ($Column->Unique) 220 $Query .= ' UNIQUE KEY `'.$Column->Name.'` (`'.$Column->Name.'`)'."\n"; 221 } 222 223 $Query .= ") ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1;"; 224 $I = 1; 225 foreach ($ModelDesc->Columns as $Column) 226 { 227 if ($Column->Type == ModelColumnType::Reference) 228 $Query .= "ALTER TABLE `".$ModelDesc->Name."` ". 229 "ADD CONSTRAINT `".$ModelDesc->Name."_ibfk_".$I."` FOREIGN KEY (`".$Column->Name."`) REFERENCES `".$Column->RefTable."` (`Id`);"; 230 } 231 $this->Database->query($Query); 232 } 233 234 function UninstallModel(ModelDesc $ModelDesc) 235 { 236 $this->Database->query('DROP TABLE `'.$ModelDesc->Name.'`'); 237 } 186 238 } 187 239 -
trunk/Packages/Common/Base.php
r887 r889 25 25 class Model extends Base 26 26 { 27 28 27 } 29 28 30 29 class View extends Base 31 30 { 32 33 31 } 34 32 35 33 class Controller extends Base 36 34 { 35 } 37 36 37 class ModelDesc 38 { 39 public string $Name; 40 public bool $Nullable; 41 public array $Columns; 42 public array $Indices; 43 public string $PrimaryKey; 44 public bool $Unique; 45 public bool $HasDefault; 46 47 function __construct(string $Name, bool $Nullable = false, bool $Unique = false) 48 { 49 $this->Name = $Name; 50 $this->Columns = array(); 51 $this->Nullable = $Nullable; 52 $this->PrimaryKey = 'Id'; 53 $this->Unique = $Unique; 54 } 55 56 function AddString(string $Name): ModelColumnString 57 { 58 $Result = new ModelColumnString($Name); 59 $this->Columns[] = $Result; 60 return $Result; 61 } 62 63 function AddInteger(string $Name): ModelColumnInteger 64 { 65 $Result = new ModelColumnInteger($Name); 66 $this->Columns[] = $Result; 67 return $Result; 68 } 69 70 function AddDateTime(string $Name): ModelColumnDateTime 71 { 72 $Result = new ModelColumnDateTime($Name); 73 $this->Columns[] = $Result; 74 return $Result; 75 } 76 77 function AddReference(string $Name, string $RefTable): ModelColumnReference 78 { 79 $Result = new ModelColumnReference($Name, $RefTable); 80 $this->Columns[] = $Result; 81 return $Result; 82 } 38 83 } 84 85 class ModelColumnType 86 { 87 const Integer = 0; 88 const String = 1; 89 const Float = 2; 90 const Text = 3; 91 const DateTime = 4; 92 const Reference = 5; 93 } 94 95 class ModelColumn 96 { 97 public string $Name; 98 public ModelColumnType $Type; 99 public 100 101 function __construct(string $Name, int $Type) 102 { 103 $this->Name = $Name; 104 $this->Type = $Type; 105 } 106 107 function GetDefault(): ?string 108 { 109 return null; 110 } 111 } 112 113 class ModelColumnString extends ModelColumn 114 { 115 public ?string $Default; 116 117 function __construct(string $Name) 118 { 119 parent::__construct($Name, ModelColumnType::String); 120 $this->HasDefault = false; 121 $this->Default = null; 122 } 123 124 function GetDefault(): ?string 125 { 126 return '"'.$this->Default.'"'; 127 } 128 } 129 130 class ModelColumnText extends ModelColumn 131 { 132 public ?string $Default; 133 134 function __construct(string $Name) 135 { 136 parent::__construct($Name, ModelColumnType::Text); 137 $this->HasDefault = false; 138 $this->Default = null; 139 } 140 141 function GetDefault(): ?string 142 { 143 return '"'.$this->Default.'"'; 144 } 145 } 146 147 class ModelColumnInteger extends ModelColumn 148 { 149 public ?int $Default; 150 151 function __construct(string $Name) 152 { 153 parent::__construct($Name, ModelColumnType::Integer); 154 $this->HasDefault = false; 155 $this->Default = null; 156 } 157 158 function GetDefault(): ?string 159 { 160 return $this->Default; 161 } 162 } 163 164 class ModelColumnDateTime extends ModelColumn 165 { 166 public ?DateTime $Default; 167 168 function __construct(string $Name) 169 { 170 parent::__construct($Name, ModelColumnType::DateTime); 171 $this->HasDefault = false; 172 $this->Default = null; 173 } 174 175 function GetDefault(): ?string 176 { 177 return '"'.$this->Default.'"'; 178 } 179 } 180 181 class ModelColumnReference extends ModelColumn 182 { 183 public string $RefTable; 184 185 function __construct(string $Name, string $RefTable) 186 { 187 parent::__construct($Name, ModelColumnType::Reference); 188 $this->RefTable = $RefTable; 189 } 190 }
Note:
See TracChangeset
for help on using the changeset viewer.