[svn] commit: r1188 - in /trunk/src/lib/auth: data_source_sqlite3.cc data_source_sqlite3.h data_source_sqlite3_unittest.cc

BIND 10 source code commits bind10-changes at lists.isc.org
Sun Mar 7 23:03:57 UTC 2010


Author: jinmei
Date: Sun Mar  7 23:03:56 2010
New Revision: 1188

Log:
defined a new exception class, Sqlite3Error, to throw an exception with
sqlite3 related errors.
used the new class when Sqlite3DataSrc::open() failed.
added a test case for this.

Modified:
    trunk/src/lib/auth/data_source_sqlite3.cc
    trunk/src/lib/auth/data_source_sqlite3.h
    trunk/src/lib/auth/data_source_sqlite3_unittest.cc

Modified: trunk/src/lib/auth/data_source_sqlite3.cc
==============================================================================
--- trunk/src/lib/auth/data_source_sqlite3.cc (original)
+++ trunk/src/lib/auth/data_source_sqlite3.cc Sun Mar  7 23:03:56 2010
@@ -15,6 +15,7 @@
 // $Id$
 
 #include <string>
+#include <sstream>
 
 #include "data_source_sqlite3.h"
 
@@ -284,7 +285,7 @@
                             const char** position) const
 {
     const char* current = name;
-    
+
     while (*current != 0) {
         const int rc = hasExactZone(current);
         if (rc >= 0) {
@@ -683,15 +684,12 @@
 //
 void
 Sqlite3DataSrc::open(const string& name) {
-    database_name = name;
-    
-    if (sqlite3_open(database_name.c_str(), &db) != 0) {
-        cerr << "open database: " << sqlite3_errmsg(db) << "\n";
+    if (sqlite3_open(name.c_str(), &db) != 0) {
         // sqlite3_close() must be called even when open fails.
         sqlite3_close(db);
-        throw("Cannot open database");
-    }
-    
+        isc_throw(Sqlite3Error, "Cannot open Sqlite3 database file: " << name);
+    }
+
     checkAndSetupSchema();
 }
 

Modified: trunk/src/lib/auth/data_source_sqlite3.h
==============================================================================
--- trunk/src/lib/auth/data_source_sqlite3.h (original)
+++ trunk/src/lib/auth/data_source_sqlite3.h Sun Mar  7 23:03:56 2010
@@ -19,6 +19,8 @@
 
 #include <string>
 
+#include <exceptions/exceptions.h>
+
 #include <sqlite3.h>
 
 #include "data_source.h"
@@ -35,6 +37,12 @@
 namespace auth {
 
 class Query;
+
+class Sqlite3Error : public Exception {
+public:
+    Sqlite3Error(const char* file, size_t line, const char* what) :
+        isc::Exception(file, line, what) {}
+};
 
 class Sqlite3DataSrc : public DataSrc {
     ///
@@ -119,7 +127,6 @@
     void checkAndSetupSchema(void);
 
     sqlite3 *db;
-    std::string database_name;
     int database_version;
     
     //

Modified: trunk/src/lib/auth/data_source_sqlite3_unittest.cc
==============================================================================
--- trunk/src/lib/auth/data_source_sqlite3_unittest.cc (original)
+++ trunk/src/lib/auth/data_source_sqlite3_unittest.cc Sun Mar  7 23:03:56 2010
@@ -42,6 +42,11 @@
 namespace {
 static const char* SQLITE_DBFILE_EXAMPLE = "testdata/test.sqlite3";
 static const char* SQLITE_DBFILE_EXAMPLE2 = "testdata/test2.sqlite3";
+// The following file must be non existent and mutt be "creatable";
+// the sqlite3 library will try to create a new DB file if it doesn't exist,
+// so to test a failure case the create operation should also fail.
+// The "nodir", a non existent directory, is inserted for this purpose.
+static const char* SQLITE_DBFILE_NOTEXIST = "testdata/nodir/notexist";
 
 static const string sigdata_common(" 20100322084538 20100220084538 "
                                    "33495 example.com. FAKEFAKEFAKEFAKE");
@@ -353,6 +358,11 @@
     EXPECT_EQ(NULL, name_match.bestDataSrc());
 }
 
+TEST_F(Sqlite3DataSourceTest, openFail) {
+    EXPECT_EQ(DataSrc::SUCCESS, data_source.close());
+    EXPECT_THROW(data_source.init(SQLITE_DBFILE_NOTEXIST), Sqlite3Error);
+}
+
 TEST_F(Sqlite3DataSourceTest, findClosestEnclosure) {
     NameMatch name_match(www_name);
     data_source.findClosestEnclosure(name_match);




More information about the bind10-changes mailing list