Completed
Push — master ( 61abbf...bcc21c )
by Daniel
09:54
created

MySQLSchemaManager::enumValuesForField()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use SilverStripe\Core\Config\Config;
6
use SilverStripe\Core\Convert;
7
8
/**
9
 * Represents schema management object for MySQL
10
 */
11
class MySQLSchemaManager extends DBSchemaManager {
12
13
	/**
14
	 * Identifier for this schema, used for configuring schema-specific table
15
	 * creation options
16
	 *
17
	 * @skipUpgrade
18
	 */
19
	const ID = 'MySQLDatabase';
20
21
	public function createTable($table, $fields = null, $indexes = null, $options = null, $advancedOptions = null) {
22
		$fieldSchemas = $indexSchemas = "";
23
24
		if (!empty($options[self::ID])) {
25
			$addOptions = $options[self::ID];
26
		} else {
27
			$addOptions = "ENGINE=InnoDB";
28
		}
29
30
		if (!isset($fields['ID'])) {
31
			$fields['ID'] = "int(11) not null auto_increment";
32
		}
33
		if ($fields) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fields of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
34
			foreach ($fields as $k => $v)
35
				$fieldSchemas .= "\"$k\" $v,\n";
36
		}
37
		if ($indexes) {
38
			foreach ($indexes as $k => $v) {
39
				$indexSchemas .= $this->getIndexSqlDefinition($k, $v) . ",\n";
40
			}
41
		}
42
43
		// Switch to "CREATE TEMPORARY TABLE" for temporary tables
44
		$temporary = empty($options['temporary'])
45
				? ""
46
				: "TEMPORARY";
47
48
		$this->query("CREATE $temporary TABLE \"$table\" (
49
				$fieldSchemas
50
				$indexSchemas
51
				primary key (ID)
52
			) {$addOptions}");
53
54
		return $table;
55
	}
56
57
	public function alterTable($tableName, $newFields = null, $newIndexes = null, $alteredFields = null,
58
		$alteredIndexes = null, $alteredOptions = null, $advancedOptions = null
59
	) {
60
		if ($this->isView($tableName)) {
61
			$this->alterationMessage(
62
				sprintf("Table %s not changed as it is a view", $tableName),
63
				"changed"
64
			);
65
			return;
66
		}
67
		$alterList = array();
68
69
		if ($newFields) {
70
			foreach ($newFields as $k => $v) {
71
				$alterList[] .= "ADD \"$k\" $v";
72
			}
73
		}
74
		if ($newIndexes) {
75
			foreach ($newIndexes as $k => $v) {
76
				$alterList[] .= "ADD " . $this->getIndexSqlDefinition($k, $v);
77
			}
78
		}
79
		if ($alteredFields) {
80
			foreach ($alteredFields as $k => $v) {
81
				$alterList[] .= "CHANGE \"$k\" \"$k\" $v";
82
			}
83
		}
84
		if ($alteredIndexes) {
85
			foreach ($alteredIndexes as $k => $v) {
86
				$alterList[] .= "DROP INDEX \"$k\"";
87
				$alterList[] .= "ADD " . $this->getIndexSqlDefinition($k, $v);
88
			}
89
		}
90
91
		$dbID = self::ID;
92
		if ($alteredOptions && isset($alteredOptions[$dbID])) {
93
			$indexList = $this->indexList($tableName);
94
			$skip = false;
95
			foreach ($indexList as $index) {
96
				if ($index['type'] === 'fulltext') {
97
					$skip = true;
98
					break;
99
				}
100
			}
101
			if ($skip) {
102
				$this->alterationMessage(
103
					sprintf(
104
						"Table %s options not changed to %s due to fulltextsearch index",
105
						$tableName,
106
						$alteredOptions[$dbID]
107
					),
108
					"changed"
109
				);
110
			} else {
111
				$this->query(sprintf("ALTER TABLE \"%s\" %s", $tableName, $alteredOptions[$dbID]));
112
				$this->alterationMessage(
113
					sprintf("Table %s options changed: %s", $tableName, $alteredOptions[$dbID]),
114
					"changed"
115
				);
116
			}
117
		}
118
119
		$alterations = implode(",\n", $alterList);
120
		$this->query("ALTER TABLE \"$tableName\" $alterations");
121
	}
122
123
	public function isView($tableName) {
124
		$info = $this->query("SHOW /*!50002 FULL*/ TABLES LIKE '$tableName'")->record();
125
		return $info && strtoupper($info['Table_type']) == 'VIEW';
0 ignored issues
show
Bug Best Practice introduced by
The expression $info of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
126
	}
127
128
	public function renameTable($oldTableName, $newTableName) {
129
		$this->query("ALTER TABLE \"$oldTableName\" RENAME \"$newTableName\"");
130
	}
131
132
	public function checkAndRepairTable($tableName) {
133
		// Flag to ensure we only send the warning about PDO + native mode once
134
		static $pdo_warning_sent = false;
135
136
		// If running PDO and not in emulated mode, check table will fail
137
		if($this->database->getConnector() instanceof PDOConnector && !PDOConnector::is_emulate_prepare()) {
138
			if (!$pdo_warning_sent) {
139
				$this->alterationMessage('CHECK TABLE command disabled for PDO in native mode', 'notice');
140
				$pdo_warning_sent = true;
141
			}
142
143
			return true;
144
		}
145
146
		// Perform check
147
		if ($this->runTableCheckCommand("CHECK TABLE \"$tableName\"")) {
148
			return true;
149
		}
150
		$this->alterationMessage(
151
			"Table $tableName: repaired",
152
			"repaired"
153
		);
154
		return $this->runTableCheckCommand("REPAIR TABLE \"$tableName\" USE_FRM");
155
	}
156
157
	/**
158
	 * Helper function used by checkAndRepairTable.
159
	 * @param string $sql Query to run.
160
	 * @return boolean Returns if the query returns a successful result.
161
	 */
162
	protected function runTableCheckCommand($sql) {
163
		$testResults = $this->query($sql);
164
		foreach ($testResults as $testRecord) {
165
			if (strtolower($testRecord['Msg_text']) != 'ok') {
166
				return false;
167
			}
168
		}
169
		return true;
170
	}
171
172
	public function hasTable($table) {
173
		// MySQLi doesn't like parameterised queries for some queries
174
		$sqlTable = $this->database->quoteString($table);
175
		return (bool) ($this->query("SHOW TABLES LIKE $sqlTable")->value());
176
	}
177
178
	public function createField($tableName, $fieldName, $fieldSpec) {
179
		$this->query("ALTER TABLE \"$tableName\" ADD \"$fieldName\" $fieldSpec");
180
	}
181
182
	public function databaseList() {
183
		return $this->query("SHOW DATABASES")->column();
184
	}
185
186
	public function databaseExists($name) {
187
		// MySQLi doesn't like parameterised queries for some queries
188
		$sqlName = $this->database->quoteString($name);
189
		return !!($this->query("SHOW DATABASES LIKE $sqlName")->value());
190
	}
191
192
	public function createDatabase($name) {
193
		$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
194
		$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
195
		$this->query("CREATE DATABASE \"$name\" DEFAULT CHARACTER SET {$charset} DEFAULT COLLATE {$collation}");
196
	}
197
198
	public function dropDatabase($name) {
199
		$this->query("DROP DATABASE \"$name\"");
200
	}
201
202
	/**
203
	 * Change the database type of the given field.
204
	 * @param string $tableName The name of the tbale the field is in.
205
	 * @param string $fieldName The name of the field to change.
206
	 * @param string $fieldSpec The new field specification
207
	 */
208
	public function alterField($tableName, $fieldName, $fieldSpec) {
209
		$this->query("ALTER TABLE \"$tableName\" CHANGE \"$fieldName\" \"$fieldName\" $fieldSpec");
210
	}
211
212
	/**
213
	 * Change the database column name of the given field.
214
	 *
215
	 * @param string $tableName The name of the tbale the field is in.
216
	 * @param string $oldName The name of the field to change.
217
	 * @param string $newName The new name of the field
218
	 */
219
	public function renameField($tableName, $oldName, $newName) {
220
		$fieldList = $this->fieldList($tableName);
221
		if (array_key_exists($oldName, $fieldList)) {
222
			$this->query("ALTER TABLE \"$tableName\" CHANGE \"$oldName\" \"$newName\" " . $fieldList[$oldName]);
223
		}
224
	}
225
226
	protected static $_cache_collation_info = array();
227
228
	public function fieldList($table) {
229
		$fields = $this->query("SHOW FULL FIELDS IN \"$table\"");
230
		$fieldList = array();
231
		foreach ($fields as $field) {
232
			$fieldSpec = $field['Type'];
233
			if (!$field['Null'] || $field['Null'] == 'NO') {
234
				$fieldSpec .= ' not null';
235
			}
236
237
			if ($field['Collation'] && $field['Collation'] != 'NULL') {
238
				// Cache collation info to cut down on database traffic
239
				if (!isset(self::$_cache_collation_info[$field['Collation']])) {
240
					self::$_cache_collation_info[$field['Collation']]
241
						= $this->query("SHOW COLLATION LIKE '{$field['Collation']}'")->record();
242
				}
243
				$collInfo = self::$_cache_collation_info[$field['Collation']];
244
				$fieldSpec .= " character set $collInfo[Charset] collate $field[Collation]";
245
			}
246
247
			if ($field['Default'] || $field['Default'] === "0") {
248
				$fieldSpec .= " default " . $this->database->quoteString($field['Default']);
249
			}
250
			if ($field['Extra']) $fieldSpec .= " " . $field['Extra'];
251
252
			$fieldList[$field['Field']] = $fieldSpec;
253
		}
254
		return $fieldList;
255
	}
256
257
	/**
258
	 * Create an index on a table.
259
	 *
260
	 * @param string $tableName The name of the table.
261
	 * @param string $indexName The name of the index.
262
	 * @param string $indexSpec The specification of the index, see {@link SS_Database::requireIndex()} for more
263
	 *                          details.
264
	 */
265
	public function createIndex($tableName, $indexName, $indexSpec) {
266
		$this->query("ALTER TABLE \"$tableName\" ADD " . $this->getIndexSqlDefinition($indexName, $indexSpec));
267
	}
268
269
	/**
270
	 * Generate SQL suitable for creating this index
271
	 *
272
	 * @param string $indexName
273
	 * @param string|array $indexSpec See {@link requireTable()} for details
274
	 * @return string MySQL compatible ALTER TABLE syntax
275
	 */
276
	protected function getIndexSqlDefinition($indexName, $indexSpec) {
277
		$indexSpec = $this->parseIndexSpec($indexName, $indexSpec);
278
		if ($indexSpec['type'] == 'using') {
279
			return "index \"$indexName\" using ({$indexSpec['value']})";
280
		} else {
281
			return "{$indexSpec['type']} \"$indexName\" ({$indexSpec['value']})";
282
		}
283
	}
284
285
	public function alterIndex($tableName, $indexName, $indexSpec) {
286
		$indexSpec = $this->parseIndexSpec($indexName, $indexSpec);
287
		$this->query("ALTER TABLE \"$tableName\" DROP INDEX \"$indexName\"");
288
		$this->query("ALTER TABLE \"$tableName\" ADD {$indexSpec['type']} \"$indexName\" {$indexSpec['value']}");
289
	}
290
291
	protected function indexKey($table, $index, $spec) {
292
		// MySQL simply uses the same index name as SilverStripe does internally
293
		return $index;
294
	}
295
296
	public function indexList($table) {
297
		$indexes = $this->query("SHOW INDEXES IN \"$table\"");
298
		$groupedIndexes = array();
299
		$indexList = array();
300
301
		foreach ($indexes as $index) {
302
			$groupedIndexes[$index['Key_name']]['fields'][$index['Seq_in_index']] = $index['Column_name'];
303
304
			if ($index['Index_type'] == 'FULLTEXT') {
305
				$groupedIndexes[$index['Key_name']]['type'] = 'fulltext';
306
			} else if (!$index['Non_unique']) {
307
				$groupedIndexes[$index['Key_name']]['type'] = 'unique';
308
			} else if ($index['Index_type'] == 'HASH') {
309
				$groupedIndexes[$index['Key_name']]['type'] = 'hash';
310
			} else if ($index['Index_type'] == 'RTREE') {
311
				$groupedIndexes[$index['Key_name']]['type'] = 'rtree';
312
			} else {
313
				$groupedIndexes[$index['Key_name']]['type'] = 'index';
314
			}
315
		}
316
317
		if ($groupedIndexes) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $groupedIndexes of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
318
			foreach ($groupedIndexes as $index => $details) {
319
				ksort($details['fields']);
320
				$indexList[$index] = $this->parseIndexSpec($index, array(
321
					'name' => $index,
322
					'value' => $this->implodeColumnList($details['fields']),
323
					'type' => $details['type']
324
				));
325
			}
326
		}
327
328
		return $indexList;
329
	}
330
331
	public function tableList() {
332
		$tables = array();
333
		foreach ($this->query("SHOW TABLES") as $record) {
334
			$table = reset($record);
335
			$tables[strtolower($table)] = $table;
336
		}
337
		return $tables;
338
	}
339
340
	public function enumValuesForField($tableName, $fieldName) {
341
		// Get the enum of all page types from the SiteTree table
342
		$classnameinfo = $this->query("DESCRIBE \"$tableName\" \"$fieldName\"")->first();
343
		preg_match_all("/'[^,]+'/", $classnameinfo["Type"], $matches);
344
345
		$classes = array();
346
		foreach ($matches[0] as $value) {
347
			$classes[] = stripslashes(trim($value, "'"));
348
		}
349
		return $classes;
350
	}
351
352
	public function dbDataType($type) {
353
		$values = Array(
354
			'unsigned integer' => 'UNSIGNED'
355
		);
356
357
		if (isset($values[$type])) return $values[$type];
358
		else return '';
359
	}
360
361
	/**
362
	 * Return a boolean type-formatted string
363
	 *
364
	 * @param array $values Contains a tokenised list of info about this data type
365
	 * @return string
366
	 */
367
	public function boolean($values) {
368
		//For reference, this is what typically gets passed to this function:
369
		//$parts=Array('datatype'=>'tinyint', 'precision'=>1, 'sign'=>'unsigned', 'null'=>'not null',
0 ignored issues
show
Unused Code Comprehensibility introduced by
83% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
370
		//'default'=>$this->default);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
371
		//DB::requireField($this->tableName, $this->name, "tinyint(1) unsigned not null default
372
		//'{$this->defaultVal}'");
373
		return 'tinyint(1) unsigned not null' . $this->defaultClause($values);
374
	}
375
376
	/**
377
	 * Return a date type-formatted string
378
	 * For MySQL, we simply return the word 'date', no other parameters are necessary
379
	 *
380
	 * @param array $values Contains a tokenised list of info about this data type
381
	 * @return string
382
	 */
383
	public function date($values) {
384
		//For reference, this is what typically gets passed to this function:
385
		//$parts=Array('datatype'=>'date');
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
386
		//DB::requireField($this->tableName, $this->name, "date");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
387
		return 'date';
388
	}
389
390
	/**
391
	 * Return a decimal type-formatted string
392
	 *
393
	 * @param array $values Contains a tokenised list of info about this data type
394
	 * @return string
395
	 */
396
	public function decimal($values) {
397
		//For reference, this is what typically gets passed to this function:
398
		//$parts=Array('datatype'=>'decimal', 'precision'=>"$this->wholeSize,$this->decimalSize");
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
399
		//DB::requireField($this->tableName, $this->name, "decimal($this->wholeSize,$this->decimalSize)");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
400
		// Avoid empty strings being put in the db
401
		if ($values['precision'] == '') {
402
			$precision = 1;
403
		} else {
404
			$precision = $values['precision'];
405
		}
406
407
		// Fix format of default value to match precision
408
		if (isset($values['default']) && is_numeric($values['default'])) {
409
			$decs = strpos($precision, ',') !== false
410
					? (int) substr($precision, strpos($precision, ',') + 1)
411
					: 0;
412
			$values['default'] = number_format($values['default'], $decs, '.', '');
413
		} else {
414
			unset($values['default']);
415
		}
416
417
		return "decimal($precision) not null" . $this->defaultClause($values);
418
	}
419
420
	/**
421
	 * Return a enum type-formatted string
422
	 *
423
	 * @param array $values Contains a tokenised list of info about this data type
424
	 * @return string
425
	 */
426
	public function enum($values) {
427
		//For reference, this is what typically gets passed to this function:
428
		//$parts=Array('datatype'=>'enum', 'enums'=>$this->enum, 'character set'=>'utf8', 'collate'=>
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
429
		// 'utf8_general_ci', 'default'=>$this->default);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
430
		//DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set
431
		// utf8 collate utf8_general_ci default '{$this->default}'");
432
		$valuesString = implode(",", Convert::raw2sql($values['enums'], true));
433
		$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
434
		$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
435
		return "enum($valuesString) character set {$charset} collate {$collation}" . $this->defaultClause($values);
436
	}
437
438
	/**
439
	 * Return a set type-formatted string
440
	 *
441
	 * @param array $values Contains a tokenised list of info about this data type
442
	 * @return string
443
	 */
444
	public function set($values) {
445
		//For reference, this is what typically gets passed to this function:
446
		//$parts=Array('datatype'=>'enum', 'enums'=>$this->enum, 'character set'=>'utf8', 'collate'=>
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
447
		// 'utf8_general_ci', 'default'=>$this->default);
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
448
		//DB::requireField($this->tableName, $this->name, "enum('" . implode("','", $this->enum) . "') character set
449
		//utf8 collate utf8_general_ci default '{$this->default}'");
450
		$valuesString = implode(",", Convert::raw2sql($values['enums'], true));
451
		$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
452
		$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
453
		return "set($valuesString) character set {$charset} collate {$collation}" . $this->defaultClause($values);
454
	}
455
456
	/**
457
	 * Return a float type-formatted string
458
	 * For MySQL, we simply return the word 'date', no other parameters are necessary
459
	 *
460
	 * @param array $values Contains a tokenised list of info about this data type
461
	 * @return string
462
	 */
463
	public function float($values) {
464
		//For reference, this is what typically gets passed to this function:
465
		//$parts=Array('datatype'=>'float');
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
466
		//DB::requireField($this->tableName, $this->name, "float");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
467
		return "float not null" . $this->defaultClause($values);
468
	}
469
470
	/**
471
	 * Return a int type-formatted string
472
	 *
473
	 * @param array $values Contains a tokenised list of info about this data type
474
	 * @return string
475
	 */
476
	public function int($values) {
477
		//For reference, this is what typically gets passed to this function:
478
		//$parts=Array('datatype'=>'int', 'precision'=>11, 'null'=>'not null', 'default'=>(int)$this->default);
0 ignored issues
show
Unused Code Comprehensibility introduced by
82% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
479
		//DB::requireField($this->tableName, $this->name, "int(11) not null default '{$this->defaultVal}'");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
480
		return "int(11) not null" . $this->defaultClause($values);
481
	}
482
483
	/**
484
	 * Return a bigint type-formatted string
485
	 *
486
	 * @param array $values Contains a tokenised list of info about this data type
487
	 * @return string
488
	 */
489
	public function bigint($values) {
490
		//For reference, this is what typically gets passed to this function:
491
		//$parts=Array('datatype'=>'bigint', 'precision'=>20, 'null'=>'not null', 'default'=>$this->defaultVal,
0 ignored issues
show
Unused Code Comprehensibility introduced by
80% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
492
		//             'arrayValue'=>$this->arrayValue);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
493
		//$values=Array('type'=>'bigint', 'parts'=>$parts);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
494
		//DB::requireField($this->tableName, $this->name, $values);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
495
496
		return 'bigint(20) not null' . $this->defaultClause($values);
497
	}
498
499
	/**
500
	 * Return a datetime type-formatted string
501
	 * For MySQL, we simply return the word 'datetime', no other parameters are necessary
502
	 *
503
	 * @param array $values Contains a tokenised list of info about this data type
504
	 * @return string
505
	 */
506
	public function datetime($values) {
507
		//For reference, this is what typically gets passed to this function:
508
		//$parts=Array('datatype'=>'datetime');
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
509
		//DB::requireField($this->tableName, $this->name, $values);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
510
		return 'datetime';
511
	}
512
513
	/**
514
	 * Return a text type-formatted string
515
	 *
516
	 * @param array $values Contains a tokenised list of info about this data type
517
	 * @return string
518
	 */
519
	public function text($values) {
520
		//For reference, this is what typically gets passed to this function:
521
		//$parts=Array('datatype'=>'mediumtext', 'character set'=>'utf8', 'collate'=>'utf8_general_ci');
0 ignored issues
show
Unused Code Comprehensibility introduced by
85% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
522
		//DB::requireField($this->tableName, $this->name, "mediumtext character set utf8 collate utf8_general_ci");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
523
		$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
524
		$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
525
		return 'mediumtext character set ' . $charset . ' collate ' . $collation . $this->defaultClause($values);
526
	}
527
528
	/**
529
	 * Return a time type-formatted string
530
	 * For MySQL, we simply return the word 'time', no other parameters are necessary
531
	 *
532
	 * @param array $values Contains a tokenised list of info about this data type
533
	 * @return string
534
	 */
535
	public function time($values) {
536
		//For reference, this is what typically gets passed to this function:
537
		//$parts=Array('datatype'=>'time');
0 ignored issues
show
Unused Code Comprehensibility introduced by
89% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
538
		//DB::requireField($this->tableName, $this->name, "time");
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
539
		return 'time';
540
	}
541
542
	/**
543
	 * Return a varchar type-formatted string
544
	 *
545
	 * @param array $values Contains a tokenised list of info about this data type
546
	 * @return string
547
	 */
548
	public function varchar($values) {
549
		//For reference, this is what typically gets passed to this function:
550
		//$parts=Array('datatype'=>'varchar', 'precision'=>$this->size, 'character set'=>'utf8', 'collate'=>
0 ignored issues
show
Unused Code Comprehensibility introduced by
79% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
551
		//'utf8_general_ci');
552
		//DB::requireField($this->tableName, $this->name, "varchar($this->size) character set utf8 collate
553
		// utf8_general_ci");
554
		$default = $this->defaultClause($values);
555
		$charset = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'charset');
556
		$collation = Config::inst()->get('SilverStripe\ORM\Connect\MySQLDatabase', 'collation');
557
		return "varchar({$values['precision']}) character set {$charset} collate {$collation}{$default}";
558
	}
559
560
	/*
561
	 * Return the MySQL-proprietary 'Year' datatype
562
	 *
563
	 * @param array $values Contains a tokenised list of info about this data type
564
	 * @return string
565
	 */
566
	public function year($values) {
567
		return 'year(4)';
568
	}
569
570
	public function IdColumn($asDbValue = false, $hasAutoIncPK = true) {
571
		return 'int(11) not null auto_increment';
572
	}
573
574
	/**
575
	 * Parses and escapes the default values for a specification
576
	 *
577
	 * @param array $values Contains a tokenised list of info about this data type
578
	 * @return string Default clause
579
	 */
580
	protected function defaultClause($values) {
581
		if(isset($values['default'])) {
582
			return ' default ' . $this->database->quoteString($values['default']);
583
		}
584
		return '';
585
	}
586
587
}
588