aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/db/jdbc.jvm.lux
blob: e73adef88e3852cc803068bd4f8199be9c33dd3a (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
(.module:
  [lux (#- and int)
   [control
    [functor (#+ Functor)]
    [apply (#+ Apply)]
    [monad (#+ Monad do)]
    ["ex" exception]
    [concurrency
     ["." promise (#+ Promise) ("promise/." monad)]]
    [security
     ["!" capability (#+ capability:)]]]
   [data
    ["." product]
    ["." error (#+ Error)]
    [text
     format]
    [collection
     [list ("list/." fold)]]]
   ["." io (#+ IO)]
   [world
    [net (#+ URL)]]
   [host (#+ import:)]]
  [//
   ["." sql]]
  [/
   ["/." input (#+ Input)]
   ["/." output (#+ Output)]])

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

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

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

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

(import: #long java/sql/DriverManager
  (#static getConnection [String String 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})

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

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

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

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

(signature: #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 (Error a)))
        (IO (Error a))))
  (do (error.with-error 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))
  (`` (structure
       (~~ (do-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 (Error (DB IO))))
  (do (error.with-error io.monad)
    [connection (java/sql/DriverManager::getConnection (get@ #url creds)
                                                       (get@ #user creds)
                                                       (get@ #password creds))]
    (wrap (: (DB IO)
             (structure
              (def: execute
                (..can-execute
                 (function (execute statement)
                   (with-statement statement connection
                     (function (_ prepared)
                       (do (error.with-error 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 (error.with-error 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 (error.with-error 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 (Error a)))
        (IO (Error a))))
  (do (error.with-error 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 (Error a)))
        (Promise (Error a))))
  (do (error.with-error promise.monad)
    [db (promise.future (..connect creds))
     result (action (..async db))
     _ (promise/wrap (io.run (!.use (:: db close) [])))]
    (wrap result)))