database.h 13 KB

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