aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/db/jdbc/output.lux
blob: 66578ac8dbc59b37b411deb83c2bb3f54cec6dff (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
181
182
183
184
185
186
187
188
(.module:
  [lux (#- and int)
   [ffi (#+ import:)]
   [control
    [functor (#+ Functor)]
    [apply (#+ Apply)]
    [monad (#+ Monad do)]
    ["ex" exception]
    ["." try (#+ Try)]]
   [time
    ["." instant (#+ Instant)]]
   ["." io (#+ IO)]
   [world
    [binary (#+ Binary)]]])

(import: java/lang/String)

(import: java/util/Date
  (getTime [] long))

(import: java/sql/Date)
(import: java/sql/Time)
(import: java/sql/Timestamp)

(`` (import: java/sql/ResultSet
      (~~ (template [<method-name> <return-class>]
            [(<method-name> [int] #try <return-class>)]
            
            [getBoolean   boolean]
            
            [getByte      byte]
            [getShort     short]
            [getInt       int]
            [getLong      long]

            [getDouble    double]
            [getFloat     float]

            [getString    java/lang/String]
            [getBytes     [byte]]
            
            [getDate      java/sql/Date]
            [getTime      java/sql/Time]
            [getTimestamp java/sql/Timestamp]
            ))
      (next [] #try boolean)
      (close [] #io #try void)))

(type: #export (Output a)
  (-> [Nat java/sql/ResultSet] (Try [Nat a])))

(structure: #export functor (Functor Output)
  (def: (map f fa)
    (function (_ idx+rs)
      (case (fa idx+rs)
        (#try.Failure error)
        (#try.Failure error)
        
        (#try.Success [idx' value])
        (#try.Success [idx' (f value)])))))

(structure: #export apply (Apply Output)
  (def: &functor ..functor)

  (def: (apply ff fa)
    (function (_ [idx rs])
      (case (ff [idx rs])
        (#try.Success [idx' f])
        (case (fa [idx' rs])
          (#try.Success [idx'' a])
          (#try.Success [idx'' (f a)])

          (#try.Failure msg)
          (#try.Failure msg))

        (#try.Failure msg)
        (#try.Failure msg)))))

(structure: #export monad (Monad Output)
  (def: &functor ..functor)

  (def: (wrap a)
    (function (_ [idx rs])
      (#.Some [idx a])))
  
  (def: (join mma)
    (function (_ [idx rs])
      (case (mma [idx rs])
        (#try.Failure error)
        (#try.Failure error)
        
        (#try.Success [idx' ma])
        (ma [idx' rs])))))

(def: #export (fail error)
  (All [a] (-> Text (Output a)))
  (function (_ [idx result-set])
    (#try.Failure error)))

(def: #export (and left right)
  (All [a b]
    (-> (Output a) (Output b) (Output [a b])))
  (do ..monad
    [=left left
     =right right]
    (wrap [=left =right])))

(template [<func-name> <method-name> <type>]
  [(def: #export <func-name>
     (Output <type>)
     (function (_ [idx result-set])
       (case (<method-name> [(.int idx)] result-set)
         (#try.Failure error)
         (#try.Failure error)

         (#try.Success value)
         (#try.Success [(inc idx) value]))))]

  [boolean java/sql/ResultSet::getBoolean Bit]

  [byte    java/sql/ResultSet::getByte    Int]
  [short   java/sql/ResultSet::getShort   Int]
  [int     java/sql/ResultSet::getInt     Int]
  [long    java/sql/ResultSet::getLong    Int]

  [float   java/sql/ResultSet::getFloat   Frac]
  [double  java/sql/ResultSet::getDouble  Frac]

  [string  java/sql/ResultSet::getString  Text]
  [bytes   java/sql/ResultSet::getBytes   Binary]
  )

(template [<func-name> <method-name>]
  [(def: #export <func-name>
     (Output Instant)
     (function (_ [idx result-set])
       (case (<method-name> [(.int idx)] result-set)
         (#try.Failure error)
         (#try.Failure error)

         (#try.Success value)
         (#try.Success [(inc idx)
                        (instant.from-millis (java/util/Date::getTime value))]))))]

  [date       java/sql/ResultSet::getDate]
  [time       java/sql/ResultSet::getTime]
  [time-stamp java/sql/ResultSet::getTimestamp]
  )

(def: #export (rows output results)
  (All [a] (-> (Output a) java/sql/ResultSet (IO (Try (List a)))))
  (case (java/sql/ResultSet::next results)
    (#try.Success has-next?)
    (if has-next?
      (case (output [1 results])
        (#.Some [_ head])
        (do io.monad
          [?tail (rows output results)]
          (case ?tail
            (#try.Success tail)
            (wrap (ex.return (#.Cons head tail)))

            (#try.Failure error)
            (do io.monad
              [temp (java/sql/ResultSet::close results)]
              (wrap (do try.monad
                      [_ temp]
                      (try.fail error))))))

        (#try.Failure error)
        (do io.monad
          [temp (java/sql/ResultSet::close results)]
          (wrap (do try.monad
                  [_ temp]
                  (try.fail error)))))
      (do io.monad
        [temp (java/sql/ResultSet::close results)]
        (wrap (do try.monad
                [_ temp]
                (wrap (list))))))

    (#try.Failure error)
    (do io.monad
      [temp (java/sql/ResultSet::close results)]
      (wrap (do try.monad
              [_ temp]
              (try.fail error))))
    ))