aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/db/jdbc.lux
blob: 5ef233daf07a2173ec9cd78a043060e57ae986a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
(.module:
  [library
   [lux (#- and int)
    [control
     [functor (#+ Functor)]
     [apply (#+ Apply)]
     [monad (#+ Monad do)]
     ["." try (#+ Try)]
     ["ex" exception]
     [concurrency
      ["." promise (#+ Promise) ("#\." monad)]]
     [security
      ["!" capability (#+ capability:)]]]
    [data
     ["." product]
     [text
      ["%" format (#+ format)]]]
    ["." io (#+ IO)]
    [world
     [net (#+ URL)]]
    [host (#+ import:)]]]
  [//
   ["." sql]]
  ["." / #_
   ["#." input (#+ Input)]
   ["#." output (#+ Output)]])

(import: java/lang/String)

(import: java/sql/ResultSet
  (getRow [] #try int)
  (next [] #try boolean)
  (close [] #io #try void))

(import: java/sql/Statement
  (#static NO_GENERATED_KEYS int)
  (#static RETURN_GENERATED_KEYS int)
  (getGeneratedKeys [] #try java/sql/ResultSet)
  (close [] #io #try void))

(import: java/sql/PreparedStatement
  (executeUpdate [] #io #try int)
  (executeQuery [] #io #try java/sql/ResultSet))

(import: java/sql/Connection
  (prepareStatement [java/lang/String int] #try java/sql/PreparedStatement)
  (isValid [int] #try boolean)
  (close [] #io #try void))

(import: java/sql/DriverManager
  (#static getConnection [java/lang/String java/lang/String java/lang/String] #io #try java/sql/Connection))

(type: #export Credentials
  {#url      URL
   #user     Text
   #password Text})

(type: #export ID Int)

(type: #export (Statement input)
  {#sql sql.Statement
   #input (Input input)
   #value input})

(template [<name> <forge> <output>]
  [(capability: #export (<name> ! i)
     (<forge> (Statement i) (! (Try <output>))))]

  [Can-Execute can-execute Nat]
  [Can-Insert  can-insert  (List ID)]
  )

(capability: #export (Can-Query ! i o)
  (can-query [(Statement i) (Output o)] (! (Try (List o)))))

(capability: #export (Can-Close !)
  (can-close Any (! (Try Any))))

(interface: #export (DB !)
  (: (Can-Execute !)
     execute)
  (: (Can-Insert !)
     insert)
  (: (Can-Query !)
     query)
  (: (Can-Close !)
     close))

(def: (with-statement statement conn action)
  (All [i a]
    (-> (Statement i) java/sql/Connection
        (-> java/sql/PreparedStatement (IO (Try a)))
        (IO (Try a))))
  (do (try.with io.monad)
    [prepared (io.io (java/sql/Connection::prepareStatement (sql.sql (get@ #sql statement))
                                                            (java/sql/Statement::RETURN_GENERATED_KEYS)
                                                            conn))
     _ (io.io ((get@ #input statement) (get@ #value statement) [1 prepared]))
     result (action prepared)
     _ (java/sql/Statement::close prepared)]
    (wrap result)))

(def: #export (async db)
  (-> (DB IO) (DB Promise))
  (`` (implementation
       (~~ (template [<name> <forge>]
             [(def: <name> (<forge> (|>> (!.use (\ db <name>)) promise.future)))]
             
             [execute can-execute]
             [insert can-insert]
             [close can-close]
             [query can-query])))))

(def: #export (connect creds)
  (-> Credentials (IO (Try (DB IO))))
  (do (try.with io.monad)
    [connection (java/sql/DriverManager::getConnection (get@ #url creds)
                                                       (get@ #user creds)
                                                       (get@ #password creds))]
    (wrap (: (DB IO)
             (implementation
              (def: execute
                (..can-execute
                 (function (execute statement)
                   (with-statement statement connection
                     (function (_ prepared)
                       (do (try.with io.monad)
                         [row-count (java/sql/PreparedStatement::executeUpdate prepared)]
                         (wrap (.nat row-count))))))))

              (def: insert
                (..can-insert
                 (function (insert statement)
                   (with-statement statement connection
                     (function (_ prepared)
                       (do (try.with io.monad)
                         [_ (java/sql/PreparedStatement::executeUpdate prepared)
                          result-set (io.io (java/sql/Statement::getGeneratedKeys prepared))]
                         (/output.rows /output.long result-set)))))))

              (def: close
                (..can-close
                 (function (close _)
                   (java/sql/Connection::close connection))))

              (def: query
                (..can-query
                 (function (query [statement output])
                   (with-statement statement connection
                     (function (_ prepared)
                       (do (try.with io.monad)
                         [result-set (java/sql/PreparedStatement::executeQuery prepared)]
                         (/output.rows output result-set)))))))
              )))))

(def: #export (with-db creds action)
  (All [a]
    (-> Credentials
        (-> (DB IO) (IO (Try a)))
        (IO (Try a))))
  (do (try.with io.monad)
    [db (..connect creds)
     result (action db)
     _ (!.use (\ db close) [])]
    (wrap result)))

(def: #export (with-async-db creds action)
  (All [a]
    (-> Credentials
        (-> (DB Promise) (Promise (Try a)))
        (Promise (Try a))))
  (do (try.with promise.monad)
    [db (promise.future (..connect creds))
     result (action (..async db))
     _ (promise\wrap (io.run (!.use (\ db close) [])))]
    (wrap result)))