sqlite3_accessor_link.cc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (C) 2012 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. #include <cc/data.h>
  15. #include <dns/rrclass.h>
  16. #include <datasrc/sqlite3_accessor.h>
  17. #include <datasrc/database.h>
  18. #include <string>
  19. using namespace std;
  20. using namespace isc::dns;
  21. using namespace isc::data;
  22. namespace isc {
  23. namespace datasrc {
  24. namespace {
  25. const char* const CONFIG_ITEM_DATABASE_FILE = "database_file";
  26. void
  27. addError(ElementPtr errors, const std::string& error) {
  28. if (errors != ElementPtr() && errors->getType() == Element::list) {
  29. errors->add(Element::create(error));
  30. }
  31. }
  32. bool
  33. checkConfig(ConstElementPtr config, ElementPtr errors) {
  34. /* Specific configuration is under discussion, right now this accepts
  35. * the 'old' configuration, see header file
  36. */
  37. bool result = true;
  38. if (!config || config->getType() != Element::map) {
  39. addError(errors, "Base config for SQlite3 backend must be a map");
  40. result = false;
  41. } else {
  42. if (!config->contains(CONFIG_ITEM_DATABASE_FILE)) {
  43. addError(errors,
  44. "Config for SQlite3 backend does not contain a '" +
  45. string(CONFIG_ITEM_DATABASE_FILE) +
  46. "' value");
  47. result = false;
  48. } else if (!config->get(CONFIG_ITEM_DATABASE_FILE) ||
  49. config->get(CONFIG_ITEM_DATABASE_FILE)->getType() !=
  50. Element::string) {
  51. addError(errors, "value of " + string(CONFIG_ITEM_DATABASE_FILE) +
  52. " in SQLite3 backend is not a string");
  53. result = false;
  54. } else if (config->get(CONFIG_ITEM_DATABASE_FILE)->stringValue() ==
  55. "") {
  56. addError(errors, "value of " + string(CONFIG_ITEM_DATABASE_FILE) +
  57. " in SQLite3 backend is empty");
  58. result = false;
  59. }
  60. }
  61. return (result);
  62. }
  63. } // end unnamed namespace
  64. DataSourceClient *
  65. createInstance(isc::data::ConstElementPtr config, std::string& error) {
  66. ElementPtr errors(Element::createList());
  67. if (!checkConfig(config, errors)) {
  68. error = "Configuration error: " + errors->str();
  69. return (NULL);
  70. }
  71. const std::string dbfile =
  72. config->get(CONFIG_ITEM_DATABASE_FILE)->stringValue();
  73. try {
  74. boost::shared_ptr<DatabaseAccessor> sqlite3_accessor(
  75. new SQLite3Accessor(dbfile, "IN")); // XXX: avoid hardcode RR class
  76. return (new DatabaseClient(isc::dns::RRClass::IN(), sqlite3_accessor));
  77. } catch (const std::exception& exc) {
  78. error = std::string("Error creating sqlite3 datasource: ") +
  79. exc.what();
  80. return (NULL);
  81. } catch (...) {
  82. error = std::string("Error creating sqlite3 datasource, "
  83. "unknown exception");
  84. return (NULL);
  85. }
  86. }
  87. void destroyInstance(DataSourceClient* instance) {
  88. delete instance;
  89. }
  90. } // end of namespace datasrc
  91. } // end of namespace isc