Total Complexity | 85 |
Total Lines | 257 |
Duplicated Lines | 0 % |
Changes | 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 |
||
23 | class SQL |
||
24 | { |
||
25 | protected static $aggregateFieldConfigs; |
||
26 | |||
27 | /** |
||
28 | * This is how MySQL escapes it's string under the hood. |
||
29 | * Keep it. We don't need a database connection to escape strings. |
||
30 | * |
||
31 | * @param string $str String to escape. |
||
32 | * @return string Escaped string. |
||
33 | */ |
||
34 | public static function escape($str) |
||
35 | { |
||
36 | return str_replace( |
||
37 | ["\\", "\x00", "\n", "\r", "'", '"', "\x1a"], |
||
38 | ["\\\\","\\0","\\n", "\\r", "\'", '\"', "\\Z"], |
||
39 | $str |
||
40 | ); |
||
41 | } |
||
42 | |||
43 | public static function compileFields($recordClass,$historyVariant = false) |
||
73 | } |
||
74 | |||
75 | public static function getFullTextColumns($recordClass) |
||
76 | { |
||
77 | $fulltextColumns = []; |
||
78 | $fields = static::getAggregateFieldOptions($recordClass); |
||
79 | |||
80 | foreach ($fields as $fieldId => $field) { |
||
81 | if ($field['columnName'] == 'RevisionID') { |
||
82 | continue; |
||
83 | } |
||
84 | |||
85 | if (!empty($field['fulltext'])) { |
||
86 | $fulltextColumns[] = $field['columnName']; |
||
87 | } |
||
88 | } |
||
89 | |||
90 | return $fulltextColumns; |
||
91 | } |
||
92 | |||
93 | public static function getContextIndex($recordClass) |
||
94 | { |
||
95 | return 'KEY `CONTEXT` (`'.$recordClass::getColumnName('ContextClass').'`,`'.$recordClass::getColumnName('ContextID').'`)'; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Generates a MySQL create table query from a Divergence\Models\ActiveRecord class. |
||
100 | * |
||
101 | * @param string $recordClass Class name |
||
102 | * @param boolean $historyVariant |
||
103 | * @return void |
||
104 | */ |
||
105 | public static function getCreateTable($recordClass, $historyVariant = false) |
||
106 | { |
||
107 | $indexes = $historyVariant ? [] : $recordClass::$indexes; |
||
|
|||
108 | $fulltextColumns = []; |
||
109 | $queryString = []; |
||
110 | |||
111 | |||
112 | // history table revisionID field |
||
113 | if ($historyVariant) { |
||
114 | $queryString[] = '`RevisionID` int(10) unsigned NOT NULL auto_increment'; |
||
115 | $queryString[] = 'PRIMARY KEY (`RevisionID`)'; |
||
116 | } |
||
117 | |||
118 | $queryString = array_merge($queryString,static::compileFields($recordClass, $historyVariant)); |
||
119 | |||
120 | if(!$historyVariant) { |
||
121 | // If ContextClass && ContextID are members of this model let's index them |
||
122 | if ($recordClass::fieldExists('ContextClass') && $recordClass::fieldExists('ContextID')) { |
||
123 | $queryString[] = static::getContextIndex($recordClass); |
||
124 | } |
||
125 | |||
126 | $fulltextColumns = static::getFullTextColumns($recordClass); |
||
127 | } |
||
128 | |||
129 | // compile indexes |
||
130 | foreach ($indexes as $indexName => $index) { |
||
131 | |||
132 | // translate field names |
||
133 | foreach ($index['fields'] as &$indexField) { |
||
134 | $indexField = $recordClass::getColumnName($indexField); |
||
135 | } |
||
136 | |||
137 | if (!empty($index['fulltext'])) { |
||
138 | $fulltextColumns = array_unique(array_merge($fulltextColumns, $index['fields'])); |
||
139 | continue; |
||
140 | } |
||
141 | |||
142 | $queryString[] = sprintf( |
||
143 | '%s KEY `%s` (`%s`)', |
||
144 | !empty($index['unique']) ? 'UNIQUE' : '', |
||
145 | $indexName, |
||
146 | join('`,`', $index['fields']) |
||
147 | ); |
||
148 | } |
||
149 | |||
150 | if (!empty($fulltextColumns)) { |
||
151 | $queryString[] = 'FULLTEXT KEY `FULLTEXT` (`'.join('`,`', $fulltextColumns).'`)'; |
||
152 | } |
||
153 | |||
154 | |||
155 | $createSQL = sprintf( |
||
156 | "CREATE TABLE IF NOT EXISTS `%s` (\n\t%s\n) ENGINE=MyISAM DEFAULT CHARSET=utf8;", |
||
157 | $historyVariant ? $recordClass::getHistoryTable() : $recordClass::$tableName, |
||
158 | join("\n\t,", $queryString) |
||
159 | ); |
||
160 | |||
161 | // append history table SQL |
||
162 | if (!$historyVariant && is_subclass_of($recordClass, 'VersionedRecord')) { |
||
163 | $createSQL .= PHP_EOL.PHP_EOL.PHP_EOL.static::getCreateTable($recordClass, true); |
||
164 | } |
||
165 | return $createSQL; |
||
166 | } |
||
167 | |||
168 | public static function getSQLType($field) |
||
222 | } |
||
223 | } |
||
224 | |||
225 | public static function getFieldDefinition($recordClass, $fieldName, $historyVariant = false) |
||
268 | } |
||
269 | |||
270 | protected static function getAggregateFieldOptions($recordClass, $field = null) |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 |