aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/db/jdbc/output.jvm.lux
blob: 7a45011f5f2ef237db889865f317f52b34e8636f (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
189
(.module:
  [lux (#- and int)
   [control
    [functor (#+ Functor)]
    [apply (#+ Apply)]
    [monad (#+ Monad do)]
    ["ex" exception]]
   [data
    ["." error (#+ Error)]]
   [time
    ["." instant (#+ Instant)]]
   ["." io (#+ IO)]
   [world
    [binary (#+ Binary)]]
   [host (#+ import:)]])

(import: #long java/lang/String)

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

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

(`` (import: #long java/sql/ResultSet
      (~~ (do-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     (Array 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] (Error [Nat a])))

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

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

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

          (#error.Failure msg)
          (#error.Failure msg))

        (#error.Failure msg)
        (#error.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])
        (#error.Failure error)
        (#error.Failure error)
        
        (#error.Success [idx' ma])
        (ma [idx' rs])))))

(def: #export (fail error)
  (All [a] (-> Text (Output a)))
  (function (_ [idx result-set])
    (#error.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])))

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

         (#error.Success value)
         (#error.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]
  )

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

         (#error.Success value)
         (#error.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 (Error (List a)))))
  (case (java/sql/ResultSet::next results)
    (#error.Success has-next?)
    (if has-next?
      (case (output [1 results])
        (#.Some [_ head])
        (do io.monad
          [?tail (rows output results)]
          (case ?tail
            (#error.Success tail)
            (wrap (ex.return (#.Cons head tail)))

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

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

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