Skip to content

Commit

Permalink
implement custom pk resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
stepansergeevitch committed Sep 11, 2024
1 parent 6676042 commit 8786a1c
Showing 1 changed file with 21 additions and 20 deletions.
41 changes: 21 additions & 20 deletions src/metabase/driver/firebolt.clj
Original file line number Diff line number Diff line change
Expand Up @@ -336,26 +336,27 @@
{:semantic-type semantic-type}))))
(fields-metadata driver conn table db-name-or-nil)))

(defn add-table-pks
"Using `metadata` find any primary keys for `table` and assoc `:pk?` to true for those columns."
[^DatabaseMetaData metadata table]
(let [pks (into #{} (common/reducible-results #(.getPrimaryKeys metadata nil nil (:name table))
(fn [^ResultSet rs]
#(.getString rs "COLUMN_NAME"))))]
(update table :fields (fn [fields]
(set (for [field fields]
(if-not (contains? pks (:name field))
field
(assoc field :pk? true))))))))

(defmethod driver/describe-table :firebolt
[driver database table]
(let [spec (sql-jdbc.conn/db->pooled-connection-spec database)]
(with-open [conn (jdbc/get-connection spec)]
(->> (assoc (select-keys table [:name :schema])
:fields (describe-table-fields driver conn table nil))
;; find PKs and mark them
(add-table-pks (.getMetaData conn))))))
(defmethod get-table-pks :firebolt
[_ ^Connection conn db-name-or-nil table]
(let [table-name (get table :name)
schema (get table :schema)
sql-query (if (nil? db-name-or-nil)
(str "SELECT primary-index FROM information_schema.tables WHERE table_name = ? AND table_schema = ?")
(str "SELECT primary-index FROM information_schema.tables WHERE table_name = ? AND table_schema = ? AND table_catalog = ?")
)
sql-params (if (nil? db-name-or-nil)
[table-name schema]
[table-name schema db-name-or-nil]
)
pk-result (jdbc/query {:connection conn}
concat ([sql-query] sql-params))
]
(->> pk-result
first ;; get the first row
:primary-index ;; get the primary-index column
(str/split ",") ;; split the primary-index column
(map str/trim) ;; trim the column names
(vec)))) ;; convert to vector

;-------------------------Supported features---------------------------

Expand Down

0 comments on commit 8786a1c

Please sign in to comment.