Git Product home page Git Product logo

clj-sql-pred's Introduction

clj-sql-pred

Convert simple sql statements to predicates for filtering collections of Clojure/ ClojureScript maps.

The sql-pred function in the core namespace takes a sql like string and returns a Clojure predicate function that works on maps.

allowed sql keywords: and, or, =, not, in, >, >=, <, <=

Clojars

[clj-sql-pred "0.1.4"]

Example usage:

clj-sql-pred.core> (def coll [{:a "clouds" :c 20} {:a "trains" :b "red" :c 35}])
#'clj-sql-pred.core/coll

clj-sql-pred.core> (filter (sql-pred "c = 20" :keywordize-keys? true) coll)
({:a "clouds", :c 20})

clj-sql-pred.core> (filter (sql-pred "c > 20" :keywordize-keys? true) coll)
({:a "trains", :b "red", :c 35})

clj-sql-pred.core> (filter (sql-pred "c > 19 and c < 50" :keywordize-keys? true) coll)
({:a "clouds", :c 20} {:a "trains", :b "red", :c 35})

clj-sql-pred.core> (filter (sql-pred "c > 19 or c < 50" :keywordize-keys? true) coll)
({:a "clouds", :c 20} {:a "trains", :b "red", :c 35})

clj-sql-pred.core> (filter (sql-pred "a = clouds" :keywordize-keys? true) coll)
({:a "clouds", :c 20})

clj-sql-pred.core> (filter (sql-pred "a in (clouds, trains, bikes, rocks)" :keywordize-keys? true) coll)
({:a "clouds", :c 20} {:a "trains", :b "red", :c 35})

clj-sql-pred.core> (filter (sql-pred "a not in (clouds, bikes, rocks)" :keywordize-keys? true) coll)
({:a "trains", :b "red", :c 35})

clj-sql-pred.core> (filter (sql-pred "a not in (clouds, bikes, rocks) or c = 20" :keywordize-keys? true) coll)
({:a "clouds", :c 20} {:a "trains", :b "red", :c 35})

clj-sql-pred.core> (filter (sql-pred "c not = 20" :keywordize-keys? true) coll)
({:a "trains", :b "red", :c 35})

;; demonstrate the use of skip-missing? the first map does not contain :b
clj-sql-pred.core> (filter (sql-pred "b = red" :keywordize-keys? true :skip-missing? true) coll)
({:a "clouds", :c 20} {:a "trains", :b "red", :c 35})
;; but still makes it through the filter

The keywordize-keys? optional boolean argument determines whether the keywords in your sql-like statement e.g. 'c' is converted to a clojure keyword, e.g. :c before any comparisons are done. In the above examples, that's necessary as the maps in the collection contain :c not "c".

The skip-missing? optional boolean argument determines what happens when a map in your collection does not contain the key being filtered on. A value of true will mean that if the map doesn't contain the key being filtered for that the map will pass successfully and not be filtered. A value of false will mean that the filtered is applied to the map, but without the key, the fetch of the key's value will return nil and unless your term is looking for nils it will be filtered.

Special cases

  1. Strings with spaces

If one of the strings to be filtered contains spaces, e.g.

(def coll [{:a "the clouds" :c 20} {:a "trains" :b "red" :c 35}])

To get a macth your sql-pred must be quoted with single quotes, e.g.

clj-sql-pred.core> (filter (sql-pred "a = 'the clouds'" :keywordize-keys? true) coll)
({:a "clouds", :c 20})
  1. Match anything

Sometimes you want a particular key in one of your to be tested maps to always pass a match test. This is where the special value "" comes in. For example.

(def coll [{:a "the clouds" :c "<all>"} {:a "trains" :b "red" :c 35}])

 clj-sql-pred.core> (filter (sql-pred "c = 0" :keywordize-keys? true) coll)
({:a "clouds", :c "<all>"})

License

MIT

clj-sql-pred's People

Contributors

judepayne avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.