fluent-community
/
vendor
/
wpfluent
/
framework
/
src
/
WPFluent
/
Database
/
Migration
/
ColumnDefinition.php
ColumnDefinition.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 2.6.01, at vendor/wpfluent/framework/src/WPFluent/Database/Migration/ColumnDefinition.php
| 1 | <?php |
| 2 | |
| 3 | namespace FluentCommunity\Framework\Database\Migration; |
| 4 | |
| 5 | use FluentCommunity\Framework\Database\Schema; |
| 6 | |
| 7 | class ColumnDefinition |
| 8 | { |
| 9 | protected $name; |
| 10 | protected $type = ''; |
| 11 | protected $isNullable = false; |
| 12 | protected $defaultValue = null; |
| 13 | protected $hasDefault = false; |
| 14 | protected $isUnsigned = false; |
| 15 | protected $afterColumn = null; |
| 16 | protected $isPrimary = false; |
| 17 | protected $isAutoIncrement = false; |
| 18 | protected $isUnique = false; |
| 19 | protected $isIndex = false; |
| 20 | protected $comment = null; |
| 21 | protected $charset = null; |
| 22 | protected $collation = null; |
| 23 | |
| 24 | public function __construct($name) |
| 25 | { |
| 26 | $this->name = $name; |
| 27 | } |
| 28 | |
| 29 | // ── Type Methods ───────────────────────────────────────── |
| 30 | |
| 31 | /** |
| 32 | * Auto-increment primary key (bigint unsigned). |
| 33 | */ |
| 34 | public function id() |
| 35 | { |
| 36 | $this->type = 'bigint(20) unsigned'; |
| 37 | $this->isAutoIncrement = true; |
| 38 | $this->isPrimary = true; |
| 39 | $this->isNullable = false; |
| 40 | return $this; |
| 41 | } |
| 42 | |
| 43 | public function string($length = 255) |
| 44 | { |
| 45 | $this->type = "varchar({$length})"; |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | public function char($length = 1) |
| 50 | { |
| 51 | $this->type = "char({$length})"; |
| 52 | return $this; |
| 53 | } |
| 54 | |
| 55 | public function text() |
| 56 | { |
| 57 | $this->type = 'text'; |
| 58 | return $this; |
| 59 | } |
| 60 | |
| 61 | public function mediumText() |
| 62 | { |
| 63 | $this->type = 'mediumtext'; |
| 64 | return $this; |
| 65 | } |
| 66 | |
| 67 | public function longText() |
| 68 | { |
| 69 | $this->type = 'longtext'; |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | public function integer() |
| 74 | { |
| 75 | $this->type = 'int'; |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | public function bigInteger() |
| 80 | { |
| 81 | $this->type = 'bigint(20)'; |
| 82 | return $this; |
| 83 | } |
| 84 | |
| 85 | public function tinyInteger() |
| 86 | { |
| 87 | $this->type = 'tinyint'; |
| 88 | return $this; |
| 89 | } |
| 90 | |
| 91 | public function smallInteger() |
| 92 | { |
| 93 | $this->type = 'smallint'; |
| 94 | return $this; |
| 95 | } |
| 96 | |
| 97 | public function mediumInteger() |
| 98 | { |
| 99 | $this->type = 'mediumint'; |
| 100 | return $this; |
| 101 | } |
| 102 | |
| 103 | public function decimal($precision = 8, $scale = 2) |
| 104 | { |
| 105 | $this->type = "decimal({$precision},{$scale})"; |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | public function float() |
| 110 | { |
| 111 | $this->type = 'float'; |
| 112 | return $this; |
| 113 | } |
| 114 | |
| 115 | public function double() |
| 116 | { |
| 117 | $this->type = 'double'; |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | public function boolean() |
| 122 | { |
| 123 | $this->type = 'tinyint(1)'; |
| 124 | return $this; |
| 125 | } |
| 126 | |
| 127 | public function json() |
| 128 | { |
| 129 | $this->type = Schema::jsonType(); |
| 130 | return $this; |
| 131 | } |
| 132 | |
| 133 | public function binary($length = 255) |
| 134 | { |
| 135 | $this->type = "binary({$length})"; |
| 136 | return $this; |
| 137 | } |
| 138 | |
| 139 | public function blob() |
| 140 | { |
| 141 | $this->type = 'blob'; |
| 142 | return $this; |
| 143 | } |
| 144 | |
| 145 | public function mediumBlob() |
| 146 | { |
| 147 | $this->type = 'mediumblob'; |
| 148 | return $this; |
| 149 | } |
| 150 | |
| 151 | public function longBlob() |
| 152 | { |
| 153 | $this->type = 'longblob'; |
| 154 | return $this; |
| 155 | } |
| 156 | |
| 157 | public function enum(array $values) |
| 158 | { |
| 159 | if (Schema::isSqlite()) { |
| 160 | $this->type = 'varchar(255)'; |
| 161 | } else { |
| 162 | $quoted = array_map(function ($v) { |
| 163 | return "'" . addslashes($v) . "'"; |
| 164 | }, $values); |
| 165 | $this->type = 'enum(' . implode(',', $quoted) . ')'; |
| 166 | } |
| 167 | return $this; |
| 168 | } |
| 169 | |
| 170 | public function set(array $values) |
| 171 | { |
| 172 | if (Schema::isSqlite()) { |
| 173 | $this->type = 'varchar(255)'; |
| 174 | } else { |
| 175 | $quoted = array_map(function ($v) { |
| 176 | return "'" . addslashes($v) . "'"; |
| 177 | }, $values); |
| 178 | $this->type = 'set(' . implode(',', $quoted) . ')'; |
| 179 | } |
| 180 | return $this; |
| 181 | } |
| 182 | |
| 183 | public function timestamp() |
| 184 | { |
| 185 | $this->type = 'timestamp'; |
| 186 | return $this; |
| 187 | } |
| 188 | |
| 189 | public function date() |
| 190 | { |
| 191 | $this->type = 'date'; |
| 192 | return $this; |
| 193 | } |
| 194 | |
| 195 | public function datetime() |
| 196 | { |
| 197 | $this->type = 'datetime'; |
| 198 | return $this; |
| 199 | } |
| 200 | |
| 201 | public function time() |
| 202 | { |
| 203 | $this->type = 'time'; |
| 204 | return $this; |
| 205 | } |
| 206 | |
| 207 | public function year() |
| 208 | { |
| 209 | $this->type = 'year'; |
| 210 | return $this; |
| 211 | } |
| 212 | |
| 213 | // ── Modifier Methods ───────────────────────────────────── |
| 214 | |
| 215 | public function nullable() |
| 216 | { |
| 217 | $this->isNullable = true; |
| 218 | return $this; |
| 219 | } |
| 220 | |
| 221 | public function default($value) |
| 222 | { |
| 223 | $this->hasDefault = true; |
| 224 | $this->defaultValue = $value; |
| 225 | return $this; |
| 226 | } |
| 227 | |
| 228 | public function unsigned() |
| 229 | { |
| 230 | $this->isUnsigned = true; |
| 231 | return $this; |
| 232 | } |
| 233 | |
| 234 | public function after($column) |
| 235 | { |
| 236 | $this->afterColumn = $column; |
| 237 | return $this; |
| 238 | } |
| 239 | |
| 240 | public function primary() |
| 241 | { |
| 242 | $this->isPrimary = true; |
| 243 | return $this; |
| 244 | } |
| 245 | |
| 246 | public function autoIncrement() |
| 247 | { |
| 248 | $this->isAutoIncrement = true; |
| 249 | return $this; |
| 250 | } |
| 251 | |
| 252 | public function unique() |
| 253 | { |
| 254 | $this->isUnique = true; |
| 255 | return $this; |
| 256 | } |
| 257 | |
| 258 | public function index() |
| 259 | { |
| 260 | $this->isIndex = true; |
| 261 | return $this; |
| 262 | } |
| 263 | |
| 264 | public function comment($comment) |
| 265 | { |
| 266 | $this->comment = $comment; |
| 267 | return $this; |
| 268 | } |
| 269 | |
| 270 | public function charset($charset) |
| 271 | { |
| 272 | $this->charset = $charset; |
| 273 | return $this; |
| 274 | } |
| 275 | |
| 276 | public function collation($collation) |
| 277 | { |
| 278 | $this->collation = $collation; |
| 279 | return $this; |
| 280 | } |
| 281 | |
| 282 | // ── SQL Generation ─────────────────────────────────────── |
| 283 | |
| 284 | public function getName() |
| 285 | { |
| 286 | return $this->name; |
| 287 | } |
| 288 | |
| 289 | public function isUnique() |
| 290 | { |
| 291 | return $this->isUnique; |
| 292 | } |
| 293 | |
| 294 | public function isIndex() |
| 295 | { |
| 296 | return $this->isIndex; |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Column definition for CREATE TABLE (used by dbDelta). |
| 301 | * e.g. "status varchar(20) NOT NULL DEFAULT 'pending'" |
| 302 | */ |
| 303 | public function toSql() |
| 304 | { |
| 305 | $parts = [$this->name, $this->type]; |
| 306 | |
| 307 | if ($this->isUnsigned && !str_contains($this->type, 'unsigned')) { |
| 308 | $parts[] = 'unsigned'; |
| 309 | } |
| 310 | |
| 311 | if ($this->charset && !Schema::isSqlite()) { |
| 312 | $parts[] = 'CHARACTER SET ' . $this->charset; |
| 313 | } |
| 314 | |
| 315 | if ($this->collation && !Schema::isSqlite()) { |
| 316 | $parts[] = 'COLLATE ' . $this->collation; |
| 317 | } |
| 318 | |
| 319 | if ($this->isAutoIncrement) { |
| 320 | $parts[] = 'NOT NULL'; |
| 321 | $parts[] = 'AUTO_INCREMENT'; |
| 322 | } else { |
| 323 | $parts[] = $this->isNullable ? 'NULL' : 'NOT NULL'; |
| 324 | } |
| 325 | |
| 326 | if ($this->hasDefault) { |
| 327 | $parts[] = 'DEFAULT ' . $this->formatDefault($this->defaultValue); |
| 328 | } |
| 329 | |
| 330 | if ($this->isPrimary && !$this->isAutoIncrement) { |
| 331 | $parts[] = 'PRIMARY KEY'; |
| 332 | } |
| 333 | |
| 334 | if ($this->comment !== null && !Schema::isSqlite()) { |
| 335 | $parts[] = "COMMENT '" . addslashes($this->comment) . "'"; |
| 336 | } |
| 337 | |
| 338 | return implode(' ', $parts); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * For ALTER TABLE ADD COLUMN. |
| 343 | */ |
| 344 | public function toAlterAddSql() |
| 345 | { |
| 346 | $sql = 'ADD COLUMN ' . $this->toSql(); |
| 347 | |
| 348 | if ($this->isUnique) { |
| 349 | $sql .= ' UNIQUE'; |
| 350 | } |
| 351 | |
| 352 | if ($this->afterColumn && !Schema::isSqlite()) { |
| 353 | $sql .= ' AFTER ' . $this->afterColumn; |
| 354 | } |
| 355 | |
| 356 | return $sql; |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * For ALTER TABLE MODIFY COLUMN. |
| 361 | */ |
| 362 | public function toAlterModifySql() |
| 363 | { |
| 364 | if (Schema::isSqlite()) { |
| 365 | return 'CHANGE COLUMN ' . $this->name . ' ' . $this->toSql(); |
| 366 | } |
| 367 | |
| 368 | $sql = 'MODIFY COLUMN ' . $this->toSql(); |
| 369 | |
| 370 | if ($this->afterColumn) { |
| 371 | $sql .= ' AFTER ' . $this->afterColumn; |
| 372 | } |
| 373 | |
| 374 | return $sql; |
| 375 | } |
| 376 | |
| 377 | protected function formatDefault($value) |
| 378 | { |
| 379 | if ($value === null) { |
| 380 | return 'NULL'; |
| 381 | } |
| 382 | |
| 383 | if (is_bool($value)) { |
| 384 | return $value ? '1' : '0'; |
| 385 | } |
| 386 | |
| 387 | if (is_int($value) || is_float($value)) { |
| 388 | return (string) $value; |
| 389 | } |
| 390 | |
| 391 | // Raw SQL expressions like CURRENT_TIMESTAMP |
| 392 | $raw = strtoupper($value); |
| 393 | if (in_array($raw, ['CURRENT_TIMESTAMP', 'NOW()', 'NULL'])) { |
| 394 | return $raw; |
| 395 | } |
| 396 | |
| 397 | return "'" . addslashes($value) . "'"; |
| 398 | } |
| 399 | } |
| 400 |