File: SQLiteDB.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,153 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
    SQLite interface library for Kaya
    Copyright (C) 2006 Edwin Brady

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

module SQLiteDB;

import Builtins;

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

import Prelude;
import public DB;

%include "sqlite_inter.h";
%imported "sqlite_inter";
%link "sqlite3";

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

type SQLiteDB = DBHandle<SQLiteConnection>;

foreign "sqlite_inter.o" {
    Ptr do_sqlite_connect(String info) = sqlite_connect;
    Bool sqlite_ok(Ptr con) = sqlite_ok;
    String sqlite_getError(Ptr con) = sqlite_getError;
    Void sqlite_close(Ptr con) = sqlite_close;
    Ptr sqlite_exec(Ptr con, String query) = sqlite_exec;

    [[DBValue]] sql_getrestable(Ptr pr) = sql_getrestable;
    [String] sql_getcolnames(Ptr pr) = sql_getcolnames;
    Int sql_numrows(Ptr pr) = sql_numrows;
    Int sql_numcols(Ptr pr) = sql_numcols;
    Int sql_resrc(Ptr pr) = sql_resrc;
}

"Open a SQLite database file.
Create it if the file does not exist."
public SQLiteDB connect(String fname)
{
    sqlc = do_sqlite_connect(fname);
    con = SQLiteCon(sqlc);
    if (sqlite_ok(sqlc)==false) {
	throw(Exception("SQLite error: "+sqlite_getError(sqlc), 1));
    }
    return DBh(con, module::exec, defaultIncExec, defaultGetRow, defaultIncDiscard, module::close);
}


"Execute a query on the given connection."
DBResult exec(SQLiteConnection con, String query)
{
    rc = sqlite_exec(con.cdata, query);
    if (sql_resrc(rc)==0) {
	dbvs = sql_getrestable(rc);
	rows = sql_numrows(rc);
	cols = sql_numcols(rc);
	colnames = sql_getcolnames(rc);
	return DBRes(dbvs, rows, cols, colnames, rc);
    }
    else {
	throw(Exception("SQLite error: "+sqlite_getError(con.cdata), 1));
    }
}

"Close a connection to the database."
Void close(SQLiteConnection con)
{
    sqlite_close(con.cdata);
}