~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

  • Committer: Brian Aker
  • Date: 2010-08-09 18:04:12 UTC
  • mfrom: (1689.3.7 staging)
  • Revision ID: brian@gaz-20100809180412-olurwh51ojllev6p
Merge in heap conversion, and case insensitive patch, and remove need for
M_HASH in session.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2010 Brian Aker
5
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
6
5
 *
7
6
 *  This program is free software; you can redistribute it and/or modify
8
7
 *  it under the terms of the GNU General Public License as published by
20
19
 
21
20
 
22
21
/* Function with list databases, tables or fields */
23
 
#include <config.h>
24
 
 
 
22
#include "config.h"
 
23
#include <drizzled/sql_select.h>
 
24
#include <drizzled/show.h>
 
25
#include <drizzled/gettext.h>
 
26
#include <drizzled/util/convert.h>
 
27
#include <drizzled/error.h>
 
28
#include <drizzled/tztime.h>
25
29
#include <drizzled/data_home.h>
26
 
#include <drizzled/error.h>
27
 
#include <drizzled/internal/my_sys.h>
28
 
#include <drizzled/plugin/storage_engine.h>
29
 
#include <drizzled/session.h>
30
 
#include <drizzled/show.h>
31
 
#include <drizzled/sql_select.h>
32
 
 
33
 
#include <drizzled/statement/show.h>
34
 
#include <drizzled/statement/show_errors.h>
35
 
#include <drizzled/statement/show_warnings.h>
36
 
 
 
30
#include <drizzled/item/blob.h>
 
31
#include <drizzled/item/cmpfunc.h>
 
32
#include <drizzled/item/return_int.h>
 
33
#include <drizzled/item/empty_string.h>
 
34
#include <drizzled/item/return_date_time.h>
 
35
#include <drizzled/sql_base.h>
 
36
#include <drizzled/db.h>
 
37
#include <drizzled/field/timestamp.h>
 
38
#include <drizzled/field/decimal.h>
 
39
#include <drizzled/lock.h>
 
40
#include <drizzled/item/return_date_time.h>
 
41
#include <drizzled/item/empty_string.h>
 
42
#include "drizzled/session_list.h"
 
43
#include <drizzled/message/schema.pb.h>
 
44
#include <drizzled/plugin/client.h>
 
45
#include <drizzled/cached_directory.h>
 
46
#include "drizzled/sql_table.h"
 
47
#include "drizzled/global_charset_info.h"
 
48
#include "drizzled/pthread_globals.h"
 
49
#include "drizzled/internal/m_string.h"
 
50
#include "drizzled/internal/my_sys.h"
37
51
 
38
52
#include <sys/stat.h>
39
53
 
54
68
  return str ? str : "<nil>";
55
69
}
56
70
 
57
 
int wild_case_compare(const charset_info_st * const cs, const char *str, const char *wildstr)
 
71
static void store_key_options(String *packet, Table *table, KeyInfo *key_info);
 
72
 
 
73
 
 
74
int wild_case_compare(const CHARSET_INFO * const cs, const char *str, const char *wildstr)
58
75
{
59
76
  register int flag;
60
77
 
64
81
    {
65
82
      if (*wildstr == internal::wild_prefix && wildstr[1])
66
83
        wildstr++;
67
 
 
68
84
      if (my_toupper(cs, *wildstr++) != my_toupper(cs, *str++))
69
85
        return (1);
70
86
    }
71
 
 
72
87
    if (! *wildstr )
73
88
      return (*str != 0);
74
 
 
75
89
    if (*wildstr++ == internal::wild_one)
76
90
    {
77
91
      if (! *str++)
81
95
    {                                           /* Found '*' */
82
96
      if (! *wildstr)
83
97
        return (0);             /* '*' as last char: OK */
84
 
 
85
98
      flag=(*wildstr != internal::wild_many && *wildstr != internal::wild_one);
86
99
      do
87
100
      {
90
103
          char cmp;
91
104
          if ((cmp= *wildstr) == internal::wild_prefix && wildstr[1])
92
105
            cmp= wildstr[1];
93
 
 
94
106
          cmp= my_toupper(cs, cmp);
95
 
 
96
107
          while (*str && my_toupper(cs, *str) != cmp)
97
108
            str++;
98
 
 
99
109
          if (! *str)
100
110
            return (1);
101
111
        }
102
 
 
103
112
        if (wild_case_compare(cs, str, wildstr) == 0)
104
113
          return (0);
105
 
 
106
114
      } while (*str++);
107
 
 
108
115
      return (1);
109
116
    }
110
117
  }
112
119
  return (*str != '\0');
113
120
}
114
121
 
 
122
 
 
123
bool drizzled_show_create(Session *session, TableList *table_list, bool is_if_not_exists)
 
124
{
 
125
  char buff[2048];
 
126
  String buffer(buff, sizeof(buff), system_charset_info);
 
127
 
 
128
  /* Only one table for now, but VIEW can involve several tables */
 
129
  if (session->openTables(table_list))
 
130
  {
 
131
    if (session->is_error())
 
132
      return true;
 
133
 
 
134
    /*
 
135
      Clear all messages with 'error' level status and
 
136
      issue a warning with 'warning' level status in
 
137
      case of invalid view and last error is ER_VIEW_INVALID
 
138
    */
 
139
    drizzle_reset_errors(session, true);
 
140
    session->clear_error();
 
141
  }
 
142
 
 
143
  buffer.length(0);
 
144
 
 
145
  if (store_create_info(table_list, &buffer, is_if_not_exists))
 
146
    return true;
 
147
 
 
148
  List<Item> field_list;
 
149
  {
 
150
    field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
 
151
    // 1024 is for not to confuse old clients
 
152
    field_list.push_back(new Item_empty_string("Create Table",
 
153
                                               max(buffer.length(),(uint32_t)1024)));
 
154
  }
 
155
 
 
156
  if (session->client->sendFields(&field_list))
 
157
    return true;
 
158
  {
 
159
    session->client->store(table_list->table->getAlias());
 
160
  }
 
161
 
 
162
  session->client->store(buffer.ptr(), buffer.length());
 
163
 
 
164
  if (session->client->flush())
 
165
    return true;
 
166
 
 
167
  session->my_eof();
 
168
  return false;
 
169
}
 
170
 
 
171
/**
 
172
  Get a CREATE statement for a given database.
 
173
 
 
174
  The database is identified by its name, passed as @c dbname parameter.
 
175
  The name should be encoded using the system character set (UTF8 currently).
 
176
 
 
177
  Resulting statement is stored in the string pointed by @c buffer. The string
 
178
  is emptied first and its character set is set to the system character set.
 
179
 
 
180
  If is_if_not_exists is set, then
 
181
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
 
182
  in @c create_options are ignored.
 
183
 
 
184
  @param  session           The current thread instance.
 
185
  @param  dbname        The name of the database.
 
186
  @param  buffer        A String instance where the statement is stored.
 
187
  @param  create_info   If not NULL, the options member influences the resulting
 
188
                        CRATE statement.
 
189
 
 
190
  @returns true if errors are detected, false otherwise.
 
191
*/
 
192
 
 
193
static bool store_db_create_info(SchemaIdentifier &schema_identifier, string &buffer, bool if_not_exists)
 
194
{
 
195
  message::Schema schema;
 
196
 
 
197
  bool found= plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema);
 
198
  if (not found)
 
199
    return false;
 
200
 
 
201
  buffer.append("CREATE DATABASE ");
 
202
 
 
203
  if (if_not_exists)
 
204
    buffer.append("IF NOT EXISTS ");
 
205
 
 
206
  buffer.append("`");
 
207
  buffer.append(schema.name());
 
208
  buffer.append("`");
 
209
 
 
210
  if (schema.has_collation())
 
211
  {
 
212
    buffer.append(" COLLATE = ");
 
213
    buffer.append(schema.collation());
 
214
  }
 
215
 
 
216
  return true;
 
217
}
 
218
 
 
219
bool mysqld_show_create_db(Session &session, SchemaIdentifier &schema_identifier, bool if_not_exists)
 
220
{
 
221
  message::Schema schema_message;
 
222
  string buffer;
 
223
 
 
224
  if (not plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema_message))
 
225
  {
 
226
    /*
 
227
      This assumes that the only reason for which store_db_create_info()
 
228
      can fail is incorrect database name (which is the case now).
 
229
    */
 
230
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
 
231
    return true;
 
232
  }
 
233
 
 
234
  if (not store_db_create_info(schema_identifier, buffer, if_not_exists))
 
235
  {
 
236
    /*
 
237
      This assumes that the only reason for which store_db_create_info()
 
238
      can fail is incorrect database name (which is the case now).
 
239
    */
 
240
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
 
241
    return true;
 
242
  }
 
243
 
 
244
  List<Item> field_list;
 
245
  field_list.push_back(new Item_empty_string("Database",NAME_CHAR_LEN));
 
246
  field_list.push_back(new Item_empty_string("Create Database",1024));
 
247
 
 
248
  if (session.client->sendFields(&field_list))
 
249
    return true;
 
250
 
 
251
  session.client->store(schema_message.name());
 
252
  session.client->store(buffer);
 
253
 
 
254
  if (session.client->flush())
 
255
    return true;
 
256
 
 
257
  session.my_eof();
 
258
 
 
259
  return false;
 
260
}
 
261
 
115
262
/*
116
263
  Get the quote character for displaying an identifier.
117
264
 
137
284
  return '`';
138
285
}
139
286
 
140
 
namespace show {
141
 
 
142
 
bool buildScemas(Session *session)
143
 
{
144
 
  session->getLex()->sql_command= SQLCOM_SELECT;
145
 
  session->getLex()->statement= new statement::Show(session);
146
 
 
147
 
  std::string column_name= "Database";
148
 
  if (session->getLex()->wild)
149
 
  {
150
 
    column_name.append(" (");
151
 
    column_name.append(session->getLex()->wild->ptr());
152
 
    column_name.append(")");
153
 
  }
154
 
 
155
 
  if (session->getLex()->current_select->where)
156
 
  {
157
 
    if (prepare_new_schema_table(session, session->getLex(), "SCHEMAS"))
158
 
      return false;
159
 
  }
160
 
  else
161
 
  {
162
 
    if (prepare_new_schema_table(session, session->getLex(), "SHOW_SCHEMAS"))
163
 
      return false;
164
 
  }
165
 
 
166
 
  Item_field *my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "SCHEMA_NAME");
167
 
  my_field->is_autogenerated_name= false;
168
 
  my_field->set_name(column_name.c_str(), column_name.length(), system_charset_info);
169
 
 
170
 
  if (session->add_item_to_list(my_field))
171
 
    return false;
172
 
 
173
 
  if (session->add_order_to_list(my_field, true))
174
 
    return false;
175
 
 
176
 
  return true;
177
 
}
178
 
 
179
 
bool buildTables(Session *session, const char *ident)
180
 
{
181
 
  session->getLex()->sql_command= SQLCOM_SELECT;
182
 
 
183
 
  drizzled::statement::Show *select= new statement::Show(session);
184
 
  session->getLex()->statement= select;
185
 
 
186
 
  std::string column_name= "Tables_in_";
187
 
 
188
 
  util::string::const_shared_ptr schema(session->schema());
189
 
  if (ident)
190
 
  {
191
 
    identifier::Schema identifier(ident);
192
 
    column_name.append(ident);
193
 
    session->getLex()->select_lex.db= const_cast<char *>(ident);
194
 
    if (not plugin::StorageEngine::doesSchemaExist(identifier))
195
 
    {
196
 
      my_error(ER_BAD_DB_ERROR, identifier);
197
 
    }
198
 
    select->setShowPredicate(ident, "");
199
 
  }
200
 
  else if (schema and not schema->empty())
201
 
  {
202
 
    column_name.append(*schema);
203
 
    select->setShowPredicate(*schema, "");
204
 
  }
205
 
  else
206
 
  {
207
 
    my_error(ER_NO_DB_ERROR, MYF(0));
208
 
    return false;
209
 
  }
210
 
 
211
 
 
212
 
  if (session->getLex()->wild)
213
 
  {
214
 
    column_name.append(" (");
215
 
    column_name.append(session->getLex()->wild->ptr());
216
 
    column_name.append(")");
217
 
  }
218
 
 
219
 
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_TABLES"))
220
 
    return false;
221
 
 
222
 
  Item_field *my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "TABLE_NAME");
223
 
  my_field->is_autogenerated_name= false;
224
 
  my_field->set_name(column_name.c_str(), column_name.length(), system_charset_info);
225
 
 
226
 
  if (session->add_item_to_list(my_field))
227
 
    return false;
228
 
 
229
 
  if (session->add_order_to_list(my_field, true))
230
 
    return false;
231
 
 
232
 
  return true;
233
 
}
234
 
 
235
 
bool buildTemporaryTables(Session *session)
236
 
{
237
 
  session->getLex()->sql_command= SQLCOM_SELECT;
238
 
 
239
 
  session->getLex()->statement= new statement::Show(session);
240
 
 
241
 
 
242
 
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_TEMPORARY_TABLES"))
243
 
    return false;
244
 
 
245
 
  if (session->add_item_to_list( new Item_field(&session->getLex()->current_select->context, NULL, NULL, "*")))
246
 
    return false;
247
 
 
248
 
  (session->getLex()->current_select->with_wild)++;
249
 
 
250
 
  return true;
251
 
}
252
 
 
253
 
bool buildTableStatus(Session *session, const char *ident)
254
 
{
255
 
  session->getLex()->sql_command= SQLCOM_SELECT;
256
 
  drizzled::statement::Show *select= new statement::Show(session);
257
 
  session->getLex()->statement= select;
258
 
 
259
 
  std::string column_name= "Tables_in_";
260
 
 
261
 
  util::string::const_shared_ptr schema(session->schema());
262
 
  if (ident)
263
 
  {
264
 
    session->getLex()->select_lex.db= const_cast<char *>(ident);
265
 
 
266
 
    identifier::Schema identifier(ident);
267
 
    if (not plugin::StorageEngine::doesSchemaExist(identifier))
268
 
    {
269
 
      my_error(ER_BAD_DB_ERROR, identifier);
270
 
    }
271
 
 
272
 
    select->setShowPredicate(ident, "");
273
 
  }
274
 
  else if (schema)
275
 
  {
276
 
    select->setShowPredicate(*schema, "");
277
 
  }
278
 
  else
279
 
  {
280
 
    my_error(ER_NO_DB_ERROR, MYF(0));
281
 
    return false;
282
 
  }
283
 
 
284
 
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_TABLE_STATUS"))
285
 
    return false;
286
 
 
287
 
  if (session->add_item_to_list( new Item_field(&session->getLex()->current_select->
288
 
                                                  context,
289
 
                                                  NULL, NULL, "*")))
290
 
    return false;
291
 
 
292
 
  (session->getLex()->current_select->with_wild)++;
293
 
 
294
 
  return true;
295
 
}
296
 
 
297
 
bool buildEngineStatus(Session *session, LEX_STRING)
298
 
{
299
 
  session->getLex()->sql_command= SQLCOM_SELECT;
300
 
  drizzled::statement::Show *select= new statement::Show(session);
301
 
  session->getLex()->statement= select;
302
 
 
303
 
  my_error(ER_USE_DATA_DICTIONARY);
304
 
  return false;
305
 
}
306
 
 
307
 
bool buildColumns(Session *session, const char *schema_ident, Table_ident *table_ident)
308
 
{
309
 
  session->getLex()->sql_command= SQLCOM_SELECT;
310
 
 
311
 
  drizzled::statement::Show *select= new statement::Show(session);
312
 
  session->getLex()->statement= select;
313
 
 
314
 
  util::string::const_shared_ptr schema(session->schema());
315
 
  if (schema_ident)
316
 
  {
317
 
    select->setShowPredicate(schema_ident, table_ident->table.str);
318
 
  }
319
 
  else if (table_ident->db.str)
320
 
  {
321
 
    select->setShowPredicate(table_ident->db.str, table_ident->table.str);
322
 
  }
323
 
  else if (schema)
324
 
  {
325
 
    select->setShowPredicate(*schema, table_ident->table.str);
326
 
  }
327
 
  else
328
 
  {
329
 
    my_error(ER_NO_DB_ERROR, MYF(0));
330
 
    return false;
331
 
  }
332
 
 
333
 
  {
334
 
    drizzled::identifier::Table identifier(select->getShowSchema().c_str(), table_ident->table.str);
335
 
    if (not plugin::StorageEngine::doesTableExist(*session, identifier))
336
 
    {
337
 
      my_error(ER_TABLE_UNKNOWN, identifier);
338
 
    }
339
 
  }
340
 
 
341
 
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_COLUMNS"))
342
 
    return false;
343
 
 
344
 
  if (session->add_item_to_list( new Item_field(&session->getLex()->current_select->context, NULL, NULL, "*")))
345
 
    return false;
346
 
 
347
 
  (session->getLex()->current_select->with_wild)++;
348
 
 
349
 
  return true;
350
 
}
351
 
 
352
 
void buildSelectWarning(Session *session)
353
 
{
354
 
  (void) create_select_for_variable(session, "warning_count");
355
 
  session->getLex()->statement= new statement::Show(session);
356
 
}
357
 
 
358
 
void buildSelectError(Session *session)
359
 
{
360
 
  (void) create_select_for_variable(session, "error_count");
361
 
  session->getLex()->statement= new statement::Show(session);
362
 
}
363
 
 
364
 
void buildWarnings(Session *session)
365
 
{
366
 
  session->getLex()->statement= new statement::ShowWarnings(session);
367
 
}
368
 
 
369
 
void buildErrors(Session *session)
370
 
{
371
 
  session->getLex()->statement= new statement::ShowErrors(session);
372
 
}
373
 
 
374
 
bool buildIndex(Session *session, const char *schema_ident, Table_ident *table_ident)
375
 
{
376
 
  session->getLex()->sql_command= SQLCOM_SELECT;
377
 
  drizzled::statement::Show *select= new statement::Show(session);
378
 
  session->getLex()->statement= select;
379
 
 
380
 
  util::string::const_shared_ptr schema(session->schema());
381
 
  if (schema_ident)
382
 
  {
383
 
    select->setShowPredicate(schema_ident, table_ident->table.str);
384
 
  }
385
 
  else if (table_ident->db.str)
386
 
  {
387
 
    select->setShowPredicate(table_ident->db.str, table_ident->table.str);
388
 
  }
389
 
  else if (schema)
390
 
  {
391
 
    select->setShowPredicate(*schema, table_ident->table.str);
392
 
  }
393
 
  else
394
 
  {
395
 
    my_error(ER_NO_DB_ERROR, MYF(0));
396
 
    return false;
397
 
  }
398
 
 
399
 
  {
400
 
    drizzled::identifier::Table identifier(select->getShowSchema().c_str(), table_ident->table.str);
401
 
    if (not plugin::StorageEngine::doesTableExist(*session, identifier))
402
 
    {
403
 
      my_error(ER_TABLE_UNKNOWN, identifier);
404
 
    }
405
 
  }
406
 
 
407
 
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_INDEXES"))
408
 
    return false;
409
 
 
410
 
  if (session->add_item_to_list( new Item_field(&session->getLex()->current_select->context, NULL, NULL, "*")))
411
 
    return false;
412
 
 
413
 
  (session->getLex()->current_select->with_wild)++;
414
 
 
415
 
  return true;
416
 
}
417
 
 
418
 
bool buildStatus(Session *session, const drizzled::sql_var_t is_global)
419
 
{
420
 
  session->getLex()->sql_command= SQLCOM_SELECT;
421
 
  session->getLex()->statement= new statement::Show(session);
422
 
 
423
 
  if (is_global == OPT_GLOBAL)
424
 
  {
425
 
    if (prepare_new_schema_table(session, session->getLex(), "GLOBAL_STATUS"))
426
 
      return false;
427
 
  }
428
 
  else
429
 
  {
430
 
    if (prepare_new_schema_table(session, session->getLex(), "SESSION_STATUS"))
431
 
      return false;
432
 
  }
433
 
 
434
 
  std::string key("Variable_name");
435
 
  std::string value("Value");
436
 
 
437
 
  Item_field *my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "VARIABLE_NAME");
438
 
  my_field->is_autogenerated_name= false;
439
 
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
440
 
 
441
 
  if (session->add_item_to_list(my_field))
442
 
    return false;
443
 
 
444
 
  my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "VARIABLE_VALUE");
445
 
  my_field->is_autogenerated_name= false;
446
 
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
447
 
 
448
 
  if (session->add_item_to_list(my_field))
449
 
    return false;
450
 
 
451
 
  return true;
452
 
}
453
 
 
454
 
bool buildCreateTable(Session *session, Table_ident *ident)
455
 
{
456
 
  session->getLex()->sql_command= SQLCOM_SELECT;
457
 
  statement::Show *select= new statement::Show(session);
458
 
  session->getLex()->statement= select;
459
 
 
460
 
  if (session->getLex()->statement == NULL)
461
 
    return false;
462
 
 
463
 
  if (prepare_new_schema_table(session, session->getLex(), "TABLE_SQL_DEFINITION"))
464
 
    return false;
465
 
 
466
 
  util::string::const_shared_ptr schema(session->schema());
467
 
  if (ident->db.str)
468
 
  {
469
 
    select->setShowPredicate(ident->db.str, ident->table.str);
470
 
  }
471
 
  else if (schema)
472
 
  {
473
 
    select->setShowPredicate(*schema, ident->table.str);
474
 
  }
475
 
  else
476
 
  {
477
 
    my_error(ER_NO_DB_ERROR, MYF(0));
478
 
    return false;
479
 
  }
480
 
 
481
 
  std::string key("Table");
482
 
  std::string value("Create Table");
483
 
 
484
 
  Item_field *my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "TABLE_NAME");
485
 
  my_field->is_autogenerated_name= false;
486
 
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
487
 
 
488
 
  if (session->add_item_to_list(my_field))
489
 
    return false;
490
 
 
491
 
  my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "TABLE_SQL_DEFINITION");
492
 
  my_field->is_autogenerated_name= false;
493
 
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
494
 
 
495
 
  if (session->add_item_to_list(my_field))
496
 
    return false;
497
 
 
498
 
  return true;
499
 
}
500
 
 
501
 
bool buildProcesslist(Session *session)
502
 
{
503
 
  session->getLex()->sql_command= SQLCOM_SELECT;
504
 
  session->getLex()->statement= new statement::Show(session);
505
 
 
506
 
  if (prepare_new_schema_table(session, session->getLex(), "PROCESSLIST"))
507
 
    return false;
508
 
 
509
 
  if (session->add_item_to_list( new Item_field(&session->getLex()->current_select->context, NULL, NULL, "*")))
510
 
    return false;
511
 
 
512
 
  (session->getLex()->current_select->with_wild)++;
513
 
 
514
 
  return true;
515
 
}
516
 
 
517
 
bool buildVariables(Session *session, const drizzled::sql_var_t is_global)
518
 
{
519
 
  session->getLex()->sql_command= SQLCOM_SELECT;
520
 
  session->getLex()->statement= new statement::Show(session);
521
 
 
522
 
  if (is_global == OPT_GLOBAL)
523
 
  {
524
 
    if (prepare_new_schema_table(session, session->getLex(), "GLOBAL_VARIABLES"))
525
 
      return false;
526
 
  }
527
 
  else
528
 
  {
529
 
    if (prepare_new_schema_table(session, session->getLex(), "SESSION_VARIABLES"))
530
 
      return false;
531
 
  }
532
 
 
533
 
  std::string key("Variable_name");
534
 
  std::string value("Value");
535
 
 
536
 
  Item_field *my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "VARIABLE_NAME");
537
 
  my_field->is_autogenerated_name= false;
538
 
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
539
 
 
540
 
  if (session->add_item_to_list(my_field))
541
 
    return false;
542
 
 
543
 
  my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "VARIABLE_VALUE");
544
 
  my_field->is_autogenerated_name= false;
545
 
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
546
 
 
547
 
  if (session->add_item_to_list(my_field))
548
 
    return false;
549
 
 
550
 
  return true;
551
 
}
552
 
 
553
 
bool buildCreateSchema(Session *session, LEX_STRING &ident)
554
 
{
555
 
  session->getLex()->sql_command= SQLCOM_SELECT;
556
 
  drizzled::statement::Show *select= new statement::Show(session);
557
 
  session->getLex()->statement= select;
558
 
 
559
 
  if (prepare_new_schema_table(session, session->getLex(), "SCHEMA_SQL_DEFINITION"))
560
 
    return false;
561
 
 
562
 
  util::string::const_shared_ptr schema(session->schema());
563
 
  if (ident.str)
564
 
  {
565
 
    select->setShowPredicate(ident.str);
566
 
  }
567
 
  else if (schema)
568
 
  {
569
 
    select->setShowPredicate(*schema);
570
 
  }
571
 
  else
572
 
  {
573
 
    my_error(ER_NO_DB_ERROR, MYF(0));
574
 
    return false;
575
 
  }
576
 
 
577
 
  std::string key("Database");
578
 
  std::string value("Create Database");
579
 
 
580
 
  Item_field *my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "SCHEMA_NAME");
581
 
  my_field->is_autogenerated_name= false;
582
 
  my_field->set_name(key.c_str(), key.length(), system_charset_info);
583
 
 
584
 
  if (session->add_item_to_list(my_field))
585
 
    return false;
586
 
 
587
 
  my_field= new Item_field(&session->getLex()->current_select->context, NULL, NULL, "SCHEMA_SQL_DEFINITION");
588
 
  my_field->is_autogenerated_name= false;
589
 
  my_field->set_name(value.c_str(), value.length(), system_charset_info);
590
 
 
591
 
  if (session->add_item_to_list(my_field))
592
 
    return false;
593
 
 
594
 
  return true;
595
 
}
596
 
 
597
 
bool buildDescribe(Session *session, Table_ident *ident)
598
 
{
599
 
  session->getLex()->lock_option= TL_READ;
600
 
  init_select(session->getLex());
601
 
  session->getLex()->current_select->parsing_place= SELECT_LIST;
602
 
  session->getLex()->sql_command= SQLCOM_SELECT;
603
 
  drizzled::statement::Show *select= new statement::Show(session);
604
 
  session->getLex()->statement= select;
605
 
  session->getLex()->select_lex.db= 0;
606
 
 
607
 
  util::string::const_shared_ptr schema(session->schema());
608
 
  if (ident->db.str)
609
 
  {
610
 
    select->setShowPredicate(ident->db.str, ident->table.str);
611
 
  }
612
 
  else if (schema)
613
 
  {
614
 
    select->setShowPredicate(*schema, ident->table.str);
615
 
  }
616
 
  else
617
 
  {
618
 
    my_error(ER_NO_DB_ERROR, MYF(0));
619
 
    return false;
620
 
  }
621
 
 
622
 
  {
623
 
    drizzled::identifier::Table identifier(select->getShowSchema().c_str(), ident->table.str);
624
 
    if (not plugin::StorageEngine::doesTableExist(*session, identifier))
625
 
    {
626
 
      my_error(ER_TABLE_UNKNOWN, identifier);
627
 
    }
628
 
  }
629
 
 
630
 
  if (prepare_new_schema_table(session, session->getLex(), "SHOW_COLUMNS"))
631
 
  {
632
 
    return false;
633
 
  }
634
 
 
635
 
  if (session->add_item_to_list( new Item_field(&session->getLex()->current_select->
636
 
                                                  context,
637
 
                                                  NULL, NULL, "*")))
638
 
  {
639
 
    return false;
640
 
  }
641
 
 
642
 
  (session->getLex()->current_select->with_wild)++;
643
 
 
644
 
  return true;
645
 
}
646
 
 
647
 
} /* namespace drizzled */
 
287
 
 
288
#define LIST_PROCESS_HOST_LEN 64
 
289
 
 
290
static bool get_field_default_value(Field *timestamp_field,
 
291
                                    Field *field, String *def_value,
 
292
                                    bool quoted)
 
293
{
 
294
  bool has_default;
 
295
  bool has_now_default;
 
296
 
 
297
  /*
 
298
     We are using CURRENT_TIMESTAMP instead of NOW because it is
 
299
     more standard
 
300
  */
 
301
  has_now_default= (timestamp_field == field &&
 
302
                    field->unireg_check != Field::TIMESTAMP_UN_FIELD);
 
303
 
 
304
  has_default= (field->type() != DRIZZLE_TYPE_BLOB &&
 
305
                !(field->flags & NO_DEFAULT_VALUE_FLAG) &&
 
306
                field->unireg_check != Field::NEXT_NUMBER);
 
307
 
 
308
  def_value->length(0);
 
309
  if (has_default)
 
310
  {
 
311
    if (has_now_default)
 
312
      def_value->append(STRING_WITH_LEN("CURRENT_TIMESTAMP"));
 
313
    else if (!field->is_null())
 
314
    {                                             // Not null by default
 
315
      char tmp[MAX_FIELD_WIDTH];
 
316
      String type(tmp, sizeof(tmp), field->charset());
 
317
      field->val_str(&type);
 
318
      if (type.length())
 
319
      {
 
320
        String def_val;
 
321
        uint32_t dummy_errors;
 
322
        /* convert to system_charset_info == utf8 */
 
323
        def_val.copy(type.ptr(), type.length(), field->charset(),
 
324
                     system_charset_info, &dummy_errors);
 
325
        if (quoted)
 
326
          append_unescaped(def_value, def_val.ptr(), def_val.length());
 
327
        else
 
328
          def_value->append(def_val.ptr(), def_val.length());
 
329
      }
 
330
      else if (quoted)
 
331
        def_value->append(STRING_WITH_LEN("''"));
 
332
    }
 
333
    else if (field->maybe_null() && quoted)
 
334
      def_value->append(STRING_WITH_LEN("NULL"));    // Null as default
 
335
    else
 
336
      return false;
 
337
  }
 
338
  return has_default;
 
339
}
 
340
 
 
341
/*
 
342
  Build a CREATE TABLE statement for a table.
 
343
 
 
344
  SYNOPSIS
 
345
    store_create_info()
 
346
    table_list        A list containing one table to write statement
 
347
                      for.
 
348
    packet            Pointer to a string where statement will be
 
349
                      written.
 
350
 
 
351
  NOTE
 
352
    Currently always return 0, but might return error code in the
 
353
    future.
 
354
 
 
355
  RETURN
 
356
    0       OK
 
357
 */
 
358
 
 
359
int store_create_info(TableList *table_list, String *packet, bool is_if_not_exists)
 
360
{
 
361
  List<Item> field_list;
 
362
  char tmp[MAX_FIELD_WIDTH], *for_str, def_value_buf[MAX_FIELD_WIDTH];
 
363
  const char *alias;
 
364
  string buff;
 
365
  String type(tmp, sizeof(tmp), system_charset_info);
 
366
  String def_value(def_value_buf, sizeof(def_value_buf), system_charset_info);
 
367
  Field **ptr,*field;
 
368
  uint32_t primary_key;
 
369
  KeyInfo *key_info;
 
370
  Table *table= table_list->table;
 
371
  Cursor *cursor= table->cursor;
 
372
  HA_CREATE_INFO create_info;
 
373
  my_bitmap_map *old_map;
 
374
 
 
375
  table->restoreRecordAsDefault(); // Get empty record
 
376
 
 
377
  if (table->getShare()->getType())
 
378
    packet->append(STRING_WITH_LEN("CREATE TEMPORARY TABLE "));
 
379
  else
 
380
    packet->append(STRING_WITH_LEN("CREATE TABLE "));
 
381
  if (is_if_not_exists)
 
382
    packet->append(STRING_WITH_LEN("IF NOT EXISTS "));
 
383
  alias= table->getShare()->getTableName();
 
384
 
 
385
  packet->append_identifier(alias, strlen(alias));
 
386
  packet->append(STRING_WITH_LEN(" (\n"));
 
387
  /*
 
388
    We need this to get default values from the table
 
389
    We have to restore the read_set if we are called from insert in case
 
390
    of row based replication.
 
391
  */
 
392
  old_map= table->use_all_columns(table->read_set);
 
393
 
 
394
  for (ptr= table->getFields() ; (field= *ptr); ptr++)
 
395
  {
 
396
    uint32_t flags = field->flags;
 
397
 
 
398
    if (ptr != table->getFields())
 
399
      packet->append(STRING_WITH_LEN(",\n"));
 
400
 
 
401
    packet->append(STRING_WITH_LEN("  "));
 
402
    packet->append_identifier(field->field_name, strlen(field->field_name));
 
403
    packet->append(' ');
 
404
    // check for surprises from the previous call to Field::sql_type()
 
405
    if (type.ptr() != tmp)
 
406
      type.set(tmp, sizeof(tmp), system_charset_info);
 
407
    else
 
408
      type.set_charset(system_charset_info);
 
409
 
 
410
    field->sql_type(type);
 
411
    packet->append(type.ptr(), type.length(), system_charset_info);
 
412
 
 
413
    if (field->has_charset())
 
414
    {
 
415
      /*
 
416
        For string types dump collation name only if
 
417
        collation is not primary for the given charset
 
418
      */
 
419
      if (!(field->charset()->state & MY_CS_PRIMARY))
 
420
      {
 
421
        packet->append(STRING_WITH_LEN(" COLLATE "));
 
422
        packet->append(field->charset()->name);
 
423
      }
 
424
    }
 
425
 
 
426
    if (flags & NOT_NULL_FLAG)
 
427
      packet->append(STRING_WITH_LEN(" NOT NULL"));
 
428
    else if (field->type() == DRIZZLE_TYPE_TIMESTAMP)
 
429
    {
 
430
      /*
 
431
        TIMESTAMP field require explicit NULL flag, because unlike
 
432
        all other fields they are treated as NOT NULL by default.
 
433
      */
 
434
      packet->append(STRING_WITH_LEN(" NULL"));
 
435
    }
 
436
    {
 
437
      /*
 
438
        Add field flags about FIELD FORMAT (FIXED or DYNAMIC)
 
439
        and about STORAGE (DISK or MEMORY).
 
440
      */
 
441
      enum column_format_type column_format= (enum column_format_type)
 
442
        ((flags >> COLUMN_FORMAT_FLAGS) & COLUMN_FORMAT_MASK);
 
443
      if (column_format)
 
444
      {
 
445
        packet->append(STRING_WITH_LEN(" /*!"));
 
446
        packet->append(STRING_WITH_LEN(" COLUMN_FORMAT"));
 
447
        if (column_format == COLUMN_FORMAT_TYPE_FIXED)
 
448
          packet->append(STRING_WITH_LEN(" FIXED */"));
 
449
        else
 
450
          packet->append(STRING_WITH_LEN(" DYNAMIC */"));
 
451
      }
 
452
    }
 
453
    if (get_field_default_value(table->timestamp_field, field, &def_value, 1))
 
454
    {
 
455
      packet->append(STRING_WITH_LEN(" DEFAULT "));
 
456
      packet->append(def_value.ptr(), def_value.length(), system_charset_info);
 
457
    }
 
458
 
 
459
    if (table->timestamp_field == field && field->unireg_check != Field::TIMESTAMP_DN_FIELD)
 
460
      packet->append(STRING_WITH_LEN(" ON UPDATE CURRENT_TIMESTAMP"));
 
461
 
 
462
    if (field->unireg_check == Field::NEXT_NUMBER)
 
463
      packet->append(STRING_WITH_LEN(" AUTO_INCREMENT"));
 
464
 
 
465
    if (field->comment.length)
 
466
    {
 
467
      packet->append(STRING_WITH_LEN(" COMMENT "));
 
468
      append_unescaped(packet, field->comment.str, field->comment.length);
 
469
    }
 
470
  }
 
471
 
 
472
  key_info= table->key_info;
 
473
  memset(&create_info, 0, sizeof(create_info));
 
474
 
 
475
  primary_key= table->getShare()->getPrimaryKey();
 
476
 
 
477
  for (uint32_t i=0 ; i < table->getShare()->sizeKeys() ; i++,key_info++)
 
478
  {
 
479
    KeyPartInfo *key_part= key_info->key_part;
 
480
    bool found_primary=0;
 
481
    packet->append(STRING_WITH_LEN(",\n  "));
 
482
 
 
483
    if (i == primary_key && is_primary_key(key_info))
 
484
    {
 
485
      found_primary=1;
 
486
      /*
 
487
        No space at end, because a space will be added after where the
 
488
        identifier would go, but that is not added for primary key.
 
489
      */
 
490
      packet->append(STRING_WITH_LEN("PRIMARY KEY"));
 
491
    }
 
492
    else if (key_info->flags & HA_NOSAME)
 
493
      packet->append(STRING_WITH_LEN("UNIQUE KEY "));
 
494
    else
 
495
      packet->append(STRING_WITH_LEN("KEY "));
 
496
 
 
497
    if (!found_primary)
 
498
     packet->append_identifier(key_info->name, strlen(key_info->name));
 
499
 
 
500
    packet->append(STRING_WITH_LEN(" ("));
 
501
 
 
502
    for (uint32_t j=0 ; j < key_info->key_parts ; j++,key_part++)
 
503
    {
 
504
      if (j)
 
505
        packet->append(',');
 
506
 
 
507
      if (key_part->field)
 
508
        packet->append_identifier(key_part->field->field_name,
 
509
                                  strlen(key_part->field->field_name));
 
510
      if (key_part->field &&
 
511
          (key_part->length !=
 
512
           table->getField(key_part->fieldnr-1)->key_length()))
 
513
      {
 
514
        buff.assign("(");
 
515
        buff.append(to_string((int32_t) key_part->length /
 
516
                              key_part->field->charset()->mbmaxlen));
 
517
        buff.append(")");
 
518
        packet->append(buff.c_str(), buff.length());
 
519
      }
 
520
    }
 
521
    packet->append(')');
 
522
    store_key_options(packet, table, key_info);
 
523
  }
 
524
 
 
525
  /*
 
526
    Get possible foreign key definitions stored in InnoDB and append them
 
527
    to the CREATE TABLE statement
 
528
  */
 
529
 
 
530
  if ((for_str= cursor->get_foreign_key_create_info()))
 
531
  {
 
532
    packet->append(for_str, strlen(for_str));
 
533
    cursor->free_foreign_key_create_info(for_str);
 
534
  }
 
535
 
 
536
  packet->append(STRING_WITH_LEN("\n)"));
 
537
  {
 
538
    /*
 
539
      Get possible table space definitions and append them
 
540
      to the CREATE TABLE statement
 
541
    */
 
542
 
 
543
    /* 
 
544
      We should always store engine since we will now be 
 
545
      making sure engines accept options (aka... no
 
546
      dangling arguments for engines.
 
547
    */
 
548
    packet->append(STRING_WITH_LEN(" ENGINE="));
 
549
    packet->append(cursor->getEngine()->getName().c_str());
 
550
 
 
551
    size_t num_engine_options= table->getShare()->getTableProto()->engine().options_size();
 
552
    for (size_t x= 0; x < num_engine_options; ++x)
 
553
    {
 
554
      const message::Engine::Option &option= table->getShare()->getTableProto()->engine().options(x);
 
555
      packet->append(" ");
 
556
      packet->append(option.name().c_str());
 
557
      packet->append("=");
 
558
      append_unescaped(packet, option.state().c_str(), option.state().length());
 
559
    }
 
560
 
 
561
    if (table->getShare()->block_size)
 
562
    {
 
563
      packet->append(STRING_WITH_LEN(" BLOCK_SIZE="));
 
564
      buff= to_string(table->getShare()->block_size);
 
565
      packet->append(buff.c_str(), buff.length());
 
566
    }
 
567
    table->cursor->append_create_info(packet);
 
568
    if (table->getMutableShare()->hasComment() && table->getMutableShare()->getCommentLength())
 
569
    {
 
570
      packet->append(STRING_WITH_LEN(" COMMENT="));
 
571
      append_unescaped(packet, table->getMutableShare()->getComment(),
 
572
                       table->getMutableShare()->getCommentLength());
 
573
    }
 
574
  }
 
575
  table->restore_column_map(old_map);
 
576
  return(0);
 
577
}
 
578
 
 
579
static void store_key_options(String *packet, Table *, KeyInfo *key_info)
 
580
{
 
581
  if (key_info->algorithm == HA_KEY_ALG_BTREE)
 
582
    packet->append(STRING_WITH_LEN(" USING BTREE"));
 
583
 
 
584
  if (key_info->algorithm == HA_KEY_ALG_HASH)
 
585
    packet->append(STRING_WITH_LEN(" USING HASH"));
 
586
 
 
587
  assert(test(key_info->flags & HA_USES_COMMENT) ==
 
588
              (key_info->comment.length > 0));
 
589
  if (key_info->flags & HA_USES_COMMENT)
 
590
  {
 
591
    packet->append(STRING_WITH_LEN(" COMMENT "));
 
592
    append_unescaped(packet, key_info->comment.str,
 
593
                     key_info->comment.length);
 
594
  }
 
595
}
 
596
 
 
597
 
 
598
/****************************************************************************
 
599
  Return info about all processes
 
600
  returns for each thread: thread id, user, host, db, command, info
 
601
****************************************************************************/
 
602
 
 
603
class thread_info
 
604
{
 
605
  thread_info();
 
606
public:
 
607
  uint64_t thread_id;
 
608
  time_t start_time;
 
609
  uint32_t   command;
 
610
  string user;
 
611
  string host;
 
612
  string db;
 
613
  string proc_info;
 
614
  string state_info;
 
615
  string query;
 
616
  thread_info(uint64_t thread_id_arg,
 
617
              time_t start_time_arg,
 
618
              uint32_t command_arg,
 
619
              const string &user_arg,
 
620
              const string &host_arg,
 
621
              const string &db_arg,
 
622
              const string &proc_info_arg,
 
623
              const string &state_info_arg,
 
624
              const string &query_arg)
 
625
    : thread_id(thread_id_arg), start_time(start_time_arg), command(command_arg),
 
626
      user(user_arg), host(host_arg), db(db_arg), proc_info(proc_info_arg),
 
627
      state_info(state_info_arg), query(query_arg)
 
628
  {}
 
629
};
648
630
 
649
631
} /* namespace drizzled */