| Total Complexity | 49 |
| Total Lines | 228 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Postgres 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 Postgres, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Postgres extends DialectAbstract{ |
||
| 18 | |||
| 19 | /** @inheritdoc */ |
||
| 20 | public function select(array $cols, array $from, string $where = null, $limit = null, $offset = null, bool $distinct = null, array $groupby = null, array $orderby = null):array{ |
||
| 21 | $sql = ['SELECT']; |
||
| 22 | |||
| 23 | if($distinct){ |
||
| 24 | $sql[] = 'DISTINCT'; |
||
| 25 | } |
||
| 26 | |||
| 27 | !empty($cols) |
||
| 28 | ? $sql[] = implode(', ', $cols) |
||
| 29 | : $sql[] = '*'; |
||
| 30 | |||
| 31 | $sql[] = 'FROM'; |
||
| 32 | $sql[] = implode(', ', $from); |
||
| 33 | $sql[] = $where; |
||
| 34 | |||
| 35 | if(!empty($groupby)){ |
||
| 36 | $sql[] = 'GROUP BY'; |
||
| 37 | $sql[] = implode(', ', $groupby); |
||
| 38 | } |
||
| 39 | |||
| 40 | if(!empty($orderby)){ |
||
| 41 | $sql[] = 'ORDER BY'; |
||
| 42 | $sql[] = implode(', ', $orderby); |
||
| 43 | } |
||
| 44 | |||
| 45 | if($offset !== null){ |
||
| 46 | $sql[] = 'OFFSET ?'; |
||
| 47 | } |
||
| 48 | |||
| 49 | if($limit !== null){ |
||
| 50 | $sql[] = 'LIMIT ?'; |
||
| 51 | } |
||
| 52 | |||
| 53 | return $sql; |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @link https://www.postgresql.org/docs/9.5/static/sql-insert.html#SQL-ON-CONFLICT |
||
| 58 | * |
||
| 59 | * @inheritdoc |
||
| 60 | */ |
||
| 61 | public function insert(string $table, array $fields, string $onConflict = null, string $conflictTarget = null):array{ |
||
| 62 | $sql = parent::insert($table, $fields); |
||
| 63 | |||
| 64 | if(in_array($onConflict, ['IGNORE', 'REPLACE'], true)){ |
||
| 65 | |||
| 66 | if(empty($conflictTarget)){ |
||
| 67 | throw new QueryException('postgres insert on conflict: no conflict target given'); |
||
| 68 | } |
||
| 69 | |||
| 70 | $sql[] = 'ON CONFLICT ('.$this->quote($conflictTarget).') DO'; |
||
| 71 | |||
| 72 | switch($onConflict){ |
||
| 73 | case 'IGNORE': |
||
| 74 | $sql[] = 'NOTHING'; |
||
| 75 | break; |
||
| 76 | case 'REPLACE': |
||
| 77 | $sql[] = $this->onConflictUpdate($fields); |
||
| 78 | break; |
||
| 79 | } |
||
| 80 | |||
| 81 | } |
||
| 82 | |||
| 83 | return $sql; |
||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param array $fields |
||
| 88 | * |
||
| 89 | * @return string |
||
| 90 | */ |
||
| 91 | protected function onConflictUpdate(array $fields):string { |
||
| 92 | $onConflictUpdate = []; |
||
| 93 | |||
| 94 | foreach($fields as $f){ |
||
| 95 | $onConflictUpdate[] = $this->quote($f).' = EXCLUDED.'.$this->quote($f); |
||
| 96 | } |
||
| 97 | |||
| 98 | return 'UPDATE SET '.implode(', ', $onConflictUpdate); |
||
| 99 | } |
||
| 100 | |||
| 101 | /** @inheritdoc */ |
||
| 102 | public function createDatabase(string $dbname, bool $ifNotExists = null, string $collate = null):array{ |
||
| 103 | $sql = ['CREATE DATABASE']; |
||
| 104 | $sql[] = $this->quote($dbname); |
||
| 105 | |||
| 106 | if($collate){ |
||
| 107 | $charset = explode(',', $collate, 3); |
||
| 108 | |||
| 109 | $count = count($charset); |
||
| 110 | |||
| 111 | if($count > 0){ |
||
| 112 | $sql[] = 'ENCODING \''.strtoupper($charset[0]).'\''; |
||
| 113 | } |
||
| 114 | |||
| 115 | if($count > 1){ |
||
| 116 | $sql[] = 'LC_COLLATE=\''.$charset[1].'\''; |
||
| 117 | } |
||
| 118 | |||
| 119 | if($count > 2){ |
||
| 120 | $sql[] = 'LC_CTYPE=\''.$charset[2].'\''; |
||
| 121 | } |
||
| 122 | |||
| 123 | } |
||
| 124 | |||
| 125 | return $sql; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** @inheritdoc */ |
||
| 129 | public function fieldspec(string $name, string $type, $length = null, string $attribute = null, string $collation = null, bool $isNull = null, string $defaultType = null, $defaultValue = null, string $extra = null):string{ |
||
| 130 | $name = trim($name); |
||
| 131 | $type = strtoupper(trim($type)); |
||
| 132 | |||
| 133 | $field = [$this->quote($name)]; |
||
| 134 | |||
| 135 | $type_translation = [ |
||
| 136 | 'TINYINT' => 'SMALLINT', |
||
| 137 | 'MEDIUMINT' => 'INT', |
||
| 138 | 'DOUBLE' => 'DOUBLE PRECISION', |
||
| 139 | 'TINYTEXT' => 'VARCHAR(255)', |
||
| 140 | 'DATETIME' => 'TIMESTAMP', |
||
| 141 | 'IMAGE' => 'BLOB', |
||
| 142 | 'MEDIUMTEXT' => 'TEXT', |
||
| 143 | 'LONGTEXT' => 'TEXT', |
||
| 144 | ][$type] ?? $type; |
||
| 145 | |||
| 146 | if((is_int($length) || is_string($length) && count(explode(',', $length)) === 2) |
||
|
|
|||
| 147 | && in_array($type, ['BIT', 'VARBIT', 'CHAR', 'VARCHAR', 'DECIMAL', 'NUMERIC', 'TIME', 'TIMESTAMP', 'INTERVAL'], true)){ |
||
| 148 | $field[] = $type_translation.'('.$length.')'; |
||
| 149 | } |
||
| 150 | else{ |
||
| 151 | $field[] = $type_translation; |
||
| 152 | } |
||
| 153 | |||
| 154 | if($collation && in_array($type, ['TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT', 'VARCHAR', 'CHAR'], true) |
||
| 155 | && !in_array(strtolower($collation), ['utf8'], true)){ |
||
| 156 | $field[] = 'COLLATE '.$collation; |
||
| 157 | } |
||
| 158 | |||
| 159 | if($isNull !== true){ |
||
| 160 | $field[] = 'NOT NULL'; |
||
| 161 | } |
||
| 162 | |||
| 163 | if($attribute){ |
||
| 164 | $field[] = strtoupper($attribute); |
||
| 165 | } |
||
| 166 | |||
| 167 | $defaultType = strtoupper($defaultType); |
||
| 168 | |||
| 169 | if($defaultType === 'USER_DEFINED'){ |
||
| 170 | |||
| 171 | switch(true){ |
||
| 172 | case $type === 'TIMESTAMP' && intval($defaultValue) === 0: |
||
| 173 | $field[] = 'DEFAULT 0'; |
||
| 174 | break; |
||
| 175 | case $type === 'BIT' || $type === 'VARBIT': |
||
| 176 | $field[] = 'DEFAULT b\''.preg_replace('/[^01]/', '0', $defaultValue).'\''; |
||
| 177 | break; |
||
| 178 | case $type === 'BOOLEAN': |
||
| 179 | $field[] = 'DEFAULT '.(preg_match('/^1|T|TRUE|YES$/i', $defaultValue) ? 'TRUE' : 'FALSE'); |
||
| 180 | break; |
||
| 181 | case strtoupper($defaultValue) === 'NULL' && $isNull === true: |
||
| 182 | $field[] = 'DEFAULT NULL'; |
||
| 183 | break; |
||
| 184 | default: |
||
| 185 | $field[] = 'DEFAULT \''.$defaultValue.'\''; |
||
| 186 | } |
||
| 187 | |||
| 188 | } |
||
| 189 | elseif($defaultType === 'CURRENT_TIMESTAMP'){ |
||
| 190 | $field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
||
| 191 | } |
||
| 192 | elseif($defaultType === 'NULL' && $isNull === true){ |
||
| 193 | $field[] = 'DEFAULT NULL'; |
||
| 194 | } |
||
| 195 | |||
| 196 | if($extra){ |
||
| 197 | $field[] = $extra; |
||
| 198 | } |
||
| 199 | |||
| 200 | return implode(' ', $field); |
||
| 201 | } |
||
| 202 | |||
| 203 | /** @inheritdoc */ |
||
| 204 | public function createTable(string $table, array $cols, string $primaryKey = null, bool $ifNotExists = null, bool $temp = null, string $dir = null):array{ |
||
| 233 | } |
||
| 234 | |||
| 235 | /** @inheritdoc */ |
||
| 236 | public function showDatabases():array{ |
||
| 239 | } |
||
| 240 | |||
| 241 | /** @inheritdoc */ |
||
| 242 | public function showTables(string $database = null, string $pattern = null, string $where = null):array{ |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * @link https://stackoverflow.com/a/16154183 |
||
| 343 |