Uploaded image for project: 'MariaDB Server'
  1. MariaDB Server
  2. MDEV-27013

COM_STMT_PREPARE returns 0 columns for INSERT ... RETURNING ...

Details

    Description

      When preparing an INSERT ... RETURNING ... statement with COM_STMT_PREPARE, the COM_STMT_PREPARE_OK response reports that the query returns 0 columns.

      However, when subsequently executing the prepared statement with COM_STMT_EXECUTE, the resultset is correctly reported.

      For example, against this table:

      CREATE TABLE tweet
      (
          id         BIGINT PRIMARY KEY AUTO_INCREMENT,
          created_at TIMESTAMP NOT NULL DEFAULT NOW(),
          text       TEXT      NOT NULL,
          owner_id   BIGINT
      );

      Preparing this statement:

      INSERT INTO tweet ( text ) VALUES ( 'Hello, World' ) RETURNING *

      The COM_STMT_PREPARE_OK response reports 0 for num_columns but the subsequent COM_STMT_EXECUTE_OK response correctly reports 4 columns.

      Context: I'm a maintainer of SQLx, which is a SQL client library for Rust. We use the COM_STMT_PREPARE_OK response to construct a map of column names to column ordinals for lookup in the ResultsetRow response, and then cache that map with the prepared statement.

      We also rely on the COM_STMT_PREPARE_OK response to generate code in the sqlx::query!() family of procedural macros. At compile time, we connect to a database with the same schema as the production database and use the metadata returned by prepared statements to emit code that maps the SQL row response to a Rust struct at runtime so the user can interact with the results of their query in Rust's strong, static type system.

      Until this is fixed, the macros will think an INSERT ... RETURNING ... query returns 0 columns, and will thus generate incorrect code.

      Related issue: https://github.com/launchbadge/sqlx/issues/1530

      Attachments

        Issue Links

          Activity

            People

              rucha174 Rucha Deodhar
              abonanderlb Austin Bonander
              Votes:
              2 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:

                Git Integration

                  Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.