sql_help.cc 21.6 KB
Newer Older
unknown's avatar
unknown committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* Copyright (C) 2000 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 "mysql_priv.h"

19 20 21 22 23
struct st_find_field
{
  const char *table_name, *field_name;
  Field *field;
};
unknown's avatar
unknown committed
24

25 26 27 28
/* Used fields */

static struct st_find_field init_used_fields[]=
{
unknown's avatar
unknown committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  { "help_topic",    "help_topic_id",      0},
  { "help_topic",    "name",               0},
  { "help_topic",    "help_category_id",   0},
  { "help_topic",    "description",        0},
  { "help_topic",    "example",            0},

  { "help_category", "help_category_id",   0},
  { "help_category", "parent_category_id", 0},
  { "help_category", "name",               0},

  { "help_keyword",  "help_keyword_id",    0},
  { "help_keyword",  "name",               0},

  { "help_relation", "help_topic_id",      0},
  { "help_relation", "help_keyword_id",    0}
44 45 46 47
};

enum enum_used_fields
{
unknown's avatar
unknown committed
48
  help_topic_help_topic_id= 0,
unknown's avatar
unknown committed
49
  help_topic_name,
unknown's avatar
unknown committed
50
  help_topic_help_category_id,
unknown's avatar
unknown committed
51
  help_topic_description,
unknown's avatar
unknown committed
52 53 54 55 56 57 58 59 60
  help_topic_example,

  help_category_help_category_id,
  help_category_parent_category_id,
  help_category_name,

  help_keyword_help_keyword_id,
  help_keyword_name,

unknown's avatar
unknown committed
61
  help_relation_help_topic_id,
unknown's avatar
unknown committed
62
  help_relation_help_keyword_id
63
};
unknown's avatar
unknown committed
64

unknown's avatar
unknown committed
65

66
/*
unknown's avatar
unknown committed
67
  Fill st_find_field structure with pointers to fields
unknown's avatar
unknown committed
68 69 70 71 72 73 74 75 76 77 78 79

  SYNOPSIS
    init_fields()
    thd          Thread handler
    tables       list of all tables for fields
    find_fields  array of structures
    count        size of previous array

  RETURN VALUES
    0           all ok
    1           one of the fileds didn't finded
*/
80 81

static bool init_fields(THD *thd, TABLE_LIST *tables,
unknown's avatar
unknown committed
82
			struct st_find_field *find_fields, uint count)
unknown's avatar
unknown committed
83
{
unknown's avatar
unknown committed
84 85
  DBUG_ENTER("init_fields");
  for (; count-- ; find_fields++)
unknown's avatar
unknown committed
86
  {
87
    /* We have to use 'new' here as field will be re_linked on free */
unknown's avatar
unknown committed
88 89
    Item_field *field= new Item_field("mysql", find_fields->table_name,
                                      find_fields->field_name);
unknown's avatar
unknown committed
90
    if (!(find_fields->field= find_field_in_tables(thd, field, tables,
unknown's avatar
VIEW  
unknown committed
91
						   0, TRUE, 1)))
unknown's avatar
unknown committed
92
      DBUG_RETURN(1);
unknown's avatar
unknown committed
93
  }
unknown's avatar
unknown committed
94
  DBUG_RETURN(0);
unknown's avatar
unknown committed
95 96
}

unknown's avatar
unknown committed
97

unknown's avatar
unknown committed
98
/*
unknown's avatar
unknown committed
99 100 101 102 103 104 105 106 107 108 109 110 111
  Returns variants of found topic for help (if it is just single topic,
    returns description and example, or else returns only names..)

  SYNOPSIS
    memorize_variant_topic()

    thd           Thread handler
    topics        Table of topics
    count         number of alredy found topics
    find_fields   Filled array of information for work with fields

  RETURN VALUES
    names         array of names of found topics (out)
unknown's avatar
unknown committed
112

unknown's avatar
unknown committed
113 114 115 116 117 118
    name          name of found topic (out)
    description   description of found topic (out)
    example       example for found topic (out)

  NOTE
    Field 'names' is set only if more than one topic is found.
unknown's avatar
unknown committed
119
    Fields 'name', 'description', 'example' are set only if
unknown's avatar
unknown committed
120 121 122 123
    found exactly one topic.
*/

void memorize_variant_topic(THD *thd, TABLE *topics, int count,
unknown's avatar
unknown committed
124
			    struct st_find_field *find_fields,
unknown's avatar
unknown committed
125 126 127 128
			    List<String> *names,
			    String *name, String *description, String *example)
{
  DBUG_ENTER("memorize_variant_topic");
unknown's avatar
unknown committed
129
  MEM_ROOT *mem_root= thd->mem_root;
unknown's avatar
unknown committed
130 131 132 133 134 135
  if (count==0)
  {
    get_field(mem_root,find_fields[help_topic_name].field,        name);
    get_field(mem_root,find_fields[help_topic_description].field, description);
    get_field(mem_root,find_fields[help_topic_example].field,     example);
  }
unknown's avatar
unknown committed
136
  else
unknown's avatar
unknown committed
137
  {
unknown's avatar
unknown committed
138
    if (count == 1)
unknown's avatar
unknown committed
139
      names->push_back(name);
unknown's avatar
unknown committed
140
    String *new_name= new (thd->mem_root) String;
unknown's avatar
unknown committed
141 142 143 144 145
    get_field(mem_root,find_fields[help_topic_name].field,new_name);
    names->push_back(new_name);
  }
  DBUG_VOID_RETURN;
}
unknown's avatar
unknown committed
146

147 148 149 150 151
/*
  Look for topics by mask

  SYNOPSIS
    search_topics()
unknown's avatar
unknown committed
152 153 154 155 156 157
    thd 	 Thread handler
    topics	 Table of topics
    find_fields  Filled array of info for fields
    select	 Function to test for matching help topic.
		 Normally 'help_topic.name like 'bit%'

158
  RETURN VALUES
unknown's avatar
unknown committed
159 160 161 162 163 164 165 166 167
    #   number of topics found

    names        array of names of found topics (out)
    name         name of found topic (out)
    description  description of found topic (out)
    example      example for found topic (out)

  NOTE
    Field 'names' is set only if more than one topic was found.
unknown's avatar
unknown committed
168
    Fields 'name', 'description', 'example' are set only if
unknown's avatar
unknown committed
169 170
    exactly one topic was found.

171 172
*/

unknown's avatar
unknown committed
173 174 175
int search_topics(THD *thd, TABLE *topics, struct st_find_field *find_fields,
		  SQL_SELECT *select, List<String> *names,
		  String *name, String *description, String *example)
unknown's avatar
unknown committed
176
{
unknown's avatar
unknown committed
177
  DBUG_ENTER("search_topics");
unknown's avatar
unknown committed
178
  int count= 0;
unknown's avatar
unknown committed
179

180 181 182
  READ_RECORD read_record_info;
  init_read_record(&read_record_info, thd, topics, select,1,0);
  while (!read_record_info.read_record(&read_record_info))
unknown's avatar
unknown committed
183
  {
unknown's avatar
unknown committed
184
    if (!select->cond->val_int())		// Doesn't match like
unknown's avatar
unknown committed
185
      continue;
unknown's avatar
unknown committed
186 187
    memorize_variant_topic(thd,topics,count,find_fields,
			   names,name,description,example);
188
    count++;
unknown's avatar
unknown committed
189
  }
190
  end_read_record(&read_record_info);
unknown's avatar
unknown committed
191

unknown's avatar
unknown committed
192 193 194
  DBUG_RETURN(count);
}

195
/*
unknown's avatar
unknown committed
196
  Look for keyword by mask
197 198

  SYNOPSIS
unknown's avatar
unknown committed
199 200 201 202 203 204 205 206
    search_keyword()
    thd          Thread handler
    keywords     Table of keywords
    find_fields  Filled array of info for fields
    select       Function to test for matching keyword.
	         Normally 'help_keyword.name like 'bit%'

    key_id       help_keyword_if of found topics (out)
207 208

  RETURN VALUES
unknown's avatar
unknown committed
209 210 211
    0   didn't find any topics matching the mask
    1   found exactly one topic matching the mask
    2   found more then one topic matching the mask
212 213
*/

unknown's avatar
unknown committed
214 215
int search_keyword(THD *thd, TABLE *keywords, struct st_find_field *find_fields,
                   SQL_SELECT *select, int *key_id)
unknown's avatar
unknown committed
216
{
unknown's avatar
unknown committed
217
  DBUG_ENTER("search_keyword");
unknown's avatar
unknown committed
218
  int count= 0;
unknown's avatar
unknown committed
219

unknown's avatar
unknown committed
220 221 222
  READ_RECORD read_record_info;
  init_read_record(&read_record_info, thd, keywords, select,1,0);
  while (!read_record_info.read_record(&read_record_info) && count<2)
unknown's avatar
unknown committed
223
  {
unknown's avatar
unknown committed
224
    if (!select->cond->val_int())		// Dosn't match like
225
      continue;
unknown's avatar
unknown committed
226

227
    *key_id= (int)find_fields[help_keyword_help_keyword_id].field->val_int();
unknown's avatar
unknown committed
228 229

    count++;
unknown's avatar
unknown committed
230
  }
231
  end_read_record(&read_record_info);
unknown's avatar
unknown committed
232

233 234
  DBUG_RETURN(count);
}
unknown's avatar
unknown committed
235

236
/*
unknown's avatar
unknown committed
237
  Look for all topics with keyword
unknown's avatar
unknown committed
238

239
  SYNOPSIS
unknown's avatar
unknown committed
240 241 242 243 244 245
    get_topics_for_keyword()
    thd		 Thread handler
    topics	 Table of topics
    relations	 Table of m:m relation "topic/keyword"
    find_fields  Filled array of info for fields
    key_id	 Primary index to use to find for keyword
unknown's avatar
unknown committed
246

247
  RETURN VALUES
unknown's avatar
unknown committed
248
    #   number of topics found
unknown's avatar
unknown committed
249

unknown's avatar
unknown committed
250
    names        array of name of found topics (out)
unknown's avatar
unknown committed
251

unknown's avatar
unknown committed
252 253 254
    name         name of found topic (out)
    description  description of found topic (out)
    example      example for found topic (out)
unknown's avatar
unknown committed
255

unknown's avatar
unknown committed
256
  NOTE
unknown's avatar
unknown committed
257
    Field 'names' is set only if more than one topic was found.
unknown's avatar
unknown committed
258
    Fields 'name', 'description', 'example' are set only if
unknown's avatar
unknown committed
259
    exactly one topic was found.
260
*/
unknown's avatar
unknown committed
261

unknown's avatar
unknown committed
262
int get_topics_for_keyword(THD *thd, TABLE *topics, TABLE *relations,
unknown's avatar
unknown committed
263 264 265
			   struct st_find_field *find_fields, int16 key_id,
			   List<String> *names,
			   String *name, String *description, String *example)
unknown's avatar
unknown committed
266
{
unknown's avatar
unknown committed
267 268
  char buff[8];	// Max int length
  int count= 0;
269
  int iindex_topic, iindex_relations;
unknown's avatar
unknown committed
270 271 272
  Field *rtopic_id, *rkey_id;

  DBUG_ENTER("get_topics_for_keyword");
unknown's avatar
unknown committed
273

274
  if ((iindex_topic= find_type((char*) primary_key_name,
275
			       &topics->keynames, 1+2)-1)<0 ||
276
      (iindex_relations= find_type((char*) primary_key_name,
277
				   &relations->keynames, 1+2)-1)<0)
unknown's avatar
unknown committed
278
  {
279 280 281 282
    send_error(thd,ER_CORRUPT_HELP_DB);
    DBUG_RETURN(-1);
  }
  rtopic_id= find_fields[help_relation_help_topic_id].field;
unknown's avatar
unknown committed
283
  rkey_id=   find_fields[help_relation_help_keyword_id].field;
unknown's avatar
unknown committed
284 285 286 287

  topics->file->ha_index_init(iindex_topic);
  relations->file->ha_index_init(iindex_relations);

unknown's avatar
unknown committed
288 289
  rkey_id->store((longlong) key_id);
  rkey_id->get_key_image(buff, rkey_id->pack_length(), rkey_id->charset(),
290 291
			 Field::itRAW);
  int key_res= relations->file->index_read(relations->record[0],
unknown's avatar
unknown committed
292
					   (byte *)buff, rkey_id->pack_length(),
293
					   HA_READ_KEY_EXACT);
unknown's avatar
unknown committed
294 295

  for ( ;
unknown's avatar
unknown committed
296
        !key_res && key_id == (int16) rkey_id->val_int() ;
297 298 299 300 301 302
	key_res= relations->file->index_next(relations->record[0]))
  {
    char topic_id_buff[8];
    longlong topic_id= rtopic_id->val_int();
    Field *field= find_fields[help_topic_help_topic_id].field;
    field->store((longlong) topic_id);
unknown's avatar
unknown committed
303
    field->get_key_image(topic_id_buff, field->pack_length(), field->charset(),
304
			 Field::itRAW);
unknown's avatar
unknown committed
305

306
    if (!topics->file->index_read(topics->record[0], (byte *)topic_id_buff,
unknown's avatar
unknown committed
307 308 309 310 311 312
				  field->pack_length(), HA_READ_KEY_EXACT))
    {
      memorize_variant_topic(thd,topics,count,find_fields,
			     names,name,description,example);
      count++;
    }
unknown's avatar
unknown committed
313
  }
unknown's avatar
unknown committed
314 315
  topics->file->ha_index_end();
  relations->file->ha_index_end();
unknown's avatar
unknown committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
  DBUG_RETURN(count);
}

/*
  Look for categories by mask

  SYNOPSIS
    search_categories()
    thd			THD for init_read_record
    categories		Table of categories
    find_fields         Filled array of info for fields
    select		Function to test for if matching help topic.
			Normally 'help_vategory.name like 'bit%'
    names		List of found categories names (out)
    res_id		Primary index of found category (only if
			found exactly one category)

  RETURN VALUES
    #			Number of categories found
*/

int search_categories(THD *thd, TABLE *categories,
		      struct st_find_field *find_fields,
		      SQL_SELECT *select, List<String> *names, int16 *res_id)
{
  Field *pfname= find_fields[help_category_name].field;
  Field *pcat_id= find_fields[help_category_help_category_id].field;
  int count= 0;
unknown's avatar
unknown committed
344
  READ_RECORD read_record_info;
unknown's avatar
unknown committed
345 346

  DBUG_ENTER("search_categories");
unknown's avatar
unknown committed
347

unknown's avatar
unknown committed
348 349 350 351 352
  init_read_record(&read_record_info, thd, categories, select,1,0);
  while (!read_record_info.read_record(&read_record_info))
  {
    if (select && !select->cond->val_int())
      continue;
unknown's avatar
unknown committed
353 354
    String *lname= new (thd->mem_root) String;
    get_field(thd->mem_root,pfname,lname);
unknown's avatar
unknown committed
355 356 357 358 359
    if (++count == 1 && res_id)
      *res_id= (int16) pcat_id->val_int();
    names->push_back(lname);
  }
  end_read_record(&read_record_info);
unknown's avatar
unknown committed
360

unknown's avatar
unknown committed
361
  DBUG_RETURN(count);
unknown's avatar
unknown committed
362 363
}

unknown's avatar
unknown committed
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
/*
  Look for all topics or subcategories of category

  SYNOPSIS
    get_all_items_for_category()
    thd	    Thread handler
    items   Table of items
    pfname  Field "name" in items
    select  "where" part of query..
    res     list of finded names
*/

void get_all_items_for_category(THD *thd, TABLE *items, Field *pfname,
				SQL_SELECT *select, List<String> *res)
{
  DBUG_ENTER("get_all_items_for_category");

  READ_RECORD read_record_info;
  init_read_record(&read_record_info, thd, items, select,1,0);
  while (!read_record_info.read_record(&read_record_info))
  {
unknown's avatar
unknown committed
385
    if (!select->cond->val_int())
unknown's avatar
unknown committed
386
      continue;
unknown's avatar
unknown committed
387 388
    String *name= new (thd->mem_root) String();
    get_field(thd->mem_root,pfname,name);
unknown's avatar
unknown committed
389 390 391 392 393 394
    res->push_back(name);
  }
  end_read_record(&read_record_info);

  DBUG_VOID_RETURN;
}
395 396 397

/*
  Send to client answer for help request
unknown's avatar
unknown committed
398

399 400 401 402
  SYNOPSIS
    send_answer_1()
    protocol - protocol for sending
    s1 - value of column "Name"
unknown's avatar
unknown committed
403 404
    s2 - value of column "Description"
    s3 - value of column "Example"
405 406 407

  IMPLEMENTATION
   Format used:
unknown's avatar
unknown committed
408 409 410 411 412
   +----------+------------+------------+
   |name      |description |example     |
   +----------+------------+------------+
   |String(64)|String(1000)|String(1000)|
   +----------+------------+------------+
413 414 415 416 417 418 419 420
   with exactly one row!

  RETURN VALUES
    1		Writing of head failed
    -1		Writing of row failed
    0		Successeful send
*/

unknown's avatar
unknown committed
421
int send_answer_1(Protocol *protocol, String *s1, String *s2, String *s3)
unknown's avatar
unknown committed
422 423 424
{
  DBUG_ENTER("send_answer_1");
  List<Item> field_list;
unknown's avatar
unknown committed
425 426 427
  field_list.push_back(new Item_empty_string("name",64));
  field_list.push_back(new Item_empty_string("description",1000));
  field_list.push_back(new Item_empty_string("example",1000));
unknown's avatar
unknown committed
428

429 430
  if (protocol->send_fields(&field_list,
                            Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF))
unknown's avatar
unknown committed
431
    DBUG_RETURN(1);
unknown's avatar
unknown committed
432

433
  protocol->prepare_for_resend();
unknown's avatar
unknown committed
434 435 436
  protocol->store(s1);
  protocol->store(s2);
  protocol->store(s3);
437
  if (protocol->write())
unknown's avatar
unknown committed
438 439 440 441
    DBUG_RETURN(-1);
  DBUG_RETURN(0);
}

442

443 444 445 446 447
/*
  Send to client help header

  SYNOPSIS
   send_header_2()
unknown's avatar
unknown committed
448 449
    protocol       - protocol for sending
    is_it_category - need column 'source_category_name'
450 451

  IMPLEMENTATION
unknown's avatar
unknown committed
452 453 454 455 456 457 458
   +-                    -+
   |+-------------------- | +----------+--------------+
   ||source_category_name | |name      |is_it_category|
   |+-------------------- | +----------+--------------+
   ||String(64)           | |String(64)|String(1)     |
   |+-------------------- | +----------+--------------+
   +-                    -+
459 460 461 462 463

  RETURN VALUES
    result of protocol->send_fields
*/

unknown's avatar
unknown committed
464
int send_header_2(Protocol *protocol, bool for_category)
unknown's avatar
unknown committed
465
{
unknown's avatar
unknown committed
466
  DBUG_ENTER("send_header_2");
unknown's avatar
unknown committed
467
  List<Item> field_list;
unknown's avatar
unknown committed
468 469 470 471
  if (for_category)
    field_list.push_back(new Item_empty_string("source_category_name",64));
  field_list.push_back(new Item_empty_string("name",64));
  field_list.push_back(new Item_empty_string("is_it_category",1));
472 473
  DBUG_RETURN(protocol->send_fields(&field_list, Protocol::SEND_NUM_ROWS |
                                                 Protocol::SEND_EOF));
unknown's avatar
unknown committed
474 475
}

unknown's avatar
unknown committed
476 477 478 479 480 481 482 483 484 485 486 487
/*
  strcmp for using in qsort

  SYNOPSIS
    strptrcmp()
    ptr1   (const void*)&str1
    ptr2   (const void*)&str2

  RETURN VALUES
    same as strcmp
*/

488
extern "C" int string_ptr_cmp(const void* ptr1, const void* ptr2)
unknown's avatar
unknown committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502
{
  String *str1= *(String**)ptr1;
  String *str2= *(String**)ptr2;
  return strcmp(str1->c_ptr(),str2->c_ptr());
}

/*
  Send to client rows in format:
   column1 : <name>
   column2 : <is_it_category>

  SYNOPSIS
    send_variant_2_list()
    protocol     Protocol for sending
unknown's avatar
unknown committed
503
    names        List of names
unknown's avatar
unknown committed
504 505 506 507 508 509 510 511
    cat	         Value of the column <is_it_category>
    source_name  name of category for all items..

  RETURN VALUES
    -1 	Writing fail
    0	Data was successefully send
*/

unknown's avatar
unknown committed
512 513
int send_variant_2_list(MEM_ROOT *mem_root, Protocol *protocol,
			List<String> *names,
unknown's avatar
unknown committed
514 515 516 517 518 519
			const char *cat, String *source_name)
{
  DBUG_ENTER("send_variant_2_list");

  String **pointers= (String**)alloc_root(mem_root,
					  sizeof(String*)*names->elements);
520 521
  String **pos;
  String **end= pointers + names->elements;
unknown's avatar
unknown committed
522 523

  List_iterator<String> it(*names);
unknown's avatar
unknown committed
524
  for (pos= pointers; pos!=end; (*pos++= it++));
unknown's avatar
unknown committed
525 526 527

  qsort(pointers,names->elements,sizeof(String*),string_ptr_cmp);

528
  for (pos= pointers; pos!=end; pos++)
unknown's avatar
unknown committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  {
    protocol->prepare_for_resend();
    if (source_name)
      protocol->store(source_name);
    protocol->store(*pos);
    protocol->store(cat,1,&my_charset_latin1);
    if (protocol->write())
      DBUG_RETURN(-1);
  }

  DBUG_RETURN(0);
}

/*
  Prepare simple SQL_SELECT table.* WHERE <Item>

  SYNOPSIS
    prepare_simple_select()
    thd      Thread handler
    cond     WHERE part of select
    tables   list of tables, used in WHERE
    table    goal table

    error    code of error (out)
unknown's avatar
unknown committed
553

unknown's avatar
unknown committed
554
  RETURN VALUES
unknown's avatar
unknown committed
555
    #  created SQL_SELECT
unknown's avatar
unknown committed
556 557
*/

unknown's avatar
unknown committed
558
SQL_SELECT *prepare_simple_select(THD *thd, Item *cond, TABLE_LIST *tables,
unknown's avatar
unknown committed
559 560 561 562
				  TABLE *table, int *error)
{
  cond->fix_fields(thd, tables, &cond);	// can never fail
  SQL_SELECT *res= make_select(table,0,0,cond,error);
unknown's avatar
unknown committed
563 564 565 566 567 568
  if (*error || (res && res->check_quick(thd, 0, HA_POS_ERROR)))
  {
    delete res;
    res=0;
  }
  return res;
unknown's avatar
unknown committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
}

/*
  Prepare simple SQL_SELECT table.* WHERE table.name LIKE mask

  SYNOPSIS
    prepare_select_for_name()
    thd      Thread handler
    mask     mask for compare with name
    mlen     length of mask
    tables   list of tables, used in WHERE
    table    goal table
    pfname   field "name" in table

    error    code of error (out)
unknown's avatar
unknown committed
584

unknown's avatar
unknown committed
585
  RETURN VALUES
unknown's avatar
unknown committed
586
    #  created SQL_SELECT
unknown's avatar
unknown committed
587 588 589 590 591 592
*/

SQL_SELECT *prepare_select_for_name(THD *thd, const char *mask, uint mlen,
				    TABLE_LIST *tables, TABLE *table,
				    Field *pfname, int *error)
{
unknown's avatar
unknown committed
593
  Item *cond= new Item_func_like(new Item_field(pfname),
unknown's avatar
unknown committed
594
				 new Item_string(mask,mlen,pfname->charset()),
595
				 new Item_string("\\",1,&my_charset_latin1));
unknown's avatar
unknown committed
596 597
  if (thd->is_fatal_error)
    return 0;					// OOM
unknown's avatar
unknown committed
598 599
  return prepare_simple_select(thd,cond,tables,table,error);
}
600

unknown's avatar
unknown committed
601

602 603 604 605 606 607 608 609 610 611 612 613 614
/*
  Server-side function 'help'

  SYNOPSIS
    mysqld_help()
    thd			Thread handler

  RETURN VALUES
    0		Success
    1		Error and send_error already commited
    -1		error && send_error should be issued (normal case)
*/

615
int mysqld_help(THD *thd, const char *mask)
unknown's avatar
unknown committed
616
{
617
  Protocol *protocol= thd->protocol;
unknown's avatar
unknown committed
618
  SQL_SELECT *select;
619
  st_find_field used_fields[array_elements(init_used_fields)];
unknown's avatar
unknown committed
620
  DBUG_ENTER("mysqld_help");
unknown's avatar
unknown committed
621

unknown's avatar
unknown committed
622
  TABLE_LIST tables[4];
623 624 625
  bzero((gptr)tables,sizeof(tables));
  tables[0].alias= tables[0].real_name= (char*) "help_topic";
  tables[0].lock_type= TL_READ;
unknown's avatar
VIEW  
unknown committed
626
  tables[0].next_global= tables[0].next_local= &tables[1];
627 628
  tables[1].alias= tables[1].real_name= (char*) "help_category";
  tables[1].lock_type= TL_READ;
unknown's avatar
VIEW  
unknown committed
629
  tables[1].next_global= tables[1].next_local= &tables[2];
630 631
  tables[2].alias= tables[2].real_name= (char*) "help_relation";
  tables[2].lock_type= TL_READ;
unknown's avatar
VIEW  
unknown committed
632
  tables[2].next_global= tables[2].next_local= &tables[3];
unknown's avatar
unknown committed
633 634
  tables[3].alias= tables[3].real_name= (char*) "help_keyword";
  tables[3].lock_type= TL_READ;
635
  tables[0].db= tables[1].db= tables[2].db= tables[3].db= (char*) "mysql";
unknown's avatar
unknown committed
636

unknown's avatar
unknown committed
637 638
  List<String> topics_list, categories_list, subcategories_list;
  String name, description, example;
639
  int res, count_topics, count_categories, error;
unknown's avatar
unknown committed
640
  uint mlen= strlen(mask);
unknown's avatar
unknown committed
641
  MEM_ROOT *mem_root= thd->mem_root;
unknown's avatar
unknown committed
642

643
  if ((res= open_and_lock_tables(thd, tables)))
644
    goto end;
unknown's avatar
VIEW  
unknown committed
645 646 647 648 649 650
  /*
    Init tables and fields to be usable from items

    tables do not contain VIEWs => we can pass 0 as conds
  */
  setup_tables(thd, tables, 0);
unknown's avatar
unknown committed
651
  memcpy((char*) used_fields, (char*) init_used_fields, sizeof(used_fields));
652 653 654 655 656
  if (init_fields(thd, tables, used_fields, array_elements(used_fields)))
  {
    res= -1;
    goto end;
  }
657 658
  size_t i;
  for (i=0; i<sizeof(tables)/sizeof(TABLE_LIST); i++)
unknown's avatar
unknown committed
659
    tables[i].table->file->init_table_handle_for_HANDLER();
unknown's avatar
unknown committed
660 661

  if (!(select=
unknown's avatar
unknown committed
662
	prepare_select_for_name(thd,mask,mlen,tables,tables[0].table,
unknown's avatar
unknown committed
663
				used_fields[help_topic_name].field,&error)))
unknown's avatar
unknown committed
664
  {
665
    res= -1;
unknown's avatar
unknown committed
666 667
    goto end;
  }
668 669

  res= 1;
unknown's avatar
unknown committed
670 671
  count_topics= search_topics(thd,tables[0].table,used_fields,
			      select,&topics_list,
unknown's avatar
unknown committed
672
			      &name, &description, &example);
unknown's avatar
unknown committed
673
  delete select;
unknown's avatar
unknown committed
674 675

  if (count_topics == 0)
unknown's avatar
unknown committed
676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
  {
    int key_id;
    if (!(select=
          prepare_select_for_name(thd,mask,mlen,tables,tables[3].table,
                                  used_fields[help_keyword_name].field,&error)))
    {
      res= -1;
      goto end;
    }
    count_topics=search_keyword(thd,tables[3].table,used_fields,select,&key_id);
    delete select;
    count_topics= (count_topics != 1) ? 0 :
                  get_topics_for_keyword(thd,tables[0].table,tables[2].table,
                                         used_fields,key_id,&topics_list,&name,
                                         &description,&example);
  }

693
  if (count_topics == 0)
unknown's avatar
unknown committed
694 695
  {
    int16 category_id;
unknown's avatar
unknown committed
696
    Field *cat_cat_id= used_fields[help_category_parent_category_id].field;
unknown's avatar
unknown committed
697 698 699 700 701 702 703 704
    if (!(select=
          prepare_select_for_name(thd,mask,mlen,tables,tables[1].table,
                                  used_fields[help_category_name].field,&error)))
    {
      res= -1;
      goto end;
    }

705
    count_categories= search_categories(thd, tables[1].table, used_fields,
unknown's avatar
unknown committed
706
					select,
unknown's avatar
unknown committed
707
					&categories_list,&category_id);
unknown's avatar
unknown committed
708
    delete select;
unknown's avatar
unknown committed
709
    if (!count_categories)
unknown's avatar
unknown committed
710
    {
unknown's avatar
unknown committed
711
      if (send_header_2(protocol,FALSE))
unknown's avatar
unknown committed
712
	goto end;
unknown's avatar
unknown committed
713 714 715
    }
    else if (count_categories > 1)
    {
unknown's avatar
unknown committed
716
      if (send_header_2(protocol,FALSE) ||
unknown's avatar
unknown committed
717
	  send_variant_2_list(mem_root,protocol,&categories_list,"Y",0))
unknown's avatar
unknown committed
718 719
	goto end;
    }
unknown's avatar
unknown committed
720
    else
unknown's avatar
unknown committed
721
    {
unknown's avatar
unknown committed
722
      Field *topic_cat_id= used_fields[help_topic_help_category_id].field;
723
      Item *cond_topic_by_cat=
unknown's avatar
unknown committed
724
	new Item_func_equal(new Item_field(topic_cat_id),
725 726
			    new Item_int((int32)category_id));
      Item *cond_cat_by_cat=
unknown's avatar
unknown committed
727
	new Item_func_equal(new Item_field(cat_cat_id),
728
			    new Item_int((int32)category_id));
unknown's avatar
unknown committed
729 730
      if (!(select= prepare_simple_select(thd,cond_topic_by_cat,
                                          tables,tables[0].table,&error)))
unknown's avatar
unknown committed
731 732
      {
	res= -1;
733
	goto end;
unknown's avatar
unknown committed
734 735 736
      }
      get_all_items_for_category(thd,tables[0].table,
				 used_fields[help_topic_name].field,
unknown's avatar
unknown committed
737 738 739 740 741 742 743 744
				 select,&topics_list);
      delete select;
      if (!(select= prepare_simple_select(thd,cond_cat_by_cat,tables,
						     tables[1].table,&error)))
      {
	res= -1;
	goto end;
      }
unknown's avatar
unknown committed
745 746
      get_all_items_for_category(thd,tables[1].table,
				 used_fields[help_category_name].field,
unknown's avatar
unknown committed
747 748
				 select,&subcategories_list);
      delete select;
unknown's avatar
unknown committed
749
      String *cat= categories_list.head();
unknown's avatar
unknown committed
750
      if (send_header_2(protocol, TRUE) ||
unknown's avatar
unknown committed
751 752
	  send_variant_2_list(mem_root,protocol,&topics_list,       "N",cat) ||
	  send_variant_2_list(mem_root,protocol,&subcategories_list,"Y",cat))
unknown's avatar
unknown committed
753 754 755
	goto end;
    }
  }
756
  else if (count_topics == 1)
unknown's avatar
unknown committed
757
  {
unknown's avatar
unknown committed
758
    if (send_answer_1(protocol,&name,&description,&example))
unknown's avatar
unknown committed
759 760
      goto end;
  }
761
  else
unknown's avatar
unknown committed
762
  {
763
    /* First send header and functions */
unknown's avatar
unknown committed
764
    if (send_header_2(protocol, FALSE) ||
unknown's avatar
unknown committed
765
	send_variant_2_list(mem_root,protocol, &topics_list, "N", 0))
766
      goto end;
unknown's avatar
unknown committed
767 768 769 770 771 772 773 774 775 776
    if (!(select=
          prepare_select_for_name(thd,mask,mlen,tables,tables[1].table,
                                  used_fields[help_category_name].field,&error)))
    {
      res= -1;
      goto end;
    }
    search_categories(thd, tables[1].table, used_fields,
		      select,&categories_list, 0);
    delete select;
777
    /* Then send categories */
unknown's avatar
unknown committed
778
    if (send_variant_2_list(mem_root,protocol, &categories_list, "Y", 0))
779
      goto end;
unknown's avatar
unknown committed
780
  }
781
  res= 0;
unknown's avatar
unknown committed
782

unknown's avatar
unknown committed
783
  send_eof(thd);
unknown's avatar
unknown committed
784

unknown's avatar
unknown committed
785 786 787
end:
  DBUG_RETURN(res);
}
unknown's avatar
unknown committed
788