aboutsummaryrefslogtreecommitdiff
path: root/lib/sql/math.ml
diff options
context:
space:
mode:
Diffstat (limited to 'lib/sql/math.ml')
-rw-r--r--lib/sql/math.ml20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/sql/math.ml b/lib/sql/math.ml
new file mode 100644
index 0000000..576d9f6
--- /dev/null
+++ b/lib/sql/math.ml
@@ -0,0 +1,20 @@
+(** Math functions *)
+
+let int : Sqlite3.Data.t -> Sqlite3.Data.t =
+ fun data ->
+ match data with
+ (* If the data is already an int, do not change it *)
+ | Sqlite3.Data.INT _ -> data
+ | Sqlite3.Data.FLOAT content -> Sqlite3.Data.INT (Int64.of_float content)
+ | Sqlite3.Data.BLOB content | Sqlite3.Data.TEXT content -> begin
+ match Int64.of_string_opt content with
+ | Some i -> Sqlite3.Data.INT i
+ | None -> begin
+ match Float.of_string_opt content with
+ | Some f -> Sqlite3.Data.INT (Int64.of_float f)
+ | None -> Sqlite3.Data.NULL
+ end
+ end
+ | _ -> Sqlite3.Data.NULL
+
+let register : Sqlite3.db -> unit = fun db -> Sqlite3.create_fun1 db "int" int