Total Complexity | 87 |
Total Lines | 259 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like SQL often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class SQL |
||
23 | { |
||
24 | protected static $aggregateFieldConfigs; |
||
25 | |||
26 | /** |
||
27 | * This is how MySQL escapes it's string under the hood. |
||
28 | * Keep it. We don't need a database connection to escape strings. |
||
29 | * |
||
30 | * @param string $str String to escape. |
||
31 | * @return string Escaped string. |
||
32 | */ |
||
33 | public static function escape($str) |
||
34 | { |
||
35 | return str_replace( |
||
36 | ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"], |
||
37 | ["\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z"], |
||
38 | $str |
||
39 | ); |
||
40 | } |
||
41 | |||
42 | public static function compileFields($recordClass, $historyVariant = false) |
||
72 | } |
||
73 | |||
74 | public static function getFullTextColumns($recordClass) |
||
75 | { |
||
76 | $fulltextColumns = []; |
||
77 | $fields = static::getAggregateFieldOptions($recordClass); |
||
78 | |||
79 | foreach ($fields as $fieldId => $field) { |
||
80 | if ($field['columnName'] == 'RevisionID') { |
||
81 | continue; |
||
82 | } |
||
83 | |||
84 | if (!empty($field['fulltext'])) { |
||
85 | $fulltextColumns[] = $field['columnName']; |
||
86 | } |
||
87 | } |
||
88 | |||
89 | return $fulltextColumns; |
||
90 | } |
||
91 | |||
92 | public static function getContextIndex($recordClass) |
||
93 | { |
||
94 | return 'KEY `CONTEXT` (`'.$recordClass::getColumnName('ContextClass').'`,`'.$recordClass::getColumnName('ContextID').'`)'; |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * Generates a MySQL create table query from a Divergence\Models\ActiveRecord class. |
||
99 | * |
||
100 | * @param string $recordClass Class name |
||
101 | * @param boolean $historyVariant |
||
102 | * @return string |
||
103 | */ |
||
104 | public static function getCreateTable($recordClass, $historyVariant = false) |
||
105 | { |
||
106 | $indexes = $historyVariant ? [] : $recordClass::$indexes; |
||
|
|||
107 | $fulltextColumns = []; |
||
108 | $queryString = []; |
||
109 | |||
110 | |||
111 | // history table revisionID field |
||
112 | if ($historyVariant) { |
||
113 | $queryString[] = '`RevisionID` int(10) unsigned NOT NULL auto_increment'; |
||
114 | $queryString[] = 'PRIMARY KEY (`RevisionID`)'; |
||
115 | } |
||
116 | |||
117 | $queryString = array_merge($queryString, static::compileFields($recordClass, $historyVariant)); |
||
118 | |||
119 | if (!$historyVariant) { |
||
120 | // If ContextClass && ContextID are members of this model let's index them |
||
121 | if ($recordClass::fieldExists('ContextClass') && $recordClass::fieldExists('ContextID')) { |
||
122 | $queryString[] = static::getContextIndex($recordClass); |
||
123 | } |
||
124 | |||
125 | $fulltextColumns = static::getFullTextColumns($recordClass); |
||
126 | } |
||
127 | |||
128 | // compile indexes |
||
129 | foreach ($indexes as $indexName => $index) { |
||
130 | |||
131 | // translate field names |
||
132 | foreach ($index['fields'] as &$indexField) { |
||
133 | $indexField = $recordClass::getColumnName($indexField); |
||
134 | } |
||
135 | |||
136 | if (!empty($index['fulltext'])) { |
||
137 | $fulltextColumns = array_unique(array_merge($fulltextColumns, $index['fields'])); |
||
138 | continue; |
||
139 | } |
||
140 | |||
141 | $queryString[] = sprintf( |
||
142 | '%s KEY `%s` (`%s`)', |
||
143 | !empty($index['unique']) ? 'UNIQUE' : '', |
||
144 | $indexName, |
||
145 | join('`,`', $index['fields']) |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | if (!empty($fulltextColumns)) { |
||
150 | $queryString[] = 'FULLTEXT KEY `FULLTEXT` (`'.join('`,`', $fulltextColumns).'`)'; |
||
151 | } |
||
152 | |||
153 | |||
154 | $createSQL = sprintf( |
||
155 | "CREATE TABLE IF NOT EXISTS `%s` (\n\t%s\n) ENGINE=MyISAM DEFAULT CHARSET=utf8;", |
||
156 | $historyVariant ? $recordClass::getHistoryTable() : $recordClass::$tableName, |
||
157 | join("\n\t,", $queryString) |
||
158 | ); |
||
159 | |||
160 | // append history table SQL |
||
161 | if (!$historyVariant && is_subclass_of($recordClass, 'VersionedRecord')) { |
||
162 | $createSQL .= PHP_EOL.PHP_EOL.PHP_EOL.static::getCreateTable($recordClass, true); |
||
163 | } |
||
164 | return $createSQL; |
||
165 | } |
||
166 | |||
167 | public static function getSQLType($field) |
||
223 | } |
||
224 | } |
||
225 | |||
226 | public static function getFieldDefinition($recordClass, $fieldName, $historyVariant = false) |
||
269 | } |
||
270 | |||
271 | protected static function getAggregateFieldOptions($recordClass, $field = null) |
||
281 | } |
||
282 | } |
||
283 | } |
||
284 |