[Pdns-users] Altering the SQL query for gmysql backend
Steven Looman
steven at krx.nl
Sat Oct 9 20:01:50 UTC 2004
Finally had some spare time and hacked some things together. No patch (yet), since I want to have comments on it and want to know if it is really going to be used.
Wrote a simple function which replaces occurences in a string:
string findAndReplace (const string& original, const string& find, const string& replace)
{
string processed = original;
string::size_type position = processed.find (find);
while (position != string::npos) {
processed = processed.replace (position, find.length(), replace);
position = processed.find (find);
}
return processed;
}
(Yes, the function can be shorter, but makes it less readable.)
Example usage (raped the coding style a little bit):
bool GSQLBackend::isMaster (const string &domain, const string &ip)
{
// char output[1024];
// snprintf(output,sizeof(output)-1,
// d_MasterOfDomainsZoneQuery.c_str(),
// sqlEscape(domain).c_str());
string output;
output = findAndReplace (d_MasterOfDomainsZoneQuery, "%domain%", sqlEscape (domain));
try {
d_db->doQuery (output.c_str(), d_result);
}
catch (SSqlException &e) {
throw AhuException ("GSQLBackend unable to retrieve list of master domains: " + e.txtReason());
}
if (d_result.empty())
return 0;
return !strcmp (ip.c_str(), d_result[0][0].c_str());
}
Note that it replaces "%domain%" and not "%s". This way you can have any order you like and use it multiple times.
I know there is no length checking (like in snprintf) for the ouput string, but the string class is dynamic and expands automatically when needed. BTW, - going to whine a bit here - why do you use strcmp, ip == d_result[0][0] would suffice and is more readable.
Any comments on this? Is it actually going to be used?
Steven
On Fri, Oct 08, 2004 at 10:26:38AM +0200, Steven Looman wrote:
> Ok, this is normal since they are using the same backend. I didn't take a look at the sources yet... Now I also know you meant something else when you asked about any ideas how to handle this. :)
>
> I guess the simplest fix would be to have a function/method that replaces certain strings. The string class in the C++ STL does provide a basic_string::replace method, which can do the trick when using %name and %type (or %n and %t as suggested by James Cloos) in combination with basic_string::find. See http://www.sgi.com/tech/stl/basic_string.html for some information.
>
> Another option would be to use a regexp library, but imo this is bloated since the first suggestion would work just fine and does not add (extra) dependencies to PowerDNS. The regexp lib is probably also slower than using basic_string::replace (but don't stick me to it ;)).
>
> I'm willing to do mess around a bit with this in my free time and send you any of my results/patches. However, as I stated before I really don't like the idea of custom patches. But maybe this can be used in a future release of PowerDNS? (tho it will break old configs which use %s)
>
> Steven Looman
>
> PS.
> Sorry for replying so much and to myself.
>
> On Fri, Oct 08, 2004 at 09:49:28AM +0200, Steven Looman wrote:
> > Same thing hapen when having more than two (in basic-query) '%s' when using the gmysql-backend.
> >
> >
> > On Fri, Oct 08, 2004 at 08:52:15AM +0200, Steven Looman wrote:
> > > Maybe it would be an idea to replace the %s with %type for type and %name for name. This way you don't need to worry about the order and makes things a bit clearer. Imo a good thing (TM).
> > >
> > > Untill now I was using the generic PostgreSQL backend. But as I suggested, I want to start using the generic MySQL backend. Yesterday I was getting tired (was staying a bit longer at work) and didn't realize i actually should be checking out the generic MySQL backend... I will do so now and post my findings.
> > >
> > > Greets,
> > >
> > > steven Looman
> > >
> > > On Thu, Oct 07, 2004 at 09:56:16PM +0200, bert hubert wrote:
> > > > On Thu, Oct 07, 2004 at 06:39:59PM +0200, Steven Looman wrote:
> > > >
> > > > > However, it does seem that powerdns cannot handle more than two (in this
> > > > > case) %s values, it crashes (signal 11).
> > > >
> > > > Indeed, I never realised people might want to add more %s's.
> > > >
> > > > > If I do something like this (just to try things, of course added \'s to the end of each (but the last) line):
> > > > > select content, ttl, prio, type, domain_id, name
> > > > > from records
> > > > > where type='%s' and name='%s' and type='%s' and name='%s'
> > > > > it also crashes (signal 11).
> > > >
> > > > You'll need to edit pdns/backends/gsql/gsqlbackend.cc a lot to get this to
> > > > work. Do you have any ideas on how PowerDNS should handle this, it needs a
> > > > specific order of %s to make this work.
> > > >
> > > > --
> > > > http://www.PowerDNS.com Open source, database driven DNS Software
> > > > http://lartc.org Linux Advanced Routing & Traffic Control HOWTO
> > > >
> > > _______________________________________________
> > > Pdns-users mailing list
> > > Pdns-users at mailman.powerdns.com
> > > http://mailman.powerdns.com/mailman/listinfo/pdns-users
> > >
> > _______________________________________________
> > Pdns-users mailing list
> > Pdns-users at mailman.powerdns.com
> > http://mailman.powerdns.com/mailman/listinfo/pdns-users
> >
> _______________________________________________
> Pdns-users mailing list
> Pdns-users at mailman.powerdns.com
> http://mailman.powerdns.com/mailman/listinfo/pdns-users
>
More information about the Pdns-users
mailing list