我是靠谱客的博主 坚强手链,最近开发中收集的这篇文章主要介绍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);
    }

}

最后

以上就是坚强手链为你收集整理的spring data jps + clickhouse缺少方言解决方法的全部内容,希望文章能够帮你解决spring data jps + clickhouse缺少方言解决方法所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(42)

评论列表共有 0 条评论

立即
投稿
返回
顶部