/[drupal]/contributions/sandbox/crell/pdo/schema.mysql.inc
ViewVC logotype

Contents of /contributions/sandbox/crell/pdo/schema.mysql.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Fri Feb 15 06:04:20 2008 UTC (21 months, 1 week ago) by crell
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +3 -3 lines
File MIME type: text/x-php
- Confirm add and drop fields.
1 <?php
2
3 /**
4 * @ingroup schemaapi
5 * @{
6 */
7
8 class DatabaseSchema_mysql extends DatabaseSchema {
9
10 public function tableExists($table) {
11 return (bool) $this->connection->runQuery("SHOW TABLES LIKE '{". $table ."}'", array(), array())->fetchOne();
12 }
13
14 public function columnExists($table, $column) {
15 return (bool) $this->connection->runQuery("SHOW COLUMNS FROM {". $this->escapeTable($table) ."} LIKE '". $this->escapeTable($column) ."'", array(), array())->fetchOne();
16 }
17
18
19 protected function createTableSql($name, $table) {
20
21 if (empty($table['mysql_suffix'])) {
22 $table['mysql_suffix'] = "/*!40100 DEFAULT CHARACTER SET UTF8 */";
23 }
24
25 $sql = "CREATE TABLE {". $name ."} (\n";
26
27 // Add the SQL statement for each field.
28 foreach ($table['fields'] as $field_name => $field) {
29 $sql .= $this->createFieldSql($field_name, $this->processField($field)) .", \n";
30 }
31
32 // Process keys & indexes.
33 $keys = $this->createKeysSql($table);
34 if (count($keys)) {
35 $sql .= implode(", \n", $keys) .", \n";
36 }
37
38 // Remove the last comma and space.
39 $sql = substr($sql, 0, -3) ."\n) ";
40
41 $sql .= $table['mysql_suffix'];
42
43 return array($sql);
44 }
45
46 protected function createFieldSql($name, $spec) {
47 $sql = "`". $name ."` ". $spec['mysql_type'];
48
49 if (isset($spec['length'])) {
50 $sql .= '('. $spec['length'] .')';
51 }
52 elseif (isset($spec['precision']) && isset($spec['scale'])) {
53 $sql .= '('. $spec['precision'] .', '. $spec['scale'] .')';
54 }
55
56 if (!empty($spec['unsigned'])) {
57 $sql .= ' unsigned';
58 }
59
60 if (!empty($spec['not null'])) {
61 $sql .= ' NOT NULL';
62 }
63
64 if (!empty($spec['auto_increment'])) {
65 $sql .= ' auto_increment';
66 }
67
68 if (isset($spec['default'])) {
69 if (is_string($spec['default'])) {
70 $spec['default'] = "'". $spec['default'] ."'";
71 }
72 $sql .= ' DEFAULT '. $spec['default'];
73 }
74
75 if (empty($spec['not null']) && !isset($spec['default'])) {
76 $sql .= ' DEFAULT NULL';
77 }
78
79 return $sql;
80 }
81
82 protected function processField($field) {
83
84 if (!isset($field['size'])) {
85 $field['size'] = 'normal';
86 }
87
88 // Set the correct database-engine specific datatype.
89 if (!isset($field['mysql_type'])) {
90 $map = db_type_map();
91 $field['mysql_type'] = $map[$field['type'] .':'. $field['size']];
92 }
93
94 if ($field['type'] == 'serial') {
95 $field['auto_increment'] = TRUE;
96 }
97
98 return $field;
99 }
100
101 public function getFieldTypeMap() {
102 // Put :normal last so it gets preserved by array_flip. This makes
103 // it much easier for modules (such as schema.module) to map
104 // database types back into schema types.
105 static $map = array(
106 'varchar:normal' => 'VARCHAR',
107 'char:normal' => 'CHAR',
108
109 'text:tiny' => 'TINYTEXT',
110 'text:small' => 'TINYTEXT',
111 'text:medium' => 'MEDIUMTEXT',
112 'text:big' => 'LONGTEXT',
113 'text:normal' => 'TEXT',
114
115 'serial:tiny' => 'TINYINT',
116 'serial:small' => 'SMALLINT',
117 'serial:medium' => 'MEDIUMINT',
118 'serial:big' => 'BIGINT',
119 'serial:normal' => 'INT',
120
121 'int:tiny' => 'TINYINT',
122 'int:small' => 'SMALLINT',
123 'int:medium' => 'MEDIUMINT',
124 'int:big' => 'BIGINT',
125 'int:normal' => 'INT',
126
127 'float:tiny' => 'FLOAT',
128 'float:small' => 'FLOAT',
129 'float:medium' => 'FLOAT',
130 'float:big' => 'DOUBLE',
131 'float:normal' => 'FLOAT',
132
133 'numeric:normal' => 'DECIMAL',
134
135 'blob:big' => 'LONGBLOB',
136 'blob:normal' => 'BLOB',
137
138 'datetime:normal' => 'DATETIME',
139 );
140 return $map;
141 }
142
143
144
145
146 protected function createKeysSql($spec) {
147 $keys = array();
148
149 if (!empty($spec['primary key'])) {
150 $keys[] = 'PRIMARY KEY ('. $this->createKeysSqlHelper($spec['primary key']) .')';
151 }
152 if (!empty($spec['unique keys'])) {
153 foreach ($spec['unique keys'] as $key => $fields) {
154 $keys[] = 'UNIQUE KEY '. $key .' ('. $this->createKeysSqlHelper($fields) .')';
155 }
156 }
157 if (!empty($spec['indexes'])) {
158 foreach ($spec['indexes'] as $index => $fields) {
159 $keys[] = 'INDEX '. $index .' ('. $this->createKeysSqlHelper($fields) .')';
160 }
161 }
162
163 return $keys;
164 }
165
166 protected function createKeySql($fields) {
167 $ret = array();
168 foreach ($fields as $field) {
169 if (is_array($field)) {
170 $ret[] = $field[0] .'('. $field[1] .')';
171 }
172 else {
173 $ret[] = $field;
174 }
175 }
176 return implode(', ', $ret);
177 }
178
179 protected function createKeysSqlHelper($fields) {
180 $ret = array();
181 foreach ($fields as $field) {
182 if (is_array($field)) {
183 $ret[] = $field[0] .'('. $field[1] .')';
184 }
185 else {
186 $ret[] = $field;
187 }
188 }
189 return implode(', ', $ret);
190 }
191
192 public function renameTable(&$ret, $table, $new_name) {
193 $ret[] = update_sql('ALTER TABLE {'. $table .'} RENAME TO {'. $new_name .'}');
194 }
195
196 public function dropTable(&$ret, $table) {
197 $ret[] = update_sql('DROP TABLE {'. $table .'}');
198 }
199
200 public function addField(&$ret, $table, $field, $spec, $keys_new = array()) {
201 $fixnull = FALSE;
202 if (!empty($spec['not null']) && !isset($spec['default'])) {
203 $fixnull = TRUE;
204 $spec['not null'] = FALSE;
205 }
206 $query = 'ALTER TABLE {'. $table .'} ADD ';
207 $query .= $this->createFieldSql($field, $this->processField($spec));
208 if (count($keys_new)) {
209 $query .= ', ADD '. implode(', ADD ', $this->createKeysSql($keys_new));
210 }
211 $ret[] = update_sql($query);
212 if (isset($spec['initial'])) {
213 // All this because update_sql does not support %-placeholders.
214 $sql = 'UPDATE {'. $table .'} SET '. $field .' = '. db_type_placeholder($spec['type']);
215 $result = db_query($sql, $spec['initial']);
216 $ret[] = array('success' => $result !== FALSE, 'query' => check_plain($sql .' ('. $spec['initial'] .')'));
217 }
218 if ($fixnull) {
219 $spec['not null'] = TRUE;
220 $this->changeField($ret, $table, $field, $field, $spec);
221 }
222 }
223
224 public function dropField(&$ret, $table, $field) {
225 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP '. $field);
226 }
227
228 public function fieldSetDefault(&$ret, $table, $field, $default) {
229 if ($default == NULL) {
230 $default = 'NULL';
231 }
232 else {
233 $default = is_string($default) ? "'$default'" : $default;
234 }
235
236 $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' SET DEFAULT '. $default);
237 }
238
239 public function fieldSetNoDefault(&$ret, $table, $field) {
240 $ret[] = update_sql('ALTER TABLE {'. $table .'} ALTER COLUMN '. $field .' DROP DEFAULT');
241 }
242
243 public function addPrimaryKey(&$ret, $table, $fields) {
244 $ret[] = update_sql('ALTER TABLE {'. $table .'} ADD PRIMARY KEY ('.
245 $this->createKeySql($fields) .')');
246 }
247
248 public function dropPrimaryKey(&$ret, $table) {
249 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP PRIMARY KEY');
250 }
251
252 public function addUniqueKey(&$ret, $table, $name, $fields) {
253 $ret[] = update_sql('ALTER TABLE {'. $table .'} ADD UNIQUE KEY '.
254 $name .' ('. $this->createKeySql($fields) .')');
255 }
256
257 public function dropUniqueKey(&$ret, $table, $name) {
258 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP KEY '. $name);
259 }
260
261 public function addIndex(&$ret, $table, $name, $fields) {
262 $query = 'ALTER TABLE {'. $table .'} ADD INDEX '. $name .' ('. $this->createKeySql($fields) .')';
263 $ret[] = update_sql($query);
264 }
265
266 public function dropIndex(&$ret, $table, $name) {
267 $ret[] = update_sql('ALTER TABLE {'. $table .'} DROP INDEX '. $name);
268 }
269
270 public function changeField(&$ret, $table, $field, $field_new, $spec, $keys_new = array()) {
271 $sql = 'ALTER TABLE {'. $table .'} CHANGE '. $field .' '.
272 $this->createFieldSql($field_new, $this->processField($spec));
273 if (count($keys_new)) {
274 $sql .= ', ADD '. implode(', ADD ', $this->createKeysSql($keys_new));
275 }
276 $ret[] = update_sql($sql);
277 }
278
279 }
280
281 /**
282 * @} End of "ingroup schemaapi".
283 */
284

  ViewVC Help
Powered by ViewVC 1.1.2