Back

as-cols (clj)

(source)

function

(as-cols cols opts)
Given a sequence of raw column names, return a string of all the formatted column names. If a raw column name is a keyword, apply `:column-fn` to its name, from the options if present. If a raw column name is a vector pair, treat it as an expression with an alias. If the first item is a keyword, apply `:column-fn` to its name, else accept it as-is. The second item should be a keyword and that will have `:column-fn` applied to its name. This allows columns to be specified as simple names, e.g., `:foo`, as simple aliases, e.g., `[:foo :bar]`, or as expressions with an alias, e.g., `["count(*)" :total]`.

Examples

next-jdbc
(ns next.jdbc.sql.builder-test
  "Tests for the SQL string building functions in next.jdbc.sql.builder."
  (:require [clojure.test :refer [deftest is testing]]
            [next.jdbc.quoted :refer [mysql sql-server]]
            [next.jdbc.sql.builder :as builder]))

(deftest test-as-cols
  (is (= (builder/as-cols [:a :b :c] {})
         "a, b, c"))
  (is (= (builder/as-cols [[:a :aa] :b ["count(*)" :c]] {})
         "a AS aa, b, count(*) AS c"))
  (is (= (builder/as-cols [[:a :aa] :b ["count(*)" :c]] {:column-fn mysql})
         "`a` AS `aa`, `b`, count(*) AS `c`")))
seancorfield/next-jdbc
(ns next.jdbc.sql.builder-test
  "Tests for the SQL string building functions in next.jdbc.sql.builder."
  (:require [clojure.test :refer [deftest is testing]]
            [next.jdbc.quoted :refer [mysql sql-server]]
            [next.jdbc.sql.builder :as builder]))

(deftest test-as-cols
  (is (= (builder/as-cols [:a :b :c] {})
         "a, b, c"))
  (is (= (builder/as-cols [[:a :aa] :b ["count(*)" :c]] {})
         "a AS aa, b, count(*) AS c"))
  (is (= (builder/as-cols [[:a :aa] :b ["count(*)" :c]] {:column-fn mysql})
         "`a` AS `aa`, `b`, count(*) AS `c`")))