Skip to content

Commit f13f32f

Browse files
committed
Move shared code down to Driver.
There isn't any platform specific SQL in _value so pushing it down to the Driver saves duplication, inheritance and makes drivers more useful.
1 parent edc5b5c commit f13f32f

File tree

3 files changed

+31
-59
lines changed

3 files changed

+31
-59
lines changed

lib/Cake/Database/Driver.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,34 @@ public function supportsQuoting() {
135135
return true;
136136
}
137137

138+
/**
139+
* Escapes values for use in schema definitions.
140+
*
141+
* @param mixed $value The value to escape.
142+
* @return string String for use in schema definitions.
143+
*/
144+
public function schemaValue($value) {
145+
if (is_null($value)) {
146+
return 'NULL';
147+
}
148+
if ($value === false) {
149+
return 'FALSE';
150+
}
151+
if ($value === true) {
152+
return 'TRUE';
153+
}
154+
if (is_float($value)) {
155+
return str_replace(',', '.', strval($value));
156+
}
157+
if ((is_int($value) || $value === '0') || (
158+
is_numeric($value) && strpos($value, ',') === false &&
159+
$value[0] != '0' && strpos($value, 'e') === false)
160+
) {
161+
return $value;
162+
}
163+
return $this->_connection->quote($value, \PDO::PARAM_STR);
164+
}
165+
138166
/**
139167
* Returns last id generated for a table or sequence in database
140168
*

lib/Cake/Database/Schema/MysqlSchema.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public function columnSql(Table $table, $name) {
240240
unset($data['default']);
241241
}
242242
if (isset($data['default']) && $data['type'] !== 'timestamp') {
243-
$out .= ' DEFAULT ' . $this->_value($data['default']);
243+
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
244244
}
245245
if (
246246
isset($data['default']) &&
@@ -250,39 +250,11 @@ public function columnSql(Table $table, $name) {
250250
$out .= ' DEFAULT CURRENT_TIMESTAMP';
251251
}
252252
if (isset($data['comment'])) {
253-
$out .= ' COMMENT ' . $this->_value($data['comment']);
253+
$out .= ' COMMENT ' . $this->_driver->schemaValue($data['comment']);
254254
}
255255
return $out;
256256
}
257257

258-
/**
259-
* Escapes values for use in schema definitions.
260-
*
261-
* @param mixed $value The value to escape.
262-
* @return string String for use in schema definitions.
263-
*/
264-
protected function _value($value) {
265-
if (is_null($value)) {
266-
return 'NULL';
267-
}
268-
if ($value === false) {
269-
return 'FALSE';
270-
}
271-
if ($value === true) {
272-
return 'TRUE';
273-
}
274-
if (is_float($value)) {
275-
return str_replace(',', '.', strval($value));
276-
}
277-
if ((is_int($value) || $value === '0') || (
278-
is_numeric($value) && strpos($value, ',') === false &&
279-
$value[0] != '0' && strpos($value, 'e') === false)
280-
) {
281-
return $value;
282-
}
283-
return $this->_driver->quote($value, \PDO::PARAM_STR);
284-
}
285-
286258
/**
287259
* Generate the SQL fragment for a single index in MySQL
288260
*

lib/Cake/Database/Schema/SqliteSchema.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -188,39 +188,11 @@ public function columnSql(Table $table, $name) {
188188
unset($data['default']);
189189
}
190190
if (isset($data['default'])) {
191-
$out .= ' DEFAULT ' . $this->_value($data['default']);
191+
$out .= ' DEFAULT ' . $this->_driver->schemaValue($data['default']);
192192
}
193193
return $out;
194194
}
195195

196-
/**
197-
* Escapes values for use in schema definitions.
198-
*
199-
* @param mixed $value The value to escape.
200-
* @return string String for use in schema definitions.
201-
*/
202-
protected function _value($value) {
203-
if (is_null($value)) {
204-
return 'NULL';
205-
}
206-
if ($value === false) {
207-
return 'FALSE';
208-
}
209-
if ($value === true) {
210-
return 'TRUE';
211-
}
212-
if (is_float($value)) {
213-
return str_replace(',', '.', strval($value));
214-
}
215-
if ((is_int($value) || $value === '0') || (
216-
is_numeric($value) && strpos($value, ',') === false &&
217-
$value[0] != '0' && strpos($value, 'e') === false)
218-
) {
219-
return $value;
220-
}
221-
return $this->_driver->quote($value, \PDO::PARAM_STR);
222-
}
223-
224196
/**
225197
* Generate the SQL fragment for a single index in MySQL
226198
*

0 commit comments

Comments
 (0)