aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/db/jdbc.lux
blob: 9c64494ad56b34ce5ef46494c70b5f95a9f419ab (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
177
178
179
180
(.using
 [library
  [lux (.except and int)
   [control
    [functor (.only Functor)]
    [apply (.only Apply)]
    [monad (.only Monad do)]
    ["[0]" try (.only Try)]
    ["ex" exception]
    [concurrency
     ["[0]" async (.only Async) (.open: "[1]#[0]" monad)]]
    [security
     ["!" capability (.only capability:)]]]
   [data
    ["[0]" product]
    [text
     ["%" format (.only format)]]]
   ["[0]" io (.only IO)]
   [world
    [net (.only URL)]]
   [host (.only import)]]]
 [//
  ["[0]" sql]]
 ["[0]" /
  ["[1][0]" input (.only Input)]
  ["[1][0]" output (.only 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: .public Credentials
  (Record
   [#url      URL
    #user     Text
    #password Text]))

(type: .public ID
  Int)

(type: .public (Statement input)
  (Record
   [#sql sql.Statement
    #input (Input input)
    #value input]))

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

  [Can_Execute can_execute Nat]
  [Can_Insert  can_insert  (List ID)]
  )

(capability: .public (Can_Query ! i o)
  (can_query [(Statement i) (Output o)] (! (Try (List o)))))

(capability: .public (Can_Close !)
  (can_close Any (! (Try Any))))

(type: .public (DB !)
  (Interface
   (is (Can_Execute !)
       execute)
   (is (Can_Insert !)
       insert)
   (is (Can_Query !)
       query)
   (is (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 (the #sql statement))
                                                            (java/sql/Statement::RETURN_GENERATED_KEYS)
                                                            conn))
     _ (io.io ((the #input statement) (the #value statement) [1 prepared]))
     result (action prepared)
     _ (java/sql/Statement::close prepared)]
    (in result)))

(def: .public (async db)
  (-> (DB IO) (DB Async))
  (`` (implementation
       (~~ (template [<name> <forge>]
             [(def: <name> (<forge> (|>> (!.use (at db <name>)) async.future)))]
             
             [execute can_execute]
             [insert can_insert]
             [close can_close]
             [query can_query])))))

(def: .public (connect creds)
  (-> Credentials (IO (Try (DB IO))))
  (do (try.with io.monad)
    [connection (java/sql/DriverManager::getConnection (the #url creds)
                                                       (the #user creds)
                                                       (the #password creds))]
    (in (is (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)]
                        (in (.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: .public (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 (at db close) [])]
    (in result)))

(def: .public (with_async_db creds action)
  (All (_ a)
    (-> Credentials
        (-> (DB Async) (Async (Try a)))
        (Async (Try a))))
  (do (try.with async.monad)
    [db (async.future (..connect creds))
     result (action (..async db))
     _ (async#in (io.run! (!.use (at db close) [])))]
    (in result)))