sqlite3_connection.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __DATASRC_SQLITE3_CONNECTION_H
  15. #define __DATASRC_SQLITE3_CONNECTION_H
  16. #include <datasrc/database.h>
  17. #include <exceptions/exceptions.h>
  18. #include <cc/data.h>
  19. #include <boost/enable_shared_from_this.hpp>
  20. #include <string>
  21. namespace isc {
  22. namespace dns {
  23. class RRClass;
  24. }
  25. namespace datasrc {
  26. /**
  27. * \brief Low-level database error
  28. *
  29. * This exception is thrown when the SQLite library complains about something.
  30. * It might mean corrupt database file, invalid request or that something is
  31. * rotten in the library.
  32. */
  33. class SQLite3Error : public Exception {
  34. public:
  35. SQLite3Error(const char* file, size_t line, const char* what) :
  36. isc::Exception(file, line, what) {}
  37. };
  38. struct SQLite3Parameters;
  39. /**
  40. * \brief Concrete implementation of DatabaseConnection for SQLite3 databases
  41. *
  42. * This opens one database file with our schema and serves data from there.
  43. * According to the design, it doesn't interpret the data in any way, it just
  44. * provides unified access to the DB.
  45. */
  46. class SQLite3Connection : public DatabaseConnection,
  47. public boost::enable_shared_from_this<SQLite3Connection> {
  48. public:
  49. /**
  50. * \brief Constructor
  51. *
  52. * This opens the database and becomes ready to serve data from there.
  53. *
  54. * This might throw SQLite3Error if the given database file doesn't work
  55. * (it is broken, doesn't exist and can't be created, etc). It might throw
  56. * DataSourceError if the provided config is invalid (it is missing the
  57. * database_file element).
  58. *
  59. * \param config The part of config describing which database file should
  60. * be used.
  61. * \param rrclass Which class of data it should serve (while the database
  62. * can contain multiple classes of data, single connection can provide
  63. * only one class).
  64. * \todo Should we pass the database filename instead of the config? It
  65. * might be cleaner if this class doesn't know anything about configs.
  66. */
  67. SQLite3Connection(const isc::data::ConstElementPtr& config,
  68. const isc::dns::RRClass& rrclass);
  69. /**
  70. * \brief Destructor
  71. *
  72. * Closes the database.
  73. */
  74. ~ SQLite3Connection();
  75. /**
  76. * \brief Look up a zone
  77. *
  78. * This implements the getZone from DatabaseConnection and looks up a zone
  79. * in the data. It looks for a zone with the exact given origin and class
  80. * passed to the constructor.
  81. *
  82. * It may throw SQLite3Error if something about the database is broken.
  83. *
  84. * \param name The name of zone to look up
  85. * \return The pair contains if the lookup was successful in the first
  86. * element and the zone id in the second if it was.
  87. */
  88. virtual std::pair<bool, int> getZone(const isc::dns::Name& name) const;
  89. virtual IteratorContextPtr getIteratorContext(const isc::dns::Name&,
  90. int id) const;
  91. private:
  92. /// \brief Private database data
  93. SQLite3Parameters* dbparameters_;
  94. /// \brief The class for which the queries are done
  95. const std::string class_;
  96. /// \brief Opens the database
  97. void open(const std::string& filename);
  98. /// \brief Closes the database
  99. void close();
  100. class Context;
  101. friend class Context;
  102. };
  103. }
  104. }
  105. #endif