spring data jps + clickhouse缺少方言解决方法

在resources/META-INF下增加spring.factories文件。自定义方言加载器。其中=的值为自定义的路径
配置文件中添加

org.springframework.data.jdbc.repository.config.DialectResolver$JdbcDialectProvider=com.cheche365.dictonary.datatrans.datatrans.config.ClickHouseDialectProvider

自定义的方言解析器(使用myslq的)

package com.cheche365.dictonary.datatrans.datatrans.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.jdbc.core.dialect.*;
import org.springframework.data.jdbc.repository.config.DialectResolver;
import org.springframework.data.relational.core.dialect.*;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
import java.util.Locale;
import java.util.Optional;

/**
 * @author sunyan
 * @date 2022/11/15 12:31
 * @description 该类在本项目中并未使用,当其他环境需要使用jpa+springBoot+clickhouse时,需要使用该类,用于定制方言。在加载ClickHouseDialectProvider时,
 * spring data jpa会扫描META-INF下的spring.factories文件
 */
@Slf4j
public class ClickHouseDialectProvider implements DialectResolver.JdbcDialectProvider {

    @Override
    public Optional<Dialect> getDialect(JdbcOperations operations) {
        return Optional.ofNullable(operations.execute((ConnectionCallback<Dialect>) ClickHouseDialectProvider::getDialect));
    }

    @Nullable
    private static Dialect getDialect(Connection connection) throws SQLException {

        DatabaseMetaData metaData = connection.getMetaData();

        String name = metaData.getDatabaseProductName().toLowerCase(Locale.ENGLISH);

        if (name.contains("clickhouse")) {
            return new MySqlDialect(getIdentifierProcessing(metaData));
        }
        
        log.info(String.format("Couldn't determine Dialect for \"%s\"", name) );
        return null;
    }

    private static IdentifierProcessing getIdentifierProcessing(DatabaseMetaData metaData) throws SQLException {

        // getIdentifierQuoteString() returns a space " " if identifier quoting is not
        // supported.
        String quoteString = metaData.getIdentifierQuoteString();
        IdentifierProcessing.Quoting quoting = StringUtils.hasText(quoteString)
                ? new IdentifierProcessing.Quoting(quoteString)
                : IdentifierProcessing.Quoting.NONE;

        IdentifierProcessing.LetterCasing letterCasing;
        // IdentifierProcessing tries to mimic the behavior of unquoted identifiers for their quoted variants.
        if (metaData.supportsMixedCaseIdentifiers()) {
            letterCasing = IdentifierProcessing.LetterCasing.AS_IS;
        } else if (metaData.storesUpperCaseIdentifiers()) {
            letterCasing = IdentifierProcessing.LetterCasing.UPPER_CASE;
        } else if (metaData.storesLowerCaseIdentifiers()) {
            letterCasing = IdentifierProcessing.LetterCasing.LOWER_CASE;
        } else { // this shouldn't happen since one of the previous cases should be true.
            // But if it does happen, we go with the ANSI default.
            letterCasing = IdentifierProcessing.LetterCasing.UPPER_CASE;
        }

        return IdentifierProcessing.create(quoting, letterCasing);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值