lib/sqlite3: use a precise type as return of column_type
[nit.git] / lib / sqlite3 / native_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-2014 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 # Low-level Sqlite3 features
19 module native_sqlite3 is pkgconfig("sqlite3")
20
21 in "C header" `{
22 #include <sqlite3.h>
23 `}
24
25 extern class Sqlite3Code `{int`}
26 new ok `{ return SQLITE_OK; `} # 0 /* Successful result */
27 fun is_ok: Bool `{ return recv == SQLITE_OK; `}
28
29 # new `{ return SQLITE_ERROR; `} # 1 /* SQL error or missing database */
30 # new `{ return SQLITE_INTERNAL; `} # 2 /* Internal logic error in SQLite */
31 # new `{ return SQLITE_PERM; `} # 3 /* Access permission denied */
32 # new `{ return SQLITE_ABORT; `} # 4 /* Callback routine requested an abort */
33 # new `{ return SQLITE_BUSY; `} # 5 /* The database file is locked */
34 # new `{ return SQLITE_LOCKED; `} # 6 /* A table in the database is locked */
35 # new `{ return SQLITE_NOMEM; `} # 7 /* A malloc() failed */
36 # new `{ return SQLITE_READONLY; `} # 8 /* Attempt to write a readonly database */
37 # new `{ return SQLITE_INTERRUPT; `} # 9 /* Operation terminated by sqlite3_interrupt()*/
38 # new `{ return SQLITE_IOERR; `} # 10 /* Some kind of disk I/O error occurred */
39 # new `{ return SQLITE_CORRUPT; `} # 11 /* The database disk image is malformed */
40 # new `{ return SQLITE_NOTFOUND; `} # 12 /* Unknown opcode in sqlite3_file_control() */
41 # new `{ return SQLITE_FULL; `} # 13 /* Insertion failed because database is full */
42 # new `{ return SQLITE_CANTOPEN; `} # 14 /* Unable to open the database file */
43 # new `{ return SQLITE_PROTOCOL; `} # 15 /* Database lock protocol error */
44 # new `{ return SQLITE_EMPTY; `} # 16 /* Database is empty */
45 # new `{ return SQLITE_SCHEMA; `} # 17 /* The database schema changed */
46 # new `{ return SQLITE_TOOBIG; `} # 18 /* String or BLOB exceeds size limit */
47 # new `{ return SQLITE_CONSTRAINT; `} # 19 /* Abort due to constraint violation */
48 # new `{ return SQLITE_MISMATCH; `} # 20 /* Data type mismatch */
49 # new `{ return SQLITE_MISUSE; `} # 21 /* Library used incorrectly */
50 # new `{ return SQLITE_NOLFS; `} # 22 /* Uses OS features not supported on host */
51 # new `{ return SQLITE_AUTH; `} # 23 /* Authorization denied */
52 # new `{ return SQLITE_FORMAT; `} # 24 /* Auxiliary database format error */
53 # new `{ return SQLITE_RANGE; `} # 25 /* 2nd parameter to sqlite3_bind out of range */
54 # new `{ return SQLITE_NOTADB; `} # 26 /* File opened that is not a database file */
55 # new `{ return SQLITE_NOTICE; `} # 27 /* Notifications from sqlite3_log() */
56 # new `{ return SQLITE_WARNING; `} # 28 /* Warnings from sqlite3_log() */
57
58 new row `{ return SQLITE_ROW; `} # 100 /* sqlite3_step() has another row ready */
59 fun is_row: Bool `{ return recv == SQLITE_ROW; `}
60
61 new done `{ return SQLITE_DONE; `} # 101 /* sqlite3_step() has finished executing */
62 fun is_done: Bool `{ return recv == SQLITE_DONE; `}
63
64 redef fun to_s: String import NativeString.to_s `{
65 #if SQLITE_VERSION_NUMBER >= 3007015
66 char *err = (char *)sqlite3_errstr(recv);
67 #else
68 char *err = "sqlite3_errstr supported only by version >= 3.7.15";
69 #endif
70 if (err == NULL) err = "";
71 return NativeString_to_s(err);
72 `}
73 end
74
75 # A prepared statement
76 extern class NativeStatement `{sqlite3_stmt*`}
77
78 # Evaluate the statement
79 fun step: Sqlite3Code `{
80 return sqlite3_step(recv);
81 `}
82
83 fun column_name(i: Int) : String import NativeString.to_s `{
84 const char * name = (sqlite3_column_name(recv, i));
85 if(name == NULL){
86 name = "";
87 }
88 char * ret = (char *) name;
89 return NativeString_to_s(ret);
90 `}
91
92 # Number of bytes in the blob or string at row `i`
93 fun column_bytes(i: Int) : Int `{
94 return sqlite3_column_bytes(recv, i);
95 `}
96
97 fun column_double(i: Int) : Float `{
98 return sqlite3_column_double(recv, i);
99 `}
100
101 fun column_int(i: Int) : Int `{
102 return sqlite3_column_int(recv, i);
103 `}
104
105 fun column_text(i: Int) : String import NativeString.to_s `{
106 char * ret = (char *) sqlite3_column_text(recv, i);
107 if( ret == NULL ){
108 ret = "";
109 }
110 return NativeString_to_s(ret);
111 `}
112
113 # Type of the entry at row `i`
114 fun column_type(i: Int): DataType `{
115 return sqlite3_column_type(recv, i);
116 `}
117
118 fun column_blob(i: Int): Pointer `{ return (void*)sqlite3_column_blob(recv, i); `}
119
120 fun column_count: Int `{
121 return sqlite3_column_count(recv);
122 `}
123
124 # Reset this statement to its original state, to be reexecuted
125 fun reset: Sqlite3Code `{ return sqlite3_reset(recv); `}
126
127 # Delete this statement
128 fun finalize: Sqlite3Code `{ return sqlite3_finalize(recv); `}
129 end
130
131 # A database connection
132 extern class NativeSqlite3 `{sqlite3 *`}
133
134 # Open a connection to a database in UTF-8
135 new open(filename: String) import String.to_cstring `{
136 sqlite3 *self = NULL;
137 sqlite3_open(String_to_cstring(filename), &self);
138 return self;
139 `}
140
141 # Has this DB been correctly opened?
142 #
143 # To know if it has been closed or interrupted, you must check for errors with `error`.
144 fun is_valid: Bool do return not address_is_null
145
146 fun destroy do close
147
148 # Close this connection
149 fun close `{ sqlite3_close_v2(recv); `}
150
151 # Execute a SQL statement
152 fun exec(sql: String): Sqlite3Code import String.to_cstring `{
153 return sqlite3_exec(recv, String_to_cstring(sql), 0, 0, 0);
154 `}
155
156 # Prepare a SQL statement
157 fun prepare(sql: String): nullable NativeStatement import String.to_cstring, NativeStatement.as nullable `{
158 sqlite3_stmt *stmt;
159 int res = sqlite3_prepare_v2(recv, String_to_cstring(sql), -1, &stmt, 0);
160 if (res == SQLITE_OK)
161 return NativeStatement_as_nullable(stmt);
162 else
163 return null_NativeStatement();
164 `}
165
166 fun last_insert_rowid: Int `{
167 return sqlite3_last_insert_rowid(recv);
168 `}
169
170 fun error: Sqlite3Code `{
171 return sqlite3_errcode(recv);
172 `}
173 end
174
175 # Sqlite data types
176 extern class DataType `{ int `}
177 fun is_integer: Bool `{ return recv == SQLITE_INTEGER; `}
178 fun is_float: Bool `{ return recv == SQLITE_FLOAT; `}
179 fun is_blob: Bool `{ return recv == SQLITE_BLOB; `}
180 fun is_null: Bool `{ return recv == SQLITE_NULL; `}
181 fun is_text: Bool `{ return recv == SQLITE_TEXT; `}
182
183 fun to_i: Int `{ return recv; `}
184 end