int prepare_schema_table(THD *thd, LEX *lex, Table_ident *table_ident,
enum enum_schema_tables schema_table_idx)
{
...
DBUG_ENTER("prepare_schema_table");
switch (schema_table_idx) {
/* BEGIN CAB MODIFICATION */
/* Reason for Modification: */
/* Add case statement for the new SHOW DISK_USAGE view. */
case SCH_DISKUSAGE:
/* END CAB MODIFICATION */
case SCH_SCHEMATA:
#if defined(DONT_ALLOW_SHOW_COMMANDS)
my_message(ER_NOT_ALLOWED_COMMAND,
修改 sql_show.cc , 在 ST_FIELD_INFO schema_privileges_fields_info 后面添加
/* BEGIN CAB MODIFICATION */
/* Reason for Modification: */
/* This section adds the code to call the new SHOW DISK_USAGE command. */
ST_FIELD_INFO disk_usage_fields_info[]=
{
{"DATABASE", 40, MYSQL_TYPE_STRING, 0, 0, NULL, SKIP_OPEN_TABLE},
{"Size_in_bytes", 21 , MYSQL_TYPE_LONG, 0, 0, NULL, SKIP_OPEN_TABLE },
{0, 0, MYSQL_TYPE_STRING, 0, 0, 0, SKIP_OPEN_TABLE}
};
/* END CAB MODIFICATION */
修改 sql_show.cc sql_show.cc 中的 schema_tables
ST_SCHEMA_TABLE schema_tables[]=
{
...
{"ENGINES", engines_fields_info, create_schema_table,
fill_schema_engines, make_old_format, 0, -1, -1, 0, 0},
/* BEGIN CAB MODIFICATION */
/* Reason for Modification: */
/* This section adds the code to call the new SHOW DISK_USAGE command. */
{"DISKUSAGE", disk_usage_fields_info, create_schema_table,
fill_disk_usage, make_old_format, 0, -1, -1, 0, 0},
/* END CAB MODIFICATION */
#ifdef HAVE_EVENT_SCHEDULER
{"EVENTS", events_fields_info, create_schema_table,
fill_schema_events, make_old_format, 0, -1, -1, 0, 0},
修改 sql_show.cc , 在schema_tables之前的地方添加实现方法
/* BEGIN CAB MODIFICATION */
/* Reason for Modification: */
/* Add code to fill the output for the new SHOW DISK_USAGE view. */
int fill_disk_usage(THD *thd, TABLE_LIST *tables, Item *cond)
{
TABLE *table= tables->table;
CHARSET_INFO *scs= system_charset_info;
List<Item> field_list;
List<LEX_STRING> dbs;
LEX_STRING *db_name;
char *path;
MY_DIR *dirp;
FILEINFO *file;
longlong fsizes = 0;
longlong lsizes = 0;
DBUG_ENTER("fill_disk_usage");
find_files_result res = find_files(thd, &dbs, 0, mysql_data_home,0,1);
if (res != FIND_FILES_OK)
DBUG_RETURN(1);
List_iterator_fast<LEX_STRING> it_dbs(dbs);
path = (char *)my_malloc(PATH_MAX, MYF(MY_ZEROFILL));
dirp = my_dir(mysql_data_home, MYF(MY_WANT_STAT));
fsizes = 0;
for (int i = 0; i < (int)dirp->number_off_files; i++)
{
file = dirp->dir_entry + i;
if (strncasecmp(file->name, "ibdata", 6) == 0)
fsizes = fsizes + file->mystat->st_size;
else if (strncasecmp(file->name, "ib", 2) == 0)
lsizes = lsizes + file->mystat->st_size;
}
/* send InnoDB data to client */
table->field[0]->store("InnoDB TableSpace",
strlen("InnoDB TableSpace"), scs);
table->field[1]->store((longlong)fsizes, TRUE);
if (schema_table_store_record(thd, table))
DBUG_RETURN(1);
table->field[0]->store("InnoDB Logs", strlen("InnoDB Logs"), scs);
table->field[1]->store((longlong)lsizes, TRUE);
if (schema_table_store_record(thd, table))
DBUG_RETURN(1);
/* now send database name and sizes of the databases */
while ((db_name = it_dbs++))
{
fsizes = 0;
strcpy(path, mysql_data_home);
strcat(path, "/");
strcat(path, db_name->str);
dirp = my_dir(path, MYF(MY_WANT_STAT));
for (int i = 0; i < (int)dirp->number_off_files; i++)
{
file = dirp->dir_entry + i;
fsizes = fsizes + file->mystat->st_size;
}
restore_record(table, s->default_values);
table->field[0]->store(db_name->str, db_name->length, scs);
table->field[1]->store((longlong)fsizes, TRUE);
if (schema_table_store_record(thd, table))
DBUG_RETURN(1);
}
/* free memory */
my_free(path);
DBUG_RETURN(0);
}
/* END CAB MODIFICATION */