Gbase8.7 和shardingsphere4.1.1适配

问题描述

在进行国产化数据库(Gbase8.7)适配的过程中发现用了shardingsphere就会包语法错误,如下图所示
Gbase8.7 docker环境详见 https://gbasedbt.com/index.php/archives/259/

Gbase8.7 和shardingsphere4.1.1适配
在debug shardingsphere源码发现在shardingsphere-sql-parser-binder模块下的ColumnMetaDataLoader类中generateEmptyResultSQL方法如下

  private static String generateEmptyResultSQL(final Connection connection, final String table, final String databaseType) throws SQLException {
        // TODO consider add a getDialectDelimeter() interface in parse module
        String delimiterLeft;
        String delimiterRight;
        if ("MySQL".equals(databaseType) || "MariaDB".equals(databaseType)) {
            delimiterLeft = "`";
            delimiterRight = "`";
        } else if (("Oracle".equals(databaseType) || "PostgreSQL".equals(databaseType) || "H2".equals(databaseType) || "SQL92".equals(databaseType))) {
            delimiterLeft = "\"";
            delimiterRight = "\"";
        } else if ("SQLServer".equals(databaseType)) {
            delimiterLeft = "[";
            delimiterRight = "]";
        } else {
            delimiterLeft = "";
            delimiterRight = "";
        }
        return "SELECT * FROM " + delimiterLeft + table + delimiterRight + " WHERE 1 != 1";
    }

发现由于shardingsphere没有为Gbase单独做适配,因此gbase归为SQL92语法一类,但是Gbase8.7在查询的时候不能在表名上加任何引号(无论是单引号还是双引号)
因此通过下载shardingsphere源码通过如下修改后,然后打包重新上传到公司的maven服务器后解决了问题,代码如下

  private static String generateEmptyResultSQL(final Connection connection, final String table, final String databaseType) throws SQLException {
        // TODO consider add a getDialectDelimeter() interface in parse module
        String url = connection.getMetaData().getURL();
        boolean containGbase = false;
        if (!Objects.isNull(url) && url.toLowerCase().contains("gbasedbt")) {
            containGbase = true;
        }

        String delimiterLeft;
        String delimiterRight;
        if ("MySQL".equals(databaseType) || "MariaDB".equals(databaseType)) {
            delimiterLeft = "`";
            delimiterRight = "`";
        } else if (("Oracle".equals(databaseType) || "PostgreSQL".equals(databaseType) || "H2".equals(databaseType) || "SQL92".equals(databaseType)) && !containGbase) {
            delimiterLeft = "\"";
            delimiterRight = "\"";
        } else if ("SQLServer".equals(databaseType)) {
            delimiterLeft = "[";
            delimiterRight = "]";
        } else {
            delimiterLeft = "";
            delimiterRight = "";
        }
        return "SELECT * FROM " + delimiterLeft + table + delimiterRight + " WHERE 1 != 1";
    }

由于为了不影响原来的结构,因此在进行上传的时候修改了jar包名称,然后pom文件引用了修改后的jar,pom文件如下

<dependency>
  <groupId>org.apache.shardingsphere</groupId>
  <artifactId>sharding-jdbc-core</artifactId>
  <version>${shardingsphere.version}</version>
  <exclusions>
  <exclusion>
    <groupId>org.apache.shardingsphere</groupId>
    <artifactId>shardingsphere-sql-parser-binder</artifactId>
  </exclusion>
  </exclusions>
</dependency>
<dependency>
  <groupId>org.apache.shardingsphere</groupId>
  <artifactId>shardingsphere-sql-parser-binder-adapt-gbase</artifactId>
  <version>4.1.1</version>
</dependency>
上一篇:C#连接字符串


下一篇:Springboot2.x +JPA 集成 Apache ShardingSphere 同库分表