1: | <?php |
2: | |
3: | |
4: | |
5: | |
6: | |
7: | |
8: | namespace Aurora\System\Db; |
9: | |
10: | |
11: | |
12: | |
13: | |
14: | |
15: | |
16: | |
17: | |
18: | class Key |
19: | { |
20: | public const TYPE_KEY = 0; |
21: | public const TYPE_UNIQUE_KEY = 1; |
22: | public const TYPE_PRIMARY_KEY = 2; |
23: | public const TYPE_INDEX = 3; |
24: | public const TYPE_FULLTEXT = 4; |
25: | |
26: | |
27: | |
28: | |
29: | protected $iType; |
30: | |
31: | |
32: | |
33: | |
34: | protected $aFields; |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | public function __construct($iType, array $aFields) |
42: | { |
43: | $this->iType = $iType; |
44: | $this->aFields = $aFields; |
45: | } |
46: | |
47: | |
48: | |
49: | |
50: | public function GetType() |
51: | { |
52: | return $this->iType; |
53: | } |
54: | |
55: | |
56: | |
57: | |
58: | |
59: | public function GetName($sTableName) |
60: | { |
61: | $aList = $this->aFields; |
62: | sort($aList); |
63: | return strtoupper($sTableName.'_'.implode('_', $aList).'_INDEX'); |
64: | } |
65: | |
66: | |
67: | |
68: | |
69: | public function GetIndexesFields() |
70: | { |
71: | return $this->aFields; |
72: | } |
73: | |
74: | |
75: | |
76: | |
77: | public function ToString($oHelper, $sTableName) |
78: | { |
79: | $sResult = ''; |
80: | if (0 < count($this->aFields)) { |
81: | switch ($this->iType) { |
82: | case Key::TYPE_PRIMARY_KEY: |
83: | $sResult .= 'PRIMARY KEY'; |
84: | break; |
85: | case Key::TYPE_UNIQUE_KEY: |
86: | $sResult .= 'UNIQUE '.$oHelper->EscapeColumn($this->GetName($sTableName)); |
87: | break; |
88: | case Key::TYPE_INDEX: |
89: | $sResult .= 'INDEX '.$oHelper->EscapeColumn($this->GetName($sTableName)); |
90: | break; |
91: | |
92: | |
93: | |
94: | } |
95: | |
96: | $aValues = array_map(array(&$oHelper, 'EscapeColumn'), $this->aFields); |
97: | $sResult .= ' ('.implode(', ', $aValues).')'; |
98: | } |
99: | |
100: | return trim($sResult); |
101: | } |
102: | |
103: | |
104: | |
105: | |
106: | public function ToSingleIndexRequest($oHelper, $sTableName) |
107: | { |
108: | return $oHelper->CreateIndexRequest($this->iType, $sTableName, $this->GetName($sTableName), $this->aFields); |
109: | } |
110: | } |
111: | |