File: DB.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 (115 lines) | stat: -rw-r--r-- 3,385 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/** -*-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 DB;

import Prelude;
// A database independent API

"DBValue represents the possible values returned by a query.
Anything which is not parseable is returned as a DBText."
public data DBValue 
    = DBText(String text)
    | DBInt(Int num)
    | DBFloat(Float float)
    | DBTime(Int timestamp)
    | DBBool(Bool bool);

"DBResult holds data about the result of a query.
<ul>
<li><em>table</em> gives the data returned.
<li><em>rows</em> is the number of rows in the table
<li><em>cols</em> is the number of columns in the table
<li><em>colnames</em> is the name of the columns
<li><em>resptr</em> is an internal pointer kept by the relevant db interface.
</ul>"
public data DBResult = DBRes([[DBValue]] table,
			     Int rows,
			     Int cols,
			     [String] colnames,
			     Ptr resptr);

"DBIncResult holds a query result for incremental return.
<ul>
<li><em>rows</em> is the number of rows in the table
<li><em>cols</em> is the number of columns in the table
<li><em>colnames</em> is the name of the columns
<li><em>resptr</em> is an internal pointer kept by the relevant db interface.
</ul>"
public data DBIncResult<a> = DBIncRes(DBHandle<a> con,
				      Int rows,
				      Int cols,
				      [String] colnames,
				      Ptr resptr);

"A database handle. 
The parameter is the specific handle type for a db, eg PGConnection.
To create a database handle in supported dbs:
<ul>
<li>Postgres: 
<pre>
      import PostgresDB;
      PGDB db_connect_pg(String info);
</pre>
</ul>"
public data DBHandle<a> = DBh(a handle, 
			      DBResult(a,String) exec,
			      DBIncResult<a>(a,String,DBHandle<a>) incexec,
			      [DBValue](DBIncResult<a>) getrow,
			      Void(DBIncResult<a>) incdiscard,
			      Void(a) close);

"Execute a query with the given connection handle."
public DBResult exec(DBHandle<a> con, String query) {
//    fn = con.exec; 
    return con.exec(con.handle, query);
}

"Execute a query with the given connection handle for incremental retrieval of results."
public DBIncResult<a> incExec(DBHandle<a> con, String query) {
//    fn = con.exec; 
    return con.incexec(con.handle, query, con);
}

"Retrieve a row from an incremental query"
public [DBValue] getRow(DBIncResult<a> res) {
    return res.con.getrow(res);
}

"Discard an incremental query once it is no longer needed"
public Void incDiscard(DBIncResult<a> res) {
  fn = res.con.incdiscard;
  fn(res);
}

"Close a connection"
public Void close(DBHandle<a> con) {
/// FIXME: Bah. The parser should be fixed to allow this to be done properly.
    fn = con.close;
    fn(con.handle);
}

"Get a column name from a result."
public String column(DBResult res, Int col) {
    return (res.colnames[col]);
}

"Default incremental retrieval function for DBs where it is unsupported"
public [DBValue] defaultGetRow(DBIncResult<a> res) {
  throw(NotImplemented);
}

"Default incremental query function for DBs where it is unsupported"
public DBIncResult<a> defaultIncExec(a con, String query, DBHandle<a> cptr) {
  throw(NotImplemented);
}

"Default incremental query discard function for DBs where it is unsupported"
public Void defaultIncDiscard(DBIncResult<a> res) {
  throw(NotImplemented);
}