Database Type Casting Per Database Connection

Created on 31 March 2009, about 16 years ago
Updated 31 March 2025, 2 days ago

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 . ')';
     }
     //....
}
✨ Feature request
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component

database system

Created by

πŸ‡³πŸ‡ΏNew Zealand Josh Waihi

Live updates comments and jobs are added and updated live.
  • stale-issue-cleanup

    To track issues in the developing policy for closing stale issues, [Policy, no patch] closing older issues

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States smustgrave

    Thank 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!

  • πŸ‡¨πŸ‡¦Canada Liam Morland Ontario, CA πŸ‡¨πŸ‡¦

    I can see this being a useful improvement to the database API. There is CastSqlInterface, but it only supports one cast.

Production build 0.71.5 2024