aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/world/db/jdbc/input.lux
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/source/library/lux/world/db/jdbc/input.lux')
-rw-r--r--stdlib/source/library/lux/world/db/jdbc/input.lux107
1 files changed, 107 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/world/db/jdbc/input.lux b/stdlib/source/library/lux/world/db/jdbc/input.lux
new file mode 100644
index 000000000..9c3de1238
--- /dev/null
+++ b/stdlib/source/library/lux/world/db/jdbc/input.lux
@@ -0,0 +1,107 @@
+(.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: #export (Input a)
+ (-> a [Nat java/sql/PreparedStatement]
+ (Try [Nat java/sql/PreparedStatement])))
+
+(implementation: #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 try.monad
+ [context (pre left context)]
+ (post right context))))
+
+(def: #export (fail error)
+ (All [a] (-> Text (Input a)))
+ (function (_ value [idx context])
+ (#try.Failure error)))
+
+(def: #export empty
+ (Input Any)
+ (function (_ value context)
+ (#try.Success context)))
+
+(template [<function> <type> <setter>]
+ [(def: #export <function>
+ (Input <type>)
+ (function (_ value [idx statement])
+ (do try.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]
+ )
+
+(template [<function> <setter> <constructor>]
+ [(def: #export <function>
+ (Input Instant)
+ (function (_ value [idx statement])
+ (do try.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]
+ )