1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
/** |
5
|
|
|
* Database Access Layer. |
6
|
|
|
* @author Doug Wright |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace DVDoug\DB; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Metadata about a database column. |
13
|
|
|
* @author Doug Wright |
14
|
|
|
*/ |
15
|
|
|
trait DDLGeneration |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Get MySQL column definition. |
19
|
|
|
*/ |
20
|
8 |
|
public function getMySQLColumnDef(): string |
21
|
|
|
{ |
22
|
8 |
|
$def = '`' . strtolower($this->getName()) . '` '; |
|
|
|
|
23
|
8 |
|
$MySQLType = $this->getMySQLType(); |
|
|
|
|
24
|
8 |
|
if (strpos($MySQLType, 'UNSIGNED') !== false) { |
25
|
4 |
|
$unsigned = true; |
26
|
4 |
|
$MySQLType = substr($MySQLType, 0, -9); |
27
|
|
|
} else { |
28
|
8 |
|
$unsigned = false; |
29
|
|
|
} |
30
|
|
|
|
31
|
8 |
|
if (in_array($MySQLType, ['ENUM', 'SET'])) { |
32
|
4 |
|
$query = sprintf('SHOW COLUMNS FROM %s.%s LIKE %s', |
33
|
4 |
|
$this->connection->quoteIdentifier($this->database), |
34
|
4 |
|
$this->connection->quoteIdentifier($this->table), |
35
|
4 |
|
$this->connection->escape($this->name)); |
36
|
|
|
|
37
|
4 |
|
$statement = $this->connection->query($query); |
38
|
4 |
|
$values = $statement->fetchAssoc(false)['Type']; |
39
|
4 |
|
$values = explode("','", substr($values, strpos($values, '(') + 2, -2)); |
40
|
4 |
|
$values = array_intersect_key($values, array_unique(array_map('strtolower', $values))); |
41
|
4 |
|
asort($values); |
42
|
|
|
|
43
|
4 |
|
$def .= $MySQLType; |
44
|
4 |
|
$def .= '(' . implode(', ', array_map(function ($c) {return "'" . addslashes($c) . "'"; }, $values)) . ')'; |
45
|
8 |
|
} elseif (in_array($MySQLType, ['CHAR', 'VARCHAR']) && $this->getLength() < 64 && $this->getDistinctValueCount() <= 16) { |
|
|
|
|
46
|
4 |
|
$query = sprintf('SELECT DISTINCT %s FROM %s.%s WHERE %s IS NOT NULL ORDER BY %s ASC', |
47
|
4 |
|
$this->connection->quoteIdentifier($this->name), |
48
|
4 |
|
$this->connection->quoteIdentifier($this->database), |
49
|
4 |
|
$this->connection->quoteIdentifier($this->table), |
50
|
4 |
|
$this->connection->quoteIdentifier($this->name), |
51
|
4 |
|
$this->connection->quoteIdentifier($this->name)); |
52
|
4 |
|
$values = []; |
53
|
4 |
|
foreach ($this->connection->query($query) as $value) { |
54
|
4 |
|
$values[] = trim($value[$this->name]); |
55
|
|
|
} |
56
|
4 |
|
$values = array_intersect_key($values, array_unique(array_map('strtolower', $values))); |
57
|
4 |
|
asort($values); |
58
|
|
|
|
59
|
4 |
|
if ($values) { |
60
|
4 |
|
$def .= 'ENUM'; |
61
|
4 |
|
$def .= '(' . implode(', ', array_map(function ($c) {return "'" . addslashes($c) . "'"; }, $values)) . ')'; |
62
|
|
|
} else { |
63
|
4 |
|
$def .= $MySQLType; |
64
|
4 |
|
if ($this->getLength() > 0) { |
65
|
4 |
|
$def .= '(' . $this->getLength() . ')'; |
66
|
|
|
} |
67
|
|
|
} |
68
|
8 |
|
} elseif (in_array($MySQLType, ['DATETIME', 'TIMESTAMP', 'TIME'])) { |
69
|
|
|
$def .= $MySQLType; |
70
|
|
|
$def .= '(' . (int) $this->getScale() . ')'; |
|
|
|
|
71
|
|
|
} else { |
72
|
8 |
|
$def .= $MySQLType; |
73
|
|
|
|
74
|
8 |
|
if ($this->getScale() && !in_array($MySQLType, ['DATE'])) { |
75
|
|
|
$def .= '(' . $this->getPrecision() . ',' . $this->getScale() . ')'; |
|
|
|
|
76
|
8 |
|
} elseif ($this->getPrecision() && !in_array($MySQLType, ['TINYINT', 'SMALLINT', 'MEDIUMINT', 'INT', 'BIGINT'])) { |
77
|
|
|
$def .= '(' . $this->getPrecision() . ')'; |
78
|
8 |
|
} elseif ($this->getLength() > 0 && !in_array($MySQLType, ['TINYINT', 'SMALLINT', 'MEDIUMINT', 'INT', 'BIGINT'])) { |
79
|
4 |
|
$def .= '(' . $this->getLength() . ')'; |
80
|
|
|
} |
81
|
|
|
|
82
|
8 |
|
if ($unsigned) { |
83
|
4 |
|
$def .= ' UNSIGNED'; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
8 |
|
if ($this->isNullable()) { |
|
|
|
|
88
|
8 |
|
$def .= ' NULL'; |
89
|
|
|
} else { |
90
|
8 |
|
$def .= ' NOT NULL'; |
91
|
|
|
} |
92
|
|
|
|
93
|
8 |
|
return $def; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get Oracle column definition. |
98
|
|
|
*/ |
99
|
8 |
|
public function getOracleColumnDef(): string |
100
|
|
|
{ |
101
|
8 |
|
$def = '`' . strtolower($this->getName()) . '` '; |
102
|
|
|
|
103
|
8 |
|
$def .= $this->getOracleType(); |
|
|
|
|
104
|
|
|
|
105
|
8 |
|
if ($this->getScale()) { |
106
|
|
|
$def .= '(' . $this->getPrecision() . ',' . $this->getScale() . ')'; |
107
|
8 |
|
} elseif ($this->getPrecision()) { |
108
|
4 |
|
$def .= '(' . $this->getPrecision() . ')'; |
109
|
4 |
|
} elseif ($this->getLength()) { |
110
|
4 |
|
$def .= '(' . $this->getLength() . ')'; |
111
|
|
|
} |
112
|
|
|
|
113
|
8 |
|
if ($this->isNullable()) { |
114
|
8 |
|
$def .= ' NULL'; |
115
|
|
|
} else { |
116
|
8 |
|
$def .= ' NOT NULL'; |
117
|
|
|
} |
118
|
|
|
|
119
|
8 |
|
return $def; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|