shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

Firebird Proxy rejects strings in alternative quotes Q'{...}'

Open makssent opened this issue 1 month ago • 2 comments

Before reporting a bug

ShardingSphere version

master, commit 01446d4fad4

ShardingSphere project

ShardingSphere-Proxy

Expected behavior

The proxy should accept strings in alternative quotes (Q'{...}') as the Firebird server does.

Actual behavior

The proxy rejects the string in alternative quotes with a syntax error.

The same statement was executed directly on the Firebird server and through the proxy:

Steps Firebird server Proxy
SELECT Q'{it's}' FROM RDB$DATABASE One row: it's Error isc_dsql_error "Dynamic SQL Error; You have an error in your SQL syntax: SELECT Q'{it's}' FROM RDB$DATABASE, null"

So through the proxy an application cannot use strings in alternative quotes, for example to write a string with apostrophes without doubling them.

Reason analysis

Steps to reproduce

Environment

  • Firebird server 5.0.
  • Client: Jaybird 6.0.7.

Steps

  1. Start ShardingSphere-Proxy with this configuration:
# global.yaml
authority:
  users:
    - user: root@%
      password: root
  privilege:
    type: ALL_PERMITTED
props:
  proxy-frontend-database-protocol-type: Firebird
# database-firebird.yaml
databaseName: firebird_db
dataSources:
  ds_0:
    url: jdbc:firebirdsql://<host>/<database file>?encoding=UTF8
    username: <user>
    password: <password>
rules:
  - !SINGLE
    tables:
      - "*.*"
  1. Execute the statements from the table under "Actual behavior" through the proxy and directly on the Firebird server, and compare the results. "Example code" runs all of them.

Example code

The statements from the table on Jaybird 6.0.7. The comment above each call shows what it prints against the Firebird server and through the proxy.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

public class AlternativeQuotes {

    // The proxy; put the URL, user and password of the Firebird server here to compare
    static final String URL = "jdbc:firebirdsql://<proxy host>:<proxy port>/firebird_db?encoding=UTF8&wireCrypt=DISABLED";

    static final String USER = "root";

    static final String PASSWORD = "root";

    public static void main(final String[] args) throws SQLException {
        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            // Firebird server: "SELECT Q'{it's}' FROM RDB$DATABASE -> CONSTANT CHAR(4): it's";
            // proxy: "SELECT Q'{it's}' FROM RDB$DATABASE -> error: Dynamic SQL Error; You have an error in your SQL syntax: SELECT Q'{it's}' FROM RDB$DATABASE, null [SQLState:42000, ISC error code:335544569]"
            execute(connection, "SELECT Q'{it's}' FROM RDB$DATABASE");
        }
    }

    // Executes the statement and prints the described columns with the rows, the update count or the error
    static void execute(final Connection connection, final String sql) {
        try (Statement statement = connection.createStatement()) {
            if (!statement.execute(sql)) {
                System.out.println(sql + " -> update count " + statement.getUpdateCount());
                return;
            }
            try (ResultSet resultSet = statement.getResultSet()) {
                ResultSetMetaData metaData = resultSet.getMetaData();
                List<String> columns = new ArrayList<>();
                for (int i = 1; i <= metaData.getColumnCount(); i++) {
                    int type = metaData.getColumnType(i);
                    boolean text = Types.CHAR == type || Types.VARCHAR == type;
                    columns.add(metaData.getColumnLabel(i) + " " + metaData.getColumnTypeName(i) + (text ? "(" + metaData.getPrecision(i) + ")" : ""));
                }
                List<String> rows = new ArrayList<>();
                while (resultSet.next()) {
                    List<String> values = new ArrayList<>();
                    for (int i = 1; i <= metaData.getColumnCount(); i++) {
                        values.add(String.valueOf(resultSet.getObject(i)));
                    }
                    rows.add(String.join(", ", values));
                }
                System.out.println(sql + " -> " + String.join(", ", columns) + ": " + (rows.isEmpty() ? "no rows" : String.join("; ", rows)));
            }
        } catch (final SQLException ex) {
            System.out.println(sql + " -> error: " + ex.getMessage());
        }
    }
}

makssent avatar Oct 19 '26 17:10 makssent

Hi @makssent, I'd like to pick this one up.

Plan: extend the Firebird STRING_ lexer rule with the Q'<delimiter>...<delimiter>' form, covering the paired delimiters ({}, (), [], <>) and the same-character form, as in Firebird's Parser.cpp. Then update FirebirdStatementVisitor#visitStringLiterals so the value is the text between the delimiters (it's for Q'{it's}'), and add parser test cases for each delimiter form.

Ayush0612005 avatar Oct 19 '26 18:10 Ayush0612005

PR is up: #40494. It adds the Q'...' forms to the Firebird lexer (bracket pairs and same-character delimiters, following Firebird's Parser.cpp) and parser test cases including the example from this issue.

Ayush0612005 avatar Nov 08 '26 18:11 Ayush0612005