-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ccc030
commit 40981a0
Showing
11 changed files
with
422 additions
and
493 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
type connection = Sqlite3.db | ||
type prepared_stmt = Sqlite3.stmt | ||
type value = Int of int | Text of string | Null | ||
|
||
let value_of_sqlite3_data = function | ||
| Sqlite3.Data.INT i -> Int (Int64.to_int i) | ||
| Sqlite3.Data.TEXT s -> Text s | ||
| Sqlite3.Data.NULL -> Null | ||
| _ -> failwith "value_of_sqlite3_data: Invalid datatype" | ||
|
||
let sqlite3_data_of_value = function | ||
| Int i -> Sqlite3.Data.INT (Int64.of_int i) | ||
| Text s -> Sqlite3.Data.TEXT s | ||
| Null -> Sqlite3.Data.NULL | ||
|
||
let in_memory_database = ":memory:" | ||
let connect path = Sqlite3.db_open path | ||
let disconnect con = Sqlite3.db_close con |> ignore | ||
let prepare conn sql = Sqlite3.prepare conn sql | ||
|
||
exception Sqlite_error of string | ||
|
||
let raise_if_not_success rc = | ||
if Sqlite3.Rc.is_success rc then () | ||
else raise (Sqlite_error (Sqlite3.Rc.to_string rc)) | ||
|
||
let query stmt values = | ||
try | ||
Sqlite3.reset stmt |> raise_if_not_success; | ||
values | ||
|> List.map sqlite3_data_of_value | ||
|> Sqlite3.bind_values stmt |> raise_if_not_success; | ||
let result, rows = | ||
Sqlite3.fold stmt ~init:[] ~f:(fun acc row -> | ||
(Array.to_list row |> List.map value_of_sqlite3_data) :: acc) | ||
in | ||
raise_if_not_success result; | ||
Ok (List.rev rows) | ||
with Sqlite_error msg -> Error msg | ||
|
||
let execute stmt values = query stmt values |> Result.map (fun _ -> ()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type connection | ||
type prepared_stmt | ||
type value = Int of int | Text of string | Null | ||
|
||
val in_memory_database : string | ||
val connect : string -> connection | ||
val disconnect : connection -> unit | ||
val prepare : connection -> string -> prepared_stmt | ||
val execute : prepared_stmt -> value list -> (unit, string) result | ||
val query : prepared_stmt -> value list -> (value list list, string) result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.