aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/db/jdbc/input.lux
blob: 790e60c834c40eb96ab4a17e935a7cfed066ea44 (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:
  [library
   [lux {"-" [and int]}
    [ffi {"+" [import:]}]
    [control
     [functor {"+" [Contravariant]}]
     [monad {"+" [Monad do]}]
     ["." try {"+" [Try]}]]
    [time
     ["." instant {"+" [Instant]}]]
    ["." io {"+" [IO]}]
    [world
     [binary {"+" [Binary]}]]]])

(import: java/lang/String)

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

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

(`` (import: java/sql/PreparedStatement
      (~~ (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 [byte]]

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

(type: .public (Input a)
  (-> a [Nat java/sql/PreparedStatement]
      (Try [Nat java/sql/PreparedStatement])))

(implementation: .public contravariant
  (Contravariant Input)
  
  (def: (each f fb)
    (function (fa value circumstance)
      (fb (f value) circumstance))))

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

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

(def: .public empty
  (Input Any)
  (function (_ value context)
    (#try.Success context)))

(template [<function> <type> <setter>]
  [(def: .public <function>
     (Input <type>)
     (function (_ value [idx statement])
       (do try.monad
         [_ (<setter> (.int idx) value statement)]
         (in [(.++ 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]
  )

(template [<function> <setter> <constructor>]
  [(def: .public <function>
     (Input Instant)
     (function (_ value [idx statement])
       (do try.monad
         [_ (<setter> (.int idx)
                      (<constructor> (instant.millis value))
                      statement)]
         (in [(.++ 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]
  )