Pārlūkot izejas kodu

using sql backends

develop
Robin Thoni 8 gadus atpakaļ
vecāks
revīzija
429b09bb36

+ 1
- 1
src/Business/PdnsSlave.cpp Parādīt failu

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

+ 0
- 5
src/DBO/Actions/Action.cpp Parādīt failu

@@ -22,11 +22,6 @@ void Action::setDomain(const std::string &domain)
22 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 25
 const std::string Action::getDhcpConf() const
31 26
 {
32 27
     return getDhcp();

+ 6
- 5
src/DBO/Actions/Action.h Parādīt failu

@@ -5,9 +5,11 @@
5 5
 #ifndef PDNS_SLAVE_ACTION_H
6 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 14
 class Action
13 15
 {
@@ -17,14 +19,13 @@ public:
17 19
 
18 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 24
     const std::string &getDomain() const;
23 25
 
24 26
     void setDomain(const std::string &domain);
25 27
 
26 28
 protected:
27
-    virtual const std::string getSql() const = 0;
28 29
 
29 30
     virtual const std::string getDhcp() const;
30 31
 

+ 3
- 11
src/DBO/Actions/ActionAddDomain.cpp Parādīt failu

@@ -3,6 +3,7 @@
3 3
 //
4 4
 
5 5
 #include "ActionAddDomain.h"
6
+#include "../../DataAccess/AbstractSql.h"
6 7
 
7 8
 ActionAddDomain::ActionAddDomain()
8 9
 {
@@ -78,16 +79,7 @@ void ActionAddDomain::setTtl(int ttl)
78 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 Parādīt failu

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

+ 3
- 18
src/DBO/Actions/ActionAddHost.cpp Parādīt failu

@@ -5,6 +5,7 @@
5 5
 #include <sstream>
6 6
 #include <vector>
7 7
 #include "ActionAddHost.h"
8
+#include "../../DataAccess/AbstractSql.h"
8 9
 
9 10
 std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
10 11
     std::stringstream ss(s);
@@ -25,25 +26,9 @@ ActionAddHost::ActionAddHost()
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 34
 const std::string &ActionAddHost::getHost() const

+ 3
- 2
src/DBO/Actions/ActionAddHost.h Parādīt failu

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

+ 3
- 7
src/DBO/Actions/ActionDelDomain.cpp Parādīt failu

@@ -3,17 +3,13 @@
3 3
 //
4 4
 
5 5
 #include "ActionDelDomain.h"
6
+#include "../../DataAccess/AbstractSql.h"
6 7
 
7 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 Parādīt failu

@@ -13,7 +13,7 @@ public:
13 13
     ActionDelDomain();
14 14
 
15 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 Parādīt failu

@@ -3,15 +3,15 @@
3 3
 //
4 4
 
5 5
 #include "ActionDelHost.h"
6
+#include "../../DataAccess/AbstractSql.h"
6 7
 
7 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 17
 const std::string &ActionDelHost::getHost() const

+ 1
- 1
src/DBO/Actions/ActionDelHost.h Parādīt failu

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

+ 12
- 0
src/DataAccess/AbstractSql.h Parādīt failu

@@ -7,6 +7,10 @@
7 7
 
8 8
 #include <DBO/Result.h>
9 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 14
 #include "DBO/Result.h"
11 15
 
12 16
 class AbstractSql
@@ -21,6 +25,14 @@ public:
21 25
 
22 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 36
 protected:
25 37
     SqlConfiguration _masterConfig;
26 38
 

+ 60
- 0
src/DataAccess/MySql.cpp Parādīt failu

@@ -78,3 +78,63 @@ BResult MySql::insertSlave(const std::string &file)
78 78
         return res.ok(true);
79 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 Parādīt failu

@@ -19,6 +19,14 @@ public:
19 19
 
20 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 30
 private:
23 31
     const std::string getTempFile();
24 32
 

+ 21
- 3
src/DataAccess/PgSql.cpp Parādīt failu

@@ -37,7 +37,6 @@ BResult PgSql::dump()
37 37
                                + "-U '" + _masterConfig.getUser() + "' "
38 38
                                + "-w -c -x -O '" + _masterConfig.getDatabase() + "' "
39 39
                                + "> '" + _dumpFilePath + "'");
40
-    std::cout << command << std::endl;
41 40
     int status = system(command.c_str());
42 41
     if (status == 0)
43 42
         return res.ok(true);
@@ -75,9 +74,28 @@ BResult PgSql::insertSlave(const std::string &file)
75 74
                                + "psql -h '" + _slaveConfig.getHost() + "' "
76 75
                                + "-U '" + _slaveConfig.getUser() + "' "
77 76
                                + "-w -d '" + _slaveConfig.getDatabase() + "' > /dev/null");
78
-    std::cout << command << std::endl;
79 77
     int status = system(command.c_str());
80 78
     if (status == 0)
81 79
         return res.ok(true);
82 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 Parādīt failu

@@ -19,6 +19,15 @@ public:
19 19
 
20 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 31
 private:
23 32
     const std::string getTempFile();
24 33
 

Notiek ielāde…
Atcelt
Saglabāt