~drizzle-trunk/drizzle/development

1063.4.5 by Padraig O'Sullivan
Fixed a stupid mistake I made in the innodb handler. Also reverted back to
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; version 2 of the License.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU General Public License
16
 *  along with this program; if not, write to the Free Software
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 */
1 by brian
clean slate
19
20
21
/* Function with list databases, tables or fields */
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
22
#include "config.h"
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
23
#include <drizzled/sql_select.h>
575.4.7 by Monty Taylor
More header cleanup.
24
#include <drizzled/show.h>
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
25
#include <drizzled/gettext.h>
492.1.8 by Monty Taylor
Replaced some int10_to_str with to_string.
26
#include <drizzled/util/convert.h>
550 by Monty Taylor
Moved error.h into just the files that need it.
27
#include <drizzled/error.h>
520.4.14 by Monty Taylor
Removed korr.h and tztime.h from common_includes. Also removed the HAVE_DTRACE block and stuck it in autoconf.
28
#include <drizzled/tztime.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
29
#include <drizzled/data_home.h>
642.1.63 by Lee
more header file cleanup
30
#include <drizzled/item/blob.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
31
#include <drizzled/item/cmpfunc.h>
642.1.17 by Lee
header file clean up
32
#include <drizzled/item/return_int.h>
642.1.23 by Lee
more header file cleanup
33
#include <drizzled/item/empty_string.h>
642.1.18 by Lee
header file clean up
34
#include <drizzled/item/return_date_time.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
35
#include <drizzled/sql_base.h>
36
#include <drizzled/db.h>
584.5.1 by Monty Taylor
Removed field includes from field.h.
37
#include <drizzled/field/timestamp.h>
670.1.1 by Monty Taylor
Renamed fdecimal.* to decimal.*. Let's see how many things we can break!
38
#include <drizzled/field/decimal.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
39
#include <drizzled/lock.h>
675 by Brian Aker
Cleanup around item includes.
40
#include <drizzled/item/return_date_time.h>
41
#include <drizzled/item/empty_string.h>
1241.9.17 by Monty Taylor
Removed more bits from server_includes.
42
#include "drizzled/session_list.h"
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
43
#include <drizzled/message/schema.pb.h>
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
44
#include <drizzled/plugin/client.h>
1241.9.44 by Monty Taylor
Made magic with cached_directory.
45
#include <drizzled/cached_directory.h>
1241.9.23 by Monty Taylor
Removed sql_table.h from server_includes.h.
46
#include "drizzled/sql_table.h"
1241.9.28 by Monty Taylor
Removed global_charset_info.h from server_includes.h
47
#include "drizzled/global_charset_info.h"
1241.9.31 by Monty Taylor
Moved global pthread variables into their own header.
48
#include "drizzled/pthread_globals.h"
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
49
#include "drizzled/internal/m_string.h"
50
#include "drizzled/internal/my_sys.h"
1638.10.2 by Stewart Smith
for store_create_info() (called in SHOW CREATE TABLE code path) call transformTableDefinitionToSql() in statement_transform lib instead of doing manual things on Table. This is the initial step in getting SHOW CREATE TABLE to be run from proto.
51
#include "drizzled/message/statement_transform.h"
52
1241.9.28 by Monty Taylor
Removed global_charset_info.h from server_includes.h
53
1108.4.2 by David Shrewsbury
Replace my_dir in find_files() with CachedDirectory. Renamed find_files() to find_dirs() to more accurately reflect what it is doing after removing dead code inside it.
54
#include <sys/stat.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
55
56
#include <string>
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
57
#include <iostream>
58
#include <sstream>
901.1.1 by Padraig
Initial work to replace an instance of DYNAMIC_ARRAY in show.cc with STL
59
#include <vector>
60
#include <algorithm>
629.4.1 by Monty Taylor
First step in support size_t sys_var stuff.
61
62
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
63
64
namespace drizzled
65
{
1 by brian
clean slate
66
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
67
inline const char *
68
str_or_nil(const char *str)
69
{
70
  return str ? str : "<nil>";
71
}
1 by brian
clean slate
72
1273.13.38 by Brian Aker
Add in new show work.
73
int wild_case_compare(const CHARSET_INFO * const cs, const char *str, const char *wildstr)
1 by brian
clean slate
74
{
75
  register int flag;
1273.13.61 by Brian Aker
Fixes a memory leak. sql_string() sucks.
76
1 by brian
clean slate
77
  while (*wildstr)
78
  {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
79
    while (*wildstr && *wildstr != internal::wild_many && *wildstr != internal::wild_one)
1 by brian
clean slate
80
    {
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
81
      if (*wildstr == internal::wild_prefix && wildstr[1])
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
82
        wildstr++;
83
      if (my_toupper(cs, *wildstr++) != my_toupper(cs, *str++))
1067.3.1 by Padraig O'Sullivan
Extracted the CHARACTER_SET I_S table into the I_S plugin.
84
        return (1);
1 by brian
clean slate
85
    }
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
86
    if (! *wildstr )
87
      return (*str != 0);
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
88
    if (*wildstr++ == internal::wild_one)
1 by brian
clean slate
89
    {
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
90
      if (! *str++)
91
        return (1);	/* One char; skip */
1 by brian
clean slate
92
    }
93
    else
94
    {						/* Found '*' */
1067.3.1 by Padraig O'Sullivan
Extracted the CHARACTER_SET I_S table into the I_S plugin.
95
      if (! *wildstr)
96
        return (0);		/* '*' as last char: OK */
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
97
      flag=(*wildstr != internal::wild_many && *wildstr != internal::wild_one);
1 by brian
clean slate
98
      do
99
      {
1067.3.1 by Padraig O'Sullivan
Extracted the CHARACTER_SET I_S table into the I_S plugin.
100
        if (flag)
101
        {
102
          char cmp;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
103
          if ((cmp= *wildstr) == internal::wild_prefix && wildstr[1])
1067.3.1 by Padraig O'Sullivan
Extracted the CHARACTER_SET I_S table into the I_S plugin.
104
            cmp= wildstr[1];
105
          cmp= my_toupper(cs, cmp);
106
          while (*str && my_toupper(cs, *str) != cmp)
107
            str++;
108
          if (! *str)
109
            return (1);
110
        }
111
        if (wild_case_compare(cs, str, wildstr) == 0)
112
          return (0);
113
      } while (*str++);
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
114
      return (1);
1 by brian
clean slate
115
    }
116
  }
1273.13.61 by Brian Aker
Fixes a memory leak. sql_string() sucks.
117
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
118
  return (*str != '\0');
1 by brian
clean slate
119
}
120
121
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
122
bool drizzled_show_create(Session *session, TableList *table_list, bool is_if_not_exists)
1 by brian
clean slate
123
{
124
  char buff[2048];
125
  String buffer(buff, sizeof(buff), system_charset_info);
126
127
  /* Only one table for now, but VIEW can involve several tables */
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
128
  if (session->openTables(table_list))
1 by brian
clean slate
129
  {
737 by Brian Aker
Updates for dead code removal (and forced assert() in delete).
130
    if (session->is_error())
1014.3.4 by Brian Aker
Remove dead session calls.
131
      return true;
1 by brian
clean slate
132
133
    /*
134
      Clear all messages with 'error' level status and
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
135
      issue a warning with 'warning' level status in
1 by brian
clean slate
136
      case of invalid view and last error is ER_VIEW_INVALID
137
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
138
    drizzle_reset_errors(session, true);
139
    session->clear_error();
1 by brian
clean slate
140
  }
141
142
  buffer.length(0);
143
1222.1.3 by Brian Aker
Remove used flag for engine.
144
  if (store_create_info(table_list, &buffer, is_if_not_exists))
1014.3.4 by Brian Aker
Remove dead session calls.
145
    return true;
1 by brian
clean slate
146
147
  List<Item> field_list;
148
  {
149
    field_list.push_back(new Item_empty_string("Table",NAME_CHAR_LEN));
150
    // 1024 is for not to confuse old clients
151
    field_list.push_back(new Item_empty_string("Create Table",
1067.4.7 by Nathan Williams
The remaining files using cmax have been converted to std::max.
152
                                               max(buffer.length(),(uint32_t)1024)));
1 by brian
clean slate
153
  }
154
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
155
  if (session->client->sendFields(&field_list))
1014.3.4 by Brian Aker
Remove dead session calls.
156
    return true;
1 by brian
clean slate
157
  {
1669.2.6 by Brian Aker
First pass through encapsulating getAlias() from table.
158
    session->client->store(table_list->table->getAlias());
1 by brian
clean slate
159
  }
160
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
161
  session->client->store(buffer.ptr(), buffer.length());
1 by brian
clean slate
162
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
163
  if (session->client->flush())
1014.3.4 by Brian Aker
Remove dead session calls.
164
    return true;
1 by brian
clean slate
165
836 by Brian Aker
Fixed session call from function to method.
166
  session->my_eof();
1014.3.4 by Brian Aker
Remove dead session calls.
167
  return false;
1 by brian
clean slate
168
}
169
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
170
/**
171
  Get a CREATE statement for a given database.
172
173
  The database is identified by its name, passed as @c dbname parameter.
174
  The name should be encoded using the system character set (UTF8 currently).
175
176
  Resulting statement is stored in the string pointed by @c buffer. The string
177
  is emptied first and its character set is set to the system character set.
178
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
179
  If is_if_not_exists is set, then
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
180
  the resulting CREATE statement contains "IF NOT EXISTS" clause. Other flags
181
  in @c create_options are ignored.
182
183
  @param  session           The current thread instance.
184
  @param  dbname        The name of the database.
185
  @param  buffer        A String instance where the statement is stored.
186
  @param  create_info   If not NULL, the options member influences the resulting
187
                        CRATE statement.
188
189
  @returns true if errors are detected, false otherwise.
190
*/
191
1415 by Brian Aker
Mass overhaul to use schema_identifier.
192
static bool store_db_create_info(SchemaIdentifier &schema_identifier, string &buffer, bool if_not_exists)
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
193
{
1118.2.1 by Stewart Smith
fix drizzled::message::Table usage so that in kernel .cc files we are 'using namespace drizzled'
194
  message::Schema schema;
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
195
1415 by Brian Aker
Mass overhaul to use schema_identifier.
196
  bool found= plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema);
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
197
  if (not found)
198
    return false;
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
199
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
200
  buffer.append("CREATE DATABASE ");
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
201
202
  if (if_not_exists)
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
203
    buffer.append("IF NOT EXISTS ");
204
205
  buffer.append("`");
206
  buffer.append(schema.name());
207
  buffer.append("`");
208
209
  if (schema.has_collation())
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
210
  {
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
211
    buffer.append(" COLLATE = ");
212
    buffer.append(schema.collation());
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
213
  }
214
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
215
  return true;
1089.10.1 by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane.
216
}
217
1415 by Brian Aker
Mass overhaul to use schema_identifier.
218
bool mysqld_show_create_db(Session &session, SchemaIdentifier &schema_identifier, bool if_not_exists)
1 by brian
clean slate
219
{
1415 by Brian Aker
Mass overhaul to use schema_identifier.
220
  message::Schema schema_message;
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
221
  string buffer;
1 by brian
clean slate
222
1415 by Brian Aker
Mass overhaul to use schema_identifier.
223
  if (not plugin::StorageEngine::getSchemaDefinition(schema_identifier, schema_message))
224
  {
225
    /*
226
      This assumes that the only reason for which store_db_create_info()
227
      can fail is incorrect database name (which is the case now).
228
    */
229
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
230
    return true;
231
  }
232
233
  if (not store_db_create_info(schema_identifier, buffer, if_not_exists))
234
  {
235
    /*
236
      This assumes that the only reason for which store_db_create_info()
237
      can fail is incorrect database name (which is the case now).
238
    */
239
    my_error(ER_BAD_DB_ERROR, MYF(0), schema_identifier.getSQLPath().c_str());
1014.3.4 by Brian Aker
Remove dead session calls.
240
    return true;
1 by brian
clean slate
241
  }
242
243
  List<Item> field_list;
244
  field_list.push_back(new Item_empty_string("Database",NAME_CHAR_LEN));
245
  field_list.push_back(new Item_empty_string("Create Database",1024));
246
1415 by Brian Aker
Mass overhaul to use schema_identifier.
247
  if (session.client->sendFields(&field_list))
248
    return true;
249
250
  session.client->store(schema_message.name());
251
  session.client->store(buffer);
252
253
  if (session.client->flush())
254
    return true;
255
256
  session.my_eof();
1273.19.28 by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it...
257
1014.3.4 by Brian Aker
Remove dead session calls.
258
  return false;
1 by brian
clean slate
259
}
260
261
/*
262
  Get the quote character for displaying an identifier.
263
264
  SYNOPSIS
265
    get_quote_char_for_identifier()
266
267
  IMPLEMENTATION
268
    Force quoting in the following cases:
269
      - name is empty (for one, it is possible when we use this function for
270
        quoting user and host names for DEFINER clause);
271
      - name is a keyword;
272
      - name includes a special character;
273
    Otherwise identifier is quoted only if the option OPTION_QUOTE_SHOW_CREATE
274
    is set.
275
276
  RETURN
277
    EOF	  No quote character is needed
278
    #	  Quote character
279
*/
280
1014.3.4 by Brian Aker
Remove dead session calls.
281
int get_quote_char_for_identifier()
1 by brian
clean slate
282
{
352.2.1 by Harrison Fisk
Fix for bugs 259843 and 256482
283
  return '`';
1 by brian
clean slate
284
}
285
286
287
#define LIST_PROCESS_HOST_LEN 64
288
289
/*
290
  Build a CREATE TABLE statement for a table.
291
292
  SYNOPSIS
293
    store_create_info()
294
    table_list        A list containing one table to write statement
295
                      for.
296
    packet            Pointer to a string where statement will be
297
                      written.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
298
1 by brian
clean slate
299
  NOTE
300
    Currently always return 0, but might return error code in the
301
    future.
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
302
1 by brian
clean slate
303
  RETURN
304
    0       OK
305
 */
306
1222.1.3 by Brian Aker
Remove used flag for engine.
307
int store_create_info(TableList *table_list, String *packet, bool is_if_not_exists)
1 by brian
clean slate
308
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
309
  Table *table= table_list->table;
1 by brian
clean slate
310
997.5.1 by chris
Replace macros around unireg.h, store_record,restore_record,cmp_record,empty_record
311
  table->restoreRecordAsDefault(); // Get empty record
1 by brian
clean slate
312
1638.10.2 by Stewart Smith
for store_create_info() (called in SHOW CREATE TABLE code path) call transformTableDefinitionToSql() in statement_transform lib instead of doing manual things on Table. This is the initial step in getting SHOW CREATE TABLE to be run from proto.
313
  string create_sql;
314
315
  enum drizzled::message::TransformSqlError transform_err;
316
317
  (void)is_if_not_exists;
318
319
  transform_err= message::transformTableDefinitionToSql(*(table->getShare()->getTableProto()),
320
                                                        create_sql,
321
                                                        message::DRIZZLE,
322
                                                        false);
323
1638.10.28 by Stewart Smith
fix a lot of quoting and escaping of things in SHOW CREATE TABLE in statement_transform
324
  packet->append(create_sql.c_str(), create_sql.length(), default_charset_info);
1638.10.49 by Stewart Smith
merge trunk
325
51.1.1 by Jay Pipes
Merged PatG's removal of various DBUG stuff with still keeping DBUG_ASSERT calls since they seem to be breaking test runs
326
  return(0);
1 by brian
clean slate
327
}
328
329
/****************************************************************************
330
  Return info about all processes
331
  returns for each thread: thread id, user, host, db, command, info
332
****************************************************************************/
333
1245.2.1 by Monty Taylor
Removed final use if I_List<> in the server.
334
class thread_info
335
{
336
  thread_info();
1 by brian
clean slate
337
public:
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
338
  uint64_t thread_id;
1 by brian
clean slate
339
  time_t start_time;
482 by Brian Aker
Remove uint.
340
  uint32_t   command;
1292 by Brian Aker
Merge in security refactor.
341
  string user;
342
  string host;
343
  string db;
344
  string proc_info;
345
  string state_info;
346
  string query;
1245.2.7 by Monty Taylor
Merged up with trunk.
347
  thread_info(uint64_t thread_id_arg,
348
              time_t start_time_arg,
349
              uint32_t command_arg,
1245.2.1 by Monty Taylor
Removed final use if I_List<> in the server.
350
              const string &user_arg,
351
              const string &host_arg,
352
              const string &db_arg,
353
              const string &proc_info_arg,
354
              const string &state_info_arg,
355
              const string &query_arg)
356
    : thread_id(thread_id_arg), start_time(start_time_arg), command(command_arg),
357
      user(user_arg), host(host_arg), db(db_arg), proc_info(proc_info_arg),
358
      state_info(state_info_arg), query(query_arg)
359
  {}
1 by brian
clean slate
360
};
361
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
362
} /* namespace drizzled */