aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/db/jdbc/output.lux
blob: a844ac78af6ea01cc9f5d8573de2b8350eb2001f (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
190
191
192
193
194
195
(.using
 [library
  [lux (.except and int)
   [ffi (.only import)]
   [control
    [functor (.only Functor)]
    [apply (.only Apply)]
    [monad (.only Monad do)]
    ["ex" exception]
    ["[0]" try (.only Try)]]
   [time
    ["[0]" instant (.only Instant)]]
   ["[0]" io (.only IO)]
   [world
    [binary (.only 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
      (~~ (with_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: .public (Output a)
  (-> [Nat java/sql/ResultSet] (Try [Nat a])))

(def .public functor
  (Functor Output)
  (implementation
   (def (each f fa)
     (function (_ idx+rs)
       (case (fa idx+rs)
         {try.#Failure error}
         {try.#Failure error}
         
         {try.#Success [idx' value]}
         {try.#Success [idx' (f value)]})))))

(def .public apply
  (Apply Output)
  (implementation
   (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})))))

(def .public monad
  (Monad Output)
  (implementation
   (def functor ..functor)

   (def (in a)
     (function (_ [idx rs])
       {.#Some [idx a]}))
   
   (def (conjoint mma)
     (function (_ [idx rs])
       (case (mma [idx rs])
         {try.#Failure error}
         {try.#Failure error}
         
         {try.#Success [idx' ma]}
         (ma [idx' rs]))))))

(def .public (fail error)
  (All (_ a) (-> Text (Output a)))
  (function (_ [idx result_set])
    {try.#Failure error}))

(def .public (and left right)
  (All (_ a b)
    (-> (Output a) (Output b) (Output [a b])))
  (do ..monad
    [=left left
     =right right]
    (in [=left =right])))

(with_template [<func_name> <method_name> <type>]
  [(def .public <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 [(++ 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]
  )

(with_template [<func_name> <method_name>]
  [(def .public <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 [(++ 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 .public (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}
            (in {try.#Success {.#Item head tail}})

            {try.#Failure error}
            (do io.monad
              [temp (java/sql/ResultSet::close results)]
              (in (do try.monad
                    [_ temp]
                    (try.failure error))))))

        {try.#Failure error}
        (do io.monad
          [temp (java/sql/ResultSet::close results)]
          (in (do try.monad
                [_ temp]
                (try.failure error)))))
      (do io.monad
        [temp (java/sql/ResultSet::close results)]
        (in (do try.monad
              [_ temp]
              (in (list))))))

    {try.#Failure error}
    (do io.monad
      [temp (java/sql/ResultSet::close results)]
      (in (do try.monad
            [_ temp]
            (try.failure error))))
    ))