sqlite3_accessor.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 <exceptions/exceptions.h>
  18. #include <boost/enable_shared_from_this.hpp>
  19. #include <boost/scoped_ptr.hpp>
  20. #include <string>
  21. #include <cc/data.h>
  22. namespace isc {
  23. namespace dns {
  24. class RRClass;
  25. }
  26. namespace datasrc {
  27. /**
  28. * \brief Low-level database error
  29. *
  30. * This exception is thrown when the SQLite library complains about something.
  31. * It might mean corrupt database file, invalid request or that something is
  32. * rotten in the library.
  33. */
  34. class SQLite3Error : public Exception {
  35. public:
  36. SQLite3Error(const char* file, size_t line, const char* what) :
  37. isc::Exception(file, line, what) {}
  38. };
  39. struct SQLite3Parameters;
  40. /**
  41. * \brief Concrete implementation of DatabaseAccessor for SQLite3 databases
  42. *
  43. * This opens one database file with our schema and serves data from there.
  44. * According to the design, it doesn't interpret the data in any way, it just
  45. * provides unified access to the DB.
  46. */
  47. class SQLite3Accessor : public DatabaseAccessor,
  48. public boost::enable_shared_from_this<SQLite3Accessor> {
  49. public:
  50. /**
  51. * \brief Constructor
  52. *
  53. * This opens the database and becomes ready to serve data from there.
  54. *
  55. * \exception SQLite3Error will be thrown if the given database file
  56. * doesn't work (it is broken, doesn't exist and can't be created, etc).
  57. *
  58. * \param filename The database file to be used.
  59. * \param rrclass Textual representation of RR class ("IN", "CH", etc),
  60. * specifying which class of data it should serve (while the database
  61. * file can contain multiple classes of data, a single accessor can
  62. * work with only one class).
  63. */
  64. SQLite3Accessor(const std::string& filename, const std::string& rrclass);
  65. /**
  66. * \brief Destructor
  67. *
  68. * Closes the database.
  69. */
  70. ~SQLite3Accessor();
  71. /// This implementation internally opens a new sqlite3 database for the
  72. /// same file name specified in the constructor of the original accessor.
  73. virtual boost::shared_ptr<DatabaseAccessor> clone();
  74. /**
  75. * \brief Look up a zone
  76. *
  77. * This implements the getZone from DatabaseAccessor and looks up a zone
  78. * in the data. It looks for a zone with the exact given origin and class
  79. * passed to the constructor.
  80. *
  81. * \exception SQLite3Error if something about the database is broken.
  82. *
  83. * \param name The (fully qualified) domain name of zone to look up
  84. * \return The pair contains if the lookup was successful in the first
  85. * element and the zone id in the second if it was.
  86. */
  87. virtual std::pair<bool, int> getZone(const std::string& name) const;
  88. /** \brief Look up all resource records for a name
  89. *
  90. * This implements the getRecords() method from DatabaseAccessor
  91. *
  92. * \exception SQLite3Error if there is an sqlite3 error when performing
  93. * the query
  94. *
  95. * \param name the name to look up
  96. * \param id the zone id, as returned by getZone()
  97. * \param subdomains Match subdomains instead of the name.
  98. * \return Iterator that contains all records with the given name
  99. */
  100. virtual IteratorContextPtr getRecords(const std::string& name,
  101. int id,
  102. bool subdomains = false) const;
  103. /** \brief Look up all resource records for a zone
  104. *
  105. * This implements the getRecords() method from DatabaseAccessor
  106. *
  107. * \exception SQLite3Error if there is an sqlite3 error when performing
  108. * the query
  109. *
  110. * \param id the zone id, as returned by getZone()
  111. * \return Iterator that contains all records in the given zone
  112. */
  113. virtual IteratorContextPtr getAllRecords(int id) const;
  114. virtual std::pair<bool, int> startUpdateZone(const std::string& zone_name,
  115. bool replace);
  116. virtual void startTransaction();
  117. /// \note we are quite impatient here: it's quite possible that the COMMIT
  118. /// fails due to other process performing SELECT on the same database
  119. /// (consider the case where COMMIT is done by xfrin or dynamic update
  120. /// server while an authoritative server is busy reading the DB).
  121. /// In a future version we should probably need to introduce some retry
  122. /// attempt and/or increase timeout before giving up the COMMIT, even
  123. /// if it still doesn't guarantee 100% success. Right now this
  124. /// implementation throws a \c DataSourceError exception in such a case.
  125. virtual void commit();
  126. /// \note In SQLite3 rollback can fail if there's another unfinished
  127. /// statement is performed for the same database structure.
  128. /// Although it's not expected to happen in our expected usage, it's not
  129. /// guaranteed to be prevented at the API level. If it ever happens, this
  130. /// method throws a \c DataSourceError exception. It should be
  131. /// considered a bug of the higher level application program.
  132. virtual void rollback();
  133. virtual void addRecordToZone(
  134. const std::string (&columns)[ADD_COLUMN_COUNT]);
  135. virtual void deleteRecordInZone(
  136. const std::string (&params)[DEL_PARAM_COUNT]);
  137. /// This derived version of the method prepares an SQLite3 statement
  138. /// for adding the diff first time it's called, and if it fails throws
  139. // an \c SQLite3Error exception.
  140. virtual void addRecordDiff(
  141. int zone_id, uint32_t serial, DiffOperation operation,
  142. const std::string (&params)[DIFF_PARAM_COUNT]);
  143. // A short term method for tests until we implement more complete
  144. // API to retrieve diffs (#1330). It returns all records of the diffs
  145. // table whose zone_id column is identical to the given value.
  146. // Since this is a short term workaround, it ignores some corner cases
  147. // (such as an SQLite3 execution failure) and is not very efficient,
  148. // in favor of brevity. Once #1330 is completed, this method must be
  149. // removed, and the tests using this method must be rewritten using the
  150. // official API.
  151. std::vector<std::vector<std::string> > getRecordDiff(int zone_id);
  152. /// The SQLite3 implementation of this method returns a string starting
  153. /// with a fixed prefix of "sqlite3_" followed by the DB file name
  154. /// removing any path name. For example, for the DB file
  155. /// /somewhere/in/the/system/bind10.sqlite3, this method will return
  156. /// "sqlite3_bind10.sqlite3".
  157. virtual const std::string& getDBName() const { return (database_name_); }
  158. /// \brief Concrete implementation of the pure virtual method
  159. virtual std::string findPreviousName(int zone_id, const std::string& rname)
  160. const;
  161. private:
  162. /// \brief Private database data
  163. boost::scoped_ptr<SQLite3Parameters> dbparameters_;
  164. /// \brief The filename of the DB (necessary for clone())
  165. const std::string filename_;
  166. /// \brief The class for which the queries are done
  167. const std::string class_;
  168. /// \brief Opens the database
  169. void open(const std::string& filename);
  170. /// \brief Closes the database
  171. void close();
  172. /// \brief SQLite3 implementation of IteratorContext
  173. class Context;
  174. friend class Context;
  175. const std::string database_name_;
  176. };
  177. /// \brief Creates an instance of the SQlite3 datasource client
  178. ///
  179. /// Currently the configuration passed here must be a MapElement, containing
  180. /// one item called "database_file", whose value is a string
  181. ///
  182. /// This configuration setup is currently under discussion and will change in
  183. /// the near future.
  184. ///
  185. /// \param config The configuration for the datasource instance
  186. /// \param error This string will be set to an error message if an error occurs
  187. /// during initialization
  188. /// \return An instance of the sqlite3 datasource client, or NULL if there was
  189. /// an error
  190. extern "C" DataSourceClient* createInstance(isc::data::ConstElementPtr config,
  191. std::string& error);
  192. /// \brief Destroy the instance created by createInstance()
  193. extern "C" void destroyInstance(DataSourceClient* instance);
  194. }
  195. }
  196. #endif // __DATASRC_SQLITE3_CONNECTION_H
  197. // Local Variables:
  198. // mode: c++
  199. // End: