"offset" reserved keyword breaks newer MariaDB/MySQL (and all Postgres)

Created on 5 September 2023, over 1 year ago
Updated 6 September 2023, over 1 year ago

Problem/Motivation

Block Inject fails on databases where "offset" is a reserved keyword with:

PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'offset FROM `block_inject_exceptions` bie WHERE (nid = '2278') AND (offset ...' at line 2: SELECT bie.offset AS offset FROM {block_inject_exceptions} bie WHERE (nid = :db_condition_placeholder_0) AND (offset IS NOT NULL ) ; Array ( [:db_condition_placeholder_0] => 2278 ) in block_inject_get_offset() (line 778 of /var/www/mysite.org/public/sites/all/modules/contrib/block_inject/block_inject.module).

Steps to reproduce

Install on a site running on MariaDB 10.11

Proposed resolution

Escape the "offset" keyword in the query that calls it.

Remaining tasks

None

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Needs review

Version

1.2

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States MegaphoneJon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Open on Drupal.org β†’
    Core: 7.x + Environment: PHP 5.3 & MySQL 5.5
    last update over 1 year ago
    Waiting for branch to pass
  • Issue created by @MegaphoneJon
  • πŸ‡ΊπŸ‡ΈUnited States MegaphoneJon

    scratch that - my fix doesn't work, the issue remains

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

    Ultimately I fixed this by changing "offset" to the very-real-not-made-up "offsetted". See diff below. This also required me to change the SQL structure with ALTER TABLE block_inject_exceptions RENAME COLUMN `offset` TO offsetted. Given how few people use this module, and given that those sites are probably zombies that will never see PHP 8 (or else I wouldn't be the first to find this!), I'm not going to write a proper upgrade script. But anyone who has this problem can find the solution here.

    diff --git a/sites/all/modules/contrib/block_inject/block_inject.module b/sites/all/modules/contrib/block_inject/block_inject.module
    index 9a2e24aa..c986d1f0 100644
    --- a/sites/all/modules/contrib/block_inject/block_inject.module
    +++ b/sites/all/modules/contrib/block_inject/block_inject.module
    @@ -713,7 +713,7 @@ function block_inject_insert_exception($nid, $bi_id, $exception, $offset = NULL)
         $query = db_update('block_inject_exceptions')
             ->fields(array(
               'except_injection' => $exception,
    -          'offset' => $offset,
    +          'offsetted' => $offset,
             ))
             ->condition('nid', $nid)
             ->execute();
    @@ -724,7 +724,7 @@ function block_inject_insert_exception($nid, $bi_id, $exception, $offset = NULL)
               'bi_id' => $bi_id,
               'nid' => $nid,
               'except_injection' => $exception,
    -          'offset' => $offset,
    +          'offsetted' => $offset,
             ))
             ->execute();
       }
    @@ -772,9 +772,9 @@ function block_inject_green_light($nid) {
      */
     function block_inject_get_offset($nid) {
       $result = db_select('block_inject_exceptions', 'bie')
    -      ->fields('bie', array('offset'))
    +      ->fields('bie', array('offsetted'))
           ->condition('nid', $nid)
    -      ->isNotNull('offset')
    +      ->isNotNull('offsetted')
           ->execute()
           ->fetchAssoc();
       return $result;
    
Production build 0.71.5 2024