lib: use Sqlite3 return codes
authorAlexis Laferrière <alexis.laf@xymus.net>
Mon, 19 Aug 2013 22:35:33 +0000 (18:35 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 20 Aug 2013 19:12:03 +0000 (15:12 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/sqlite3/sqlite3.nit
tests/sav/test_sqlite3.sav
tests/test_sqlite3.nit

index d78e239..94d6e7c 100644 (file)
@@ -1,7 +1,7 @@
 # This file is part of NIT ( http://www.nitlanguage.org ).
 #
 # Copyright 2013 Guillaume Auger <jeho@resist.ca>
-#
+# Copyright 2013 Alexis Laferrière <alexis.laf@xymus.net>
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -26,6 +26,46 @@ in "C header" `{
 
 `}
 
+extern class Sqlite3Code `{int`}
+       new ok `{ return SQLITE_OK; `} #         0   /* Successful result */
+       fun is_ok: Bool `{ return recv == SQLITE_OK; `}
+
+       # new  `{ return SQLITE_ERROR; `} #      1   /* SQL error or missing database */
+       # new  `{ return SQLITE_INTERNAL; `} #    2   /* Internal logic error in SQLite */
+       # new  `{ return SQLITE_PERM; `} #        3   /* Access permission denied */
+       # new  `{ return SQLITE_ABORT; `} #       4   /* Callback routine requested an abort */
+       # new  `{ return SQLITE_BUSY; `} #        5   /* The database file is locked */
+       # new  `{ return SQLITE_LOCKED; `} #      6   /* A table in the database is locked */
+       # new  `{ return SQLITE_NOMEM; `} #       7   /* A malloc() failed */
+       # new  `{ return SQLITE_READONLY; `} #    8   /* Attempt to write a readonly database */
+       # new  `{ return SQLITE_INTERRUPT; `} #   9   /* Operation terminated by sqlite3_interrupt()*/
+       # new  `{ return SQLITE_IOERR; `} #      10   /* Some kind of disk I/O error occurred */
+       # new  `{ return SQLITE_CORRUPT; `} #    11   /* The database disk image is malformed */
+       # new  `{ return SQLITE_NOTFOUND; `} #   12   /* Unknown opcode in sqlite3_file_control() */
+       # new  `{ return SQLITE_FULL; `} #       13   /* Insertion failed because database is full */
+       # new  `{ return SQLITE_CANTOPEN; `} #   14   /* Unable to open the database file */
+       # new  `{ return SQLITE_PROTOCOL; `} #   15   /* Database lock protocol error */
+       # new  `{ return SQLITE_EMPTY; `} #      16   /* Database is empty */
+       # new  `{ return SQLITE_SCHEMA; `} #     17   /* The database schema changed */
+       # new  `{ return SQLITE_TOOBIG; `} #     18   /* String or BLOB exceeds size limit */
+       # new  `{ return SQLITE_CONSTRAINT; `} # 19   /* Abort due to constraint violation */
+       # new  `{ return SQLITE_MISMATCH; `} #   20   /* Data type mismatch */
+       # new  `{ return SQLITE_MISUSE; `} #     21   /* Library used incorrectly */
+       # new  `{ return SQLITE_NOLFS; `} #      22   /* Uses OS features not supported on host */
+       # new  `{ return SQLITE_AUTH; `} #       23   /* Authorization denied */
+       # new  `{ return SQLITE_FORMAT; `} #     24   /* Auxiliary database format error */
+       # new  `{ return SQLITE_RANGE; `} #      25   /* 2nd parameter to sqlite3_bind out of range */
+       # new  `{ return SQLITE_NOTADB; `} #     26   /* File opened that is not a database file */
+       # new  `{ return SQLITE_NOTICE; `} #     27   /* Notifications from sqlite3_log() */
+       # new  `{ return SQLITE_WARNING; `} #    28   /* Warnings from sqlite3_log() */
+
+       new row `{ return SQLITE_ROW; `} #        100  /* sqlite3_step() has another row ready */
+       fun is_row: Bool `{ return recv == SQLITE_ROW; `}
+
+       new done `{ return SQLITE_DONE; `} #       101  /* sqlite3_step() has finished executing */
+       fun is_done: Bool `{ return recv == SQLITE_DONE; `}
+end
+
 extern Sqlite3 `{struct Data*`}
        new `{
                struct Data* data = malloc(sizeof(data));
@@ -39,21 +79,21 @@ extern Sqlite3 `{struct Data*`}
        `}
 
        fun close `{
-               sqlite3_close(( recv)->ppDb);
+               sqlite3_close(recv->ppDb);
                free(recv);
        `}
 
-       fun exec(sql : String): Int import String::to_cstring `{
+       fun exec(sql : String): Sqlite3Code import String::to_cstring `{
                struct Data * data = recv;
                return sqlite3_exec(data->ppDb, String_to_cstring(sql), 0, 0, 0);
        `}
 
-       fun prepare(sql : String) import String::to_cstring `{
+       fun prepare(sql : String): Sqlite3Code import String::to_cstring `{
                struct Data * data = recv;
-               int ret = sqlite3_prepare_v2(data->ppDb, String_to_cstring(sql), -1, &(data->stmt), 0);
+               return sqlite3_prepare_v2(data->ppDb, String_to_cstring(sql), -1, &(data->stmt), 0);
        `}
 
-       fun step: Int `{
+       fun step: Sqlite3Code `{
                return sqlite3_step(recv->stmt);
        `}
 
index e69de29..b78ff8a 100644 (file)
@@ -0,0 +1,6 @@
+Bob
+zzz
+1
+Guillaume
+xxx
+1
index e4065f3..b012a6f 100644 (file)
@@ -43,15 +43,11 @@ assert sqlite_insert_2: db.get_error == 0
 db.prepare(select_req)
 assert sqlite_select: db.get_error == 0
 
-# Get first row
-db.step
-assert sqlite_column_0_0: db.column_text(0) == "Bob"
-assert sqlite_column_0_1: db.column_text(1) == "zzz"
-assert sqlite_column_0_2: db.column_text(2) == "1"
-db.step
-assert sqlite_column_1_0: db.column_text(0) == "Guillaume"
-assert sqlite_column_1_1: db.column_text(1) == "xxx"
-assert sqlite_column_1_2: db.column_text(2) == "1"
+while db.step.is_row do
+       print db.column_text(0)
+       print db.column_text(1)
+       print db.column_text(2)
+end
 
 db.close