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