This commit is contained in:
VilhoRaatikka
2014-11-06 20:57:37 +02:00
8 changed files with 82 additions and 22 deletions

View File

@ -141,7 +141,7 @@ const char *progname = NULL;
static struct option long_options[] = {
{"homedir", required_argument, 0, 'c'},
{"config", required_argument, 0, 'f'},
{"nodeamon", required_argument, 0, 'd'},
{"nodaemon", no_argument, 0, 'd'},
{"log", required_argument, 0, 'l'},
{"version", no_argument, 0, 'v'},
{"help", no_argument, 0, '?'},

View File

@ -175,6 +175,51 @@ GWBUF *addition;
return orig;
}
/**
* Extract the SQL from a COM_QUERY packet and return in a NULL terminated buffer.
* The buffer shoudl be freed by the caller when it is no longer required.
*
* If the packet is not a COM_QUERY packet then the function will return NULL
*
* @param buf The buffer chain
* @return Null terminated string containing query text or NULL on error
*/
char *
modutil_get_SQL(GWBUF *buf)
{
unsigned int len, length;
unsigned char *ptr, *dptr, *rval = NULL;
if (!modutil_is_SQL(buf))
return rval;
ptr = GWBUF_DATA(buf);
length = *ptr++;
length += (*ptr++ << 8);
length += (*ptr++ << 16);
if ((rval = (char *)malloc(length + 1)) == NULL)
return NULL;
dptr = rval;
ptr += 2; // Skip sequence id and COM_QUERY byte
len = GWBUF_LENGTH(buf) - 5;
while (buf && length > 0)
{
int clen = length > len ? len : length;
memcpy(dptr, ptr, clen);
dptr += clen;
length -= clen;
buf = buf->next;
if (buf)
{
ptr = GWBUF_DATA(buf);
len = GWBUF_LENGTH(buf);
}
}
*dptr = 0;
return rval;
}
/**
* Copy query string from GWBUF buffer to separate memory area.
*

View File

@ -37,6 +37,7 @@
extern int modutil_is_SQL(GWBUF *);
extern int modutil_extract_SQL(GWBUF *, char **, int *);
extern int modutil_MySQL_Query(GWBUF *, char **, int *, int *);
extern char *modutil_get_SQL(GWBUF *);
extern GWBUF *modutil_replace_SQL(GWBUF *, char *);
extern char *modutil_get_query(GWBUF* buf);
extern int modutil_send_mysql_err_packet(DCB *, int, int, int, const char *, const char *);

View File

@ -202,7 +202,7 @@ int i;
free(my_instance->filebase);
my_instance->filebase = NULL;
}
my_instance->source = strdup(params[i]->value);
my_instance->filebase = strdup(params[i]->value);
}
else if (!filter_standard_parameter(params[i]->name))
{
@ -408,7 +408,7 @@ struct timeval tv;
{
queue = gwbuf_make_contiguous(queue);
}
if (modutil_extract_SQL(queue, &ptr, &length) != 0)
if ((ptr = modutil_get_SQL(queue)) != NULL)
{
if ((my_instance->match == NULL ||
regexec(&my_instance->re, ptr, 0, NULL, 0) == 0) &&
@ -424,6 +424,7 @@ struct timeval tv;
fwrite(ptr, sizeof(char), length, my_session->fp);
fwrite("\n", sizeof(char), 1, my_session->fp);
}
free(ptr);
}
}
/* Pass the query downstream */

View File

@ -310,7 +310,8 @@ int length;
{
queue = gwbuf_make_contiguous(queue);
}
modutil_extract_SQL(queue, &sql, &length);
if ((sql = modutil_get_SQL(queue)) != NULL)
{
newsql = regex_replace(sql, length, &my_instance->re,
my_instance->replace);
if (newsql)
@ -322,6 +323,8 @@ int length;
}
else
my_session->no_change++;
free(sql);
}
}
return my_session->down.routeQuery(my_session->down.instance,

View File

@ -403,14 +403,16 @@ GWBUF *clone = NULL;
if (my_session->residual < 0)
my_session->residual = 0;
}
else if (my_session->active &&
modutil_MySQL_Query(queue, &ptr, &length, &residual))
else if (my_session->active && (ptr = modutil_get_SQL(queue) != NULL))
{
if ((my_instance->match == NULL ||
regexec(&my_instance->re, ptr, 0, NULL, 0) == 0) &&
(my_instance->nomatch == NULL ||
regexec(&my_instance->nore,ptr,0,NULL, 0) != 0))
{
char *dummy;
modutil_MySQL_Query(queue, &dummy, &length, &residual);
clone = gwbuf_clone(queue);
my_session->residual = residual;
}

View File

@ -453,7 +453,6 @@ routeQuery(FILTER *instance, void *session, GWBUF *queue)
TOPN_INSTANCE *my_instance = (TOPN_INSTANCE *)instance;
TOPN_SESSION *my_session = (TOPN_SESSION *)session;
char *ptr;
int length;
if (my_session->active)
{
@ -461,7 +460,7 @@ int length;
{
queue = gwbuf_make_contiguous(queue);
}
if (modutil_extract_SQL(queue, &ptr, &length) != 0)
if ((ptr = modutil_get_SQL(queue)) != NULL)
{
if ((my_instance->match == NULL ||
regexec(&my_instance->re, ptr, 0, NULL, 0) == 0) &&
@ -472,7 +471,11 @@ int length;
if (my_session->current)
free(my_session->current);
gettimeofday(&my_session->start, NULL);
my_session->current = strndup(ptr, length);
my_session->current = ptr;
}
else
{
free(ptr);
}
}
}

View File

@ -1406,7 +1406,7 @@ void check_drop_tmp_table(
{
int tsize = 0, klen = 0,i;
char** tbl;
char** tbl = NULL;
char *hkey,*dbname;
MYSQL_session* data;
@ -1447,9 +1447,11 @@ void check_drop_tmp_table(
free(tbl[i]);
free(hkey);
}
if(tbl != NULL){
free(tbl);
}
}
}
/**
* Check if the query targets a temporary table.
@ -1468,7 +1470,7 @@ skygw_query_type_t is_read_tmp_table(
bool target_tmp_table = false;
int tsize = 0, klen = 0,i;
char** tbl;
char** tbl = NULL;
char *hkey,*dbname;
MYSQL_session* data;
@ -1529,7 +1531,10 @@ skygw_query_type_t is_read_tmp_table(
{
free(tbl[i]);
}
if(tbl != NULL){
free(tbl);
}
return qtype;
}