sqlite3_connection.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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_CONNECTION_H
  15. #define __DATASRC_SQLITE3_CONNECTION_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 DatabaseConnection 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 SQLite3Connection : public DatabaseConnection {
  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. * can contain multiple classes of data, single connection can provide
  57. * only one class).
  58. */
  59. SQLite3Connection(const std::string& filename,
  60. const isc::dns::RRClass& rrclass);
  61. /**
  62. * \brief Destructor
  63. *
  64. * Closes the database.
  65. */
  66. ~SQLite3Connection();
  67. /**
  68. * \brief Look up a zone
  69. *
  70. * This implements the getZone from DatabaseConnection 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. */
  93. virtual void searchForRecords(int zone_id, const std::string& name);
  94. /**
  95. * \brief Retrieve the next record from the search started with
  96. * searchForRecords
  97. *
  98. * This implements the getNextRecord from DatabaseConnection.
  99. * See the documentation there for more information.
  100. *
  101. * If this method raises an exception, the contents of columns are undefined.
  102. *
  103. * \exception DataSourceError if there is an error returned by sqlite_step()
  104. * When this exception is raised, the current
  105. * search as initialized by searchForRecords() is
  106. * NOT reset, and the caller is expected to take
  107. * care of that.
  108. * \param columns This vector will be cleared, and the fields of the record will
  109. * be appended here as strings (in the order rdtype, ttl, sigtype,
  110. * and rdata). If there was no data (i.e. if this call returns
  111. * false), the vector is untouched.
  112. * \return true if there was a next record, false if there was not
  113. */
  114. virtual bool getNextRecord(std::string columns[], size_t column_count);
  115. /**
  116. * \brief Resets any state created by searchForRecords
  117. *
  118. * This implements the resetSearch from DatabaseConnection.
  119. * See the documentation there for more information.
  120. *
  121. * This function never throws.
  122. */
  123. virtual void resetSearch();
  124. private:
  125. /// \brief Private database data
  126. SQLite3Parameters* dbparameters_;
  127. /// \brief The class for which the queries are done
  128. const std::string class_;
  129. /// \brief Opens the database
  130. void open(const std::string& filename);
  131. /// \brief Closes the database
  132. void close();
  133. };
  134. }
  135. }
  136. #endif