Quellcode durchsuchen

sql queries

develop
Robin Thoni vor 8 Jahren
Ursprung
Commit
4f73c26b35

+ 2
- 0
src/Business/PdnsSlave.cpp Datei anzeigen

50
     if (!res)
50
     if (!res)
51
         return res;
51
         return res;
52
     _actions = res.getData();
52
     _actions = res.getData();
53
+    for (auto action : _actions)
54
+        std::cout << action->getSqlQuery() << std::endl << std::endl;
53
     return res;
55
     return res;
54
 }
56
 }

+ 5
- 0
src/DBO/Actions/Action.cpp Datei anzeigen

21
 {
21
 {
22
     _domain = domain;
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 Datei anzeigen

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

+ 3
- 1
src/DBO/Actions/ActionAddDomain.cpp Datei anzeigen

10
 
10
 
11
 const std::string ActionAddDomain::getSql() const
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 Datei anzeigen

12
 public:
12
 public:
13
     ActionAddDomain();
13
     ActionAddDomain();
14
 
14
 
15
+protected:
15
     const std::string getSql() const override;
16
     const std::string getSql() const override;
16
 };
17
 };
17
 
18
 

+ 53
- 2
src/DBO/Actions/ActionAddHost.cpp Datei anzeigen

1
 //
1
 //
2
 // Created by robin on 8/9/15.
2
 // Created by robin on 8/9/15.
3
 //
3
 //
4
-
4
+#include <string>
5
+#include <sstream>
6
+#include <vector>
5
 #include "ActionAddHost.h"
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
 ActionAddHost::ActionAddHost()
24
 ActionAddHost::ActionAddHost()
8
 {
25
 {
9
 }
26
 }
10
 
27
 
11
 const std::string ActionAddHost::getSql() const
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
 const std::string &ActionAddHost::getHost() const
48
 const std::string &ActionAddHost::getHost() const
71
 {
103
 {
72
     _reverseEnabled = reverseEnabled;
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 Datei anzeigen

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

+ 6
- 1
src/DBO/Actions/ActionDelDomain.cpp Datei anzeigen

10
 
10
 
11
 const std::string ActionDelDomain::getSql() const
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 Datei anzeigen

12
 public:
12
 public:
13
     ActionDelDomain();
13
     ActionDelDomain();
14
 
14
 
15
+protected:
15
     const std::string getSql() const override;
16
     const std::string getSql() const override;
16
 };
17
 };
17
 
18
 

+ 2
- 1
src/DBO/Actions/ActionDelHost.cpp Datei anzeigen

10
 
10
 
11
 const std::string ActionDelHost::getSql() const
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
 const std::string &ActionDelHost::getHost() const
17
 const std::string &ActionDelHost::getHost() const

+ 3
- 2
src/DBO/Actions/ActionDelHost.h Datei anzeigen

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

Laden…
Abbrechen
Speichern