Maintainers
Pricing
Enterprise
More
More
Documentation
Try Live
Rankings
Install
Blog
Docs7
Ask
New
Add Docs
DSQL
https://github.com/healthsamurai/dsql
Admin
dsql is a pure Clojure SQL constructor that generates SQL from data structures, offering
...
Tokens:
620
Snippets:
6
Trust Score:
10
Update:
1 year ago
Context
Chat
Benchmark
Latest
Show doc for...
Code
Info
Show Results
Tokens:
Raw
Copy
Link
Tokens:
Raw
Copy
Link
### Basic PostgreSQL SELECT Query (Clojure) Source: https://github.com/healthsamurai/dsql/blob/master/README.md Demonstrates formatting a simple Clojure data structure into a basic SQL SELECT query using the `dsql.pg/format` function. This example shows the default `:ql/type` for `dsql.pg` and generates a simple 'SELECT * FROM product' statement. ```Clojure (dsql/format {:ql/type :pg/select ;; :select :* :from :product}) ``` -------------------------------- ### Accumulating SQL Parts and Parameters (Clojure) Source: https://github.com/healthsamurai/dsql/blob/master/README.md Illustrates how the accumulator vector (`acc`) is used within `to-sql` methods to build the SQL string and collect parameters. Examples show adding literal strings and parameter placeholders represented as vectors. ```Clojure ["select" "*" "from" "table" "where" "id" "=" ["?" "id"]] ``` ```Clojure (conj acc "current_timestamp()") ``` ```Clojure (conj acc ['?' 'param']) ``` -------------------------------- ### Using Functions and Subqueries (Clojure) Source: https://github.com/healthsamurai/dsql/blob/master/README.md Demonstrates how to represent SQL functions (`^:pg/fn[...]`) and nested subqueries within the dsql data structure. The example shows how `ql/format` correctly generates the corresponding SQL with the function call and the subquery as an argument. ```Clojure (ql/format {:select ^:pg/fn[:function "arg" {:select :* from :user :limit 1}]}) ``` -------------------------------- ### Complex PostgreSQL Query with JSONB and Arrays (Clojure) Source: https://github.com/healthsamurai/dsql/blob/master/README.md Provides a more complex example of using `dsql.pg/format` to construct a query involving JSONB manipulation (`:pg/op[:|| ...]`, `:pg/obj{...}`, `:pg/fn[...]`), array parameters (`[:pg/array-param ...]`), and aliases. It illustrates how dsql handles advanced PostgreSQL features and parameterization. ```Clojure (dsql.pg/format {:select {:resource ^:pg/op[:|| :resource ^:pg/obj{:id :id :resourceType "Patient"}]} :from {:pt :patient} :where {:match ^:pg/op[:&& ^:pg/fn[:knife_extract_text :resource ^:pg/jsonb[["match"]]] [:pg/array-param :text ["a-1" "b-2"]]]} :limit 10}) ``` -------------------------------- ### Defining a Custom to-sql Method (Clojure) Source: https://github.com/healthsamurai/dsql/blob/master/README.md Shows the signature for defining a custom method for the `dsql.core/to-sql` multimethod. This is the primary mechanism for extending dsql's syntax and handling new data structure nodes by providing a custom constructor. ```Clojure (defmethod ql/to-sql :<key> [acc opts node] ) ``` -------------------------------- ### Requiring dsql.pg Namespace (Clojure) Source: https://github.com/healthsamurai/dsql/blob/master/README.md Imports the `dsql.pg` namespace, aliasing it as `dsql`, which is necessary for using the PostgreSQL dialect formatting functions provided by the library. ```Clojure (require '[dsql.pg :as dsql]) ```