Sfoglia il codice sorgente

using sql backends

develop
Robin Thoni 8 anni fa
parent
commit
429b09bb36

+ 1
- 1
src/Business/PdnsSlave.cpp Vedi File

83
         return res;
83
         return res;
84
     std::string sql;
84
     std::string sql;
85
     for (auto a : _actions)
85
     for (auto a : _actions)
86
-        sql += a->getSqlQuery();
86
+        sql += a->getSql(sqlDb);
87
     res = sqlDb->override(sql);
87
     res = sqlDb->override(sql);
88
     delete sqlDb;
88
     delete sqlDb;
89
     return res;
89
     return res;

+ 0
- 5
src/DBO/Actions/Action.cpp Vedi File

22
     _domain = domain;
22
     _domain = domain;
23
 }
23
 }
24
 
24
 
25
-const std::string Action::getSqlQuery() const
26
-{
27
-    return "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + _domain + "\");\n" + getSql();
28
-}
29
-
30
 const std::string Action::getDhcpConf() const
25
 const std::string Action::getDhcpConf() const
31
 {
26
 {
32
     return getDhcp();
27
     return getDhcp();

+ 6
- 5
src/DBO/Actions/Action.h Vedi File

5
 #ifndef PDNS_SLAVE_ACTION_H
5
 #ifndef PDNS_SLAVE_ACTION_H
6
 #define PDNS_SLAVE_ACTION_H
6
 #define PDNS_SLAVE_ACTION_H
7
 
7
 
8
-# include <string>
9
-# include <memory>
10
-# include <vector>
8
+#include <string>
9
+#include <memory>
10
+#include <vector>
11
+
12
+class AbstractSql;
11
 
13
 
12
 class Action
14
 class Action
13
 {
15
 {
17
 
19
 
18
     const std::string getDhcpConf() const;
20
     const std::string getDhcpConf() const;
19
 
21
 
20
-    const std::string getSqlQuery() const;
22
+    virtual const std::string getSql(AbstractSql* sqlDb) const = 0;
21
 
23
 
22
     const std::string &getDomain() const;
24
     const std::string &getDomain() const;
23
 
25
 
24
     void setDomain(const std::string &domain);
26
     void setDomain(const std::string &domain);
25
 
27
 
26
 protected:
28
 protected:
27
-    virtual const std::string getSql() const = 0;
28
 
29
 
29
     virtual const std::string getDhcp() const;
30
     virtual const std::string getDhcp() const;
30
 
31
 

+ 3
- 11
src/DBO/Actions/ActionAddDomain.cpp Vedi File

3
 //
3
 //
4
 
4
 
5
 #include "ActionAddDomain.h"
5
 #include "ActionAddDomain.h"
6
+#include "../../DataAccess/AbstractSql.h"
6
 
7
 
7
 ActionAddDomain::ActionAddDomain()
8
 ActionAddDomain::ActionAddDomain()
8
 {
9
 {
78
     _ttl = ttl;
79
     _ttl = ttl;
79
 }
80
 }
80
 
81
 
81
-const std::string ActionAddDomain::getSql() const
82
+const std::string ActionAddDomain::getSql(AbstractSql* sqlDb) const
82
 {
83
 {
83
-    auto soa = _soaNs + " " + _soaMail + " " + std::to_string(time(nullptr)) + " " + std::to_string(_soaRefresh)
84
-             + " " + std::to_string(_soaRetry)+ " " + std::to_string(_soaExpire) + " " + std::to_string(_soaTtl);
85
-
86
-    return "INSERT INTO domains (name, type) VALUES (\"" + _domain + "\", \"MASTER\");\n"
87
-            "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + _domain + "\");\n"
88
-            "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES (@domain_id, "
89
-            "(SELECT id FROM users ORDER BY id LIMIT 1), 0);\n"
90
-            "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
91
-            "    VALUES(@domain_id, \"" + _domain + "\", \"SOA\","
92
-            " \"" + soa + "\", " + std::to_string(_ttl) + ", 0, " + std::to_string(time(nullptr)) + ");\n";
84
+    return sqlDb->getAddDomainQuery(*this);
93
 }
85
 }

+ 1
- 1
src/DBO/Actions/ActionAddDomain.h Vedi File

41
     void setSoaMail(const std::string &soaMail);
41
     void setSoaMail(const std::string &soaMail);
42
 
42
 
43
 protected:
43
 protected:
44
-    const std::string getSql() const override;
44
+    const std::string getSql(AbstractSql* sqlDb) const override;
45
 
45
 
46
 private:
46
 private:
47
     std::string _soaNs;
47
     std::string _soaNs;

+ 3
- 18
src/DBO/Actions/ActionAddHost.cpp Vedi File

5
 #include <sstream>
5
 #include <sstream>
6
 #include <vector>
6
 #include <vector>
7
 #include "ActionAddHost.h"
7
 #include "ActionAddHost.h"
8
+#include "../../DataAccess/AbstractSql.h"
8
 
9
 
9
 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
10
 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
10
     std::stringstream ss(s);
11
     std::stringstream ss(s);
25
 {
26
 {
26
 }
27
 }
27
 
28
 
28
-const std::string ActionAddHost::getSql() const
29
+const std::string ActionAddHost::getSql(AbstractSql* sqlDb) const
29
 {
30
 {
30
-    auto host = _host.empty() ? _domain : _host + "." + _domain;
31
-    auto query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
32
-            "    VALUES(@domain_id, \"" + host + "\", \"" + _recordType + "\","
33
-            " \"" + _recordValue + "\", " + std::to_string(_ttl) + ", 0, " + std::to_string(time(nullptr)) + ");\n";
34
-
35
-    if (_reverseEnabled && _recordType == "A")
36
-    {
37
-        auto reversedValue = getReversedValue();
38
-        auto reverseDomain = _reverseDomain.empty() ? "in-addr.arpa" : _reverseDomain + ".in-addr.arpa";
39
-
40
-        query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
41
-                 "    VALUES((SELECT id FROM domains WHERE name=\"" + reverseDomain + "\"), \""
42
-                 + reversedValue + "." + reverseDomain + "\", \"PTR\","
43
-                 " \"" + _host + "." + _domain + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
44
-    }
45
-
46
-    return query;
31
+    return sqlDb->getAddHostQuery(*this);
47
 }
32
 }
48
 
33
 
49
 const std::string &ActionAddHost::getHost() const
34
 const std::string &ActionAddHost::getHost() const

+ 3
- 2
src/DBO/Actions/ActionAddHost.h Vedi File

40
 
40
 
41
     void setTtl(int ttl);
41
     void setTtl(int ttl);
42
 
42
 
43
+    const std::string getReversedValue() const;
44
+
43
 protected:
45
 protected:
44
-    const std::string getSql() const override;
46
+    const std::string getSql(AbstractSql* sqlDb) const override;
45
 
47
 
46
     const std::string getDhcp() const override;
48
     const std::string getDhcp() const override;
47
 
49
 
48
 private:
50
 private:
49
-    const std::string getReversedValue() const;
50
 
51
 
51
     std::string _host;
52
     std::string _host;
52
 
53
 

+ 3
- 7
src/DBO/Actions/ActionDelDomain.cpp Vedi File

3
 //
3
 //
4
 
4
 
5
 #include "ActionDelDomain.h"
5
 #include "ActionDelDomain.h"
6
+#include "../../DataAccess/AbstractSql.h"
6
 
7
 
7
 ActionDelDomain::ActionDelDomain()
8
 ActionDelDomain::ActionDelDomain()
8
 {
9
 {
9
 }
10
 }
10
 
11
 
11
-const std::string ActionDelDomain::getSql() const
12
+const std::string ActionDelDomain::getSql(AbstractSql* sqlDb) const
12
 {
13
 {
13
-    return "DELETE FROM records WHERE domain_id=@domain_id;\n"
14
-            "DELETE FROM comments WHERE domain_id=@domain_id;\n"
15
-            "DELETE FROM zones WHERE domain_id=@domain_id;\n"
16
-            "DELETE FROM cryptokeys WHERE domain_id=@domain_id;\n"
17
-            "DELETE FROM domainmetadata WHERE domain_id=@domain_id;\n"
18
-            "DELETE FROM domains WHERE id=@domain_id;\n";
14
+    return sqlDb->getDelDomainQuery(*this);
19
 }
15
 }

+ 1
- 1
src/DBO/Actions/ActionDelDomain.h Vedi File

13
     ActionDelDomain();
13
     ActionDelDomain();
14
 
14
 
15
 protected:
15
 protected:
16
-    const std::string getSql() const override;
16
+    const std::string getSql(AbstractSql* sqlDb) const override;
17
 };
17
 };
18
 
18
 
19
 
19
 

+ 3
- 3
src/DBO/Actions/ActionDelHost.cpp Vedi File

3
 //
3
 //
4
 
4
 
5
 #include "ActionDelHost.h"
5
 #include "ActionDelHost.h"
6
+#include "../../DataAccess/AbstractSql.h"
6
 
7
 
7
 ActionDelHost::ActionDelHost()
8
 ActionDelHost::ActionDelHost()
8
 {
9
 {
9
 }
10
 }
10
 
11
 
11
-const std::string ActionDelHost::getSql() const
12
+const std::string ActionDelHost::getSql(AbstractSql* sqlDb) const
12
 {
13
 {
13
-    return "DELETE FROM records WHERE name=\"" + _host + "." + _domain + "\" AND "
14
-           "domain_id=@domain_id;";
14
+    return sqlDb->getDelHostQuery(*this);
15
 }
15
 }
16
 
16
 
17
 const std::string &ActionDelHost::getHost() const
17
 const std::string &ActionDelHost::getHost() const

+ 1
- 1
src/DBO/Actions/ActionDelHost.h Vedi File

17
     void setHost(const std::string &host);
17
     void setHost(const std::string &host);
18
 
18
 
19
 protected:
19
 protected:
20
-    const std::string getSql() const override;
20
+    const std::string getSql(AbstractSql* sqlDb) const override;
21
 
21
 
22
 private:
22
 private:
23
     std::string _host;
23
     std::string _host;

+ 12
- 0
src/DataAccess/AbstractSql.h Vedi File

7
 
7
 
8
 #include <DBO/Result.h>
8
 #include <DBO/Result.h>
9
 #include <DBO/SqlConfiguration.h>
9
 #include <DBO/SqlConfiguration.h>
10
+#include <DBO/Actions/ActionAddDomain.h>
11
+#include <DBO/Actions/ActionAddHost.h>
12
+#include <DBO/Actions/ActionDelDomain.h>
13
+#include <DBO/Actions/ActionDelHost.h>
10
 #include "DBO/Result.h"
14
 #include "DBO/Result.h"
11
 
15
 
12
 class AbstractSql
16
 class AbstractSql
21
 
25
 
22
     virtual BResult override(const std::string& sql) = 0;
26
     virtual BResult override(const std::string& sql) = 0;
23
 
27
 
28
+    virtual std::string getAddDomainQuery(const ActionAddDomain& action) = 0;
29
+
30
+    virtual std::string getAddHostQuery(const ActionAddHost& action) = 0;
31
+
32
+    virtual std::string getDelDomainQuery(const ActionDelDomain& action) = 0;
33
+
34
+    virtual std::string getDelHostQuery(const ActionDelHost& action) = 0;
35
+
24
 protected:
36
 protected:
25
     SqlConfiguration _masterConfig;
37
     SqlConfiguration _masterConfig;
26
 
38
 

+ 60
- 0
src/DataAccess/MySql.cpp Vedi File

78
         return res.ok(true);
78
         return res.ok(true);
79
     return res.error("mysql exited with code " + std::to_string(status));
79
     return res.error("mysql exited with code " + std::to_string(status));
80
 }
80
 }
81
+
82
+std::string MySql::getAddDomainQuery(const ActionAddDomain &action)
83
+{
84
+    auto soa = action.getSoaNs() + " " + action.getSoaMail() + " " + std::to_string(time(nullptr)) + " "
85
+               + std::to_string(action.getSoaRefresh()) + " " + std::to_string(action.getSoaRetry()) + " "
86
+               + std::to_string(action.getSoaExpire()) + " " + std::to_string(action.getSoaTtl());
87
+
88
+    return "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + action.getDomain() + "\");\n"
89
+            "INSERT INTO domains (name, type) VALUES (\"" + action.getDomain() + "\", \"MASTER\");\n"
90
+            "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + action.getDomain() + "\");\n"
91
+                   "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES (@domain_id, "
92
+                   "(SELECT id FROM users ORDER BY id LIMIT 1), 0);\n"
93
+                   "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
94
+                   "    VALUES(@domain_id, \"" + action.getDomain() + "\", \"SOA\","
95
+                   " \"" + soa + "\", " + std::to_string(action.getTtl()) + ", 0, "
96
+           + std::to_string(time(nullptr)) + ");\n";
97
+}
98
+
99
+std::string MySql::getAddHostQuery(const ActionAddHost &action)
100
+{
101
+    auto host = action.getHost().empty() ? action.getDomain() : action.getHost() + "." + action.getDomain();
102
+    auto query = "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + action.getDomain() + "\");\n"
103
+                         "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
104
+                         "    VALUES(@domain_id, \"" + host + "\", \"" + action.getRecordType() + "\","
105
+                         " \"" + action.getRecordValue() + "\", " + std::to_string(action.getTtl())
106
+                 + ", 0, " + std::to_string(time(nullptr)) + ");\n";
107
+
108
+    if (action.isReverseEnabled() && action.getRecordType() == "A")
109
+    {
110
+        auto reversedValue = action.getReversedValue();
111
+        auto reverseDomain = action.getReverseDomain().empty() ? "in-addr.arpa" :
112
+                             action.getReverseDomain() + ".in-addr.arpa";
113
+
114
+        query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
115
+                         "    VALUES((SELECT id FROM domains WHERE name=\"" + reverseDomain + "\"), \""
116
+                 + reversedValue + "." + reverseDomain + "\", \"PTR\","
117
+                         " \"" + action.getHost() + "." + action.getDomain() + "\", 84600, 0, "
118
+                 + std::to_string(time(nullptr)) + ");\n";
119
+    }
120
+
121
+    return query;
122
+}
123
+
124
+std::string MySql::getDelDomainQuery(const ActionDelDomain &action)
125
+{
126
+    return "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + action.getDomain() + "\");\n"
127
+            "DELETE FROM records WHERE domain_id=@domain_id;\n"
128
+            "DELETE FROM comments WHERE domain_id=@domain_id;\n"
129
+            "DELETE FROM zones WHERE domain_id=@domain_id;\n"
130
+            "DELETE FROM cryptokeys WHERE domain_id=@domain_id;\n"
131
+            "DELETE FROM domainmetadata WHERE domain_id=@domain_id;\n"
132
+            "DELETE FROM domains WHERE id=@domain_id;\n";
133
+}
134
+
135
+std::string MySql::getDelHostQuery(const ActionDelHost &action)
136
+{
137
+    return "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + action.getDomain() + "\");\n"
138
+            "DELETE FROM records WHERE name=\"" + action.getHost() + "." + action.getDomain() + "\" AND "
139
+            "domain_id=@domain_id;";
140
+}

+ 8
- 0
src/DataAccess/MySql.h Vedi File

19
 
19
 
20
     BResult override(const std::string& sql);
20
     BResult override(const std::string& sql);
21
 
21
 
22
+    virtual std::string getAddDomainQuery(const ActionAddDomain &action) override;
23
+
24
+    virtual std::string getAddHostQuery(const ActionAddHost &action) override;
25
+
26
+    virtual std::string getDelDomainQuery(const ActionDelDomain &action) override;
27
+
28
+    virtual std::string getDelHostQuery(const ActionDelHost &action) override;
29
+
22
 private:
30
 private:
23
     const std::string getTempFile();
31
     const std::string getTempFile();
24
 
32
 

+ 21
- 3
src/DataAccess/PgSql.cpp Vedi File

37
                                + "-U '" + _masterConfig.getUser() + "' "
37
                                + "-U '" + _masterConfig.getUser() + "' "
38
                                + "-w -c -x -O '" + _masterConfig.getDatabase() + "' "
38
                                + "-w -c -x -O '" + _masterConfig.getDatabase() + "' "
39
                                + "> '" + _dumpFilePath + "'");
39
                                + "> '" + _dumpFilePath + "'");
40
-    std::cout << command << std::endl;
41
     int status = system(command.c_str());
40
     int status = system(command.c_str());
42
     if (status == 0)
41
     if (status == 0)
43
         return res.ok(true);
42
         return res.ok(true);
75
                                + "psql -h '" + _slaveConfig.getHost() + "' "
74
                                + "psql -h '" + _slaveConfig.getHost() + "' "
76
                                + "-U '" + _slaveConfig.getUser() + "' "
75
                                + "-U '" + _slaveConfig.getUser() + "' "
77
                                + "-w -d '" + _slaveConfig.getDatabase() + "' > /dev/null");
76
                                + "-w -d '" + _slaveConfig.getDatabase() + "' > /dev/null");
78
-    std::cout << command << std::endl;
79
     int status = system(command.c_str());
77
     int status = system(command.c_str());
80
     if (status == 0)
78
     if (status == 0)
81
         return res.ok(true);
79
         return res.ok(true);
82
     return res.error("psql exited with code " + std::to_string(status));
80
     return res.error("psql exited with code " + std::to_string(status));
83
-}
81
+}
82
+
83
+std::string PgSql::getAddDomainQuery(const ActionAddDomain &action)
84
+{
85
+    return "";
86
+}
87
+
88
+std::string PgSql::getAddHostQuery(const ActionAddHost &action)
89
+{
90
+    return "";
91
+}
92
+
93
+std::string PgSql::getDelDomainQuery(const ActionDelDomain &action)
94
+{
95
+    return "";
96
+}
97
+
98
+std::string PgSql::getDelHostQuery(const ActionDelHost &action)
99
+{
100
+    return "";
101
+}

+ 9
- 0
src/DataAccess/PgSql.h Vedi File

19
 
19
 
20
     BResult override(const std::string& sql);
20
     BResult override(const std::string& sql);
21
 
21
 
22
+    virtual std::string getAddDomainQuery(const ActionAddDomain &action) override;
23
+
24
+    virtual std::string getAddHostQuery(const ActionAddHost &action) override;
25
+
26
+    virtual std::string getDelDomainQuery(const ActionDelDomain &action) override;
27
+
28
+    virtual std::string getDelHostQuery(const ActionDelHost &action) override;
29
+
30
+
22
 private:
31
 private:
23
     const std::string getTempFile();
32
     const std::string getTempFile();
24
 
33
 

Loading…
Annulla
Salva