For some reason lately, I've been looking at alot of bugs related to casting column values from one type to another. Its seems like a hole in Drupal that hasn't been addressed yet. Mainly because, what mysql and postgresql can CAST to are different and what is returned is different.
Example:
mysql> select cast(57 as char);
+------------------+
| cast(57 as char) |
+------------------+
| 57 |
+------------------+
psql> select CAST(57 AS CHAR);
bpchar
--------
5
(1 row)
psql> select CAST(57 AS VARCHAR);
varchar
---------
57
(1 row)
I understand that there is only so much we can do - we can't turn lead to gold. But we can do it better.
I suggest we get the Database layer, somewhere (schema?), to do the work and CAST accordingly using DatabaseSchema::getFieldTypeMap()
class DatabaseSchema_pgsql extends DatabaseSchema {
//....
public function cast($column_name, $type) {
$map = $this->getFieldTypeMap();
if (array_key_exists($type, $map)) {
$type = $map[$type];
}
if (strpos($type, 'serial') !== FALSE) {
$type = 'int';
}
return 'CAST(' . $column_name . ' AS ' . $type . ')';
}
//....
}
Comments
Comment #1
Crell commentedEh, API change, so we'll get back to this later.
Comment #2
damien tournoud commentedOnly feature requests and tasks can be assigned to D8 for now, because the Drupal 8 tree is not opened yet.
Fortunately, this is a feature request.
Comment #3
damien tournoud commentedComment #4
liam morlandAnother useful cast would be for integers. MySQL doesn't support "CAST(value AS integer)"; it needs to be "signed" or "unsigned" in place of "integer".
Comment #18
smustgrave commentedThank you for sharing your idea for improving Drupal.
We are working to decide if this proposal meets the Criteria for evaluating proposed changes. There hasn't been any discussion here for over 8 years which suggests that this has either been implemented or there is no community support. Your thoughts on this will allow a decision to be made.
Since we need more information to move forward with this issue, the status is now Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.
Thanks!
Comment #19
liam morlandI can see this being a useful improvement to the database API. There is
CastSqlInterface, but it only supports one cast.Comment #20
smustgrave commentedWill bump one more time then.
Comment #21
smustgrave commented