sqlite3_accessor.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 <string>
  19. namespace isc {
  20. namespace dns {
  21. class RRClass;
  22. }
  23. namespace datasrc {
  24. /**
  25. * \brief Low-level database error
  26. *
  27. * This exception is thrown when the SQLite library complains about something.
  28. * It might mean corrupt database file, invalid request or that something is
  29. * rotten in the library.
  30. */
  31. class SQLite3Error : public Exception {
  32. public:
  33. SQLite3Error(const char* file, size_t line, const char* what) :
  34. isc::Exception(file, line, what) {}
  35. };
  36. struct SQLite3Parameters;
  37. /**
  38. * \brief Concrete implementation of DatabaseAccessor for SQLite3 databases
  39. *
  40. * This opens one database file with our schema and serves data from there.
  41. * According to the design, it doesn't interpret the data in any way, it just
  42. * provides unified access to the DB.
  43. */
  44. class SQLite3Database : public DatabaseAccessor {
  45. public:
  46. /**
  47. * \brief Constructor
  48. *
  49. * This opens the database and becomes ready to serve data from there.
  50. *
  51. * \exception SQLite3Error will be thrown if the given database file
  52. * doesn't work (it is broken, doesn't exist and can't be created, etc).
  53. *
  54. * \param filename The database file to be used.
  55. * \param rrclass Which class of data it should serve (while the database
  56. * file can contain multiple classes of data, single database can
  57. * provide only one class).
  58. */
  59. SQLite3Database(const std::string& filename,
  60. const isc::dns::RRClass& rrclass);
  61. /**
  62. * \brief Destructor
  63. *
  64. * Closes the database.
  65. */
  66. ~SQLite3Database();
  67. /**
  68. * \brief Look up a zone
  69. *
  70. * This implements the getZone from DatabaseAccessor and looks up a zone
  71. * in the data. It looks for a zone with the exact given origin and class
  72. * passed to the constructor.
  73. *
  74. * \exception SQLite3Error if something about the database is broken.
  75. *
  76. * \param name The name of zone to look up
  77. * \return The pair contains if the lookup was successful in the first
  78. * element and the zone id in the second if it was.
  79. */
  80. virtual std::pair<bool, int> getZone(const isc::dns::Name& name) const;
  81. /**
  82. * \brief Start a new search for the given name in the given zone.
  83. *
  84. * This implements the searchForRecords from DatabaseConnection.
  85. * This particular implementation does not raise DataSourceError.
  86. *
  87. * \exception DataSourceError when sqlite3_bind_int() or
  88. * sqlite3_bind_text() fails
  89. *
  90. * \param zone_id The zone to seach in, as returned by getZone()
  91. * \param name The name to find records for
  92. * \param subdomains Match subdomains instead of the name.
  93. */
  94. virtual void searchForRecords(int zone_id, const std::string& name,
  95. bool subdomains = false);
  96. /**
  97. * \brief Retrieve the next record from the search started with
  98. * searchForRecords
  99. *
  100. * This implements the getNextRecord from DatabaseConnection.
  101. * See the documentation there for more information.
  102. *
  103. * If this method raises an exception, the contents of columns are undefined.
  104. *
  105. * \exception DataSourceError if there is an error returned by sqlite_step()
  106. * When this exception is raised, the current
  107. * search as initialized by searchForRecords() is
  108. * NOT reset, and the caller is expected to take
  109. * care of that.
  110. * \param columns This vector will be cleared, and the fields of the record will
  111. * be appended here as strings (in the order rdtype, ttl, sigtype,
  112. * and rdata). If there was no data (i.e. if this call returns
  113. * false), the vector is untouched.
  114. * \return true if there was a next record, false if there was not
  115. */
  116. virtual bool getNextRecord(std::string columns[], size_t column_count);
  117. /**
  118. * \brief Resets any state created by searchForRecords
  119. *
  120. * This implements the resetSearch from DatabaseConnection.
  121. * See the documentation there for more information.
  122. *
  123. * This function never throws.
  124. */
  125. virtual void resetSearch();
  126. /// The SQLite3 implementation of this method returns a string starting
  127. /// with a fixed prefix of "sqlite3_" followed by the DB file name
  128. /// removing any path name. For example, for the DB file
  129. /// /somewhere/in/the/system/bind10.sqlite3, this method will return
  130. /// "sqlite3_bind10.sqlite3".
  131. virtual const std::string& getDBName() const { return (database_name_); }
  132. private:
  133. /// \brief Private database data
  134. SQLite3Parameters* dbparameters_;
  135. /// \brief The class for which the queries are done
  136. const std::string class_;
  137. /// \brief Opens the database
  138. void open(const std::string& filename);
  139. /// \brief Closes the database
  140. void close();
  141. const std::string database_name_;
  142. };
  143. }
  144. }
  145. #endif