Skip to content

Commit

Permalink
Merge pull request metabase#2286 from metabase/fix-sql-server-connect…
Browse files Browse the repository at this point in the history
…ions

Fix non-SSL SQL Server connections
  • Loading branch information
camsaul committed Apr 1, 2016
2 parents ffefcec + d0c02e8 commit 89b92e6
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions src/metabase/driver/sqlserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

(defn- column->base-type
"See [this page](https://msdn.microsoft.com/en-us/library/ms187752.aspx) for details."
[_ column-type]
[column-type]
({:bigint :BigIntegerField
:binary :UnknownField
:bit :BooleanField ; actually this is 1 / 0 instead of true / false ...
Expand Down Expand Up @@ -48,8 +48,11 @@
:xml :UnknownField
(keyword "int identity") :IntegerField} column-type)) ; auto-incrementing integer (ie pk) field

(defn- connection-details->spec [_ {:keys [domain instance ssl], :as details}]
(-> (kdb/mssql details)
(defn- connection-details->spec [{:keys [domain instance ssl], :as details}]
(-> ;; Having the `:ssl` key present, even if it is `false`, will make the driver attempt to connect with SSL
(kdb/mssql (if ssl
details
(dissoc details :ssl)))
;; swap out Microsoft Driver details for jTDS ones
(assoc :classname "net.sourceforge.jtds.jdbc.Driver"
:subprotocol "jtds:sqlserver")
Expand All @@ -75,7 +78,7 @@

(defn- date
"See also the [jTDS SQL <-> Java types table](http://jtds.sourceforge.net/typemap.html)"
[_ unit expr]
[unit expr]
(case unit
:default (kx/->datetime expr)
:minute (kx/cast :SMALLDATETIME expr)
Expand Down Expand Up @@ -107,21 +110,21 @@
:quarter-of-year (date-part :quarter expr)
:year (date-part :year expr)))

(defn- date-interval [_ unit amount]
(defn- date-interval [unit amount]
(date-add unit amount (k/sqlfn :GETUTCDATE)))

(defn- unix-timestamp->timestamp [_ expr seconds-or-milliseconds]
(defn- unix-timestamp->timestamp [expr seconds-or-milliseconds]
(case seconds-or-milliseconds
;; The second argument to DATEADD() gets casted to a 32-bit integer. BIGINT is 64 bites, so we tend to run into
;; integer overflow errors (especially for millisecond timestamps).
;; Work around this by converting the timestamps to minutes instead before calling DATEADD().
:seconds (date-add :minute (kx// expr 60) (kx/literal "1970-01-01"))
:milliseconds (recur nil (kx// expr 1000) :seconds)))
:milliseconds (recur (kx// expr 1000) :seconds)))

(defn- apply-limit [_ korma-query {value :limit}]
(defn- apply-limit [korma-query {value :limit}]
(k/modifier korma-query (format "TOP %d" value)))

(defn- apply-page [_ korma-query {{:keys [items page]} :page}]
(defn- apply-page [korma-query {{:keys [items page]} :page}]
(k/offset korma-query (format "%d ROWS FETCH NEXT %d ROWS ONLY"
(* items (dec page))
items)))
Expand All @@ -133,7 +136,7 @@
(u/strict-extend SQLServerDriver
driver/IDriver
(merge (sql/IDriverSQLDefaultsMixin)
{:date-interval date-interval
{:date-interval (u/drop-first-arg date-interval)
:details-fields (constantly [{:name "host"
:display-name "Host"
:default "localhost"}
Expand Down Expand Up @@ -166,15 +169,15 @@

sql/ISQLDriver
(merge (sql/ISQLDriverDefaultsMixin)
{:apply-limit apply-limit
:apply-page apply-page
:column->base-type column->base-type
:connection-details->spec connection-details->spec
{:apply-limit (u/drop-first-arg apply-limit)
:apply-page (u/drop-first-arg apply-page)
:column->base-type (u/drop-first-arg column->base-type)
:connection-details->spec (u/drop-first-arg connection-details->spec)
:current-datetime-fn (constantly (k/sqlfn* :GETUTCDATE))
:date date
:date (u/drop-first-arg date)
:excluded-schemas (constantly #{"sys" "INFORMATION_SCHEMA"})
:stddev-fn (constantly :STDEV)
:string-length-fn (constantly :LEN)
:unix-timestamp->timestamp unix-timestamp->timestamp}))
:unix-timestamp->timestamp (u/drop-first-arg unix-timestamp->timestamp)}))

(driver/register-driver! :sqlserver (SQLServerDriver.))

0 comments on commit 89b92e6

Please sign in to comment.