Browse Source

pgsql backend

develop
Robin Thoni 8 years ago
parent
commit
08d8a407c8
3 changed files with 44 additions and 8 deletions
  1. 2
    2
      conf/hosts.json.default
  2. 1
    2
      src/DataAccess/MySql.cpp
  3. 41
    4
      src/DataAccess/PgSql.cpp

+ 2
- 2
conf/hosts.json.default View File

6
     "reverse_enabled": true,
6
     "reverse_enabled": true,
7
     "record_type": "A",
7
     "record_type": "A",
8
     "dhcp_mac": "",
8
     "dhcp_mac": "",
9
-    "record_ttl": 84600,
9
+    "record_ttl": 600,
10
     "soa_ns": "ns.example.com",
10
     "soa_ns": "ns.example.com",
11
     "soa_mail": "root.example.com",
11
     "soa_mail": "root.example.com",
12
-    "soa_refresh": 86400,
12
+    "soa_refresh": 600,
13
     "soa_retry": 7200,
13
     "soa_retry": 7200,
14
     "soa_expire": 3600000,
14
     "soa_expire": 3600000,
15
     "soa_ttl": 172800
15
     "soa_ttl": 172800

+ 1
- 2
src/DataAccess/MySql.cpp View File

85
                + std::to_string(action.getSoaRefresh()) + " " + std::to_string(action.getSoaRetry()) + " "
85
                + std::to_string(action.getSoaRefresh()) + " " + std::to_string(action.getSoaRetry()) + " "
86
                + std::to_string(action.getSoaExpire()) + " " + std::to_string(action.getSoaTtl());
86
                + std::to_string(action.getSoaExpire()) + " " + std::to_string(action.getSoaTtl());
87
 
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"
88
+    return "INSERT INTO domains (name, type) VALUES (\"" + action.getDomain() + "\", \"MASTER\");\n"
90
             "SET @domain_id=(SELECT id FROM domains WHERE name=\"" + action.getDomain() + "\");\n"
89
             "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, "
90
                    "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES (@domain_id, "
92
                    "(SELECT id FROM users ORDER BY id LIMIT 1), 0);\n"
91
                    "(SELECT id FROM users ORDER BY id LIMIT 1), 0);\n"

+ 41
- 4
src/DataAccess/PgSql.cpp View File

82
 
82
 
83
 std::string PgSql::getAddDomainQuery(const ActionAddDomain &action)
83
 std::string PgSql::getAddDomainQuery(const ActionAddDomain &action)
84
 {
84
 {
85
-    return "";
85
+    auto soa = action.getSoaNs() + " " + action.getSoaMail() + " " + std::to_string(time(nullptr)) + " "
86
+               + std::to_string(action.getSoaRefresh()) + " " + std::to_string(action.getSoaRetry()) + " "
87
+               + std::to_string(action.getSoaExpire()) + " " + std::to_string(action.getSoaTtl());
88
+
89
+    return "INSERT INTO domains (name, type) VALUES (\'" + action.getDomain() + "\', \'MASTER\');\n"
90
+                   "INSERT INTO zones (domain_id, owner, zone_templ_id) VALUES "
91
+                   "((SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\'), "
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((SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\'), \'"
95
+           + action.getDomain() + "\', \'SOA\', \'" + soa + "\', " + std::to_string(action.getTtl()) + ", 0, "
96
+           + std::to_string(time(nullptr)) + ");\n";
86
 }
97
 }
87
 
98
 
88
 std::string PgSql::getAddHostQuery(const ActionAddHost &action)
99
 std::string PgSql::getAddHostQuery(const ActionAddHost &action)
89
 {
100
 {
90
-    return "";
101
+    auto host = action.getHost().empty() ? action.getDomain() : action.getHost() + "." + action.getDomain();
102
+    auto query = "INSERT INTO records (domain_id, name, type, content, ttl, prio, change_date)\n"
103
+                 "    VALUES((SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\'), \'"
104
+                 + 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\', \'" + action.getHost()
117
+                 + "." + action.getDomain() + "\', " + std::to_string(action.getTtl()) + ", 0, "
118
+                 + std::to_string(time(nullptr)) + ");\n";
119
+    }
120
+
121
+    return query;
91
 }
122
 }
92
 
123
 
93
 std::string PgSql::getDelDomainQuery(const ActionDelDomain &action)
124
 std::string PgSql::getDelDomainQuery(const ActionDelDomain &action)
94
 {
125
 {
95
-    return "";
126
+    return "DELETE FROM records WHERE domain_id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');\n"
127
+           "DELETE FROM comments WHERE domain_id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');\n"
128
+           "DELETE FROM zones WHERE domain_id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');\n"
129
+           "DELETE FROM cryptokeys WHERE domain_id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');\n"
130
+           "DELETE FROM domainmetadata WHERE domain_id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');\n"
131
+           "DELETE FROM domains WHERE id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');\n";
96
 }
132
 }
97
 
133
 
98
 std::string PgSql::getDelHostQuery(const ActionDelHost &action)
134
 std::string PgSql::getDelHostQuery(const ActionDelHost &action)
99
 {
135
 {
100
-    return "";
136
+    return "DELETE FROM records WHERE name=\'" + action.getHost() + "." + action.getDomain() + "\' AND "
137
+            "domain_id=(SELECT id FROM domains WHERE name=\'" + action.getDomain() + "\');";
101
 }
138
 }

Loading…
Cancel
Save