File: test1.rb

package info (click to toggle)
libpgsql-ruby 0.7.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 192 kB
  • ctags: 231
  • sloc: ruby: 1,359; ansic: 1,260; makefile: 55; sh: 9
file content (63 lines) | stat: -rw-r--r-- 1,285 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#! /usr/bin/env ruby
#
# original file src/test/examples/testlibpq.c
# 
require 'postgres'

def main
  pghost = nil
  pgport = nil
  pgoptions = nil
  pgtty = nil
  dbname = "template1"
  begin
    conn = PGconn.connect(pghost,pgport,pgoptions,pgtty,dbname)
    if $DEBUG
      fd = open("/tmp/trace.out","w")
      conn.trace(fd)
    end 
    res = conn.exec("BEGIN")
    res.clear
    res = conn.exec("DECLARE myportal CURSOR FOR select * from pg_database")
    res.clear

    res = conn.exec("FETCH ALL in myportal")
    if (res.status != PGresult::TUPLES_OK)
      raise PGerror,"FETCH ALL command didn't return tuples properly\n"
    end

    for fld in res.fields
      printf("%-15s",fld)
    end
    printf("\n\n")

    res.result.each do |tupl|
      tupl.each do |fld|
	printf("%-15s",fld)
      end
      printf("\n")
    end
    res = conn.exec("CLOSE myportal")
    res = conn.exec("END")
    res.clear
    conn.close

    if $DEBUG
      fl.close
    end
  rescue PGError
    if (conn.status == PGconn::CONNECTION_BAD)
      printf(STDERR, "We have lost the connection to the backend, so ")
      printf(STDERR, "further processing is impossible.  ")
      printf(STDERR, "Terminating.\n")
    else
      printf(STDERR, conn.error)
    end
    exit(1)
  end
end

main