Browse Source

sql queries

develop
Robin Thoni 9 years ago
parent
commit
4f73c26b35

+ 2
- 0
src/Business/PdnsSlave.cpp View File

@@ -50,5 +50,7 @@ Result<Actions> PdnsSlave::readHosts()
50 50
     if (!res)
51 51
         return res;
52 52
     _actions = res.getData();
53
+    for (auto action : _actions)
54
+        std::cout << action->getSqlQuery() << std::endl << std::endl;
53 55
     return res;
54 56
 }

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

@@ -21,3 +21,8 @@ void Action::setDomain(const std::string &domain)
21 21
 {
22 22
     _domain = domain;
23 23
 }
24
+
25
+const std::string Action::getSqlQuery() const
26
+{
27
+    return "SET @domain_id=(SELECT get_domain_id(\"" + _domain + "\"));\n" + getSql();
28
+}

+ 4
- 2
src/DBO/Actions/Action.h View File

@@ -15,13 +15,15 @@ public:
15 15
     Action();
16 16
     virtual ~Action();
17 17
 
18
-    virtual const std::string getSql() const = 0;
18
+    const std::string getSqlQuery() const;
19 19
 
20 20
     const std::string &getDomain() const;
21 21
 
22 22
     void setDomain(const std::string &domain);
23 23
 
24
-private:
24
+protected:
25
+    virtual const std::string getSql() const = 0;
26
+
25 27
     std::string _domain;
26 28
 };
27 29
 

+ 3
- 1
src/DBO/Actions/ActionAddDomain.cpp View File

@@ -10,5 +10,7 @@ ActionAddDomain::ActionAddDomain()
10 10
 
11 11
 const std::string ActionAddDomain::getSql() const
12 12
 {
13
-    return "";
13
+    return "INSERT INTO domains (name, type) VALUES (\"" + _domain + "\", \"MASTER\");\n"
14
+            "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES ((SELECT get_domain_id(\""
15
+           + _domain + "\")), (SELECT id FROM users ORDER BY id LIMIT 1), 0);";
14 16
 }

+ 1
- 0
src/DBO/Actions/ActionAddDomain.h View File

@@ -12,6 +12,7 @@ class ActionAddDomain : public Action
12 12
 public:
13 13
     ActionAddDomain();
14 14
 
15
+protected:
15 16
     const std::string getSql() const override;
16 17
 };
17 18
 

+ 53
- 2
src/DBO/Actions/ActionAddHost.cpp View File

@@ -1,16 +1,48 @@
1 1
 //
2 2
 // Created by robin on 8/9/15.
3 3
 //
4
-
4
+#include <string>
5
+#include <sstream>
6
+#include <vector>
5 7
 #include "ActionAddHost.h"
6 8
 
9
+std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems) {
10
+    std::stringstream ss(s);
11
+    std::string item;
12
+    while (std::getline(ss, item, delim)) {
13
+        elems.push_back(item);
14
+    }
15
+    return elems;
16
+}
17
+
18
+std::vector<std::string> split(const std::string &s, char delim) {
19
+    std::vector<std::string> elems;
20
+    split(s, delim, elems);
21
+    return elems;
22
+}
23
+
7 24
 ActionAddHost::ActionAddHost()
8 25
 {
9 26
 }
10 27
 
11 28
 const std::string ActionAddHost::getSql() const
12 29
 {
13
-    return "";
30
+    auto query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
31
+            "    VALUES(@domain_id, \"" + _host + "." + _domain + "\", \"" + _recordType + "\","
32
+            " \"" + _recordValue + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
33
+
34
+    if (_reverseEnabled && _recordType == "A")
35
+    {
36
+        auto reversedValue = getReversedValue();
37
+        auto reverseDomain = _reverseDomain.empty() ? "in-addr.arpa" : _reverseDomain + ".in-addr.arpa";
38
+
39
+        query += "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
40
+                 "    VALUES((SELECT get_domain_id(\"" + reverseDomain + "\")), \""
41
+                 + reversedValue + "." + reverseDomain + "\", \"PTR\","
42
+                 " \"" + _host + "." + _domain + "\", 84600, 0, " + std::to_string(time(nullptr)) + ");\n";
43
+    }
44
+
45
+    return query;
14 46
 }
15 47
 
16 48
 const std::string &ActionAddHost::getHost() const
@@ -71,3 +103,22 @@ void ActionAddHost::setReverseEnabled(bool reverseEnabled)
71 103
 {
72 104
     _reverseEnabled = reverseEnabled;
73 105
 }
106
+
107
+const std::string ActionAddHost::getReversedValue() const
108
+{
109
+    auto splitValue = split(_recordValue, '.');
110
+    int index = 1;
111
+
112
+    for (; index < splitValue.size(); ++index)
113
+    {
114
+        std::string reversedValue;
115
+        for (int i = (int) splitValue.size() - 1 - index; i >= 0; --i)
116
+            reversedValue += splitValue[i] + (i == 0 ? "" : ".");
117
+        if (_reverseDomain == reversedValue)
118
+            break;
119
+    }
120
+    std::string reversedValue;
121
+    for (int i = (int) splitValue.size() - 1; i >= (int) splitValue.size() - index; --i)
122
+        reversedValue += std::string(splitValue[i]) + (i == (int) splitValue.size() - index ? "" : ".");
123
+    return reversedValue;
124
+}

+ 5
- 2
src/DBO/Actions/ActionAddHost.h View File

@@ -12,8 +12,6 @@ class ActionAddHost : public Action
12 12
 public:
13 13
     ActionAddHost();
14 14
 
15
-    const std::string getSql() const override;
16
-
17 15
     const std::string &getHost() const;
18 16
 
19 17
     void setHost(const std::string &host);
@@ -38,7 +36,12 @@ public:
38 36
 
39 37
     void setReverseEnabled(bool reverseEnabled);
40 38
 
39
+protected:
40
+    const std::string getSql() const override;
41
+
41 42
 private:
43
+    const std::string getReversedValue() const;
44
+
42 45
     std::string _host;
43 46
 
44 47
     std::string _recordValue;

+ 6
- 1
src/DBO/Actions/ActionDelDomain.cpp View File

@@ -10,5 +10,10 @@ ActionDelDomain::ActionDelDomain()
10 10
 
11 11
 const std::string ActionDelDomain::getSql() const
12 12
 {
13
-    return "";
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 19
 }

+ 1
- 0
src/DBO/Actions/ActionDelDomain.h View File

@@ -12,6 +12,7 @@ class ActionDelDomain : public Action
12 12
 public:
13 13
     ActionDelDomain();
14 14
 
15
+protected:
15 16
     const std::string getSql() const override;
16 17
 };
17 18
 

+ 2
- 1
src/DBO/Actions/ActionDelHost.cpp View File

@@ -10,7 +10,8 @@ ActionDelHost::ActionDelHost()
10 10
 
11 11
 const std::string ActionDelHost::getSql() const
12 12
 {
13
-    return "";
13
+    return "DELETE FROM records WHERE name=\"" + _host + "." + _domain + "\" AND "
14
+           "domain_id=@domain_id;";
14 15
 }
15 16
 
16 17
 const std::string &ActionDelHost::getHost() const

+ 3
- 2
src/DBO/Actions/ActionDelHost.h View File

@@ -12,12 +12,13 @@ class ActionDelHost : public Action
12 12
 public:
13 13
     ActionDelHost();
14 14
 
15
-    const std::string getSql() const override;
16
-
17 15
     const std::string &getHost() const;
18 16
 
19 17
     void setHost(const std::string &host);
20 18
 
19
+protected:
20
+    const std::string getSql() const override;
21
+
21 22
 private:
22 23
     std::string _host;
23 24
 };

Loading…
Cancel
Save