BIND 10 trac1061, updated. 65e4595c21bf9c01fb0b7da61577ae8a79d29c30 [1061] Yet another renaming
BIND 10 source code commits
bind10-changes at lists.isc.org
Tue Aug 9 07:02:39 UTC 2011
The branch, trac1061 has been updated
via 65e4595c21bf9c01fb0b7da61577ae8a79d29c30 (commit)
from 97153d16eb9ecb7281ed9dc76783091964e769dd (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 65e4595c21bf9c01fb0b7da61577ae8a79d29c30
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date: Tue Aug 9 09:02:03 2011 +0200
[1061] Yet another renaming
This time to DatabaseAccessor, Abstraction might be misleading in other
ways.
-----------------------------------------------------------------------
Summary of changes:
src/lib/datasrc/Makefile.am | 2 +-
src/lib/datasrc/database.cc | 4 +-
src/lib/datasrc/database.h | 22 ++++++++++----------
.../{sqlite3_database.cc => sqlite3_accessor.cc} | 2 +-
.../{sqlite3_database.h => sqlite3_accessor.h} | 10 ++++----
src/lib/datasrc/tests/Makefile.am | 2 +-
src/lib/datasrc/tests/database_unittest.cc | 10 ++++----
...se_unittest.cc => sqlite3_accessor_unittest.cc} | 2 +-
8 files changed, 27 insertions(+), 27 deletions(-)
rename src/lib/datasrc/{sqlite3_database.cc => sqlite3_accessor.cc} (99%)
rename src/lib/datasrc/{sqlite3_database.h => sqlite3_accessor.h} (91%)
rename src/lib/datasrc/tests/{sqlite3_database_unittest.cc => sqlite3_accessor_unittest.cc} (98%)
-----------------------------------------------------------------------
diff --git a/src/lib/datasrc/Makefile.am b/src/lib/datasrc/Makefile.am
index 6792365..db67781 100644
--- a/src/lib/datasrc/Makefile.am
+++ b/src/lib/datasrc/Makefile.am
@@ -23,7 +23,7 @@ libdatasrc_la_SOURCES += result.h
libdatasrc_la_SOURCES += logger.h logger.cc
libdatasrc_la_SOURCES += client.h
libdatasrc_la_SOURCES += database.h database.cc
-libdatasrc_la_SOURCES += sqlite3_database.h sqlite3_database.cc
+libdatasrc_la_SOURCES += sqlite3_accessor.h sqlite3_accessor.cc
nodist_libdatasrc_la_SOURCES = datasrc_messages.h datasrc_messages.cc
libdatasrc_la_LIBADD = $(top_builddir)/src/lib/exceptions/libexceptions.la
diff --git a/src/lib/datasrc/database.cc b/src/lib/datasrc/database.cc
index 2d30ba2..0e1418d 100644
--- a/src/lib/datasrc/database.cc
+++ b/src/lib/datasrc/database.cc
@@ -22,7 +22,7 @@ using isc::dns::Name;
namespace isc {
namespace datasrc {
-DatabaseClient::DatabaseClient(boost::shared_ptr<DatabaseAbstraction>
+DatabaseClient::DatabaseClient(boost::shared_ptr<DatabaseAccessor>
database) :
database_(database)
{
@@ -55,7 +55,7 @@ DatabaseClient::findZone(const Name& name) const {
return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
}
-DatabaseClient::Finder::Finder(boost::shared_ptr<DatabaseAbstraction>
+DatabaseClient::Finder::Finder(boost::shared_ptr<DatabaseAccessor>
database, int zone_id) :
database_(database),
zone_id_(zone_id)
diff --git a/src/lib/datasrc/database.h b/src/lib/datasrc/database.h
index 7a6cd6b..1f6bd22 100644
--- a/src/lib/datasrc/database.h
+++ b/src/lib/datasrc/database.h
@@ -43,7 +43,7 @@ namespace datasrc {
* allows having multiple open queries at one connection, the connection
* class may share it.
*/
-class DatabaseAbstraction : boost::noncopyable {
+class DatabaseAccessor : boost::noncopyable {
public:
/**
* \brief Destructor
@@ -51,7 +51,7 @@ public:
* It is empty, but needs a virtual one, since we will use the derived
* classes in polymorphic way.
*/
- virtual ~DatabaseAbstraction() { }
+ virtual ~DatabaseAccessor() { }
/**
* \brief Retrieve a zone identifier
*
@@ -79,14 +79,14 @@ public:
*
* This class (together with corresponding versions of ZoneFinder,
* ZoneIterator, etc.) translates high-level data source queries to
- * low-level calls on DatabaseAbstraction. It calls multiple queries
+ * low-level calls on DatabaseAccessor. It calls multiple queries
* if necessary and validates data from the database, allowing the
- * DatabaseAbstraction to be just simple translation to SQL/other
+ * DatabaseAccessor to be just simple translation to SQL/other
* queries to database.
*
* While it is possible to subclass it for specific database in case
* of special needs, it is not expected to be needed. This should just
- * work as it is with whatever DatabaseAbstraction.
+ * work as it is with whatever DatabaseAccessor.
*/
class DatabaseClient : public DataSourceClient {
public:
@@ -102,7 +102,7 @@ public:
* suggests, the client takes ownership of the database and will
* delete it when itself deleted.
*/
- DatabaseClient(boost::shared_ptr<DatabaseAbstraction> database);
+ DatabaseClient(boost::shared_ptr<DatabaseAccessor> database);
/**
* \brief Corresponding ZoneFinder implementation
*
@@ -126,10 +126,10 @@ public:
* \param database The database (shared with DatabaseClient) to
* be used for queries (the one asked for ID before).
* \param zone_id The zone ID which was returned from
- * DatabaseAbstraction::getZone and which will be passed to further
+ * DatabaseAccessor::getZone and which will be passed to further
* calls to the database.
*/
- Finder(boost::shared_ptr<DatabaseAbstraction> database, int zone_id);
+ Finder(boost::shared_ptr<DatabaseAccessor> database, int zone_id);
// The following three methods are just implementations of inherited
// ZoneFinder's pure virtual methods.
virtual isc::dns::Name getOrigin() const;
@@ -154,11 +154,11 @@ public:
* passed to the constructor. This is meant for testing purposes and
* normal applications shouldn't need it.
*/
- const DatabaseAbstraction& database() const {
+ const DatabaseAccessor& database() const {
return (*database_);
}
private:
- boost::shared_ptr<DatabaseAbstraction> database_;
+ boost::shared_ptr<DatabaseAccessor> database_;
const int zone_id_;
};
/**
@@ -178,7 +178,7 @@ public:
virtual FindResult findZone(const isc::dns::Name& name) const;
private:
/// \brief Our database.
- const boost::shared_ptr<DatabaseAbstraction> database_;
+ const boost::shared_ptr<DatabaseAccessor> database_;
};
}
diff --git a/src/lib/datasrc/sqlite3_accessor.cc b/src/lib/datasrc/sqlite3_accessor.cc
new file mode 100644
index 0000000..352768d
--- /dev/null
+++ b/src/lib/datasrc/sqlite3_accessor.cc
@@ -0,0 +1,322 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <sqlite3.h>
+
+#include <datasrc/sqlite3_accessor.h>
+#include <datasrc/logger.h>
+#include <datasrc/data_source.h>
+
+namespace isc {
+namespace datasrc {
+
+struct SQLite3Parameters {
+ SQLite3Parameters() :
+ db_(NULL), version_(-1),
+ q_zone_(NULL) /*, q_record_(NULL), q_addrs_(NULL), q_referral_(NULL),
+ q_any_(NULL), q_count_(NULL), q_previous_(NULL), q_nsec3_(NULL),
+ q_prevnsec3_(NULL) */
+ {}
+ sqlite3* db_;
+ int version_;
+ sqlite3_stmt* q_zone_;
+ /*
+ TODO: Yet unneeded statements
+ sqlite3_stmt* q_record_;
+ sqlite3_stmt* q_addrs_;
+ sqlite3_stmt* q_referral_;
+ sqlite3_stmt* q_any_;
+ sqlite3_stmt* q_count_;
+ sqlite3_stmt* q_previous_;
+ sqlite3_stmt* q_nsec3_;
+ sqlite3_stmt* q_prevnsec3_;
+ */
+};
+
+SQLite3Database::SQLite3Database(const std::string& filename,
+ const isc::dns::RRClass& rrclass) :
+ dbparameters_(new SQLite3Parameters),
+ class_(rrclass.toText())
+{
+ LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_NEWCONN);
+
+ open(filename);
+}
+
+namespace {
+
+// This is a helper class to initialize a Sqlite3 DB safely. An object of
+// this class encapsulates all temporary resources that are necessary for
+// the initialization, and release them in the destructor. Once everything
+// is properly initialized, the move() method moves the allocated resources
+// to the main object in an exception free manner. This way, the main code
+// for the initialization can be exception safe, and can provide the strong
+// exception guarantee.
+class Initializer {
+public:
+ ~Initializer() {
+ if (params_.q_zone_ != NULL) {
+ sqlite3_finalize(params_.q_zone_);
+ }
+ /*
+ if (params_.q_record_ != NULL) {
+ sqlite3_finalize(params_.q_record_);
+ }
+ if (params_.q_addrs_ != NULL) {
+ sqlite3_finalize(params_.q_addrs_);
+ }
+ if (params_.q_referral_ != NULL) {
+ sqlite3_finalize(params_.q_referral_);
+ }
+ if (params_.q_any_ != NULL) {
+ sqlite3_finalize(params_.q_any_);
+ }
+ if (params_.q_count_ != NULL) {
+ sqlite3_finalize(params_.q_count_);
+ }
+ if (params_.q_previous_ != NULL) {
+ sqlite3_finalize(params_.q_previous_);
+ }
+ if (params_.q_nsec3_ != NULL) {
+ sqlite3_finalize(params_.q_nsec3_);
+ }
+ if (params_.q_prevnsec3_ != NULL) {
+ sqlite3_finalize(params_.q_prevnsec3_);
+ }
+ */
+ if (params_.db_ != NULL) {
+ sqlite3_close(params_.db_);
+ }
+ }
+ void move(SQLite3Parameters* dst) {
+ *dst = params_;
+ params_ = SQLite3Parameters(); // clear everything
+ }
+ SQLite3Parameters params_;
+};
+
+const char* const SCHEMA_LIST[] = {
+ "CREATE TABLE schema_version (version INTEGER NOT NULL)",
+ "INSERT INTO schema_version VALUES (1)",
+ "CREATE TABLE zones (id INTEGER PRIMARY KEY, "
+ "name STRING NOT NULL COLLATE NOCASE, "
+ "rdclass STRING NOT NULL COLLATE NOCASE DEFAULT 'IN', "
+ "dnssec BOOLEAN NOT NULL DEFAULT 0)",
+ "CREATE INDEX zones_byname ON zones (name)",
+ "CREATE TABLE records (id INTEGER PRIMARY KEY, "
+ "zone_id INTEGER NOT NULL, name STRING NOT NULL COLLATE NOCASE, "
+ "rname STRING NOT NULL COLLATE NOCASE, ttl INTEGER NOT NULL, "
+ "rdtype STRING NOT NULL COLLATE NOCASE, sigtype STRING COLLATE NOCASE, "
+ "rdata STRING NOT NULL)",
+ "CREATE INDEX records_byname ON records (name)",
+ "CREATE INDEX records_byrname ON records (rname)",
+ "CREATE TABLE nsec3 (id INTEGER PRIMARY KEY, zone_id INTEGER NOT NULL, "
+ "hash STRING NOT NULL COLLATE NOCASE, "
+ "owner STRING NOT NULL COLLATE NOCASE, "
+ "ttl INTEGER NOT NULL, rdtype STRING NOT NULL COLLATE NOCASE, "
+ "rdata STRING NOT NULL)",
+ "CREATE INDEX nsec3_byhash ON nsec3 (hash)",
+ NULL
+};
+
+const char* const q_zone_str = "SELECT id FROM zones WHERE name=?1 AND rdclass = ?2";
+
+/* TODO: Prune the statements, not everything will be needed maybe?
+const char* const q_record_str = "SELECT rdtype, ttl, sigtype, rdata "
+ "FROM records WHERE zone_id=?1 AND name=?2 AND "
+ "((rdtype=?3 OR sigtype=?3) OR "
+ "(rdtype='CNAME' OR sigtype='CNAME') OR "
+ "(rdtype='NS' OR sigtype='NS'))";
+
+const char* const q_addrs_str = "SELECT rdtype, ttl, sigtype, rdata "
+ "FROM records WHERE zone_id=?1 AND name=?2 AND "
+ "(rdtype='A' OR sigtype='A' OR rdtype='AAAA' OR sigtype='AAAA')";
+
+const char* const q_referral_str = "SELECT rdtype, ttl, sigtype, rdata FROM "
+ "records WHERE zone_id=?1 AND name=?2 AND"
+ "(rdtype='NS' OR sigtype='NS' OR rdtype='DS' OR sigtype='DS' OR "
+ "rdtype='DNAME' OR sigtype='DNAME')";
+
+const char* const q_any_str = "SELECT rdtype, ttl, sigtype, rdata "
+ "FROM records WHERE zone_id=?1 AND name=?2";
+
+const char* const q_count_str = "SELECT COUNT(*) FROM records "
+ "WHERE zone_id=?1 AND rname LIKE (?2 || '%');";
+
+const char* const q_previous_str = "SELECT name FROM records "
+ "WHERE zone_id=?1 AND rdtype = 'NSEC' AND "
+ "rname < $2 ORDER BY rname DESC LIMIT 1";
+
+const char* const q_nsec3_str = "SELECT rdtype, ttl, rdata FROM nsec3 "
+ "WHERE zone_id = ?1 AND hash = $2";
+
+const char* const q_prevnsec3_str = "SELECT hash FROM nsec3 "
+ "WHERE zone_id = ?1 AND hash <= $2 ORDER BY hash DESC LIMIT 1";
+ */
+
+sqlite3_stmt*
+prepare(sqlite3* const db, const char* const statement) {
+ sqlite3_stmt* prepared = NULL;
+ if (sqlite3_prepare_v2(db, statement, -1, &prepared, NULL) != SQLITE_OK) {
+ isc_throw(SQLite3Error, "Could not prepare SQLite statement: " <<
+ statement);
+ }
+ return (prepared);
+}
+
+void
+checkAndSetupSchema(Initializer* initializer) {
+ sqlite3* const db = initializer->params_.db_;
+
+ sqlite3_stmt* prepared = NULL;
+ if (sqlite3_prepare_v2(db, "SELECT version FROM schema_version", -1,
+ &prepared, NULL) == SQLITE_OK &&
+ sqlite3_step(prepared) == SQLITE_ROW) {
+ initializer->params_.version_ = sqlite3_column_int(prepared, 0);
+ sqlite3_finalize(prepared);
+ } else {
+ logger.info(DATASRC_SQLITE_SETUP);
+ if (prepared != NULL) {
+ sqlite3_finalize(prepared);
+ }
+ for (int i = 0; SCHEMA_LIST[i] != NULL; ++i) {
+ if (sqlite3_exec(db, SCHEMA_LIST[i], NULL, NULL, NULL) !=
+ SQLITE_OK) {
+ isc_throw(SQLite3Error,
+ "Failed to set up schema " << SCHEMA_LIST[i]);
+ }
+ }
+ }
+
+ initializer->params_.q_zone_ = prepare(db, q_zone_str);
+ /* TODO: Yet unneeded statements
+ initializer->params_.q_record_ = prepare(db, q_record_str);
+ initializer->params_.q_addrs_ = prepare(db, q_addrs_str);
+ initializer->params_.q_referral_ = prepare(db, q_referral_str);
+ initializer->params_.q_any_ = prepare(db, q_any_str);
+ initializer->params_.q_count_ = prepare(db, q_count_str);
+ initializer->params_.q_previous_ = prepare(db, q_previous_str);
+ initializer->params_.q_nsec3_ = prepare(db, q_nsec3_str);
+ initializer->params_.q_prevnsec3_ = prepare(db, q_prevnsec3_str);
+ */
+}
+
+}
+
+void
+SQLite3Database::open(const std::string& name) {
+ LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_CONNOPEN).arg(name);
+ if (dbparameters_->db_ != NULL) {
+ // There shouldn't be a way to trigger this anyway
+ isc_throw(DataSourceError, "Duplicate SQLite open with " << name);
+ }
+
+ Initializer initializer;
+
+ if (sqlite3_open(name.c_str(), &initializer.params_.db_) != 0) {
+ isc_throw(SQLite3Error, "Cannot open SQLite database file: " << name);
+ }
+
+ checkAndSetupSchema(&initializer);
+ initializer.move(dbparameters_);
+}
+
+SQLite3Database::~SQLite3Database() {
+ LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_DROPCONN);
+ if (dbparameters_->db_ != NULL) {
+ close();
+ }
+ delete dbparameters_;
+}
+
+void
+SQLite3Database::close(void) {
+ LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_CONNCLOSE);
+ if (dbparameters_->db_ == NULL) {
+ isc_throw(DataSourceError,
+ "SQLite data source is being closed before open");
+ }
+
+ // XXX: sqlite3_finalize() could fail. What should we do in that case?
+ sqlite3_finalize(dbparameters_->q_zone_);
+ dbparameters_->q_zone_ = NULL;
+
+ /* TODO: Once they are needed or not, uncomment or drop
+ sqlite3_finalize(dbparameters->q_record_);
+ dbparameters->q_record_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_addrs_);
+ dbparameters->q_addrs_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_referral_);
+ dbparameters->q_referral_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_any_);
+ dbparameters->q_any_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_count_);
+ dbparameters->q_count_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_previous_);
+ dbparameters->q_previous_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_prevnsec3_);
+ dbparameters->q_prevnsec3_ = NULL;
+
+ sqlite3_finalize(dbparameters->q_nsec3_);
+ dbparameters->q_nsec3_ = NULL;
+ */
+
+ sqlite3_close(dbparameters_->db_);
+ dbparameters_->db_ = NULL;
+}
+
+std::pair<bool, int>
+SQLite3Database::getZone(const isc::dns::Name& name) const {
+ int rc;
+
+ // Take the statement (simple SELECT id FROM zones WHERE...)
+ // and prepare it (bind the parameters to it)
+ sqlite3_reset(dbparameters_->q_zone_);
+ rc = sqlite3_bind_text(dbparameters_->q_zone_, 1, name.toText().c_str(),
+ -1, SQLITE_STATIC);
+ if (rc != SQLITE_OK) {
+ isc_throw(SQLite3Error, "Could not bind " << name <<
+ " to SQL statement (zone)");
+ }
+ rc = sqlite3_bind_text(dbparameters_->q_zone_, 2, class_.c_str(), -1,
+ SQLITE_STATIC);
+ if (rc != SQLITE_OK) {
+ isc_throw(SQLite3Error, "Could not bind " << class_ <<
+ " to SQL statement (zone)");
+ }
+
+ // Get the data there and see if it found anything
+ rc = sqlite3_step(dbparameters_->q_zone_);
+ std::pair<bool, int> result;
+ if (rc == SQLITE_ROW) {
+ result = std::pair<bool, int>(true,
+ sqlite3_column_int(dbparameters_->
+ q_zone_, 0));
+ } else {
+ result = std::pair<bool, int>(false, 0);
+ }
+ // Free resources
+ sqlite3_reset(dbparameters_->q_zone_);
+
+ return (result);
+}
+
+}
+}
diff --git a/src/lib/datasrc/sqlite3_accessor.h b/src/lib/datasrc/sqlite3_accessor.h
new file mode 100644
index 0000000..0d7ddee
--- /dev/null
+++ b/src/lib/datasrc/sqlite3_accessor.h
@@ -0,0 +1,105 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+
+#ifndef __DATASRC_SQLITE3_ACCESSOR_H
+#define __DATASRC_SQLITE3_ACCESSOR_H
+
+#include <datasrc/database.h>
+
+#include <exceptions/exceptions.h>
+
+#include <string>
+
+namespace isc {
+namespace dns {
+class RRClass;
+}
+
+namespace datasrc {
+
+/**
+ * \brief Low-level database error
+ *
+ * This exception is thrown when the SQLite library complains about something.
+ * It might mean corrupt database file, invalid request or that something is
+ * rotten in the library.
+ */
+class SQLite3Error : public Exception {
+public:
+ SQLite3Error(const char* file, size_t line, const char* what) :
+ isc::Exception(file, line, what) {}
+};
+
+struct SQLite3Parameters;
+
+/**
+ * \brief Concrete implementation of DatabaseAccessor for SQLite3 databases
+ *
+ * This opens one database file with our schema and serves data from there.
+ * According to the design, it doesn't interpret the data in any way, it just
+ * provides unified access to the DB.
+ */
+class SQLite3Database : public DatabaseAccessor {
+public:
+ /**
+ * \brief Constructor
+ *
+ * This opens the database and becomes ready to serve data from there.
+ *
+ * \exception SQLite3Error will be thrown if the given database file
+ * doesn't work (it is broken, doesn't exist and can't be created, etc).
+ *
+ * \param filename The database file to be used.
+ * \param rrclass Which class of data it should serve (while the database
+ * file can contain multiple classes of data, single database can
+ * provide only one class).
+ */
+ SQLite3Database(const std::string& filename,
+ const isc::dns::RRClass& rrclass);
+ /**
+ * \brief Destructor
+ *
+ * Closes the database.
+ */
+ ~SQLite3Database();
+ /**
+ * \brief Look up a zone
+ *
+ * This implements the getZone from DatabaseAccessor and looks up a zone
+ * in the data. It looks for a zone with the exact given origin and class
+ * passed to the constructor.
+ *
+ * \exception SQLite3Error if something about the database is broken.
+ *
+ * \param name The name of zone to look up
+ * \return The pair contains if the lookup was successful in the first
+ * element and the zone id in the second if it was.
+ */
+ virtual std::pair<bool, int> getZone(const isc::dns::Name& name) const;
+private:
+ /// \brief Private database data
+ SQLite3Parameters* dbparameters_;
+ /// \brief The class for which the queries are done
+ const std::string class_;
+ /// \brief Opens the database
+ void open(const std::string& filename);
+ /// \brief Closes the database
+ void close();
+};
+
+}
+}
+
+#endif
diff --git a/src/lib/datasrc/sqlite3_database.cc b/src/lib/datasrc/sqlite3_database.cc
deleted file mode 100644
index 2fdd240..0000000
--- a/src/lib/datasrc/sqlite3_database.cc
+++ /dev/null
@@ -1,322 +0,0 @@
-// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <sqlite3.h>
-
-#include <datasrc/sqlite3_database.h>
-#include <datasrc/logger.h>
-#include <datasrc/data_source.h>
-
-namespace isc {
-namespace datasrc {
-
-struct SQLite3Parameters {
- SQLite3Parameters() :
- db_(NULL), version_(-1),
- q_zone_(NULL) /*, q_record_(NULL), q_addrs_(NULL), q_referral_(NULL),
- q_any_(NULL), q_count_(NULL), q_previous_(NULL), q_nsec3_(NULL),
- q_prevnsec3_(NULL) */
- {}
- sqlite3* db_;
- int version_;
- sqlite3_stmt* q_zone_;
- /*
- TODO: Yet unneeded statements
- sqlite3_stmt* q_record_;
- sqlite3_stmt* q_addrs_;
- sqlite3_stmt* q_referral_;
- sqlite3_stmt* q_any_;
- sqlite3_stmt* q_count_;
- sqlite3_stmt* q_previous_;
- sqlite3_stmt* q_nsec3_;
- sqlite3_stmt* q_prevnsec3_;
- */
-};
-
-SQLite3Database::SQLite3Database(const std::string& filename,
- const isc::dns::RRClass& rrclass) :
- dbparameters_(new SQLite3Parameters),
- class_(rrclass.toText())
-{
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_NEWCONN);
-
- open(filename);
-}
-
-namespace {
-
-// This is a helper class to initialize a Sqlite3 DB safely. An object of
-// this class encapsulates all temporary resources that are necessary for
-// the initialization, and release them in the destructor. Once everything
-// is properly initialized, the move() method moves the allocated resources
-// to the main object in an exception free manner. This way, the main code
-// for the initialization can be exception safe, and can provide the strong
-// exception guarantee.
-class Initializer {
-public:
- ~Initializer() {
- if (params_.q_zone_ != NULL) {
- sqlite3_finalize(params_.q_zone_);
- }
- /*
- if (params_.q_record_ != NULL) {
- sqlite3_finalize(params_.q_record_);
- }
- if (params_.q_addrs_ != NULL) {
- sqlite3_finalize(params_.q_addrs_);
- }
- if (params_.q_referral_ != NULL) {
- sqlite3_finalize(params_.q_referral_);
- }
- if (params_.q_any_ != NULL) {
- sqlite3_finalize(params_.q_any_);
- }
- if (params_.q_count_ != NULL) {
- sqlite3_finalize(params_.q_count_);
- }
- if (params_.q_previous_ != NULL) {
- sqlite3_finalize(params_.q_previous_);
- }
- if (params_.q_nsec3_ != NULL) {
- sqlite3_finalize(params_.q_nsec3_);
- }
- if (params_.q_prevnsec3_ != NULL) {
- sqlite3_finalize(params_.q_prevnsec3_);
- }
- */
- if (params_.db_ != NULL) {
- sqlite3_close(params_.db_);
- }
- }
- void move(SQLite3Parameters* dst) {
- *dst = params_;
- params_ = SQLite3Parameters(); // clear everything
- }
- SQLite3Parameters params_;
-};
-
-const char* const SCHEMA_LIST[] = {
- "CREATE TABLE schema_version (version INTEGER NOT NULL)",
- "INSERT INTO schema_version VALUES (1)",
- "CREATE TABLE zones (id INTEGER PRIMARY KEY, "
- "name STRING NOT NULL COLLATE NOCASE, "
- "rdclass STRING NOT NULL COLLATE NOCASE DEFAULT 'IN', "
- "dnssec BOOLEAN NOT NULL DEFAULT 0)",
- "CREATE INDEX zones_byname ON zones (name)",
- "CREATE TABLE records (id INTEGER PRIMARY KEY, "
- "zone_id INTEGER NOT NULL, name STRING NOT NULL COLLATE NOCASE, "
- "rname STRING NOT NULL COLLATE NOCASE, ttl INTEGER NOT NULL, "
- "rdtype STRING NOT NULL COLLATE NOCASE, sigtype STRING COLLATE NOCASE, "
- "rdata STRING NOT NULL)",
- "CREATE INDEX records_byname ON records (name)",
- "CREATE INDEX records_byrname ON records (rname)",
- "CREATE TABLE nsec3 (id INTEGER PRIMARY KEY, zone_id INTEGER NOT NULL, "
- "hash STRING NOT NULL COLLATE NOCASE, "
- "owner STRING NOT NULL COLLATE NOCASE, "
- "ttl INTEGER NOT NULL, rdtype STRING NOT NULL COLLATE NOCASE, "
- "rdata STRING NOT NULL)",
- "CREATE INDEX nsec3_byhash ON nsec3 (hash)",
- NULL
-};
-
-const char* const q_zone_str = "SELECT id FROM zones WHERE name=?1 AND rdclass = ?2";
-
-/* TODO: Prune the statements, not everything will be needed maybe?
-const char* const q_record_str = "SELECT rdtype, ttl, sigtype, rdata "
- "FROM records WHERE zone_id=?1 AND name=?2 AND "
- "((rdtype=?3 OR sigtype=?3) OR "
- "(rdtype='CNAME' OR sigtype='CNAME') OR "
- "(rdtype='NS' OR sigtype='NS'))";
-
-const char* const q_addrs_str = "SELECT rdtype, ttl, sigtype, rdata "
- "FROM records WHERE zone_id=?1 AND name=?2 AND "
- "(rdtype='A' OR sigtype='A' OR rdtype='AAAA' OR sigtype='AAAA')";
-
-const char* const q_referral_str = "SELECT rdtype, ttl, sigtype, rdata FROM "
- "records WHERE zone_id=?1 AND name=?2 AND"
- "(rdtype='NS' OR sigtype='NS' OR rdtype='DS' OR sigtype='DS' OR "
- "rdtype='DNAME' OR sigtype='DNAME')";
-
-const char* const q_any_str = "SELECT rdtype, ttl, sigtype, rdata "
- "FROM records WHERE zone_id=?1 AND name=?2";
-
-const char* const q_count_str = "SELECT COUNT(*) FROM records "
- "WHERE zone_id=?1 AND rname LIKE (?2 || '%');";
-
-const char* const q_previous_str = "SELECT name FROM records "
- "WHERE zone_id=?1 AND rdtype = 'NSEC' AND "
- "rname < $2 ORDER BY rname DESC LIMIT 1";
-
-const char* const q_nsec3_str = "SELECT rdtype, ttl, rdata FROM nsec3 "
- "WHERE zone_id = ?1 AND hash = $2";
-
-const char* const q_prevnsec3_str = "SELECT hash FROM nsec3 "
- "WHERE zone_id = ?1 AND hash <= $2 ORDER BY hash DESC LIMIT 1";
- */
-
-sqlite3_stmt*
-prepare(sqlite3* const db, const char* const statement) {
- sqlite3_stmt* prepared = NULL;
- if (sqlite3_prepare_v2(db, statement, -1, &prepared, NULL) != SQLITE_OK) {
- isc_throw(SQLite3Error, "Could not prepare SQLite statement: " <<
- statement);
- }
- return (prepared);
-}
-
-void
-checkAndSetupSchema(Initializer* initializer) {
- sqlite3* const db = initializer->params_.db_;
-
- sqlite3_stmt* prepared = NULL;
- if (sqlite3_prepare_v2(db, "SELECT version FROM schema_version", -1,
- &prepared, NULL) == SQLITE_OK &&
- sqlite3_step(prepared) == SQLITE_ROW) {
- initializer->params_.version_ = sqlite3_column_int(prepared, 0);
- sqlite3_finalize(prepared);
- } else {
- logger.info(DATASRC_SQLITE_SETUP);
- if (prepared != NULL) {
- sqlite3_finalize(prepared);
- }
- for (int i = 0; SCHEMA_LIST[i] != NULL; ++i) {
- if (sqlite3_exec(db, SCHEMA_LIST[i], NULL, NULL, NULL) !=
- SQLITE_OK) {
- isc_throw(SQLite3Error,
- "Failed to set up schema " << SCHEMA_LIST[i]);
- }
- }
- }
-
- initializer->params_.q_zone_ = prepare(db, q_zone_str);
- /* TODO: Yet unneeded statements
- initializer->params_.q_record_ = prepare(db, q_record_str);
- initializer->params_.q_addrs_ = prepare(db, q_addrs_str);
- initializer->params_.q_referral_ = prepare(db, q_referral_str);
- initializer->params_.q_any_ = prepare(db, q_any_str);
- initializer->params_.q_count_ = prepare(db, q_count_str);
- initializer->params_.q_previous_ = prepare(db, q_previous_str);
- initializer->params_.q_nsec3_ = prepare(db, q_nsec3_str);
- initializer->params_.q_prevnsec3_ = prepare(db, q_prevnsec3_str);
- */
-}
-
-}
-
-void
-SQLite3Database::open(const std::string& name) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_CONNOPEN).arg(name);
- if (dbparameters_->db_ != NULL) {
- // There shouldn't be a way to trigger this anyway
- isc_throw(DataSourceError, "Duplicate SQLite open with " << name);
- }
-
- Initializer initializer;
-
- if (sqlite3_open(name.c_str(), &initializer.params_.db_) != 0) {
- isc_throw(SQLite3Error, "Cannot open SQLite database file: " << name);
- }
-
- checkAndSetupSchema(&initializer);
- initializer.move(dbparameters_);
-}
-
-SQLite3Database::~SQLite3Database() {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_DROPCONN);
- if (dbparameters_->db_ != NULL) {
- close();
- }
- delete dbparameters_;
-}
-
-void
-SQLite3Database::close(void) {
- LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_CONNCLOSE);
- if (dbparameters_->db_ == NULL) {
- isc_throw(DataSourceError,
- "SQLite data source is being closed before open");
- }
-
- // XXX: sqlite3_finalize() could fail. What should we do in that case?
- sqlite3_finalize(dbparameters_->q_zone_);
- dbparameters_->q_zone_ = NULL;
-
- /* TODO: Once they are needed or not, uncomment or drop
- sqlite3_finalize(dbparameters->q_record_);
- dbparameters->q_record_ = NULL;
-
- sqlite3_finalize(dbparameters->q_addrs_);
- dbparameters->q_addrs_ = NULL;
-
- sqlite3_finalize(dbparameters->q_referral_);
- dbparameters->q_referral_ = NULL;
-
- sqlite3_finalize(dbparameters->q_any_);
- dbparameters->q_any_ = NULL;
-
- sqlite3_finalize(dbparameters->q_count_);
- dbparameters->q_count_ = NULL;
-
- sqlite3_finalize(dbparameters->q_previous_);
- dbparameters->q_previous_ = NULL;
-
- sqlite3_finalize(dbparameters->q_prevnsec3_);
- dbparameters->q_prevnsec3_ = NULL;
-
- sqlite3_finalize(dbparameters->q_nsec3_);
- dbparameters->q_nsec3_ = NULL;
- */
-
- sqlite3_close(dbparameters_->db_);
- dbparameters_->db_ = NULL;
-}
-
-std::pair<bool, int>
-SQLite3Database::getZone(const isc::dns::Name& name) const {
- int rc;
-
- // Take the statement (simple SELECT id FROM zones WHERE...)
- // and prepare it (bind the parameters to it)
- sqlite3_reset(dbparameters_->q_zone_);
- rc = sqlite3_bind_text(dbparameters_->q_zone_, 1, name.toText().c_str(),
- -1, SQLITE_STATIC);
- if (rc != SQLITE_OK) {
- isc_throw(SQLite3Error, "Could not bind " << name <<
- " to SQL statement (zone)");
- }
- rc = sqlite3_bind_text(dbparameters_->q_zone_, 2, class_.c_str(), -1,
- SQLITE_STATIC);
- if (rc != SQLITE_OK) {
- isc_throw(SQLite3Error, "Could not bind " << class_ <<
- " to SQL statement (zone)");
- }
-
- // Get the data there and see if it found anything
- rc = sqlite3_step(dbparameters_->q_zone_);
- std::pair<bool, int> result;
- if (rc == SQLITE_ROW) {
- result = std::pair<bool, int>(true,
- sqlite3_column_int(dbparameters_->
- q_zone_, 0));
- } else {
- result = std::pair<bool, int>(false, 0);
- }
- // Free resources
- sqlite3_reset(dbparameters_->q_zone_);
-
- return (result);
-}
-
-}
-}
diff --git a/src/lib/datasrc/sqlite3_database.h b/src/lib/datasrc/sqlite3_database.h
deleted file mode 100644
index 9607e69..0000000
--- a/src/lib/datasrc/sqlite3_database.h
+++ /dev/null
@@ -1,105 +0,0 @@
-// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-
-#ifndef __DATASRC_SQLITE3_CONNECTION_H
-#define __DATASRC_SQLITE3_CONNECTION_H
-
-#include <datasrc/database.h>
-
-#include <exceptions/exceptions.h>
-
-#include <string>
-
-namespace isc {
-namespace dns {
-class RRClass;
-}
-
-namespace datasrc {
-
-/**
- * \brief Low-level database error
- *
- * This exception is thrown when the SQLite library complains about something.
- * It might mean corrupt database file, invalid request or that something is
- * rotten in the library.
- */
-class SQLite3Error : public Exception {
-public:
- SQLite3Error(const char* file, size_t line, const char* what) :
- isc::Exception(file, line, what) {}
-};
-
-struct SQLite3Parameters;
-
-/**
- * \brief Concrete implementation of DatabaseAbstraction for SQLite3 databases
- *
- * This opens one database file with our schema and serves data from there.
- * According to the design, it doesn't interpret the data in any way, it just
- * provides unified access to the DB.
- */
-class SQLite3Database : public DatabaseAbstraction {
-public:
- /**
- * \brief Constructor
- *
- * This opens the database and becomes ready to serve data from there.
- *
- * \exception SQLite3Error will be thrown if the given database file
- * doesn't work (it is broken, doesn't exist and can't be created, etc).
- *
- * \param filename The database file to be used.
- * \param rrclass Which class of data it should serve (while the database
- * file can contain multiple classes of data, single database can
- * provide only one class).
- */
- SQLite3Database(const std::string& filename,
- const isc::dns::RRClass& rrclass);
- /**
- * \brief Destructor
- *
- * Closes the database.
- */
- ~SQLite3Database();
- /**
- * \brief Look up a zone
- *
- * This implements the getZone from DatabaseAbstraction and looks up a zone
- * in the data. It looks for a zone with the exact given origin and class
- * passed to the constructor.
- *
- * \exception SQLite3Error if something about the database is broken.
- *
- * \param name The name of zone to look up
- * \return The pair contains if the lookup was successful in the first
- * element and the zone id in the second if it was.
- */
- virtual std::pair<bool, int> getZone(const isc::dns::Name& name) const;
-private:
- /// \brief Private database data
- SQLite3Parameters* dbparameters_;
- /// \brief The class for which the queries are done
- const std::string class_;
- /// \brief Opens the database
- void open(const std::string& filename);
- /// \brief Closes the database
- void close();
-};
-
-}
-}
-
-#endif
diff --git a/src/lib/datasrc/tests/Makefile.am b/src/lib/datasrc/tests/Makefile.am
index 3667306..4a7f322 100644
--- a/src/lib/datasrc/tests/Makefile.am
+++ b/src/lib/datasrc/tests/Makefile.am
@@ -29,7 +29,7 @@ run_unittests_SOURCES += zonetable_unittest.cc
run_unittests_SOURCES += memory_datasrc_unittest.cc
run_unittests_SOURCES += logger_unittest.cc
run_unittests_SOURCES += database_unittest.cc
-run_unittests_SOURCES += sqlite3_database_unittest.cc
+run_unittests_SOURCES += sqlite3_accessor_unittest.cc
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
diff --git a/src/lib/datasrc/tests/database_unittest.cc b/src/lib/datasrc/tests/database_unittest.cc
index 7de3e80..ab4423e 100644
--- a/src/lib/datasrc/tests/database_unittest.cc
+++ b/src/lib/datasrc/tests/database_unittest.cc
@@ -30,7 +30,7 @@ namespace {
* A virtual database connection that pretends it contains single zone --
* example.org.
*/
-class MockAbstraction : public DatabaseAbstraction {
+class MockAccessor : public DatabaseAccessor {
public:
virtual std::pair<bool, int> getZone(const Name& name) const {
if (name == Name("example.org")) {
@@ -51,12 +51,12 @@ public:
* times per test.
*/
void createClient() {
- current_database_ = new MockAbstraction();
- client_.reset(new DatabaseClient(shared_ptr<DatabaseAbstraction>(
+ current_database_ = new MockAccessor();
+ client_.reset(new DatabaseClient(shared_ptr<DatabaseAccessor>(
current_database_)));
}
// Will be deleted by client_, just keep the current value for comparison.
- MockAbstraction* current_database_;
+ MockAccessor* current_database_;
shared_ptr<DatabaseClient> client_;
/**
* Check the zone finder is a valid one and references the zone ID and
@@ -92,7 +92,7 @@ TEST_F(DatabaseClientTest, superZone) {
}
TEST_F(DatabaseClientTest, noConnException) {
- EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseAbstraction>()),
+ EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseAccessor>()),
isc::InvalidParameter);
}
diff --git a/src/lib/datasrc/tests/sqlite3_accessor_unittest.cc b/src/lib/datasrc/tests/sqlite3_accessor_unittest.cc
new file mode 100644
index 0000000..101c02b
--- /dev/null
+++ b/src/lib/datasrc/tests/sqlite3_accessor_unittest.cc
@@ -0,0 +1,103 @@
+// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <datasrc/sqlite3_accessor.h>
+#include <datasrc/data_source.h>
+
+#include <dns/rrclass.h>
+
+#include <gtest/gtest.h>
+
+using namespace isc::datasrc;
+using isc::data::ConstElementPtr;
+using isc::data::Element;
+using isc::dns::RRClass;
+using isc::dns::Name;
+
+namespace {
+// Some test data
+std::string SQLITE_DBFILE_EXAMPLE = TEST_DATA_DIR "/test.sqlite3";
+std::string SQLITE_DBFILE_EXAMPLE2 = TEST_DATA_DIR "/example2.com.sqlite3";
+std::string SQLITE_DBFILE_EXAMPLE_ROOT = TEST_DATA_DIR "/test-root.sqlite3";
+std::string SQLITE_DBFILE_BROKENDB = TEST_DATA_DIR "/brokendb.sqlite3";
+std::string SQLITE_DBFILE_MEMORY = ":memory:";
+
+// The following file must be non existent and must be non"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.
+std::string SQLITE_DBFILE_NOTEXIST = TEST_DATA_DIR "/nodir/notexist";
+
+// Opening works (the content is tested in different tests)
+TEST(SQLite3Open, common) {
+ EXPECT_NO_THROW(SQLite3Database db(SQLITE_DBFILE_EXAMPLE,
+ RRClass::IN()));
+}
+
+// The file can't be opened
+TEST(SQLite3Open, notExist) {
+ EXPECT_THROW(SQLite3Database db(SQLITE_DBFILE_NOTEXIST,
+ RRClass::IN()), SQLite3Error);
+}
+
+// It rejects broken DB
+TEST(SQLite3Open, brokenDB) {
+ EXPECT_THROW(SQLite3Database db(SQLITE_DBFILE_BROKENDB,
+ RRClass::IN()), SQLite3Error);
+}
+
+// Test we can create the schema on the fly
+TEST(SQLite3Open, memoryDB) {
+ EXPECT_NO_THROW(SQLite3Database db(SQLITE_DBFILE_MEMORY,
+ RRClass::IN()));
+}
+
+// Test fixture for querying the db
+class SQLite3Conn : public ::testing::Test {
+public:
+ SQLite3Conn() {
+ initConn(SQLITE_DBFILE_EXAMPLE, RRClass::IN());
+ }
+ // So it can be re-created with different data
+ void initConn(const std::string& filename, const RRClass& rrclass) {
+ db.reset(new SQLite3Database(filename, rrclass));
+ }
+ // The tested db
+ std::auto_ptr<SQLite3Database> db;
+};
+
+// This zone exists in the data, so it should be found
+TEST_F(SQLite3Conn, getZone) {
+ std::pair<bool, int> result(db->getZone(Name("example.com")));
+ EXPECT_TRUE(result.first);
+ EXPECT_EQ(1, result.second);
+}
+
+// But it should find only the zone, nothing below it
+TEST_F(SQLite3Conn, subZone) {
+ EXPECT_FALSE(db->getZone(Name("sub.example.com")).first);
+}
+
+// This zone is not there at all
+TEST_F(SQLite3Conn, noZone) {
+ EXPECT_FALSE(db->getZone(Name("example.org")).first);
+}
+
+// This zone is there, but in different class
+TEST_F(SQLite3Conn, noClass) {
+ initConn(SQLITE_DBFILE_EXAMPLE, RRClass::CH());
+ EXPECT_FALSE(db->getZone(Name("example.com")).first);
+}
+
+}
diff --git a/src/lib/datasrc/tests/sqlite3_database_unittest.cc b/src/lib/datasrc/tests/sqlite3_database_unittest.cc
deleted file mode 100644
index cf82d19..0000000
--- a/src/lib/datasrc/tests/sqlite3_database_unittest.cc
+++ /dev/null
@@ -1,103 +0,0 @@
-// Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
-//
-// Permission to use, copy, modify, and/or distribute this software for any
-// purpose with or without fee is hereby granted, provided that the above
-// copyright notice and this permission notice appear in all copies.
-//
-// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
-// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
-// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
-// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
-// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
-// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
-// PERFORMANCE OF THIS SOFTWARE.
-
-#include <datasrc/sqlite3_database.h>
-#include <datasrc/data_source.h>
-
-#include <dns/rrclass.h>
-
-#include <gtest/gtest.h>
-
-using namespace isc::datasrc;
-using isc::data::ConstElementPtr;
-using isc::data::Element;
-using isc::dns::RRClass;
-using isc::dns::Name;
-
-namespace {
-// Some test data
-std::string SQLITE_DBFILE_EXAMPLE = TEST_DATA_DIR "/test.sqlite3";
-std::string SQLITE_DBFILE_EXAMPLE2 = TEST_DATA_DIR "/example2.com.sqlite3";
-std::string SQLITE_DBFILE_EXAMPLE_ROOT = TEST_DATA_DIR "/test-root.sqlite3";
-std::string SQLITE_DBFILE_BROKENDB = TEST_DATA_DIR "/brokendb.sqlite3";
-std::string SQLITE_DBFILE_MEMORY = ":memory:";
-
-// The following file must be non existent and must be non"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.
-std::string SQLITE_DBFILE_NOTEXIST = TEST_DATA_DIR "/nodir/notexist";
-
-// Opening works (the content is tested in different tests)
-TEST(SQLite3Open, common) {
- EXPECT_NO_THROW(SQLite3Database db(SQLITE_DBFILE_EXAMPLE,
- RRClass::IN()));
-}
-
-// The file can't be opened
-TEST(SQLite3Open, notExist) {
- EXPECT_THROW(SQLite3Database db(SQLITE_DBFILE_NOTEXIST,
- RRClass::IN()), SQLite3Error);
-}
-
-// It rejects broken DB
-TEST(SQLite3Open, brokenDB) {
- EXPECT_THROW(SQLite3Database db(SQLITE_DBFILE_BROKENDB,
- RRClass::IN()), SQLite3Error);
-}
-
-// Test we can create the schema on the fly
-TEST(SQLite3Open, memoryDB) {
- EXPECT_NO_THROW(SQLite3Database db(SQLITE_DBFILE_MEMORY,
- RRClass::IN()));
-}
-
-// Test fixture for querying the db
-class SQLite3Conn : public ::testing::Test {
-public:
- SQLite3Conn() {
- initConn(SQLITE_DBFILE_EXAMPLE, RRClass::IN());
- }
- // So it can be re-created with different data
- void initConn(const std::string& filename, const RRClass& rrclass) {
- db.reset(new SQLite3Database(filename, rrclass));
- }
- // The tested db
- std::auto_ptr<SQLite3Database> db;
-};
-
-// This zone exists in the data, so it should be found
-TEST_F(SQLite3Conn, getZone) {
- std::pair<bool, int> result(db->getZone(Name("example.com")));
- EXPECT_TRUE(result.first);
- EXPECT_EQ(1, result.second);
-}
-
-// But it should find only the zone, nothing below it
-TEST_F(SQLite3Conn, subZone) {
- EXPECT_FALSE(db->getZone(Name("sub.example.com")).first);
-}
-
-// This zone is not there at all
-TEST_F(SQLite3Conn, noZone) {
- EXPECT_FALSE(db->getZone(Name("example.org")).first);
-}
-
-// This zone is there, but in different class
-TEST_F(SQLite3Conn, noClass) {
- initConn(SQLITE_DBFILE_EXAMPLE, RRClass::CH());
- EXPECT_FALSE(db->getZone(Name("example.com")).first);
-}
-
-}
More information about the bind10-changes
mailing list