1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/* Copyright (C) 2003 MySQL AB
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#if defined(__WIN__) || defined(_WIN32) || defined(_WIN64)
#include <my_global.h>
#include <m_ctype.h>
#endif
#include <myx_util_public_interface.h>
#include <myx_qb_library.h>
// needed from my_global.h, cant include it because of conflicts
#ifdef __GNUC__
typedef char pchar; /* Mixed prototypes can take char */
typedef char puchar; /* Mixed prototypes can take char */
typedef char pbool; /* Mixed prototypes can take char */
typedef short pshort; /* Mixed prototypes can take short int */
typedef float pfloat; /* Mixed prototypes can take float */
#endif
/*
* functions
*/
int myx_get_qb_public_interface_version()
{
return libmysqlqb_PUBLIC_INTERFACE_VERSION;
}
static const char *explain_fields[]= {
"id", // 0
"select_type", // 1
"table", // 2
"type", // 3
"possible_keys", // 4
"key", // 5
"key_len", // 6
"ref", // 7
"rows", // 8
"Extra" // 9
};
MYX_EXPLAIN_RESULT *myx_query_explain(MYSQL *mysql, const char *query)
{
MYX_EXPLAIN_RESULT *result= NULL;
MYSQL_RES *res;
MYSQL_ROW row;
char *tmp= g_strdup_printf("EXPLAIN %s", query);
if (myx_mysql_query(mysql, tmp) < 0)
{
g_free(tmp);
return NULL;
}
g_free(tmp);
if ((res= mysql_store_result(mysql)) != NULL)
{
unsigned int i= 0;
unsigned int num_fields = mysql_num_fields(res);
MYSQL_FIELD * fields= mysql_fetch_fields(res);
int fi[10];
build_field_subst(explain_fields,explain_fields+sizeof(explain_fields)/sizeof(char*),
fields,fields+num_fields,fi);
result= g_malloc(sizeof(MYX_EXPLAIN_RESULT));
result->rows_num= mysql_num_rows(res);
result->rows= g_malloc(sizeof(MYX_EXPLAIN_ROW)*(gulong)result->rows_num);
while ((row= mysql_fetch_row(res)) != NULL)
{
MYX_EXPLAIN_ROW *expl= result->rows+i++;
#define GET_STR_FIELD(n) fi[n]==-1 ? NULL : g_strdup(row[fi[n]]?row[fi[n]]:"")
expl->id= GET_STR_FIELD(0);
expl->select_type= GET_STR_FIELD(1);
expl->table= GET_STR_FIELD(2);
expl->join_type= GET_STR_FIELD(3);
expl->possible_keys= fi[4]>=0 && row[fi[4]] ?g_strsplit(row[fi[4]], ",", -1):NULL;
expl->possible_keys_num= 0;
if (expl->possible_keys)
while (expl->possible_keys[expl->possible_keys_num])
expl->possible_keys_num++;
expl->key= GET_STR_FIELD(5);
expl->key_len= GET_STR_FIELD(6);
expl->ref= GET_STR_FIELD(7);
expl->rows= GET_STR_FIELD(8);
expl->extra= GET_STR_FIELD(9);
#undef GET_STR_FIELD
}
}
return result;
}
int myx_free_explain_result(MYX_EXPLAIN_RESULT *res)
{
unsigned int i;
for (i= 0; i < res->rows_num; i++)
{
g_free(res->rows[i].id);
g_free(res->rows[i].select_type);
g_free(res->rows[i].table);
g_free(res->rows[i].join_type);
if (res->rows[i].possible_keys)
g_strfreev(res->rows[i].possible_keys);
g_free(res->rows[i].key);
g_free(res->rows[i].key_len);
g_free(res->rows[i].ref);
g_free(res->rows[i].rows);
g_free(res->rows[i].extra);
}
g_free(res->rows);
g_free(res);
return 0;
}
|