File: cursor.ml

package info (click to toggle)
postgresql-ocaml 1.5.4-2%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 320 kB
  • ctags: 536
  • sloc: ml: 1,143; ansic: 524; sh: 118; makefile: 99
file content (36 lines) | stat: -rw-r--r-- 1,112 bytes parent folder | download | duplicates (9)
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
(* Dump a table to stdout (using a cursor for demonstration) *)

open Postgresql

let _ =
  if Array.length Sys.argv <> 3 then (
    Printf.printf "\
      Usage:  cursor conninfo table\n\
      Connect to PostgreSQL with [conninfo] (e.g. \"host=localhost\"),\n\
      and copy [table] to stdout using a cursor\n";
    exit 1)

let main () =
  let c = new connection ~conninfo:Sys.argv.(1) () in
  ignore (c#exec ~expect:[Command_ok] "BEGIN");
  ignore (
    c#exec
      ~expect:[Command_ok]
      ("DECLARE my_cursor CURSOR FOR SELECT * FROM " ^ Sys.argv.(2)));
  let rec loop () =
    let res = c#exec ~expect:[Tuples_ok] "FETCH IN my_cursor" in
    if res#ntuples <> 0 then (
      let tpl = res#get_tuple 0 in
      print_string tpl.(0);
      for i = 1 to Array.length tpl - 1 do print_string (" " ^ tpl.(i)) done;
      print_newline ();
      loop ()) in
  loop ();
  ignore (c#exec ~expect:[Command_ok] "CLOSE my_cursor");
  ignore (c#exec ~expect:[Command_ok] "END");
  c#finish

let _ =
  try main () with
  | Error e -> prerr_endline (string_of_error e)
  | e -> prerr_endline (Printexc.to_string e)