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 |
||
57 | public function sqlForCreateTable(string $table, bool $autoIncrement, string $style) |
||
58 | { |
||
59 | $clauses = []; |
||
60 | $sequences = []; |
||
61 | |||
62 | $status = $this->driver->tableStatus($table); |
||
63 | if ($status !== null && $this->driver->isView($status)) { |
||
64 | $view = $this->driver->view($table); |
||
65 | return rtrim("CREATE VIEW " . $this->escapeId($table) . " AS $view[select]", ";"); |
||
66 | } |
||
67 | $fields = $this->driver->fields($table); |
||
68 | $indexes = $this->driver->indexes($table); |
||
69 | ksort($indexes); |
||
70 | $constraints = $this->constraints($table); |
||
71 | |||
72 | if (empty($status) || empty($fields)) { |
||
73 | return ''; |
||
74 | } |
||
75 | |||
76 | $query = "CREATE TABLE " . $this->escapeId($status->schema) . "." . |
||
77 | $this->escapeId($status->name) . " (\n "; |
||
78 | |||
79 | // fields' definitions |
||
80 | foreach ($fields as $field_name => $field) { |
||
81 | $part = $this->escapeId($field->name) . ' ' . $field->fullType . |
||
82 | $this->driver->defaultValue($field) . ($field->attnotnull ? " NOT NULL" : ""); |
||
83 | $clauses[] = $part; |
||
84 | |||
85 | // sequences for fields |
||
86 | if (preg_match('~nextval\(\'([^\']+)\'\)~', $field->default, $matches)) { |
||
87 | $sequence_name = $matches[1]; |
||
88 | $sq = reset($this->driver->rows($this->driver->minVersion(10) ? |
||
89 | "SELECT *, cache_size AS cache_value FROM pg_sequences " . |
||
90 | "WHERE schemaname = current_schema() AND sequencename = " . |
||
91 | $this->driver->quote($sequence_name) : "SELECT * FROM $sequence_name")); |
||
92 | $sequences[] = ($style == "DROP+CREATE" ? "DROP SEQUENCE IF EXISTS $sequence_name;\n" : "") . |
||
93 | "CREATE SEQUENCE $sequence_name INCREMENT $sq[increment_by] MINVALUE $sq[min_value] MAXVALUE $sq[max_value]" . |
||
94 | ($autoIncrement && $sq['last_value'] ? " START $sq[last_value]" : "") . " CACHE $sq[cache_value];"; |
||
95 | } |
||
96 | } |
||
97 | |||
98 | // adding sequences before table definition |
||
99 | if (!empty($sequences)) { |
||
100 | $query = implode("\n\n", $sequences) . "\n\n$query"; |
||
101 | } |
||
102 | |||
103 | // primary + unique keys |
||
104 | foreach ($indexes as $index_name => $index) { |
||
105 | switch ($index->type) { |
||
106 | case 'UNIQUE': |
||
107 | $clauses[] = "CONSTRAINT " . $this->escapeId($index_name) . |
||
108 | " UNIQUE (" . implode(', ', array_map(function ($column) { |
||
109 | return $this->escapeId($column); |
||
110 | }, $index->columns)) . ")"; |
||
111 | break; |
||
112 | case 'PRIMARY': |
||
113 | $clauses[] = "CONSTRAINT " . $this->escapeId($index_name) . |
||
114 | " PRIMARY KEY (" . implode(', ', array_map(function ($column) { |
||
115 | return $this->escapeId($column); |
||
116 | }, $index->columns)) . ")"; |
||
117 | break; |
||
118 | } |
||
119 | } |
||
120 | |||
121 | foreach ($constraints as $conname => $consrc) { |
||
122 | $clauses[] = "CONSTRAINT " . $this->escapeId($conname) . " CHECK $consrc"; |
||
123 | } |
||
124 | |||
125 | $query .= implode(",\n ", $clauses) . "\n) WITH (oids = " . ($status->oid ? 'true' : 'false') . ");"; |
||
126 | |||
127 | // "basic" indexes after table definition |
||
128 | foreach ($indexes as $index_name => $index) { |
||
129 | if ($index->type == 'INDEX') { |
||
130 | $columns = []; |
||
131 | foreach ($index->columns as $key => $val) { |
||
132 | $columns[] = $this->escapeId($val) . ($index->descs[$key] ? " DESC" : ""); |
||
133 | } |
||
134 | $query .= "\n\nCREATE INDEX " . $this->escapeId($index_name) . " ON " . |
||
135 | $this->escapeId($status->schema) . "." . $this->escapeId($status->name) . |
||
136 | " USING btree (" . implode(', ', $columns) . ");"; |
||
137 | } |
||
138 | } |
||
139 | |||
140 | // coments for table & fields |
||
141 | if ($status->comment) { |
||
142 | $query .= "\n\nCOMMENT ON TABLE " . $this->escapeId($status->schema) . "." . |
||
143 | $this->escapeId($status->name) . " IS " . $this->driver->quote($status->comment) . ";"; |
||
144 | } |
||
145 | |||
146 | foreach ($fields as $field_name => $field) { |
||
147 | if ($field->comment) { |
||
148 | $query .= "\n\nCOMMENT ON COLUMN " . $this->escapeId($status->schema) . "." . |
||
149 | $this->escapeId($status->name) . "." . $this->escapeId($field_name) . |
||
150 | " IS " . $this->driver->quote($field->comment) . ";"; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | return rtrim($query, ';'); |
||
155 | } |
||
190 |