From: Aki Tuomi Date: Wed, 5 Aug 2015 16:05:57 +0000 (+0300) Subject: Use searchRecords and searchComments in GSQL X-Git-Tag: auth-3.4.6~10^2~9 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3b88a08cc84ef12ece44b0290f1ffc0d313ac197;p=pdns Use searchRecords and searchComments in GSQL --- diff --git a/pdns/backends/gsql/gsqlbackend.cc b/pdns/backends/gsql/gsqlbackend.cc index f6b259ffc..1d995d486 100644 --- a/pdns/backends/gsql/gsqlbackend.cc +++ b/pdns/backends/gsql/gsqlbackend.cc @@ -356,6 +356,9 @@ GSQLBackend::GSQLBackend(const string &mode, const string &suffix) d_setTSIGKeyQuery = getArg("set-tsig-key-query"); d_deleteTSIGKeyQuery = getArg("delete-tsig-key-query"); d_getTSIGKeysQuery = getArg("get-tsig-keys-query"); + + d_SearchRecordsQuery = getArg("search-records-query"); + d_SearchCommentsQuery = getArg("search-comments-query"); } bool GSQLBackend::updateDNSSECOrderAndAuth(uint32_t domain_id, const std::string& zonename, const std::string& qname, bool auth) @@ -1035,27 +1038,7 @@ bool GSQLBackend::get(DNSResourceRecord &r) // L << "GSQLBackend get() was called for "<getRow(row)) { - r.content=row[0]; - if (row[1].empty()) - r.ttl = ::arg().asNum( "default-ttl" ); - else - r.ttl=atol(row[1].c_str()); - r.priority=atol(row[2].c_str()); - if(!d_qname.empty()) - r.qname=d_qname; - else - r.qname=row[6]; - r.qtype=row[3]; - r.last_modified=0; - - if(d_dnssecQueries) - r.auth = !row[7].empty() && row[7][0]=='1'; - else - r.auth = 1; - - r.disabled = !row[5].empty() && row[5][0]=='1'; - - r.domain_id=atoi(row[4].c_str()); + extractRecord(row, r); return true; } @@ -1294,13 +1277,7 @@ bool GSQLBackend::getComment(Comment& comment) } // domain_id,name,type,modified_at,account,comment - comment.domain_id = atol(row[0].c_str()); - comment.qname = row[1]; - comment.qtype = row[2]; - comment.modified_at = atol(row[3].c_str()); - comment.account = row[4]; - comment.content = row[5]; - + extractComment(row, comment); return true; } @@ -1345,3 +1322,98 @@ bool GSQLBackend::replaceComments(const uint32_t domain_id, const string& qname, return true; } + +string GSQLBackend::pattern2SQLPattern(const string &pattern) +{ + string escaped_pattern = boost::replace_all_copy(pattern,"\\","\\\\"); + boost::replace_all(escaped_pattern,"_","\\_"); + boost::replace_all(escaped_pattern,"%","\\%"); + boost::replace_all(escaped_pattern,"*","%"); + boost::replace_all(escaped_pattern,"?","_"); + return escaped_pattern; +} + +bool GSQLBackend::searchRecords(const string &pattern, int maxResults, vector& result) +{ + string escaped_pattern = pattern2SQLPattern(pattern); + string query = (GSQLformat(d_SearchRecordsQuery) + % escaped_pattern + % escaped_pattern + % maxResults).str(); + + try { + SSql::row_t row; + d_db->doQuery(query); + while(d_db->getRow(row)) { + DNSResourceRecord r; + extractRecord(row, r); + result.push_back(r); + } + return true; + } + catch (SSqlException &e) { + throw PDNSException("GSQLBackend unable to execute query: "+e.txtReason()); + } + + return false; +} + +bool GSQLBackend::searchComments(const string &pattern, int maxResults, vector& result) +{ + string escaped_pattern = pattern2SQLPattern(pattern); + string query = (GSQLformat(d_SearchCommentsQuery) + % escaped_pattern + % escaped_pattern + % maxResults).str(); + + try { + SSql::row_t row; + d_db->doQuery(query); + while(d_db->getRow(row)) { + Comment c; + extractComment(row, c); + result.push_back(c); + } + return true; + } + catch (SSqlException &e) { + throw PDNSException("GSQLBackend unable to execute query: "+e.txtReason()); + } + + return false; +} + +void GSQLBackend::extractRecord(const SSql::row_t& row, DNSResourceRecord& r) +{ + r.content=row[0]; + if (row[1].empty()) + r.ttl = ::arg().asNum( "default-ttl" ); + else + r.ttl=atol(row[1].c_str()); + r.priority=atol(row[2].c_str()); + if(!d_qname.empty()) + r.qname=d_qname; + else + r.qname=row[6]; + r.qtype=row[3]; + r.last_modified=0; + + if(d_dnssecQueries) + r.auth = !row[7].empty() && row[7][0]=='1'; + else + r.auth = 1; + + r.disabled = !row[5].empty() && row[5][0]=='1'; + + r.domain_id=atoi(row[4].c_str()); +} + +void GSQLBackend::extractComment(const SSql::row_t& row, Comment& comment) +{ + comment.domain_id = atol(row[0].c_str()); + comment.qname = row[1]; + comment.qtype = row[2]; + comment.modified_at = atol(row[3].c_str()); + comment.account = row[4]; + comment.content = row[5]; +} diff --git a/pdns/backends/gsql/gsqlbackend.hh b/pdns/backends/gsql/gsqlbackend.hh index 246ce5100..792460c5e 100644 --- a/pdns/backends/gsql/gsqlbackend.hh +++ b/pdns/backends/gsql/gsqlbackend.hh @@ -88,6 +88,13 @@ public: void feedComment(const Comment& comment); bool replaceComments(const uint32_t domain_id, const string& qname, const QType& qt, const vector& comments); + bool searchRecords(const string &pattern, int maxResults, vector& result); + bool searchComments(const string &pattern, int maxResults, vector& result); + +protected: + string pattern2SQLPattern(const string& pattern); + void extractRecord(const SSql::row_t& row, DNSResourceRecord& rr); + void extractComment(const SSql::row_t& row, Comment& comment); private: string d_qname; SSql *d_db; @@ -165,6 +172,9 @@ private: string d_DeleteCommentRRsetQuery; string d_DeleteCommentsQuery; + string d_SearchRecordsQuery; + string d_SearchCommentsQuery; + protected: bool d_dnssecQueries; };