aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/world/db/jdbc/input.jvm.lux
blob: 8e1797ab0e5f9dab4ad17492e912d8b8c652f253 (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
(.module:
  [lux (#- and int)
   [control
    [functor (#+ Contravariant)]
    [monad (#+ Monad do)]]
   [data
    ["." error (#+ Error)]
    [collection
     ["." list ("#/." fold)]]]
   [time
    ["." instant (#+ Instant)]]
   ["." io (#+ IO)]
   [world
    [binary (#+ Binary)]]
   [host (#+ import:)]])

(import: #long java/lang/String)

(do-template [<class>]
  [(import: #long <class>
     (new [long]))]

  [java/sql/Date] [java/sql/Time] [java/sql/Timestamp]
  )

(`` (import: #long java/sql/PreparedStatement
      (~~ (do-template [<name> <type>]
            [(<name> [int <type>] #try void)]

            [setBoolean boolean]

            [setByte byte]
            [setShort short]
            [setInt int]
            [setLong long]
            
            [setFloat float]
            [setDouble double]

            [setString java/lang/String]
            [setBytes (Array byte)]

            [setDate java/sql/Date]
            [setTime java/sql/Time]
            [setTimestamp java/sql/Timestamp]
            ))))

(type: #export (Input a)
  (-> a [Nat java/sql/PreparedStatement]
      (Error [Nat java/sql/PreparedStatement])))

(structure: #export contravariant (Contravariant Input)
  (def: (map-1 f fb)
    (function (fa value circumstance)
      (fb (f value) circumstance))))

(def: #export (and pre post)
  (All [l r] (-> (Input l) (Input r) (Input [l r])))
  (function (_ [left right] context)
    (do error.monad
      [context (pre left context)]
      (post right context))))

(def: #export (fail error)
  (All [a] (-> Text (Input a)))
  (function (_ value [idx context])
    (#error.Failure error)))

(def: #export empty
  (Input Any)
  (function (_ value context)
    (#error.Success context)))

(do-template [<function> <type> <setter>]
  [(def: #export <function>
     (Input <type>)
     (function (_ value [idx statement])
       (do error.monad
         [_ (<setter> (.int idx) value statement)]
         (wrap [(.inc idx) statement]))))]

  [boolean Bit java/sql/PreparedStatement::setBoolean]

  [byte   Int    java/sql/PreparedStatement::setByte]
  [short  Int    java/sql/PreparedStatement::setShort]
  [int    Int    java/sql/PreparedStatement::setInt]
  [long   Int    java/sql/PreparedStatement::setLong]

  [float  Frac   java/sql/PreparedStatement::setFloat]
  [double Frac   java/sql/PreparedStatement::setDouble]

  [string Text   java/sql/PreparedStatement::setString]
  [bytes  Binary java/sql/PreparedStatement::setBytes]
  )

(do-template [<function> <setter> <constructor>]
  [(def: #export <function>
     (Input Instant)
     (function (_ value [idx statement])
       (do error.monad
         [_ (<setter> (.int idx)
                      (<constructor> (instant.to-millis value))
                      statement)]
         (wrap [(.inc idx) statement]))))]

  [date       java/sql/PreparedStatement::setDate      java/sql/Date::new]
  [time       java/sql/PreparedStatement::setTime      java/sql/Time::new]
  [time-stamp java/sql/PreparedStatement::setTimestamp java/sql/Timestamp::new]
  )