Gangmax Blog

Yesql

Refer here.

Yesql is a Clojure library for using SQL. This is a note of my steps to test it.

Preparation

  • Create a lein project:
1
2
lein new test-yesql
cd test-yesql
  • Update the “project.clj” file by adding the following lines into the “dependencies” section:
1
2
[yesql "0.5.2"]
[mysql/mysql-connector-java "5.1.38"]
  • Download the dependencies:
1
lein deps
  • create a “resources/sql/users.sql” file with the content below:
1
2
3
4
-- name: users-by-id
SELECT *
FROM users
WHERE id = :user_id
  • Start repl:
1
lein repl

Clojure code in lein

1
2
3
4
5
6
7
8
9
10
11
12
user=> (require '[yesql.core :refer [defquery defqueries]])
nil
user=> (def db-spec {:classname "com.mysql.jdbc.Driver"
#_=> :subprotocol "mysql"
#_=> :subname "//10.2.92.41:3306/testlink"
#_=> :user "root"
#_=> :password "ChangeMe"})
#'user/db-spec
user=> (defqueries "sql/users.sql" {:connection db-spec})
(#'user/users-by-id)
user=> (users-by-id {:user_id 7})
({:email "test@abc.com", :locale "en_GB", :password "64a57893f4a7af9728a783e8a6e6bef9", :default_testproject_id nil, :login "test01", :cookie_string "a9e700b2dab56ce15c3974374b5c350df96d24d5c9e674c6226da8a94d38884a", :script_key "7bd1406ea603ed938f5da76a38ab750f", :active true, :id 7, :role_id 8, :first "Test", :last "User"})

Comments