Fork me on GitHub
#sql
<
2018-10-22
>
seancorfield17:10:55

I'm not familiar with that form of DATETIME but we regularly do this sort of thing DATE_SUB( NOW(), INTERVAL ? DAY ) and that works just fine (MySQL) @martinklepsch

martinklepsch17:10:03

might be a sqlite thing but yeah, fair point to look for a datetime helper that takes a number as input

hiredman20:10:32

it because jdbc's syntax using '?' isn't a kind of blind search an replace, the '?' token is only valid in certain places, and it isn't valid in the middle of a string literal

seancorfield21:10:55

Ah, I didn't notice it was in a string in the first piece of code -- no, that definitely won't work. The example I gave isn't using a string. Good 👀 there @hiredman!

tdantas21:10:39

hey guys, need some help here I’m doing a CRUDish application and I have the follow relations

Session 1--------* Orders 1-------* Gateway-Transactions (gonna call transactions only )
                                  |1
                                  |1
                           Movements
 
just a simple usecase to exemplify the relations the system A creates a session with our service (payment system) and will register one sale POST /sessions/1/sale we are going to create one entry into Orders , another one into Transactions ( braintree/paypal/stripe whatever ) and the result , on success, we are going to register one movement ( CREDIT ). easy enough isn’t ? my problem is how to manage relationships using clojure. I’m using hugsql + defrecords to map my tables I would love, without N+1, fetch for instance, all movements with the transactions associated with them

tdantas21:10:23

I’m doing some joins into my Movement domain to fetch all data related with movements and transactions. but I need to map to the proper defrecord (Transaction) for instance

tdantas21:10:52

( missing some ORM … maybe too newbie , need to improve that )

seancorfield21:10:18

It's just data. A joined query will be a sequence of hash maps.

seancorfield21:10:30

You can transform it in memory to whatever shape you would prefer.

seancorfield21:10:50

(ORMs are evil 🙂 )

tdantas21:10:56

yeah @seancorfield , (need to get that into my brain ) the shape (defrecord ) make my life easy to map to tables. so my problem is that the Movement namespace will require the Transaction namespace to shape the result

tdantas21:10:04

I’m coupling both 😞

tdantas21:10:25

(ns movement)
  
 (defn find-all-with-transactions-by-session [session-id] 
     (SQL QUERY WITH JOIN RETURN ALL DATA))

tdantas21:10:53

what I’m doing is:

(defn find-all-with-transactions-by-session [session-id] 
     (map create-movement-transaction (SQL QUERY WITH JOIN RETURN ALL DATA))

tdantas21:10:12

something like that and I’m felling dirty , really dirty ( like OO into functional )

seancorfield22:10:35

Do you really need to separate these things out into separate namespaces? These aren't classes. If movements and transactions are related and you can't have one without the other, why have separate namespaces?

seancorfield22:10:45

Also, why use records here rather than plain ol' maps?

gleisonsilva22:10:03

hello @seancorfield! I'm using clojure.java.jdbc/query to query my database and everything is just fine... but... now I'm facing an issue because I need to extract the column types of the first row for a given query... it works just fine if the columns for that first row all have values, because it is a map, so, doing (type a-column-value) works. But when a column is null on database, a-column-value is also null (`nil`) and I can't get the type

gleisonsilva22:10:58

so I think that if I could get the ResultsetMetadata I could get the type of that column.

gleisonsilva22:10:17

But I can't figure out how to do that.

seancorfield22:10:12

Sounds like you want the metadata for the table really, not for the query?

seancorfield22:10:25

http://clojure-doc.org/articles/ecosystem/java_jdbc/using_ddl.html#accessing-metadata -- that's a rough example that gets metadata for all tables... I'd have to look at the docs for .getTables in the Java SQL API docs for how to get just the one table you want...

seancorfield22:10:05

(and part of me wonders why you need the types of the columns? @gleisonsilva)

gleisonsilva23:10:21

> (and part of me wonders why you need the types of the columns?) because I need to generate a Avro schema for dumping the results of those queries as Avro files

seancorfield23:10:20

Oh, OK. Well, using the metdata from the tables themselves is going to be a better approach anyway I think.