Connection::escapeFields() strips white space from column names

Created on 21 April 2021, over 3 years ago
Updated 6 September 2023, about 1 year ago

Problem/Motivation

The method Connection::escapeFields() will strip all spaces from column names. This appears to apply to all of the "dynamic" queries. It does not occur/get called by a static query call. I have a MySQL database which has certain tables that have spaces in the column names. While this may not be the "preferred" standard - it's not "illegal" and I need to keep the column names as they are.

Steps to reproduce

Given a table 'csvtable' with columns 'Column One', 'Column Two', 'Column3'...

Running this query code:

        $this->db->delete('csvtable')
          ->condition('Column One', $value1)
          ->condition('Column Two', $value2)
          ->execute();

will produce something similar to the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ColumnOne' in 'where clause': DELETE FROM @csvtable
WHERE ("ColumnOne" = :db_condition_placeholder_0) AND ("ColumnTwo" = :db_condition_placeholder_1); Array
(
    [:db_condition_placeholder_0] => 1234
    [:db_condition_placeholder_1] => TextValue
)

Proposed resolution

Modify the escapeField() function to allow space characters in the column name.

line (1270):

$escaped = preg_replace('/[^A-Za-z0-9_.]+/', '', $field);

becomes:

$escaped = preg_replace('/[^A-Za-z0-9_. ]+/', '', $field); // adding 'space' to the allowed characters

Optionally - make the use of spaces in column names a "configurable" value with a default of False.

Remaining tasks

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

✨ Feature request
Status

Active

Version

11.0 πŸ”₯

Component
DatabaseΒ  β†’

Last updated 1 minute ago

  • Maintained by
  • πŸ‡³πŸ‡±Netherlands @daffie
Created by

πŸ‡ΊπŸ‡ΈUnited States BobCaverly Germantown, MD

Live updates comments and jobs are added and updated live.
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.

Production build 0.71.5 2024