Conditions | 25 |
Paths | 18242 |
Total Lines | 98 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
66 | public function sqlForCreateTable(string $table, bool $autoIncrement, string $style) |
||
67 | { |
||
68 | $clauses = []; |
||
69 | $sequences = []; |
||
70 | |||
71 | $status = $this->driver->tableStatus($table); |
||
72 | if ($status !== null && $this->driver->isView($status)) { |
||
73 | $view = $this->driver->view($table); |
||
74 | return rtrim("CREATE VIEW " . $this->escapeId($table) . " AS $view[select]", ";"); |
||
75 | } |
||
76 | $fields = $this->driver->fields($table); |
||
77 | $indexes = $this->driver->indexes($table); |
||
78 | ksort($indexes); |
||
79 | $constraints = $this->constraints($table); |
||
80 | |||
81 | if (empty($status) || empty($fields)) { |
||
82 | return ''; |
||
83 | } |
||
84 | |||
85 | $query = "CREATE TABLE " . $this->escapeId($status->schema) . "." . |
||
86 | $this->escapeId($status->name) . " (\n "; |
||
87 | |||
88 | // fields' definitions |
||
89 | foreach ($fields as $field_name => $field) { |
||
90 | $part = $this->escapeId($field->name) . ' ' . $field->fullType . |
||
91 | $this->driver->defaultValue($field) . ($field->attnotnull ? " NOT NULL" : ""); |
||
92 | $clauses[] = $part; |
||
93 | |||
94 | // sequences for fields |
||
95 | if (preg_match('~nextval\(\'([^\']+)\'\)~', $field->default, $matches)) { |
||
96 | $sequence_name = $matches[1]; |
||
97 | $sq = reset($this->driver->rows($this->driver->minVersion(10) ? |
||
98 | "SELECT *, cache_size AS cache_value FROM pg_sequences " . |
||
99 | "WHERE schemaname = current_schema() AND sequencename = " . |
||
100 | $this->driver->quote($sequence_name) : "SELECT * FROM $sequence_name")); |
||
101 | $sequences[] = ($style == "DROP+CREATE" ? "DROP SEQUENCE IF EXISTS $sequence_name;\n" : "") . |
||
102 | "CREATE SEQUENCE $sequence_name INCREMENT $sq[increment_by] MINVALUE $sq[min_value] MAXVALUE $sq[max_value]" . |
||
103 | ($autoIncrement && $sq['last_value'] ? " START $sq[last_value]" : "") . " CACHE $sq[cache_value];"; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | // adding sequences before table definition |
||
108 | if (!empty($sequences)) { |
||
109 | $query = implode("\n\n", $sequences) . "\n\n$query"; |
||
110 | } |
||
111 | |||
112 | // primary + unique keys |
||
113 | foreach ($indexes as $index_name => $index) { |
||
114 | switch ($index->type) { |
||
115 | case 'UNIQUE': |
||
116 | $clauses[] = "CONSTRAINT " . $this->escapeId($index_name) . |
||
117 | " UNIQUE (" . implode(', ', array_map(function ($column) { |
||
118 | return $this->escapeId($column); |
||
119 | }, $index->columns)) . ")"; |
||
120 | break; |
||
121 | case 'PRIMARY': |
||
122 | $clauses[] = "CONSTRAINT " . $this->escapeId($index_name) . |
||
123 | " PRIMARY KEY (" . implode(', ', array_map(function ($column) { |
||
124 | return $this->escapeId($column); |
||
125 | }, $index->columns)) . ")"; |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | |||
130 | foreach ($constraints as $conname => $consrc) { |
||
131 | $clauses[] = "CONSTRAINT " . $this->escapeId($conname) . " CHECK $consrc"; |
||
132 | } |
||
133 | |||
134 | $query .= implode(",\n ", $clauses) . "\n) WITH (oids = " . ($status->oid ? 'true' : 'false') . ");"; |
||
135 | |||
136 | // "basic" indexes after table definition |
||
137 | foreach ($indexes as $index_name => $index) { |
||
138 | if ($index->type == 'INDEX') { |
||
139 | $columns = []; |
||
140 | foreach ($index->columns as $key => $val) { |
||
141 | $columns[] = $this->escapeId($val) . ($index->descs[$key] ? " DESC" : ""); |
||
142 | } |
||
143 | $query .= "\n\nCREATE INDEX " . $this->escapeId($index_name) . " ON " . |
||
144 | $this->escapeId($status->schema) . "." . $this->escapeId($status->name) . |
||
145 | " USING btree (" . implode(', ', $columns) . ");"; |
||
146 | } |
||
147 | } |
||
148 | |||
149 | // coments for table & fields |
||
150 | if ($status->comment) { |
||
151 | $query .= "\n\nCOMMENT ON TABLE " . $this->escapeId($status->schema) . "." . |
||
152 | $this->escapeId($status->name) . " IS " . $this->driver->quote($status->comment) . ";"; |
||
153 | } |
||
154 | |||
155 | foreach ($fields as $field_name => $field) { |
||
156 | if ($field->comment) { |
||
157 | $query .= "\n\nCOMMENT ON COLUMN " . $this->escapeId($status->schema) . "." . |
||
158 | $this->escapeId($status->name) . "." . $this->escapeId($field_name) . |
||
159 | " IS " . $this->driver->quote($field->comment) . ";"; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | return rtrim($query, ';'); |
||
164 | } |
||
199 |