Commit 56083614 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[1061] Rename DatabaseConnection to DatabaseAbstraction

As database connection might be confusing, use something better. Also
renamed SQLiteConnection to SQLiteDatabase
parent 885d7987
......@@ -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_connection.h sqlite3_connection.cc
libdatasrc_la_SOURCES += sqlite3_database.h sqlite3_database.cc
nodist_libdatasrc_la_SOURCES = datasrc_messages.h datasrc_messages.cc
libdatasrc_la_LIBADD = $(top_builddir)/src/lib/exceptions/libexceptions.la
......
......@@ -22,32 +22,32 @@ using isc::dns::Name;
namespace isc {
namespace datasrc {
DatabaseClient::DatabaseClient(boost::shared_ptr<DatabaseConnection>
connection) :
connection_(connection)
DatabaseClient::DatabaseClient(boost::shared_ptr<DatabaseAbstraction>
database) :
database_(database)
{
if (connection_.get() == NULL) {
if (database_.get() == NULL) {
isc_throw(isc::InvalidParameter,
"No connection provided to DatabaseClient");
"No database provided to DatabaseClient");
}
}
DataSourceClient::FindResult
DatabaseClient::findZone(const Name& name) const {
std::pair<bool, int> zone(connection_->getZone(name));
std::pair<bool, int> zone(database_->getZone(name));
// Try exact first
if (zone.first) {
return (FindResult(result::SUCCESS,
ZoneFinderPtr(new Finder(connection_,
ZoneFinderPtr(new Finder(database_,
zone.second))));
}
// Than super domains
// Start from 1, as 0 is covered above
for (size_t i(1); i < name.getLabelCount(); ++i) {
zone = connection_->getZone(name.split(i));
zone = database_->getZone(name.split(i));
if (zone.first) {
return (FindResult(result::PARTIALMATCH,
ZoneFinderPtr(new Finder(connection_,
ZoneFinderPtr(new Finder(database_,
zone.second))));
}
}
......@@ -55,9 +55,9 @@ DatabaseClient::findZone(const Name& name) const {
return (FindResult(result::NOTFOUND, ZoneFinderPtr()));
}
DatabaseClient::Finder::Finder(boost::shared_ptr<DatabaseConnection>
connection, int zone_id) :
connection_(connection),
DatabaseClient::Finder::Finder(boost::shared_ptr<DatabaseAbstraction>
database, int zone_id) :
database_(database),
zone_id_(zone_id)
{ }
......
......@@ -21,7 +21,7 @@ namespace isc {
namespace datasrc {
/**
* \brief Abstract connection to database with DNS data
* \brief Abstraction of lowlevel database with DNS data
*
* This class is defines interface to databases. Each supported database
* will provide methods for accessing the data stored there in a generic
......@@ -39,10 +39,11 @@ namespace datasrc {
* be better for that than copy constructor.
*
* \note The same application may create multiple connections to the same
* database. If the database allows having multiple open queries at one
* connection, the connection class may share it.
* database, having multiple instances of this class. If the database
* allows having multiple open queries at one connection, the connection
* class may share it.
*/
class DatabaseConnection : boost::noncopyable {
class DatabaseAbstraction : boost::noncopyable {
public:
/**
* \brief Destructor
......@@ -50,7 +51,7 @@ public:
* It is empty, but needs a virtual one, since we will use the derived
* classes in polymorphic way.
*/
virtual ~DatabaseConnection() { }
virtual ~DatabaseAbstraction() { }
/**
* \brief Retrieve a zone identifier
*
......@@ -67,7 +68,7 @@ public:
* was found. In case it was, the second part is internal zone ID.
* This one will be passed to methods finding data in the zone.
* It is not required to keep them, in which case whatever might
* be returned - the ID is only passed back to the connection as
* be returned - the ID is only passed back to the database as
* an opaque handle.
*/
virtual std::pair<bool, int> getZone(const isc::dns::Name& name) const = 0;
......@@ -78,36 +79,36 @@ public:
*
* This class (together with corresponding versions of ZoneFinder,
* ZoneIterator, etc.) translates high-level data source queries to
* low-level calls on DatabaseConnection. It calls multiple queries
* low-level calls on DatabaseAbstraction. It calls multiple queries
* if necessary and validates data from the database, allowing the
* DatabaseConnection to be just simple translation to SQL/other
* DatabaseAbstraction 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 DatabaseConnection.
* work as it is with whatever DatabaseAbstraction.
*/
class DatabaseClient : public DataSourceClient {
public:
/**
* \brief Constructor
*
* It initializes the client with a connection.
* It initializes the client with a database.
*
* \exception isc::InvalidParameter if connection is NULL. It might throw
* \exception isc::InvalidParameter if database is NULL. It might throw
* standard allocation exception as well, but doesn't throw anything else.
*
* \param connection The connection to use to get data. As the parameter
* suggests, the client takes ownership of the connection and will
* \param database The database to use to get data. As the parameter
* suggests, the client takes ownership of the database and will
* delete it when itself deleted.
*/
DatabaseClient(boost::shared_ptr<DatabaseConnection> connection);
DatabaseClient(boost::shared_ptr<DatabaseAbstraction> database);
/**
* \brief Corresponding ZoneFinder implementation
*
* The zone finder implementation for database data sources. Similarly
* to the DatabaseClient, it translates the queries to methods of the
* connection.
* database.
*
* Application should not come directly in contact with this class
* (it should handle it trough generic ZoneFinder pointer), therefore
......@@ -122,13 +123,13 @@ public:
/**
* \brief Constructor
*
* \param connection The connection (shared with DatabaseClient) to
* \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
* DatabaseConnection::getZone and which will be passed to further
* calls to the connection.
* DatabaseAbstraction::getZone and which will be passed to further
* calls to the database.
*/
Finder(boost::shared_ptr<DatabaseConnection> connection, int zone_id);
Finder(boost::shared_ptr<DatabaseAbstraction> database, int zone_id);
virtual isc::dns::Name getOrigin() const;
virtual isc::dns::RRClass getClass() const;
virtual FindResult find(const isc::dns::Name& name,
......@@ -145,23 +146,23 @@ public:
*/
int zone_id() const { return (zone_id_); }
/**
* \brief The database connection.
* \brief The database.
*
* This function provides the database connection stored inside as
* This function provides the database stored inside as
* passed to the constructor. This is meant for testing purposes and
* normal applications shouldn't need it.
*/
const DatabaseConnection& connection() const {
return (*connection_);
const DatabaseAbstraction& database() const {
return (*database_);
}
private:
boost::shared_ptr<DatabaseConnection> connection_;
boost::shared_ptr<DatabaseAbstraction> database_;
const int zone_id_;
};
/**
* \brief Find a zone in the database
*
* This queries connection's getZone to find the best matching zone.
* This queries database's getZone to find the best matching zone.
* It will propagate whatever exceptions are thrown from that method
* (which is not restricted in any way).
*
......@@ -172,8 +173,8 @@ public:
*/
virtual FindResult findZone(const isc::dns::Name& name) const;
private:
/// \brief Our connection.
const boost::shared_ptr<DatabaseConnection> connection_;
/// \brief Our database.
const boost::shared_ptr<DatabaseAbstraction> database_;
};
}
......
......@@ -14,7 +14,7 @@
#include <sqlite3.h>
#include <datasrc/sqlite3_connection.h>
#include <datasrc/sqlite3_database.h>
#include <datasrc/logger.h>
#include <datasrc/data_source.h>
......@@ -44,7 +44,7 @@ struct SQLite3Parameters {
*/
};
SQLite3Connection::SQLite3Connection(const std::string& filename,
SQLite3Database::SQLite3Database(const std::string& filename,
const isc::dns::RRClass& rrclass) :
dbparameters_(new SQLite3Parameters),
class_(rrclass.toText())
......@@ -215,7 +215,7 @@ checkAndSetupSchema(Initializer* initializer) {
}
void
SQLite3Connection::open(const std::string& name) {
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
......@@ -232,7 +232,7 @@ SQLite3Connection::open(const std::string& name) {
initializer.move(dbparameters_);
}
SQLite3Connection::~SQLite3Connection() {
SQLite3Database::~SQLite3Database() {
LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_DROPCONN);
if (dbparameters_->db_ != NULL) {
close();
......@@ -241,7 +241,7 @@ SQLite3Connection::~SQLite3Connection() {
}
void
SQLite3Connection::close(void) {
SQLite3Database::close(void) {
LOG_DEBUG(logger, DBG_TRACE_BASIC, DATASRC_SQLITE_CONNCLOSE);
if (dbparameters_->db_ == NULL) {
isc_throw(DataSourceError,
......@@ -283,7 +283,7 @@ SQLite3Connection::close(void) {
}
std::pair<bool, int>
SQLite3Connection::getZone(const isc::dns::Name& name) const {
SQLite3Database::getZone(const isc::dns::Name& name) const {
int rc;
// Take the statement (simple SELECT id FROM zones WHERE...)
......
......@@ -45,13 +45,13 @@ public:
struct SQLite3Parameters;
/**
* \brief Concrete implementation of DatabaseConnection for SQLite3 databases
* \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 SQLite3Connection : public DatabaseConnection {
class SQLite3Database : public DatabaseAbstraction {
public:
/**
* \brief Constructor
......@@ -63,21 +63,21 @@ public:
*
* \param filename The database file to be used.
* \param rrclass Which class of data it should serve (while the database
* can contain multiple classes of data, single connection can provide
* only one class).
* file can contain multiple classes of data, single database can
* provide only one class).
*/
SQLite3Connection(const std::string& filename,
const isc::dns::RRClass& rrclass);
SQLite3Database(const std::string& filename,
const isc::dns::RRClass& rrclass);
/**
* \brief Destructor
*
* Closes the database.
*/
~SQLite3Connection();
~SQLite3Database();
/**
* \brief Look up a zone
*
* This implements the getZone from DatabaseConnection and looks 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.
*
......
......@@ -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_connection_unittest.cc
run_unittests_SOURCES += sqlite3_database_unittest.cc
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
......
......@@ -30,7 +30,7 @@ namespace {
* A virtual database connection that pretends it contains single zone --
* example.org.
*/
class MockConnection : public DatabaseConnection {
class MockAbstraction : public DatabaseAbstraction {
public:
virtual std::pair<bool, int> getZone(const Name& name) const {
if (name == Name("example.org")) {
......@@ -51,16 +51,16 @@ public:
* times per test.
*/
void createClient() {
current_connection_ = new MockConnection();
client_.reset(new DatabaseClient(shared_ptr<DatabaseConnection>(
current_connection_)));
current_database_ = new MockAbstraction();
client_.reset(new DatabaseClient(shared_ptr<DatabaseAbstraction>(
current_database_)));
}
// Will be deleted by client_, just keep the current value for comparison.
MockConnection* current_connection_;
MockAbstraction* current_database_;
shared_ptr<DatabaseClient> client_;
/**
* Check the zone finder is a valid one and references the zone ID and
* connection available here.
* database available here.
*/
void checkZoneFinder(const DataSourceClient::FindResult& zone) {
ASSERT_NE(ZoneFinderPtr(), zone.zone_finder) << "No zone finder";
......@@ -69,7 +69,7 @@ public:
ASSERT_NE(shared_ptr<DatabaseClient::Finder>(), finder) <<
"Wrong type of finder";
EXPECT_EQ(42, finder->zone_id());
EXPECT_EQ(current_connection_, &finder->connection());
EXPECT_EQ(current_database_, &finder->database());
}
};
......@@ -92,7 +92,7 @@ TEST_F(DatabaseClientTest, superZone) {
}
TEST_F(DatabaseClientTest, noConnException) {
EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseConnection>()),
EXPECT_THROW(DatabaseClient(shared_ptr<DatabaseAbstraction>()),
isc::InvalidParameter);
}
......
......@@ -12,7 +12,7 @@
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include <datasrc/sqlite3_connection.h>
#include <datasrc/sqlite3_database.h>
#include <datasrc/data_source.h>
#include <dns/rrclass.h>
......@@ -41,29 +41,29 @@ 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(SQLite3Connection conn(SQLITE_DBFILE_EXAMPLE,
RRClass::IN()));
EXPECT_NO_THROW(SQLite3Database db(SQLITE_DBFILE_EXAMPLE,
RRClass::IN()));
}
// The file can't be opened
TEST(SQLite3Open, notExist) {
EXPECT_THROW(SQLite3Connection conn(SQLITE_DBFILE_NOTEXIST,
RRClass::IN()), SQLite3Error);
EXPECT_THROW(SQLite3Database db(SQLITE_DBFILE_NOTEXIST,
RRClass::IN()), SQLite3Error);
}
// It rejects broken DB
TEST(SQLite3Open, brokenDB) {
EXPECT_THROW(SQLite3Connection conn(SQLITE_DBFILE_BROKENDB,
RRClass::IN()), SQLite3Error);
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(SQLite3Connection conn(SQLITE_DBFILE_MEMORY,
RRClass::IN()));
EXPECT_NO_THROW(SQLite3Database db(SQLITE_DBFILE_MEMORY,
RRClass::IN()));
}
// Test fixture for querying the connection
// Test fixture for querying the db
class SQLite3Conn : public ::testing::Test {
public:
SQLite3Conn() {
......@@ -71,33 +71,33 @@ public:
}
// So it can be re-created with different data
void initConn(const std::string& filename, const RRClass& rrclass) {
conn.reset(new SQLite3Connection(filename, rrclass));
db.reset(new SQLite3Database(filename, rrclass));
}
// The tested connection
std::auto_ptr<SQLite3Connection> conn;
// 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(conn->getZone(Name("example.com")));
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(conn->getZone(Name("sub.example.com")).first);
EXPECT_FALSE(db->getZone(Name("sub.example.com")).first);
}
// This zone is not there at all
TEST_F(SQLite3Conn, noZone) {
EXPECT_FALSE(conn->getZone(Name("example.org")).first);
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(conn->getZone(Name("example.com")).first);
EXPECT_FALSE(db->getZone(Name("example.com")).first);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment