sqlite3_accessor.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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_ACCESSOR_H
  15. #define __DATASRC_SQLITE3_ACCESSOR_H
  16. #include <datasrc/database.h>
  17. #include <datasrc/data_source.h>
  18. #include <exceptions/exceptions.h>
  19. #include <boost/enable_shared_from_this.hpp>
  20. #include <boost/scoped_ptr.hpp>
  21. #include <string>
  22. #include <cc/data.h>
  23. namespace isc {
  24. namespace dns {
  25. class RRClass;
  26. }
  27. namespace datasrc {
  28. /**
  29. * \brief Low-level database error
  30. *
  31. * This exception is thrown when the SQLite library complains about something.
  32. * It might mean corrupt database file, invalid request or that something is
  33. * rotten in the library.
  34. */
  35. class SQLite3Error : public DataSourceError {
  36. public:
  37. SQLite3Error(const char* file, size_t line, const char* what) :
  38. DataSourceError(file, line, what) {}
  39. };
  40. class IncompatibleDbVersion : public Exception {
  41. public:
  42. IncompatibleDbVersion(const char* file, size_t line, const char* what) :
  43. isc::Exception(file, line, what) {}
  44. };
  45. /**
  46. * \brief Too Much Data
  47. *
  48. * Thrown if a query expecting a certain number of rows back returned too
  49. * many rows.
  50. */
  51. class TooMuchData : public DataSourceError {
  52. public:
  53. TooMuchData(const char* file, size_t line, const char* what) :
  54. DataSourceError(file, line, what) {}
  55. };
  56. /**
  57. * \brief Too Little Data
  58. *
  59. * Thrown if a query expecting a certain number of rows back returned too
  60. * few rows (including none).
  61. */
  62. class TooLittleData : public DataSourceError {
  63. public:
  64. TooLittleData(const char* file, size_t line, const char* what) :
  65. DataSourceError(file, line, what) {}
  66. };
  67. struct SQLite3Parameters;
  68. /**
  69. * \brief Concrete implementation of DatabaseAccessor for SQLite3 databases
  70. *
  71. * This opens one database file with our schema and serves data from there.
  72. * According to the design, it doesn't interpret the data in any way, it just
  73. * provides unified access to the DB.
  74. */
  75. class SQLite3Accessor : public DatabaseAccessor,
  76. public boost::enable_shared_from_this<SQLite3Accessor> {
  77. public:
  78. /**
  79. * \brief Constructor
  80. *
  81. * This opens the database and becomes ready to serve data from there.
  82. *
  83. * \exception SQLite3Error will be thrown if the given database file
  84. * doesn't work (it is broken, doesn't exist and can't be created, etc).
  85. *
  86. * \param filename The database file to be used.
  87. * \param rrclass Textual representation of RR class ("IN", "CH", etc),
  88. * specifying which class of data it should serve (while the database
  89. * file can contain multiple classes of data, a single accessor can
  90. * work with only one class).
  91. */
  92. SQLite3Accessor(const std::string& filename, const std::string& rrclass);
  93. /**
  94. * \brief Destructor
  95. *
  96. * Closes the database.
  97. */
  98. ~SQLite3Accessor();
  99. /// This implementation internally opens a new sqlite3 database for the
  100. /// same file name specified in the constructor of the original accessor.
  101. virtual boost::shared_ptr<DatabaseAccessor> clone();
  102. /**
  103. * \brief Look up a zone
  104. *
  105. * This implements the getZone from DatabaseAccessor and looks up a zone
  106. * in the data. It looks for a zone with the exact given origin and class
  107. * passed to the constructor.
  108. *
  109. * \exception SQLite3Error if something about the database is broken.
  110. *
  111. * \param name The (fully qualified) domain name of zone to look up
  112. * \return The pair contains if the lookup was successful in the first
  113. * element and the zone id in the second if it was.
  114. */
  115. virtual std::pair<bool, int> getZone(const std::string& name) const;
  116. /** \brief Look up all resource records for a name
  117. *
  118. * This implements the getRecords() method from DatabaseAccessor
  119. *
  120. * \exception SQLite3Error if there is an sqlite3 error when performing
  121. * the query
  122. *
  123. * \param name the name to look up
  124. * \param id the zone id, as returned by getZone()
  125. * \param subdomains Match subdomains instead of the name.
  126. * \return Iterator that contains all records with the given name
  127. */
  128. virtual IteratorContextPtr getRecords(const std::string& name,
  129. int id,
  130. bool subdomains = false) const;
  131. /// \brief Look up NSEC3 records for the given hash
  132. ///
  133. /// This implements the getNSEC3Records of DatabaseAccessor.
  134. ///
  135. /// \todo Actually implement, currently throws NotImplemented.
  136. virtual IteratorContextPtr getNSEC3Records(const std::string& hash,
  137. int id) const;
  138. /** \brief Look up all resource records for a zone
  139. *
  140. * This implements the getRecords() method from DatabaseAccessor
  141. *
  142. * \exception SQLite3Error if there is an sqlite3 error when performing
  143. * the query
  144. *
  145. * \param id the zone id, as returned by getZone()
  146. * \return Iterator that contains all records in the given zone
  147. */
  148. virtual IteratorContextPtr getAllRecords(int id) const;
  149. /** \brief Creates an iterator context for a set of differences.
  150. *
  151. * Implements the getDiffs() method from DatabaseAccessor
  152. *
  153. * \exception NoSuchSerial if either of the versions do not exist in
  154. * the difference table.
  155. * \exception SQLite3Error if there is an sqlite3 error when performing
  156. * the query
  157. *
  158. * \param id The ID of the zone, returned from getZone().
  159. * \param start The SOA serial number of the version of the zone from
  160. * which the difference sequence should start.
  161. * \param end The SOA serial number of the version of the zone at which
  162. * the difference sequence should end.
  163. *
  164. * \return Iterator containing difference records.
  165. */
  166. virtual IteratorContextPtr
  167. getDiffs(int id, uint32_t start, uint32_t end) const;
  168. virtual std::pair<bool, int> startUpdateZone(const std::string& zone_name,
  169. bool replace);
  170. virtual void startTransaction();
  171. /// \note we are quite impatient here: it's quite possible that the COMMIT
  172. /// fails due to other process performing SELECT on the same database
  173. /// (consider the case where COMMIT is done by xfrin or dynamic update
  174. /// server while an authoritative server is busy reading the DB).
  175. /// In a future version we should probably need to introduce some retry
  176. /// attempt and/or increase timeout before giving up the COMMIT, even
  177. /// if it still doesn't guarantee 100% success. Right now this
  178. /// implementation throws a \c DataSourceError exception in such a case.
  179. virtual void commit();
  180. /// \note In SQLite3 rollback can fail if there's another unfinished
  181. /// statement is performed for the same database structure.
  182. /// Although it's not expected to happen in our expected usage, it's not
  183. /// guaranteed to be prevented at the API level. If it ever happens, this
  184. /// method throws a \c DataSourceError exception. It should be
  185. /// considered a bug of the higher level application program.
  186. virtual void rollback();
  187. virtual void addRecordToZone(
  188. const std::string (&columns)[ADD_COLUMN_COUNT]);
  189. virtual void addNSEC3RecordToZone(
  190. const std::string (&columns)[ADD_NSEC3_COLUMN_COUNT]);
  191. virtual void deleteRecordInZone(
  192. const std::string (&params)[DEL_PARAM_COUNT]);
  193. virtual void deleteNSEC3RecordInZone(
  194. const std::string (&params)[DEL_PARAM_COUNT]);
  195. /// This derived version of the method prepares an SQLite3 statement
  196. /// for adding the diff first time it's called, and if it fails throws
  197. // an \c SQLite3Error exception.
  198. virtual void addRecordDiff(
  199. int zone_id, uint32_t serial, DiffOperation operation,
  200. const std::string (&params)[DIFF_PARAM_COUNT]);
  201. /// The SQLite3 implementation of this method returns a string starting
  202. /// with a fixed prefix of "sqlite3_" followed by the DB file name
  203. /// removing any path name. For example, for the DB file
  204. /// /somewhere/in/the/system/bind10.sqlite3, this method will return
  205. /// "sqlite3_bind10.sqlite3".
  206. virtual const std::string& getDBName() const { return (database_name_); }
  207. /// \brief Concrete implementation of the pure virtual method
  208. virtual std::string findPreviousName(int zone_id, const std::string& rname)
  209. const;
  210. /// \brief Conrete implemantion of the pure virtual method of
  211. /// DatabaseAccessor
  212. virtual std::string findPreviousNSEC3Hash(int zone_id,
  213. const std::string& hash) const;
  214. private:
  215. /// \brief Private database data
  216. boost::scoped_ptr<SQLite3Parameters> dbparameters_;
  217. /// \brief The filename of the DB (necessary for clone())
  218. const std::string filename_;
  219. /// \brief The class for which the queries are done
  220. const std::string class_;
  221. /// \brief Database name
  222. const std::string database_name_;
  223. /// \brief Opens the database
  224. void open(const std::string& filename);
  225. /// \brief Closes the database
  226. void close();
  227. /// \brief SQLite3 implementation of IteratorContext for all records
  228. class Context;
  229. friend class Context;
  230. /// \brief SQLite3 implementation of IteratorContext for differences
  231. class DiffContext;
  232. friend class DiffContext;
  233. };
  234. /// \brief Creates an instance of the SQlite3 datasource client
  235. ///
  236. /// Currently the configuration passed here must be a MapElement, containing
  237. /// one item called "database_file", whose value is a string
  238. ///
  239. /// This configuration setup is currently under discussion and will change in
  240. /// the near future.
  241. ///
  242. /// \param config The configuration for the datasource instance
  243. /// \param error This string will be set to an error message if an error occurs
  244. /// during initialization
  245. /// \return An instance of the sqlite3 datasource client, or NULL if there was
  246. /// an error
  247. extern "C" DataSourceClient* createInstance(isc::data::ConstElementPtr config,
  248. std::string& error);
  249. /// \brief Destroy the instance created by createInstance()
  250. extern "C" void destroyInstance(DataSourceClient* instance);
  251. }
  252. }
  253. #endif // __DATASRC_SQLITE3_CONNECTION_H
  254. // Local Variables:
  255. // mode: c++
  256. // End: