Skip to content

Commit 88e04a7

Browse files
committed
Added automatic formatting and GitHub workflows
1 parent 297a6ff commit 88e04a7

22 files changed

+2123
-2210
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
BasedOnStyle: LLVM

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# EditorConfig: https://EditorConfig.org
2+
3+
# Top-most EditorConfig file
4+
root = true
5+
6+
# Default settings for all files
7+
[*]
8+
charset = utf-8
9+
end_of_line = lf
10+
insert_final_newline = true
11+
trim_trailing_whitespace = true
12+
indent_style = space
13+
indent_size = 2
14+
max_line_length = 80
15+
16+
# Makefile
17+
[Makefile]
18+
# Makefiles require tabs instead of spaces
19+
indent_style = tab

.github/workflows/main.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Main workflow
2+
3+
on:
4+
- pull_request
5+
- push
6+
- workflow_dispatch
7+
8+
jobs:
9+
build:
10+
strategy:
11+
fail-fast: false
12+
matrix:
13+
include:
14+
- os: ubuntu-latest
15+
ocaml-compiler: 5.2.0
16+
- os: macos-latest
17+
ocaml-compiler: 5.2.0
18+
19+
runs-on: ${{ matrix.os }}
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v4
24+
25+
- name: Use OCaml ${{ matrix.ocaml-compiler }}
26+
uses: ocaml/setup-ocaml@v2
27+
with:
28+
ocaml-compiler: ${{ matrix.ocaml-compiler }}
29+
30+
- run: opam install -t --deps-only .
31+
32+
- run: opam exec -- make
33+
34+
- run: opam exec -- make -C test
35+
# vim: filetype=yaml

.ocamlformat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
version = 0.26.2
2+
profile = conventional
3+
4+
# Default overrides
5+
wrap-comments = true
6+
parse-docstrings = true

dune

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
(env
2-
(dev
3-
(flags (:standard -w -9 -principal))
4-
(c_flags (:standard -Wall -pedantic -Wextra -Wunused)))
5-
(release (ocamlopt_flags (:standard -O3)))
6-
)
2+
(dev
3+
(flags
4+
(:standard -w -9 -principal))
5+
(c_flags
6+
(:standard -Wall -pedantic -Wextra -Wunused)))
7+
(release
8+
(ocamlopt_flags
9+
(:standard -O3))))

examples/async.ml

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,82 +5,81 @@ let failwith_f fmt = ksprintf failwith fmt
55

66
let _ =
77
if Array.length Sys.argv <> 2 then (
8-
Printf.printf "\
9-
Usage: async conninfo\n\
10-
Connect to PostgreSQL with [conninfo] (e.g. \"host=localhost\"),\n\
11-
and run async tests on a temporary table\n";
8+
Printf.printf
9+
"Usage: async conninfo\n\
10+
Connect to PostgreSQL with [conninfo] (e.g. \"host=localhost\"),\n\
11+
and run async tests on a temporary table\n";
1212
exit 1)
1313

1414
let wait_for_result c =
1515
c#consume_input;
1616
while c#is_busy do
17-
ignore (Unix.select [Obj.magic c#socket] [] [] (-.1.0));
17+
ignore (Unix.select [ Obj.magic c#socket ] [] [] (-1.0));
1818
c#consume_input
1919
done
2020

21-
let fetch_result c = wait_for_result c; c#get_result
21+
let fetch_result c =
22+
wait_for_result c;
23+
c#get_result
2224

2325
let fetch_single_result c =
2426
match fetch_result c with
2527
| None -> assert false
26-
| Some r -> assert (fetch_result c = None); r
28+
| Some r ->
29+
assert (fetch_result c = None);
30+
r
2731

2832
(* See http://www.postgresql.org/docs/devel/static/libpq-connect.html *)
2933
let rec finish_conn socket_fd connect_poll = function
30-
| Polling_failed ->
31-
printf "f\n%!"
34+
| Polling_failed -> printf "f\n%!"
3235
| Polling_reading ->
33-
printf "r,%!";
34-
ignore (Unix.select [socket_fd] [] [] (-1.0));
35-
finish_conn socket_fd connect_poll (connect_poll ())
36+
printf "r,%!";
37+
ignore (Unix.select [ socket_fd ] [] [] (-1.0));
38+
finish_conn socket_fd connect_poll (connect_poll ())
3639
| Polling_writing ->
37-
printf "w,%!";
38-
ignore (Unix.select [] [socket_fd] [] (-1.0));
39-
finish_conn socket_fd connect_poll (connect_poll ())
40-
| Polling_ok ->
41-
printf "c\n%!"
40+
printf "w,%!";
41+
ignore (Unix.select [] [ socket_fd ] [] (-1.0));
42+
finish_conn socket_fd connect_poll (connect_poll ())
43+
| Polling_ok -> printf "c\n%!"
4244

4345
let test (c : connection) =
4446
(* Create a table using a non-prepared statement. *)
45-
c#send_query "\
46-
CREATE TEMPORARY TABLE postgresql_ocaml_async \
47-
(id SERIAL PRIMARY KEY, a INTEGER NOT NULL, b TEXT NOT NULL)";
47+
c#send_query
48+
"CREATE TEMPORARY TABLE postgresql_ocaml_async (id SERIAL PRIMARY KEY, a \
49+
INTEGER NOT NULL, b TEXT NOT NULL)";
4850
assert ((fetch_single_result c)#status = Command_ok);
4951

5052
(* Create another table which will trigger a notice. *)
51-
c#send_query "\
52-
CREATE TEMPORARY TABLE postgresql_ocaml_async_2 \
53-
(id INTEGER PRIMARY KEY \
54-
REFERENCES postgresql_ocaml_async ON DELETE CASCADE)";
53+
c#send_query
54+
"CREATE TEMPORARY TABLE postgresql_ocaml_async_2 (id INTEGER PRIMARY KEY \
55+
REFERENCES postgresql_ocaml_async ON DELETE CASCADE)";
5556
assert ((fetch_single_result c)#status = Command_ok);
5657

57-
begin
58-
c#send_query
59-
~param_types:Postgresql.[|oid_of_ftype INT8; oid_of_ftype INT8|]
60-
~params:[|"4100100100"; "5100100100"|]
61-
"SELECT $1 + $2";
62-
let r = fetch_single_result c in
63-
assert (r#status = Tuples_ok);
64-
assert (r#nfields = 1);
65-
assert (r#ntuples = 1);
66-
assert (r#getvalue 0 0 = "9200200200");
67-
end;
58+
(c#send_query
59+
~param_types:Postgresql.[| oid_of_ftype INT8; oid_of_ftype INT8 |]
60+
~params:[| "4100100100"; "5100100100" |]
61+
"SELECT $1 + $2";
62+
let r = fetch_single_result c in
63+
assert (r#status = Tuples_ok);
64+
assert (r#nfields = 1);
65+
assert (r#ntuples = 1);
66+
assert (r#getvalue 0 0 = "9200200200"));
6867

6968
(* Populate using a prepared statement. *)
7069
let shown_ntuples = 10 in
7170
let expected_ntuples = 3 * 100 in
7271
for i = 0 to 2 do
7372
let stmt = sprintf "test_ins_%d" i in
7473
let param_types =
75-
Array.sub Postgresql.[|oid_of_ftype INT4; oid_of_ftype TEXT|] 0 i
74+
Array.sub Postgresql.[| oid_of_ftype INT4; oid_of_ftype TEXT |] 0 i
7675
in
7776
c#send_prepare stmt ~param_types
7877
"INSERT INTO postgresql_ocaml_async (a, b) VALUES ($1, $2)";
7978
assert ((fetch_single_result c)#status = Command_ok);
8079
for j = 1 to 100 do
81-
let c0 = string_of_int (i + 3 * j) in
82-
let c1 = sprintf "The number %d." (i + 3 * j) in
83-
c#send_query_prepared ~params:[|c0; c1|] stmt;
80+
let c0 = string_of_int (i + (3 * j)) in
81+
let c1 = sprintf "The number %d." (i + (3 * j)) in
82+
c#send_query_prepared ~params:[| c0; c1 |] stmt;
8483
assert ((fetch_single_result c)#status = Command_ok)
8584
done
8685
done;
@@ -116,11 +115,11 @@ let test (c : connection) =
116115
match fetch_result c with
117116
| None -> assert false
118117
| Some r when i < expected_ntuples ->
119-
assert (r#status = Single_tuple);
120-
if i < shown_ntuples then
121-
printf "%s, %s, %s\n" (r#getvalue 0 0) (r#getvalue 0 1) (r#getvalue 0 2)
122-
| Some r ->
123-
assert (r#status = Tuples_ok)
118+
assert (r#status = Single_tuple);
119+
if i < shown_ntuples then
120+
printf "%s, %s, %s\n" (r#getvalue 0 0) (r#getvalue 0 1)
121+
(r#getvalue 0 2)
122+
| Some r -> assert (r#status = Tuples_ok)
124123
done;
125124
printf "[...]\n";
126125
assert (fetch_result c = None);

examples/binary.ml

Lines changed: 36 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,103 +1,87 @@
1-
(* Create a table, insert a binary string various ways, and read it various ways *)
1+
(* Create a table, insert a binary string various ways, and read it various
2+
ways *)
23

34
open! Postgresql
45

56
let _ =
67
if Array.length Sys.argv <> 3 then (
7-
Printf.printf "\
8-
Usage: dump conninfo table\n\
9-
Connect to PostgreSQL with [conninfo] (e.g. \"host=localhost\"),\n\
10-
and create, write to, read from, and then DELETE [table]\n";
8+
Printf.printf
9+
"Usage: dump conninfo table\n\
10+
Connect to PostgreSQL with [conninfo] (e.g. \"host=localhost\"),\n\
11+
and create, write to, read from, and then DELETE [table]\n";
1112
exit 1)
1213

1314
let table = Sys.argv.(2)
15+
1416
let c =
1517
try new connection ~conninfo:Sys.argv.(1) () with
1618
| Error e ->
17-
prerr_endline (string_of_error e);
18-
exit 34
19+
prerr_endline (string_of_error e);
20+
exit 34
1921
| e ->
20-
prerr_endline (Printexc.to_string e);
21-
exit 35
22-
22+
prerr_endline (Printexc.to_string e);
23+
exit 35
2324

2425
let create_table () : unit =
25-
c#exec ~expect:[Command_ok]
26-
("create table " ^ table ^ " (data bytea)")
26+
c#exec ~expect:[ Command_ok ] ("create table " ^ table ^ " (data bytea)")
2727
|> ignore
2828

2929
let write_escaped data : unit =
30-
c#exec ~expect:[Command_ok]
31-
("insert into " ^ table
32-
^ " (data)"
33-
^ " VALUES ('"
34-
^ c#escape_bytea data
35-
^ "')")
30+
c#exec ~expect:[ Command_ok ]
31+
("insert into " ^ table ^ " (data)" ^ " VALUES ('" ^ c#escape_bytea data
32+
^ "')")
3633
|> ignore
3734

3835
let write_binary data : unit =
39-
c#exec ~expect:[Command_ok]
40-
~params:[|data|] ~binary_params:[|true|]
41-
("insert into " ^ table
42-
^ " (data)"
43-
^ " VALUES ($1)")
36+
c#exec ~expect:[ Command_ok ] ~params:[| data |] ~binary_params:[| true |]
37+
("insert into " ^ table ^ " (data)" ^ " VALUES ($1)")
4438
|> ignore
4539

4640
let read_escaped () : string list =
47-
let result =
48-
c#exec ~expect:[Tuples_ok]
49-
("select data from " ^ table)
50-
in
51-
result#get_all_lst
52-
|> List.map List.hd
53-
|> List.map unescape_bytea
41+
let result = c#exec ~expect:[ Tuples_ok ] ("select data from " ^ table) in
42+
result#get_all_lst |> List.map List.hd |> List.map unescape_bytea
5443

5544
let read_binary () : string list =
5645
let result =
57-
c#exec ~expect:[Tuples_ok] ~binary_result:true
46+
c#exec ~expect:[ Tuples_ok ] ~binary_result:true
5847
("select data from " ^ table)
5948
in
60-
result#get_all_lst
61-
|> List.map List.hd
49+
result#get_all_lst |> List.map List.hd
6250

6351
let delete_table () : unit =
64-
c#exec ~expect:[Command_ok]
65-
("drop table if exists " ^ table ^ " cascade")
52+
c#exec ~expect:[ Command_ok ] ("drop table if exists " ^ table ^ " cascade")
6653
|> ignore
6754

68-
let string_list_to_string l =
69-
"["
70-
^ (String.concat ", " l)
71-
^ "]"
55+
let string_list_to_string l = "[" ^ String.concat ", " l ^ "]"
7256

7357
let main () =
7458
delete_table ();
7559
create_table ();
7660
(* first 'line' of a compiled binary, pasted in *)
77-
let data = "ELF>0L@–„$@8 @%$@@@¯¯888†¿†¿ p√p√0p√0ÿ‚! (ƒ(ƒ0(ƒ000TTTDDPÂtd`‘`‘`‘Ã[Ã[QÂtdRÂtdp√p√0p√0ê ê ∑Åù[»·≥¢l∆'„¶•0ë5!@ÇÄ%$ √ bFÄ@ê Ú0êÄàê‡ÄÉê– " in
61+
let data =
62+
"ELF>0L@–„$@8\t@%$@@@¯¯888†¿†¿ p√p√0p√0ÿ‚! \
63+
(ƒ(ƒ0(ƒ000TTTDDPÂtd`‘`‘`‘Ã[Ã[QÂtdRÂtdp√p√0p√0ê ê ∑Åù[»·≥¢l∆'„¶•0ë5!@ÇÄ%$ \
64+
√ bFÄ@ê Ú0êÄàê‡ÄÉê– "
65+
in
7866
write_escaped data;
7967
write_binary data;
8068
let read_esc = read_escaped () in
8169
let read_bin = read_binary () in
82-
let expected = [data; data] in
70+
let expected = [ data; data ] in
8371
(* print_endline ("expected: " ^ string_list_to_string expected); *)
8472
(* print_endline ("read_escaped: " ^ string_list_to_string read_esc); *)
8573
(* print_endline ("read_binary: " ^ string_list_to_string read_bin); *)
86-
if read_esc <> expected
87-
then prerr_endline "read_escaped <> expected";
88-
if read_bin <> expected
89-
then prerr_endline "read_binary <> expected";
90-
if read_esc <> read_bin
91-
then prerr_endline "read_escaped <> read_binary";
74+
if read_esc <> expected then prerr_endline "read_escaped <> expected";
75+
if read_bin <> expected then prerr_endline "read_binary <> expected";
76+
if read_esc <> read_bin then prerr_endline "read_escaped <> read_binary";
9277
delete_table ();
9378
()
9479

95-
9680
let _ =
9781
try main () with
9882
| Error e ->
99-
prerr_endline (string_of_error e);
100-
exit 23
83+
prerr_endline (string_of_error e);
84+
exit 23
10185
| e ->
102-
prerr_endline (Printexc.to_string e);
103-
exit 24
86+
prerr_endline (Printexc.to_string e);
87+
exit 24

0 commit comments

Comments
 (0)