File: PostgresDB.k

package info (click to toggle)
kaya 0.2.0-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,012 kB
  • ctags: 1,307
  • sloc: cpp: 6,691; haskell: 4,833; sh: 2,868; yacc: 768; makefile: 700; perl: 87
file content (76 lines) | stat: -rw-r--r-- 2,182 bytes parent folder | download
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
64
65
66
67
68
69
70
71
72
73
74
75
76
/** -*-C-*-ish
    Kaya standard library
    Copyright (C) 2004, 2005 Edwin Brady

    This file is distributed under the terms of the GNU Lesser General
    Public Licence. See COPYING for licence.
*/

module PostgresDB;

import Builtins;

// Postgres API suitable for use with the DB module.

import Prelude;
import public DB;

%include "pg_inter.h";
%imported "pg_inter";
%link "pq";

"PGConnection holds data about a connection to a database.
Use this with the functions in DB to get a database independent interface.
<pre>PGDB</pre> is a type synonym for <pre>DBHandle&lt;PGConnection></pre>"
abstract data PGConnection = PGCon(Ptr cdata);

type PGDB = DBHandle<PGConnection>;

foreign "pg_inter.o" {
    Ptr pg_connect(String info) = pg_connect;
    Bool pg_ok(Ptr p) = pg_ok;
    Ptr pg_exec(Ptr vm, Ptr cdata, String query) = pg_exec;
    [[DBValue]] pg_getstrs(Ptr pr) = pg_getstrs;
    Int pg_numrows(Ptr pr) = pg_numrows;
    Int pg_numcols(Ptr pr) = pg_numcols;
    String pg_columnname(Ptr vm, Ptr pr, Int x) = pg_columnname;
    String pg_getError(Ptr pgc) = pg_getError;
    Void pg_close(Ptr cdata) = pg_close;
}

"DEPRECATED synonym for connect"
public DBHandle<PGConnection> connect_pg(String info)
{
    pgc = pg_connect(info);
    if (pg_ok(pgc)==false) {
	throw(Exception(pg_getError(pgc),1));
    }
    con = PGCon(pgc);
    return(DBh(con, exec_pg, defaultIncExec, defaultGetRow, defaultIncDiscard, close_pg));
}

"Connect to a database. 
<em>info</em> holds data about the connection.
Usually, this is the only function you should use from this module; use
functions from the DB module instead with the handle returned by
this function."
public DBHandle<PGConnection> connect(String info) = connect_pg(info);

"Execute a query on the given connection."
DBResult exec_pg(PGConnection con, String query)
{
    pr = pg_exec(getVM(),con.cdata, query);
    strs = pg_getstrs(pr);
    rows = pg_numrows(pr);
    cols = pg_numcols(pr);
    for x in range(0,cols-1) {
	colnames[x] = pg_columnname(getVM(),pr,x);
    }
    return DBRes(strs,rows,cols,colnames,pr);
}

"Close a connection to the database."
Void close_pg(PGConnection con)
{
    pg_close(con.cdata);
}