Really intriguing article about a SQL syntax extension that has apparently already been trialed at Google.

As someone who works with SQL for hours every week, this makes me hopeful for potential improvements, although the likelihood of any changes to SQL arriving in my sector before I retire seems slim.

  • anti-idpol action@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    edit-2
    3 months ago

    in Clojure it’s -> for inserting the piped argument at the head position in the args of whatever it’s passed to and ->> at the tail. It’s great for working with immutable data in series of approachable transformations and also what I believe to be a reason behind so many DSLs for generative programming were written in that language (besides REPL). Oh and don’t worry about excessive copying as that is generally well optimized.

    It can be super useful with what itself is a kind of a DSL for SQL more than an ORM, called HoneySQL, like

    (defn apply-filters [query filters]
    "applies WHERE clauses to a query"
      (reduce (fn [q [column value]]
                (helpers/where q [:= column value]))
              query
              filters))
    
    (defn build-dynamic-query [{:keys [table columns filters sort-by limit]}]
      (-> {}
          (helpers/select columns)
          (helpers/from table)
          (apply-filters filters)
          (helpers/order-by sort-by)
          (helpers/limit limit)
          sql/format))
    
    ;; Result - a super readable function call that resembles a natural language 
    (build-dynamic-query 
      {:table :products 
       :columns [:id :name :price] 
       :filters {:category "electronics" :in-stock true}
       :sort-by [:price :desc]
       :limit 20})