Use a database with Clojure

后端 未结 12 1547
青春惊慌失措
青春惊慌失措 2021-01-31 08:22

What methods to use a database from Clojure are there?

I know from Clojure you can do anything you can with Java, but that means that I may end up using something overly

相关标签:
12条回答
  • 2021-01-31 08:34

    Nowadays there are Datomic and many similar solutions like DataScript for very interesting relational (non-sql!) database capabilities as well.

    0 讨论(0)
  • 2021-01-31 08:37

    The latest and greatest for SQL databases seems to be HoneySQL and Yesql.

    HoneySQL is a quite elegant DSL to generate SQL queries. There are rumours it can even modify the statements to be highly optimized, see the clojure-group thread "Current best-of-breed JDBC libraries?" from Feb 24 2015.

    Niels van Klaveren says in the above-mentioned thread:

    "Basically, it [HoneySQL] generates SQL scripts to relink foreign key references to clean up duplicates in a database. It takes a honeysql select query with (at least) a from table, a group-by and an order-by clause as a base definition what are to be considered doubles, and in which order records should be preserved. In combination with JDBC metadata that query effectively gets rewritten to generate:

    • A temporary replacement table
    • Queries to unify unique indexes, to prevent clashes when foreign key references are updated
    • Queries to update all foreign key references
    • Delete statements to remove all duplicates

    To create the best performing, but still database independent SQL, I had to extend honeysql with extra clauses like OVER and PARTITION BY. I wouldn't say it was a breeze, but seemed to work very well.

    ...

    That cut down SQL to (sometimes) GB's of script to around a few 100 lines of SQL, and on one occasion, a runtime from 19 hours to 1.5 minutes."

    Yesql, on the other hand, aim for total simplicity. It defines some functions to load parameterized .sql-files.

    It's webpage mentions the following "USPs":

    • No syntactic surprises. Your database doesn't stick to the SQL standard - none of them do - but Yesql doesn't care. You will never spend time hunting for "the equivalent sexp syntax". You will never need to fall back to a (raw-sql "some('funky'::SYNTAX)") function.
    • Better editor support. Your editor probably already has great SQL support. By keeping the SQL as SQL, you get to use it.
    • Team interoperability. Your DBAs can read and write the SQL you use in your Clojure project.
    • Easier performance tuning. Need to EXPLAIN that query plan? It's much easier when your query is ordinary SQL.
    • Query reuse. Drop the same SQL files into other projects, because they're just plain ol' SQL. Share them as a submodule.
    0 讨论(0)
  • 2021-01-31 08:43

    If you are open to using a Java library but want something that embraces simplicity, perhaps you'll like Persist. It'll only take you 10 minutes to have a look and see if it fits your needs.

    0 讨论(0)
  • 2021-01-31 08:43

    If you need persistent connections and/or connections to multiple databases and do not want to reestablish connections every so often I would recommend using DB connection pools. Something like BoneCP or Tomcat CP.

    You can supply DataSources from those packages to (clojure.contrib.sql/with-connection ...).

    0 讨论(0)
  • 2021-01-31 08:44

    Firstly, Import libraries from

     (ns clojureexercise.test
        (:require [clojure.java.jdbc :as sql])) ;;sql will alias used further in code to access java jdbc feature.
    

    Secondly, the function below allows you to connect MySQL server. Like in Java, we declare Database variable to start DB, here it is the same way we have to define Database connectivity and in below code you can see db variable is defined in the dbconnect function. db variable will be used further in running queries.

    (defn dbconnect []
      (def db{   
             :classname "com.mysql.jdbc.Driver" 
             :subprotocol "mysql"
             :subname "//127.0.0.1:3306/testdb" ;;testdb is the name of database
             :user "root"
             :password "password"}))
    

    Now,

    ;;Inserting Data into Database 
    ;;Table Name is patientinfo, consist columns {id, firstname, lastname, birthdate, gender}
     (defn insertdata []
      (sql/insert! db :patientinfo                    ;;used sql alias and db variable to insert data into table 
      {:id 1 :firstname "Satyam" :lastname "Ramawat" 
       :birthdate "1/1/2018" :gender "Male" }))
    

    I will elaborate this more:

    sql/insert! db :patientinfo

    sql will enable insert query functionality and db will allow the system to understand on which table the record has to be inserted of which database connection.

    0 讨论(0)
  • 2021-01-31 08:49

    I'd like to add an as-of-Nov-2011 answer for the sake of anybody coming here from Google.

    The current core SQL access library in Clojure 1.3 is clojure.java.jdbc. There are some very good libraries built on top of this like ClojureQL and Korma.

    0 讨论(0)
提交回复
热议问题