domaframework / doma-codegen-plugin

Generates Java, Kotlin, and SQL files from Database

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MariaDB Connector/J対応について

orimajp opened this issue · comments

AWSのAurora MySQLを使うため、build.gradleのdomaCodeGen設定において以下のように記述すると、[DOMAGEN0024] Cannot infer the driver class name from the property "url", so this plugin cannot create a DataSource instance. Correct the url property or specify the property "dataSource" explicitly.が発生します。

domaCodeGen {
    // make an arbitrary named block
    dev {
        url = 'jdbc:mariadb:aurora//xxxxxxxx/maindb'

        // 省略
    }
}

DomaGenでは問題なく使えていたのですが、doma-code-genではサポート対象外でしょうか。

ドライバはdependenciesの他、以下のように指定しています。

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        // specify your JDBC driver
        classpath 'org.mariadb.jdbc:mariadb-java-client:2.7.2'
    }
}

About MariaDB Connector/J - MariaDB Knowledge Base

追記 2021/4/2 23:30

DomaGenでMariaDB Connector/Jが利用できている環境では、以下の事前設定を行っています。

configurations {
    domaGenRuntime
}

repositories {
    mavenCentral()
}

dependencies {
    domaGenRuntime 'org.seasar.doma:doma-gen:2.19.2'
    domaGenRuntime group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.2.6'
}

DomaGenタスクではdialectおよびJDBCドライバに以下の設定を行っています。

dialectName:'mysql'
driverClassName:'org.mariadb.jdbc.Driver'

詳細な説明ありがとうございます。
doma-codegen-pluginではJDBCのurlからDriverを推測する機能があるのですが、現在、MariaDBには対応できていません。
コードで言うとこの辺りです。
https://github.com/domaframework/doma-codegen-plugin/blob/v1.2.1/codegen/src/main/java/org/seasar/doma/gradle/codegen/util/JdbcUtil.java#L104-L129

ただ、dataSourcecodeGenDialect を明示的に設定してもらえればどのDBMSであっても動くようになっています。
以下のように設定してみてもらえないでしょうか?

import org.mariadb.jdbc.Driver
import org.seasar.doma.gradle.codegen.dialect.MysqlCodeGenDialect
import org.seasar.doma.gradle.codegen.jdbc.SimpleDataSource

// 省略

domaCodeGen {
    // make an arbitrary named block
    dev {
        // データソースを自前で作る
        def ds = new SimpleDataSource()
        ds.setDriver(new Driver())
        ds.setUrl('jdbc:mariadb:aurora//xxxxxxxx/maindb')
        ds.setUser(yourUserName)
        ds.setPassword(yourPassword)
        
        // 以下の2つのプロパティを明示的に設定
        dataSource = ds
        codeGenDialect = new MysqlCodeGenDialect()
        
        // 省略
    }
}

ありがとうございます。うまくいきました。
ちなみに、url、user、passwordはDaoテストクラスの生成で利用しているみたいなので、残しておく必要があるようですね。