// no actual readers right now, select the first available writer using the common
// ordering 'weight DESC, hostname DESC, port DESC' and use it as a reader.
if(num_readers==0){
q=(char*)"INSERT OR REPLACE INTO mysql_servers_incoming (hostgroup_id,hostname,port,gtid_port,status,weight,compression,max_connections,max_replication_lag,use_ssl,max_latency_ms,comment) SELECT %d,hostname,port,gtid_port,status,weight,compression,max_connections,max_replication_lag,use_ssl,max_latency_ms,comment FROM mysql_servers_incoming WHERE hostgroup_id=%d AND status=0 ORDER BY weight DESC, hostname DESC, port DESC LIMIT 1";
// there are readers available, we should remove the previously placed writer
// from the reader hostgroup.
q=(char*)"DELETE FROM mysql_servers_incoming WHERE (hostgroup_id, hostname, port) IN (SELECT r.hostgroup_id,r.hostname,r.port FROM mysql_servers_incoming r JOIN (SELECT hostname,port FROM mysql_servers_incoming WHERE hostgroup_id=%d AND status=0 ORDER BY weight DESC, hostname DESC, port DESC) s ON r.hostname=s.hostname AND r.port=s.port WHERE r.hostgroup_id=%d)";