b210690b1958113790a31954efc63c5f6921b626
[nit.git] / lib / sqlite3 / sqlite3.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2013 Guillaume Auger <jeho@resist.ca>
4 # Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 module sqlite3
19
20 in "C header" `{
21 #include "sqlite3.h"
22 `}
23
24 extern class Sqlite3Code `{int`}
25 new ok `{ return SQLITE_OK; `} # 0 /* Successful result */
26 fun is_ok: Bool `{ return recv == SQLITE_OK; `}
27
28 # new `{ return SQLITE_ERROR; `} # 1 /* SQL error or missing database */
29 # new `{ return SQLITE_INTERNAL; `} # 2 /* Internal logic error in SQLite */
30 # new `{ return SQLITE_PERM; `} # 3 /* Access permission denied */
31 # new `{ return SQLITE_ABORT; `} # 4 /* Callback routine requested an abort */
32 # new `{ return SQLITE_BUSY; `} # 5 /* The database file is locked */
33 # new `{ return SQLITE_LOCKED; `} # 6 /* A table in the database is locked */
34 # new `{ return SQLITE_NOMEM; `} # 7 /* A malloc() failed */
35 # new `{ return SQLITE_READONLY; `} # 8 /* Attempt to write a readonly database */
36 # new `{ return SQLITE_INTERRUPT; `} # 9 /* Operation terminated by sqlite3_interrupt()*/
37 # new `{ return SQLITE_IOERR; `} # 10 /* Some kind of disk I/O error occurred */
38 # new `{ return SQLITE_CORRUPT; `} # 11 /* The database disk image is malformed */
39 # new `{ return SQLITE_NOTFOUND; `} # 12 /* Unknown opcode in sqlite3_file_control() */
40 # new `{ return SQLITE_FULL; `} # 13 /* Insertion failed because database is full */
41 # new `{ return SQLITE_CANTOPEN; `} # 14 /* Unable to open the database file */
42 # new `{ return SQLITE_PROTOCOL; `} # 15 /* Database lock protocol error */
43 # new `{ return SQLITE_EMPTY; `} # 16 /* Database is empty */
44 # new `{ return SQLITE_SCHEMA; `} # 17 /* The database schema changed */
45 # new `{ return SQLITE_TOOBIG; `} # 18 /* String or BLOB exceeds size limit */
46 # new `{ return SQLITE_CONSTRAINT; `} # 19 /* Abort due to constraint violation */
47 # new `{ return SQLITE_MISMATCH; `} # 20 /* Data type mismatch */
48 # new `{ return SQLITE_MISUSE; `} # 21 /* Library used incorrectly */
49 # new `{ return SQLITE_NOLFS; `} # 22 /* Uses OS features not supported on host */
50 # new `{ return SQLITE_AUTH; `} # 23 /* Authorization denied */
51 # new `{ return SQLITE_FORMAT; `} # 24 /* Auxiliary database format error */
52 # new `{ return SQLITE_RANGE; `} # 25 /* 2nd parameter to sqlite3_bind out of range */
53 # new `{ return SQLITE_NOTADB; `} # 26 /* File opened that is not a database file */
54 # new `{ return SQLITE_NOTICE; `} # 27 /* Notifications from sqlite3_log() */
55 # new `{ return SQLITE_WARNING; `} # 28 /* Warnings from sqlite3_log() */
56
57 new row `{ return SQLITE_ROW; `} # 100 /* sqlite3_step() has another row ready */
58 fun is_row: Bool `{ return recv == SQLITE_ROW; `}
59
60 new done `{ return SQLITE_DONE; `} # 101 /* sqlite3_step() has finished executing */
61 fun is_done: Bool `{ return recv == SQLITE_DONE; `}
62
63 redef fun to_s: String import String::from_cstring `{
64 #if SQLITE_VERSION_NUMBER >= 3007015
65 char *err = (char *)sqlite3_errstr(recv);
66 #else
67 char *err = "sqlite3_errstr supported only by version >= 3.7.15";
68 #endif
69 if (err == NULL) err = "";
70 return new_String_from_cstring(err);
71 `}
72 end
73
74 extern class Statement `{sqlite3_stmt*`}
75
76 fun step: Sqlite3Code `{
77 return sqlite3_step(recv);
78 `}
79
80 fun column_name(i: Int) : String import String::from_cstring `{
81 const char * name = (sqlite3_column_name(recv, i));
82 if(name == NULL){
83 name = "";
84 }
85 char * ret = (char *) name;
86 return new_String_from_cstring(ret);
87 `}
88
89 fun column_bytes(i: Int) : Int `{
90 return sqlite3_column_bytes(recv, i);
91 `}
92
93 fun column_double(i: Int) : Float `{
94 return sqlite3_column_double(recv, i);
95 `}
96
97 fun column_int(i: Int) : Int `{
98 return sqlite3_column_int(recv, i);
99 `}
100
101 fun column_text(i: Int) : String import String::from_cstring `{
102 char * ret = (char *) sqlite3_column_text(recv, i);
103 if( ret == NULL ){
104 ret = "";
105 }
106 return new_String_from_cstring(ret);
107 `}
108
109 fun column_type(i: Int) : Int `{
110 return sqlite3_column_type(recv, i);
111 `}
112
113 # fun column_blob(i : Int) : String `{
114 # TODO
115 # `}
116
117 fun column_count: Int `{
118 return sqlite3_column_count(recv);
119 `}
120 end
121
122 extern class Sqlite3 `{sqlite3 *`}
123 new open(filename: String) import String::to_cstring `{
124 sqlite3 *self;
125 sqlite3_open(String_to_cstring(filename), &self);
126 return self;
127 `}
128
129 fun destroy do close
130
131 fun close `{ sqlite3_close(recv); `}
132
133 fun exec(sql : String): Sqlite3Code import String::to_cstring `{
134 return sqlite3_exec(recv, String_to_cstring(sql), 0, 0, 0);
135 `}
136
137 fun prepare(sql: String): nullable Statement import String::to_cstring, Statement as nullable `{
138 sqlite3_stmt *stmt;
139 int res = sqlite3_prepare_v2(recv, String_to_cstring(sql), -1, &stmt, 0);
140 if (res == SQLITE_OK)
141 return Statement_as_nullable(stmt);
142 else
143 return null_Statement();
144 `}
145
146 fun last_insert_rowid: Int `{
147 return sqlite3_last_insert_rowid(recv);
148 `}
149
150 fun error: Sqlite3Code `{
151 return sqlite3_errcode(recv);
152 `}
153 end