Fork me on GitHub
#sql
<
2018-03-08
>
byrongibby09:03:14

Hi I am new to Clojure as well as SQL, so pretty much stumbling around in the dark. What I want to do is execute a stored procedure on MS SQL Server. I can do this successfully in Java:

// Create a variable for the connection string.
        String connectionUrl = "jdbc:" +
                "databaseName=TSA_SARB_v5;integratedSecurity=true";

        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // Establish the connection.
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection(connectionUrl);

            // Create and execute an SQL statement that returns some data.
            String SQL = "pc_ListMyTimeseriesObservations  @TimeseriesCodes='EACM003A,EACM003A',  @StartDate=' ', @EndDate=' '";
            stmt = con.createStatement();
            rs = stmt.executeQuery(SQL);

            // Iterate through the data in the result set and display it.
            while (rs.next()) {
                System.out.println(rs.getString(4) + " " + rs.getString(5));
            }
        }
But I get an error in Clojure: com.microsoft.sqlserver.jdbc.SQLServerException: A result set was generated for update.
(ns jdbc-test.core
  (:require [clojure.java.jdbc :as j]))

(def tsa-sarb
  {:connection-uri (str "jdbc:"
                        "databaseName=TSA_SARB_v5;integratedSecurity=true")})

(j/db-do-prepared tsa-sarb (str "EXEC pc_ListMyTimeseriesObservations "
                                "@TimeseriesCodes='EACM003A,EACM004A', "
                                "@StartDate=' ', @EndDate=' ' "))

seancorfield16:03:48

@byrongibby Try sing j/query instead of j/db-do-prepared (although support for stored procedures is a bit limited right now in clojure.java.jdbc).

seancorfield16:03:20

@venantius Nice to see Korma is being worked on again -- can you provide some background on how and why it has been resurrected?

venantius16:03:14

@seancorfield I use it in a fair number of projects and was alarmed that it didn’t seem to be getting enough maintenance resources.

seancorfield16:03:43

Do you use the ORM-like features of it?

myguidingstar16:03:39

@venantius fyi I'm also working on a brand new clojure library for sql https://github.com/walkable-server/walkable

myguidingstar16:03:10

It just needs some more doc now

venantius16:03:22

I would not expect Korma to start moving “full speed ahead” again. But over the last 18 months people have merged a number of bugfixes and done work to refactor the internal engine, and I wanted to make sure that work didn’t end up in vain

venantius16:03:42

I’m not speaking for the rest of the maintainers, here. I’m just helping out a bit to provide the project with a bit of boost since it’s still in use by a lot of people

seancorfield16:03:02

Due to the lack of maintenance (and the ORM-like nature of it), many folks in the Clojure world tend to warn folks to stay away from Korma. It'll be interesting to see how that shifts now it's getting regularly maintained again.

seancorfield16:03:27

(even if it's not "full speed" 🙂 )

seancorfield17:03:38

@venantius Any thoughts on upgrading Korma to clojure.java.jdbc 0.7.x releases?

justinlee17:03:53

@seancorfield although i can understand that ORM features are not what people want in a language like clojure, i’m actually a little surprised that there isn’t more use of the language to help automate some of what i consider tedious aspects of dealing with sql. am i wrong to say that the “standard” approach is to deal with DDL out of band and write queries by hand or with a query builder?

seancorfield17:03:18

You are pretty much correct @lee.justin.m -- I recommend HoneySQL as a query builder (I started to write my own and integrate it into java.jdbc but community feedback was to keep that concern separate from the core library -- so I just dropped that idea since HoneySQL was well-maintained).

seancorfield17:03:46

I think Korma is the only thing out there that tries to manage key relationships in query/update operations?

justinlee17:03:46

i just can’t get over how weird that is when you have the best metaprogramming tool right there

venantius17:03:10

@seancorfield that sounds great to me 🙂