diff --git a/CHANGELOG.md b/CHANGELOG.md index 22e656e..e53d23a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ Only accretive/fixative changes will be made from now on. The following changes have been made to **master** since the 1.0.405 build: -None. +* Address #100 by adding support for MariaDB (@green-coder). Set `NEXT_JDBC_TEST_MARIADB=true` as well as `NEXT_JDBC_TEST_MYSQL=true` in order to run tests against MariaDB. ## Stable Builds diff --git a/deps.edn b/deps.edn index 88e9fd6..b518e46 100644 --- a/deps.edn +++ b/deps.edn @@ -16,6 +16,7 @@ org.hsqldb/hsqldb {:mvn/version "2.5.0"} com.h2database/h2 {:mvn/version "1.4.199"} net.sourceforge.jtds/jtds {:mvn/version "1.3.1"} + org.mariadb.jdbc/mariadb-java-client {:mvn/version "2.5.4"} mysql/mysql-connector-java {:mvn/version "8.0.19"} org.postgresql/postgresql {:mvn/version "42.2.10"} com.opentable.components/otj-pg-embedded {:mvn/version "0.13.3"} diff --git a/doc/tips-and-tricks.md b/doc/tips-and-tricks.md index 6cab6dd..0d10f0e 100644 --- a/doc/tips-and-tricks.md +++ b/doc/tips-and-tricks.md @@ -38,9 +38,13 @@ Consult the [java.sql.Blob documentation](https://docs.oracle.com/javase/8/docs/ ## MS SQL Server +In MS SQL Server, the generated key from an insert comes back as `:GENERATED_KEYS`. + By default, you won't get table names as qualifiers with Microsoft's JDBC driver (you might with the jTDS drive -- I haven't tried that recently). See this [MSDN forum post about `.getTableName()`](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/55e8cbb2-b11c-446e-93ab-dc30658caf99/resultsetmetadatagettablename-returns-instead-of-table-name) for details. According to one of the answers posted there, if you specify `:result-type` and `:concurrency` in the options for `execute!`, `execute-one!`, `plan`, or `prepare`, that will cause SQL Server to return table names for columns. `:result-type` needs to be `:scoll-sensitive` or `:scroll-insensitive` for this to work. `:concurrency` can be `:read-only` or `:updatable`. -## MySQL +## MySQL & MariaDB + +In MySQL, the generated key from an insert comes back as `:GENERATED_KEY`. In MariaDB, the generated key from an insert comes back as `:insert_id`. MySQL generally stores tables as files so they are case-sensitive if your O/S is (Linux) or case-insensitive if your O/S is not (Mac, Windows) but the column names are generally case-insensitive. This can matter when if you use `next.jdbc.result-set/as-lower-maps` because that will lower-case the table names (as well as the column names) so if you are round-tripping based on the keys you get back, you may produce an incorrect table name in terms of case. You'll also need to be careful about `:table-fn`/`:column-fn` because of this. diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index a495043..e1465be 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -78,12 +78,12 @@ :port 1433} "jtds:sqlserver" {:classname "net.sourceforge.jtds.jdbc.Driver" :port 1433} + "mariadb" {:classname "org.mariadb.jdbc.Driver" + :port 3306} "mssql" {:classname "com.microsoft.sqlserver.jdbc.SQLServerDriver" :alias-for "sqlserver" :dbname-separator ";DATABASENAME=" :port 1433} - "mariadb" {:classname "org.mariadb.jdbc.Driver" - :port 3306} "mysql" {:classname ["com.mysql.cj.jdbc.Driver" "com.mysql.jdbc.Driver"] :port 3306} diff --git a/test/next/jdbc/sql_test.clj b/test/next/jdbc/sql_test.clj index 5c0421e..5fdaf0e 100644 --- a/test/next/jdbc/sql_test.clj +++ b/test/next/jdbc/sql_test.clj @@ -7,7 +7,7 @@ [next.jdbc.sql :as sql] [next.jdbc.test-fixtures :refer [with-test-db ds column default-options - derby? mssql? mysql? postgres? sqlite?]])) + derby? maria? mssql? mysql? postgres? sqlite?]])) (set! *warn-on-reflection* true) @@ -68,6 +68,7 @@ (deftest test-insert-delete (let [new-key (cond (derby?) :1 (mssql?) :GENERATED_KEYS + (maria?) :insert_id (mysql?) :GENERATED_KEY (postgres?) :fruit/id (sqlite?) (keyword "last_insert_rowid()") diff --git a/test/next/jdbc/test_fixtures.clj b/test/next/jdbc/test_fixtures.clj index 8905a48..932def9 100644 --- a/test/next/jdbc/test_fixtures.clj +++ b/test/next/jdbc/test_fixtures.clj @@ -27,8 +27,11 @@ (defonce embedded-pg (future (EmbeddedPostgres/start))) (def ^:private test-mysql-map - {:dbtype "mysql" :dbname "clojure_test" :useSSL false - :user "root" :password (System/getenv "MYSQL_ROOT_PASSWORD")}) + (merge (if (System/getenv "NEXT_JDBC_TEST_MARIADB") + {:dbtype "mariadb"} + {:dbtype "mysql" :disableMariaDbDriver true}) + {:dbname "clojure_test" :useSSL false + :user "root" :password (System/getenv "MYSQL_ROOT_PASSWORD")})) (def ^:private test-mysql (when (System/getenv "NEXT_JDBC_TEST_MYSQL") test-mysql-map)) @@ -47,9 +50,11 @@ (defn derby? [] (= "derby" (:dbtype @test-db-spec))) +(defn maria? [] (= "mariadb" (:dbtype @test-db-spec))) + (defn mssql? [] (= "mssql" (:dbtype @test-db-spec))) -(defn mysql? [] (= "mysql" (:dbtype @test-db-spec))) +(defn mysql? [] (#{"mariadb" "mysql"} (:dbtype @test-db-spec))) (defn postgres? [] (= "embedded-postgres" (:dbtype @test-db-spec))) diff --git a/test/next/jdbc_test.clj b/test/next/jdbc_test.clj index 2a77008..65217bd 100644 --- a/test/next/jdbc_test.clj +++ b/test/next/jdbc_test.clj @@ -297,7 +297,8 @@ VALUES ('Pear', 'green', 49, 47) ds (jdbc/get-datasource (assoc etc :jdbcUrl url))] (cond (derby?) (is (= {:create true} etc)) (mssql?) (is (= #{:user :password} (set (keys etc)))) - (mysql?) (is (= #{:user :password :useSSL} (set (keys etc)))) + (mysql?) (is (= #{:user :password :useSSL} + (disj (set (keys etc)) :disableMariaDbDriver))) :else (is (= {} etc))) (is (instance? javax.sql.DataSource ds)) (is (str/index-of (pr-str ds) (str "jdbc:"