 (defn- cards:database
   "Return all `Cards` belonging to `Database` with DATABASE-ID."
   [database-id]
-  (sel :many Card (k/order :name :ASC), :database_id database-id, :archived false))
+  (db/select Card, :database_id database-id, :archived false, {:order-by [[:name :asc]]}))
 
 (defn- cards:table
   "Return all `Cards` belonging to `Table` with TABLE-ID."
   [table-id]
-  (sel :many Card (k/order :name :ASC), :table_id table-id, :archived false))
+  (db/select Card, :table_id table-id, :archived false, {:order-by [[:name :asc]]}))
 
 (defn- cards-with-ids
   "Return unarchived `Cards` with CARD-IDS.
    Make sure cards are returned in the same order as CARD-IDS`; `[in card-ids]` won't preserve the order."
   [card-ids]
   {:pre [(every? integer? card-ids)]}
-  (let [card-id->card (sel :many :field->obj [Card :id], :id [in card-ids], :archived false)]
+  (let [card-id->card (db/select-id->obj Card, :id [:in card-ids], :archived false)]
     (filter identity (map card-id->card card-ids))))
 
 (defn- cards:recent
   "Return the 10 `Cards` most recently viewed by the current user, sorted by how recently they were viewed."
   []
-  (cards-with-ids (map :model_id (k/select ViewLog
-                                   (k/aggregate (max :timestamp) :max)
-                                   (k/group :model_id)
-                                   (k/fields :model_id)
-                                   (k/where {:model (kx/literal "card"), :user_id *current-user-id*})
-                                   (k/order :max :DESC)
-                                   (k/limit 10)))))
+  (cards-with-ids (map :model_id (db/select [ViewLog :model_id [:%max.timestamp :max]]
+                                   :model   "card"
+                                   :user_id *current-user-id*
+                                   (-> (h/group :model_id)
+                                       (h/order-by [:max :desc])
+                                       (h/limit 10))))))
 
 (defn- cards:popular
   "All `Cards`, sorted by popularity (the total number of times they are viewed in `ViewLogs`).
    (yes, this isn't actually filtering anything, but for the sake of simplicitiy it is included amongst the filter options for the time being)."
   []
-  (cards-with-ids (map :model_id (k/select ViewLog
-                                   (k/aggregate (count (k/raw "*")) :count)
-                                   (k/group :model_id)
-                                   (k/fields :model_id)
-                                   (k/where {:model (kx/literal "card")})
-                                   (k/order :count :DESC)))))
+  (cards-with-ids (map :model_id (db/select [ViewLog :model_id [:%count.* :count]]
+                                   :model "card"
+                                   (-> (h/group :model_id)
+                                       (h/order-by [:count :desc]))))))
 
 (defn- cards:archived
   "`Cards` that have been archived."
   []
-  (sel :many Card, :archived true, (k/order :name :ASC)))
+  (db/select Card, :archived true, {:order-by [[:name :asc]]}))
 
 (def ^:private filter-option->fn
   "Functions that should be used to return cards for a given filter option. These functions are all be called with `model-id` as the sole paramenter;

(defn- hydrate-favorites
  "Efficiently add `favorite` status for a large collection of `Cards`."
  [cards]
  (let [favorite-card-ids (set (sel :many :field [CardFavorite :card_id], :owner_id *current-user-id*, :card_id [in (map :id cards)]))]
    (for [card cards]
      (assoc card :favorite (contains? favorite-card-ids (:id card))))))

(defn- cards:all
  "Return all `Cards`."
  []
  (sel :many Card, :archived false, (k/order :name :ASC)))

(defn- cards:mine
  "Return all `Cards` created by current user."
  []
  (sel :many Card, :creator_id *current-user-id*, :archived false, (k/order :name :ASC)))

(defn- cards:fav
  "Return all `Cards` favorited by the current user."
  []
  (->> (hydrate (sel :many [CardFavorite :card_id], :owner_id *current-user-id*)
                :card)
       (map :card)
       (filter (complement :archived))

                             [label :refer [Label]]
                             [table :refer [Table]]
                             [view-log :refer [ViewLog]])
            [metabase.util :as u]))

(defn- hydrate-labels
  "Efficiently hydrate the `Labels` for a large collection of `Cards`."
  [cards]
  (let [card-labels          (db/select [CardLabel :card_id :label_id])
        label-id->label      (when (seq card-labels)
                               (db/select-id->obj Label :id [:in (map :label_id card-labels)]))
        card-id->card-labels (group-by :card_id card-labels)]
    (for [card cards]
      (assoc card :labels (for [card-label (card-id->card-labels (:id card))] ; TODO - do these need to be sorted ?
