#include <sqlite3.h>
`}
+redef class Sys
+ # Last error raised when calling `Sqlite3::open`
+ var sqlite_open_error: nullable Sqlite3Code = null
+end
+
extern class Sqlite3Code `{int`}
new ok `{ return SQLITE_OK; `} # 0 /* Successful result */
fun is_ok: Bool `{ return recv == SQLITE_OK; `}
return sqlite3_column_int(recv, i);
`}
- fun column_text(i: Int) : String import NativeString.to_s `{
- char * ret = (char *) sqlite3_column_text(recv, i);
- if( ret == NULL ){
- ret = "";
- }
- return NativeString_to_s(ret);
+ fun column_text(i: Int): NativeString `{
+ return (char *)sqlite3_column_text(recv, i);
`}
# Type of the entry at row `i`
extern class NativeSqlite3 `{sqlite3 *`}
# Open a connection to a database in UTF-8
- new open(filename: String) import String.to_cstring `{
+ new open(filename: String) import String.to_cstring, set_sys_sqlite_open_error `{
sqlite3 *self = NULL;
- sqlite3_open(String_to_cstring(filename), &self);
+ int err = sqlite3_open(String_to_cstring(filename), &self);
+ NativeSqlite3_set_sys_sqlite_open_error(self, (void*)(long)err);
+ // The previous cast is a hack, using non pointers in extern classes is not
+ // yet in the spec of the FFI.
return self;
`}
+ # Utility method to set `Sys.sqlite_open_error`
+ private fun set_sys_sqlite_open_error(err: Sqlite3Code) do sys.sqlite_open_error = err
+
# Has this DB been correctly opened?
#
# To know if it has been closed or interrupted, you must check for errors with `error`.
fun destroy do close
# Close this connection
- fun close `{ sqlite3_close_v2(recv); `}
+ fun close `{
+#if SQLITE_VERSION_NUMBER >= 3007014
+ sqlite3_close_v2(recv);
+#else
+ // A program using the older version should not rely on the garbage-collector
+ // to close its connections. They must be closed manually after the associated
+ // prepare statements have been finalized.
+ sqlite3_close(recv);
+#endif
+ `}
# Execute a SQL statement
fun exec(sql: String): Sqlite3Code import String.to_cstring `{