database.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 __DATABASE_DATASRC_H
  15. #define __DATABASE_DATASRC_H
  16. #include <datasrc/client.h>
  17. namespace isc {
  18. namespace datasrc {
  19. /**
  20. * \brief Abstraction of lowlevel database with DNS data
  21. *
  22. * This class is defines interface to databases. Each supported database
  23. * will provide methods for accessing the data stored there in a generic
  24. * manner. The methods are meant to be low-level, without much or any knowledge
  25. * about DNS and should be possible to translate directly to queries.
  26. *
  27. * On the other hand, how the communication with database is done and in what
  28. * schema (in case of relational/SQL database) is up to the concrete classes.
  29. *
  30. * This class is non-copyable, as copying connections to database makes little
  31. * sense and will not be needed.
  32. *
  33. * \todo Is it true this does not need to be copied? For example the zone
  34. * iterator might need it's own copy. But a virtual clone() method might
  35. * be better for that than copy constructor.
  36. *
  37. * \note The same application may create multiple connections to the same
  38. * database, having multiple instances of this class. If the database
  39. * allows having multiple open queries at one connection, the connection
  40. * class may share it.
  41. */
  42. class DatabaseAccessor : boost::noncopyable {
  43. public:
  44. /**
  45. * \brief Destructor
  46. *
  47. * It is empty, but needs a virtual one, since we will use the derived
  48. * classes in polymorphic way.
  49. */
  50. virtual ~DatabaseAccessor() { }
  51. /**
  52. * \brief Retrieve a zone identifier
  53. *
  54. * This method looks up a zone for the given name in the database. It
  55. * should match only exact zone name (eg. name is equal to the zone's
  56. * apex), as the DatabaseClient will loop trough the labels itself and
  57. * find the most suitable zone.
  58. *
  59. * It is not specified if and what implementation of this method may throw,
  60. * so code should expect anything.
  61. *
  62. * \param name The name of the zone's apex to be looked up.
  63. * \return The first part of the result indicates if a matching zone
  64. * was found. In case it was, the second part is internal zone ID.
  65. * This one will be passed to methods finding data in the zone.
  66. * It is not required to keep them, in which case whatever might
  67. * be returned - the ID is only passed back to the database as
  68. * an opaque handle.
  69. */
  70. virtual std::pair<bool, int> getZone(const isc::dns::Name& name) const = 0;
  71. /**
  72. * \brief Starts a new search for records of the given name in the given zone
  73. *
  74. * The data searched by this call can be retrieved with subsequent calls to
  75. * getNextRecord().
  76. *
  77. * \exception DataSourceError if there is a problem connecting to the
  78. * backend database
  79. *
  80. * \param zone_id The zone to search in, as returned by getZone()
  81. * \param name The name of the records to find
  82. */
  83. virtual void searchForRecords(int zone_id, const std::string& name) = 0;
  84. /**
  85. * \brief Retrieves the next record from the search started with searchForRecords()
  86. *
  87. * Returns a boolean specifying whether or not there was more data to read.
  88. * In the case of a database error, a DatasourceError is thrown.
  89. *
  90. * The columns passed is an array of std::strings consisting of
  91. * DatabaseConnection::COLUMN_COUNT elements, the elements of which
  92. * are defined in DatabaseConnection::RecordColumns, in their basic
  93. * string representation.
  94. *
  95. * If you are implementing a derived database connection class, you
  96. * should have this method check the column_count value, and fill the
  97. * array with strings conforming to their description in RecordColumn.
  98. *
  99. * \exception DatasourceError if there was an error reading from the database
  100. *
  101. * \param columns The elements of this array will be filled with the data
  102. * for one record as defined by RecordColumns
  103. * If there was no data, the array is untouched.
  104. * \return true if there was a next record, false if there was not
  105. */
  106. virtual bool getNextRecord(std::string columns[], size_t column_count) = 0;
  107. /**
  108. * \brief Resets the current search initiated with searchForRecords()
  109. *
  110. * This method will be called when the called of searchForRecords() and
  111. * getNextRecord() finds bad data, and aborts the current search.
  112. * It should clean up whatever handlers searchForRecords() created, and
  113. * any other state modified or needed by getNextRecord()
  114. *
  115. * Of course, the implementation of getNextRecord may also use it when
  116. * it is done with a search. If it does, the implementation of this
  117. * method should make sure it can handle being called multiple times.
  118. *
  119. * The implementation for this method should make sure it never throws.
  120. */
  121. virtual void resetSearch() = 0;
  122. /**
  123. * Definitions of the fields as they are required to be filled in
  124. * by getNextRecord()
  125. *
  126. * When implementing getNextRecord(), the columns array should
  127. * be filled with the values as described in this enumeration,
  128. * in this order, i.e. TYPE_COLUMN should be the first element
  129. * (index 0) of the array, TTL_COLUMN should be the second element
  130. * (index 1), etc.
  131. */
  132. enum RecordColumns {
  133. TYPE_COLUMN = 0, ///< The RRType of the record (A/NS/TXT etc.)
  134. TTL_COLUMN = 1, ///< The TTL of the record (a
  135. SIGTYPE_COLUMN = 2, ///< For RRSIG records, this contains the RRTYPE
  136. ///< the RRSIG covers. In the current implementation,
  137. ///< this field is ignored.
  138. RDATA_COLUMN = 3 ///< Full text representation of the record's RDATA
  139. };
  140. /// The number of fields the columns array passed to getNextRecord should have
  141. static const size_t COLUMN_COUNT = 4;
  142. /**
  143. * \brief Returns a string identifying this dabase backend
  144. *
  145. * The returned string is mainly intended to be used for
  146. * debugging/logging purposes.
  147. *
  148. * Any implementation is free to choose the exact string content,
  149. * but it is advisable to make it a name that is distinguishable
  150. * from the others.
  151. *
  152. * \return the name of the database
  153. */
  154. virtual const std::string& getDBName() const = 0;
  155. };
  156. /**
  157. * \brief Concrete data source client oriented at database backends.
  158. *
  159. * This class (together with corresponding versions of ZoneFinder,
  160. * ZoneIterator, etc.) translates high-level data source queries to
  161. * low-level calls on DatabaseAccessor. It calls multiple queries
  162. * if necessary and validates data from the database, allowing the
  163. * DatabaseAccessor to be just simple translation to SQL/other
  164. * queries to database.
  165. *
  166. * While it is possible to subclass it for specific database in case
  167. * of special needs, it is not expected to be needed. This should just
  168. * work as it is with whatever DatabaseAccessor.
  169. */
  170. class DatabaseClient : public DataSourceClient {
  171. public:
  172. /**
  173. * \brief Constructor
  174. *
  175. * It initializes the client with a database.
  176. *
  177. * \exception isc::InvalidParameter if database is NULL. It might throw
  178. * standard allocation exception as well, but doesn't throw anything else.
  179. *
  180. * \param database The database to use to get data. As the parameter
  181. * suggests, the client takes ownership of the database and will
  182. * delete it when itself deleted.
  183. */
  184. DatabaseClient(boost::shared_ptr<DatabaseAccessor> database);
  185. /**
  186. * \brief Corresponding ZoneFinder implementation
  187. *
  188. * The zone finder implementation for database data sources. Similarly
  189. * to the DatabaseClient, it translates the queries to methods of the
  190. * database.
  191. *
  192. * Application should not come directly in contact with this class
  193. * (it should handle it trough generic ZoneFinder pointer), therefore
  194. * it could be completely hidden in the .cc file. But it is provided
  195. * to allow testing and for rare cases when a database needs slightly
  196. * different handling, so it can be subclassed.
  197. *
  198. * Methods directly corresponds to the ones in ZoneFinder.
  199. */
  200. class Finder : public ZoneFinder {
  201. public:
  202. /**
  203. * \brief Constructor
  204. *
  205. * \param database The database (shared with DatabaseClient) to
  206. * be used for queries (the one asked for ID before).
  207. * \param zone_id The zone ID which was returned from
  208. * DatabaseAccessor::getZone and which will be passed to further
  209. * calls to the database.
  210. */
  211. Finder(boost::shared_ptr<DatabaseAccessor> database, int zone_id);
  212. // The following three methods are just implementations of inherited
  213. // ZoneFinder's pure virtual methods.
  214. virtual isc::dns::Name getOrigin() const;
  215. virtual isc::dns::RRClass getClass() const;
  216. /**
  217. * \brief Find an RRset in the datasource
  218. *
  219. * Searches the datasource for an RRset of the given name and
  220. * type. If there is a CNAME at the given name, the CNAME rrset
  221. * is returned.
  222. * (this implementation is not complete, and currently only
  223. * does full matches, CNAMES, and the signatures for matches and
  224. * CNAMEs)
  225. * \note target was used in the original design to handle ANY
  226. * queries. This is not implemented yet, and may use
  227. * target again for that, but it might also use something
  228. * different. It is left in for compatibility at the moment.
  229. * \note options are ignored at this moment
  230. *
  231. * \note Maybe counter intuitively, this method is not a const member
  232. * function. This is intentional; some of the underlying implementations
  233. * are expected to use a database backend, and would internally contain
  234. * some abstraction of "database connection". In the most strict sense
  235. * any (even read only) operation might change the internal state of
  236. * such a connection, and in that sense the operation cannot be considered
  237. * "const". In order to avoid giving a false sense of safety to the
  238. * caller, we indicate a call to this method may have a surprising
  239. * side effect. That said, this view may be too strict and it may
  240. * make sense to say the internal database connection doesn't affect
  241. * external behavior in terms of the interface of this method. As
  242. * we gain more experiences with various kinds of backends we may
  243. * revisit the constness.
  244. *
  245. * \exception DataSourceError when there is a problem reading
  246. * the data from the dabase backend.
  247. * This can be a connection, code, or
  248. * data (parse) error.
  249. *
  250. * \param name The name to find
  251. * \param type The RRType to find
  252. * \param target Unused at this moment
  253. * \param options Unused at this moment
  254. */
  255. virtual FindResult find(const isc::dns::Name& name,
  256. const isc::dns::RRType& type,
  257. isc::dns::RRsetList* target = NULL,
  258. const FindOptions options = FIND_DEFAULT);
  259. /**
  260. * \brief The zone ID
  261. *
  262. * This function provides the stored zone ID as passed to the
  263. * constructor. This is meant for testing purposes and normal
  264. * applications shouldn't need it.
  265. */
  266. int zone_id() const { return (zone_id_); }
  267. /**
  268. * \brief The database.
  269. *
  270. * This function provides the database stored inside as
  271. * passed to the constructor. This is meant for testing purposes and
  272. * normal applications shouldn't need it.
  273. */
  274. const DatabaseAccessor& database() const {
  275. return (*database_);
  276. }
  277. private:
  278. boost::shared_ptr<DatabaseAccessor> database_;
  279. const int zone_id_;
  280. };
  281. /**
  282. * \brief Find a zone in the database
  283. *
  284. * This queries database's getZone to find the best matching zone.
  285. * It will propagate whatever exceptions are thrown from that method
  286. * (which is not restricted in any way).
  287. *
  288. * \param name Name of the zone or data contained there.
  289. * \return FindResult containing the code and an instance of Finder, if
  290. * anything is found. However, application should not rely on the
  291. * ZoneFinder being instance of Finder (possible subclass of this class
  292. * may return something else and it may change in future versions), it
  293. * should use it as a ZoneFinder only.
  294. */
  295. virtual FindResult findZone(const isc::dns::Name& name) const;
  296. private:
  297. /// \brief Our database.
  298. const boost::shared_ptr<DatabaseAccessor> database_;
  299. };
  300. }
  301. }
  302. #endif