~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2004 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
1802.10.2 by Monty Taylor
Update all of the copyright headers to include the correct address.
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
1 by brian
clean slate
15
16
/* drop and alter of tables */
17
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
18
#include "config.h"
992.1.25 by Monty Taylor
Moved myisam to new plugin system.
19
#include <plugin/myisam/myisam.h>
575.4.7 by Monty Taylor
More header cleanup.
20
#include <drizzled/show.h>
549 by Monty Taylor
Took gettext.h out of header files.
21
#include <drizzled/error.h>
538 by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib.
22
#include <drizzled/gettext.h>
520.6.7 by Monty Taylor
Moved a bunch of crap out of common_includes.
23
#include <drizzled/data_home.h>
520.8.2 by Monty Taylor
Moved sql_parse.h and sql_error.h out of common_includes.
24
#include <drizzled/sql_parse.h>
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
25
#include <drizzled/my_hash.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.
26
#include <drizzled/sql_lex.h>
27
#include <drizzled/session.h>
28
#include <drizzled/sql_base.h>
1241.9.12 by Monty Taylor
Trims more out of server_includes.h.
29
#include "drizzled/strfunc.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.
30
#include <drizzled/db.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
31
#include <drizzled/lock.h>
32
#include <drizzled/unireg.h>
642.1.21 by Lee
header file clean up
33
#include <drizzled/item/int.h>
675 by Brian Aker
Cleanup around item includes.
34
#include <drizzled/item/empty_string.h>
1336.2.2 by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers.
35
#include <drizzled/transaction_services.h>
1273.1.2 by Jay Pipes
This patch does not change any algorithms or code paths,
36
#include "drizzled/transaction_services.h"
1095.3.2 by Stewart Smith
remove copy_table_proto_file and replace with read and write proto calls. affects CREATE TABLE LIKE
37
#include <drizzled/table_proto.h>
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
38
#include <drizzled/plugin/client.h>
1660.1.1 by Brian Aker
Merge in move identifier work.
39
#include <drizzled/identifier.h>
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
40
#include "drizzled/internal/m_string.h"
1241.9.28 by Monty Taylor
Removed global_charset_info.h from server_includes.h
41
#include "drizzled/global_charset_info.h"
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
42
#include "drizzled/charset.h"
1241.9.28 by Monty Taylor
Removed global_charset_info.h from server_includes.h
43
1938.4.4 by Brian Aker
Remove dead getShare() call which should have been a call on the cache
44
#include "drizzled/definition/cache.h"
45
988.1.6 by Jay Pipes
Removed old protobuf_replicator plugin, fixed up db.cc and other files to use new
46
1225.1.4 by Padraig O'Sullivan
The alter_table header file still needs to be included by sql_table
47
#include "drizzled/statement/alter_table.h"
1241.9.23 by Monty Taylor
Removed sql_table.h from server_includes.h.
48
#include "drizzled/sql_table.h"
1241.9.31 by Monty Taylor
Moved global pthread variables into their own header.
49
#include "drizzled/pthread_globals.h"
1225.1.4 by Padraig O'Sullivan
The alter_table header file still needs to be included by sql_table
50
1067.4.4 by Nathan Williams
The rest of the files in the drizzled directory were purged of the cmin macro and replace with std::min (except for the definition in globals.h and 1 usage in stacktrace.cc).
51
#include <algorithm>
1241.9.17 by Monty Taylor
Removed more bits from server_includes.
52
#include <sstream>
1001.1.3 by Andrew Ettinger
Revert bad spacing
53
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
54
#include <boost/unordered_set.hpp>
55
670.3.3 by Toru Maesaka
Added namespacing for std to .cc files that needed it
56
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
57
58
namespace drizzled
59
{
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
60
1241.9.31 by Monty Taylor
Moved global pthread variables into their own header.
61
extern pid_t current_pid;
62
1535 by Brian Aker
Rename of KEY to KeyInfo
63
bool is_primary_key(KeyInfo *key_info)
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
64
{
65
  static const char * primary_key_name="PRIMARY";
66
  return (strcmp(key_info->name, primary_key_name)==0);
67
}
68
69
const char* is_primary_key_name(const char* key_name)
70
{
71
  static const char * primary_key_name="PRIMARY";
72
  if (strcmp(key_name, primary_key_name)==0)
73
    return key_name;
74
  else
75
    return NULL;
76
}
1 by brian
clean slate
77
1535 by Brian Aker
Rename of KEY to KeyInfo
78
static bool check_if_keyname_exists(const char *name,KeyInfo *start, KeyInfo *end);
79
static char *make_unique_key_name(const char *field_name,KeyInfo *start,KeyInfo *end);
1 by brian
clean slate
80
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
81
static bool prepare_blob_field(Session *session, CreateField *sql_field);
1 by brian
clean slate
82
1223.4.3 by Brian Aker
More identifier work.
83
void set_table_default_charset(HA_CREATE_INFO *create_info, const char *db)
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
84
{
85
  /*
86
    If the table character set was not given explicitly,
87
    let's fetch the database default character set and
88
    apply it to the table.
89
  */
1415 by Brian Aker
Mass overhaul to use schema_identifier.
90
  SchemaIdentifier identifier(db);
1014.3.1 by Brian Aker
Simplify the calling stack for getting schema collation. We need to extend
91
  if (create_info->default_table_charset == NULL)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
92
    create_info->default_table_charset= plugin::StorageEngine::getSchemaCollation(identifier);
820.1.11 by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex.
93
}
94
1 by brian
clean slate
95
/*
96
  SYNOPSIS
97
    write_bin_log()
520.1.22 by Brian Aker
Second pass of thd cleanup
98
    session                           Thread object
1 by brian
clean slate
99
    query                         Query to log
100
    query_length                  Length of query
101
102
  RETURN VALUES
103
    NONE
104
105
  DESCRIPTION
106
    Write the binlog if open, routine used in multiple places in this
1208.3.2 by brian
Update for Cursor renaming.
107
    cursor
1 by brian
clean slate
108
*/
109
1921.4.13 by Brian Aker
Fix issue where session info might not be correct.
110
void write_bin_log(Session *session, const std::string &query)
1 by brian
clean slate
111
{
1336.2.2 by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers.
112
  TransactionServices &transaction_services= TransactionServices::singleton();
113
  transaction_services.rawStatement(session, query);
1 by brian
clean slate
114
}
115
116
/*
117
  Execute the drop of a normal or temporary table
118
119
  SYNOPSIS
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
120
    rm_table_part2()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
121
    session			Thread Cursor
1 by brian
clean slate
122
    tables		Tables to drop
123
    if_exists		If set, don't give an error if table doesn't exists.
124
			In this case we give an warning of level 'NOTE'
125
    drop_temporary	Only drop temporary tables
126
1672.3.4 by Brian Aker
This change the style on a few TODO, and fixes an error path to correctly
127
  @todo
1 by brian
clean slate
128
    When logging to the binary log, we should log
129
    tmp_tables and transactional tables as separate statements if we
130
    are in a transaction;  This is needed to get these tables into the
131
    cached binary log that is only written on COMMIT.
132
133
   The current code only writes DROP statements that only uses temporary
134
   tables to the cache binary log.  This should be ok on most cases, but
135
   not all.
136
137
 RETURN
138
   0	ok
139
   1	Error
140
   -1	Thread was killed
141
*/
142
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
143
int rm_table_part2(Session *session, TableList *tables, bool if_exists,
1223.4.10 by Brian Aker
Split remove table into 2 parts. The piece for dropDatabase() needs to be
144
                         bool drop_temporary)
1 by brian
clean slate
145
{
327.2.4 by Brian Aker
Refactoring table.h
146
  TableList *table;
1 by brian
clean slate
147
  String wrong_tables;
148
  int error= 0;
1172 by Brian Aker
Update to delete table to centralize the replication logic.
149
  bool foreign_key_error= false;
1 by brian
clean slate
150
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
151
  do
1960.1.2 by Brian Aker
Move mutex behind scoped ().
152
  {
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
153
    boost::mutex::scoped_lock scopedLock(table::Cache::singleton().mutex());
1960.1.2 by Brian Aker
Move mutex behind scoped ().
154
155
    if (not drop_temporary && session->lock_table_names_exclusively(tables))
156
    {
157
      return 1;
1 by brian
clean slate
158
    }
159
1960.1.2 by Brian Aker
Move mutex behind scoped ().
160
    /* Don't give warnings for not found errors, as we already generate notes */
161
    session->no_warnings_for_error= 1;
162
163
    for (table= tables; table; table= table->next_local)
1 by brian
clean slate
164
    {
1960.1.2 by Brian Aker
Move mutex behind scoped ().
165
      TableIdentifier tmp_identifier(table->getSchemaName(), table->getTableName());
166
167
      error= session->drop_temporary_table(tmp_identifier);
168
169
      switch (error) {
170
      case  0:
171
        // removed temporary table
172
        continue;
173
      case -1:
174
        error= 1;
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
175
        break;
1960.1.2 by Brian Aker
Move mutex behind scoped ().
176
      default:
177
        // temporary table not found
1308.2.4 by Jay Pipes
Adds DROP TABLE to the list of non RAW_SQL statements in replication stream
178
        error= 0;
1960.1.2 by Brian Aker
Move mutex behind scoped ().
179
      }
180
181
      if (drop_temporary == false)
182
      {
183
        Table *locked_table;
184
        abort_locked_tables(session, tmp_identifier);
185
        table::Cache::singleton().removeTable(session, tmp_identifier,
186
                                              RTFC_WAIT_OTHER_THREAD_FLAG |
187
                                              RTFC_CHECK_KILLED_FLAG);
188
        /*
189
          If the table was used in lock tables, remember it so that
190
          unlock_table_names can free it
191
        */
192
        if ((locked_table= drop_locked_tables(session, tmp_identifier)))
193
          table->table= locked_table;
194
195
        if (session->getKilled())
196
        {
197
          error= -1;
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
198
          break;
1960.1.2 by Brian Aker
Move mutex behind scoped ().
199
        }
200
      }
201
      TableIdentifier identifier(table->getSchemaName(), table->getTableName(), table->getInternalTmpTable() ? message::Table::INTERNAL : message::Table::STANDARD);
202
203
      if (drop_temporary || not plugin::StorageEngine::doesTableExist(*session, identifier))
204
      {
205
        // Table was not found on disk and table can't be created from engine
206
        if (if_exists)
207
          push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
208
                              ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR),
209
                              table->getTableName());
210
        else
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
211
        {
1960.1.2 by Brian Aker
Move mutex behind scoped ().
212
          error= 1;
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
213
        }
1960.1.2 by Brian Aker
Move mutex behind scoped ().
214
      }
215
      else
216
      {
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
217
        drizzled::error_t local_error;
1960.1.2 by Brian Aker
Move mutex behind scoped ().
218
2016.3.1 by David Shrewsbury
No longer pass DROP SCHEMA/TABLE through the replication stream if it uses IF EXISTS and does not drop anything. Fix up test cases for this change.
219
        /* Generate transaction event ONLY when we successfully drop */ 
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
220
        if (plugin::StorageEngine::dropTable(*session, identifier, local_error))
2016.3.1 by David Shrewsbury
No longer pass DROP SCHEMA/TABLE through the replication stream if it uses IF EXISTS and does not drop anything. Fix up test cases for this change.
221
        {
222
          TransactionServices &transaction_services= TransactionServices::singleton();
2048.1.2 by David Shrewsbury
Change TransactionServices::dropTable() to take a TableIdentifier rather than strings.
223
          transaction_services.dropTable(session, identifier, if_exists);
2016.3.1 by David Shrewsbury
No longer pass DROP SCHEMA/TABLE through the replication stream if it uses IF EXISTS and does not drop anything. Fix up test cases for this change.
224
        }
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
225
        else
226
        {
227
          if (local_error == HA_ERR_NO_SUCH_TABLE and if_exists)
228
          {
229
            error= 0;
230
            session->clear_error();
231
          }
232
233
          if (local_error == HA_ERR_ROW_IS_REFERENCED)
234
          {
235
            /* the table is referenced by a foreign key constraint */
236
            foreign_key_error= true;
237
          }
238
          error= local_error;
1960.1.2 by Brian Aker
Move mutex behind scoped ().
239
        }
240
      }
241
242
      if (error)
243
      {
244
        if (wrong_tables.length())
245
          wrong_tables.append(',');
246
        wrong_tables.append(String(table->getTableName(), system_charset_info));
247
      }
248
    }
2064.2.3 by Brian Aker
Merge in clarification on lock for drop table.
249
250
    tables->unlock_table_names();
251
252
  } while (0);
1412 by Brian Aker
Innodb is now in the house (aka... it handls its own DFE).
253
1 by brian
clean slate
254
  if (wrong_tables.length())
255
  {
1412 by Brian Aker
Innodb is now in the house (aka... it handls its own DFE).
256
    if (not foreign_key_error)
257
    {
1 by brian
clean slate
258
      my_printf_error(ER_BAD_TABLE_ERROR, ER(ER_BAD_TABLE_ERROR), MYF(0),
259
                      wrong_tables.c_ptr());
1412 by Brian Aker
Innodb is now in the house (aka... it handls its own DFE).
260
    }
1 by brian
clean slate
261
    else
186 by Brian Aker
Partial fix for alter table
262
    {
1 by brian
clean slate
263
      my_message(ER_ROW_IS_REFERENCED, ER(ER_ROW_IS_REFERENCED), MYF(0));
186 by Brian Aker
Partial fix for alter table
264
    }
1 by brian
clean slate
265
    error= 1;
266
  }
267
520.1.22 by Brian Aker
Second pass of thd cleanup
268
  session->no_warnings_for_error= 0;
1034.1.7 by Brian Aker
Remove dead bits to the end of functions.
269
1412 by Brian Aker
Innodb is now in the house (aka... it handls its own DFE).
270
  return error;
1 by brian
clean slate
271
}
272
273
/*
274
  Sort keys in the following order:
275
  - PRIMARY KEY
276
  - UNIQUE keys where all column are NOT NULL
277
  - UNIQUE keys that don't contain partial segments
278
  - Other UNIQUE keys
279
  - Normal keys
280
  - Fulltext keys
281
282
  This will make checking for duplicated keys faster and ensure that
283
  PRIMARY keys are prioritized.
284
*/
285
1535 by Brian Aker
Rename of KEY to KeyInfo
286
static int sort_keys(KeyInfo *a, KeyInfo *b)
1 by brian
clean slate
287
{
288
  ulong a_flags= a->flags, b_flags= b->flags;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
289
1 by brian
clean slate
290
  if (a_flags & HA_NOSAME)
291
  {
292
    if (!(b_flags & HA_NOSAME))
293
      return -1;
53.2.14 by Monty Taylor
Removed HA_END_SPACE_KEY and references to it. It was _supposed_ to be gone anyway, but the ifdef around it was broken (MYSQL_VERSION_ID was actually undefined.)
294
    if ((a_flags ^ b_flags) & (HA_NULL_PART_KEY))
1 by brian
clean slate
295
    {
296
      /* Sort NOT NULL keys before other keys */
53.2.14 by Monty Taylor
Removed HA_END_SPACE_KEY and references to it. It was _supposed_ to be gone anyway, but the ifdef around it was broken (MYSQL_VERSION_ID was actually undefined.)
297
      return (a_flags & (HA_NULL_PART_KEY)) ? 1 : -1;
1 by brian
clean slate
298
    }
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
299
    if (is_primary_key(a))
1 by brian
clean slate
300
      return -1;
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
301
    if (is_primary_key(b))
1 by brian
clean slate
302
      return 1;
303
    /* Sort keys don't containing partial segments before others */
304
    if ((a_flags ^ b_flags) & HA_KEY_HAS_PART_KEY_SEG)
305
      return (a_flags & HA_KEY_HAS_PART_KEY_SEG) ? 1 : -1;
306
  }
307
  else if (b_flags & HA_NOSAME)
308
    return 1;					// Prefer b
309
310
  /*
311
    Prefer original key order.	usable_key_parts contains here
312
    the original key position.
313
  */
314
  return ((a->usable_key_parts < b->usable_key_parts) ? -1 :
315
          (a->usable_key_parts > b->usable_key_parts) ? 1 :
316
          0);
317
}
318
319
/*
320
  Check TYPELIB (set or enum) for duplicates
321
322
  SYNOPSIS
323
    check_duplicates_in_interval()
324
    set_or_name   "SET" or "ENUM" string for warning message
325
    name	  name of the checked column
326
    typelib	  list of values for the column
327
    dup_val_count  returns count of duplicate elements
328
329
  DESCRIPTION
330
    This function prints an warning for each value in list
331
    which has some duplicates on its right
332
333
  RETURN VALUES
334
    0             ok
335
    1             Error
336
*/
337
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
338
class typelib_set_member
339
{
340
public:
341
  string s;
342
  const CHARSET_INFO * const cs;
343
344
  typelib_set_member(const char* value, unsigned int length,
345
                     const CHARSET_INFO * const charset)
346
    : s(value, length),
347
      cs(charset)
348
  {}
349
};
350
351
static bool operator==(typelib_set_member const& a, typelib_set_member const& b)
352
{
353
  return (my_strnncoll(a.cs,
354
                       (const unsigned char*)a.s.c_str(), a.s.length(),
355
                       (const unsigned char*)b.s.c_str(), b.s.length())==0);
356
}
357
358
1596.1.3 by Monty Taylor
Modified hash function into a function object so that Sun Studio would
359
namespace
360
{
361
class typelib_set_member_hasher
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
362
{
363
  boost::hash<string> hasher;
1596.1.3 by Monty Taylor
Modified hash function into a function object so that Sun Studio would
364
public:
365
  std::size_t operator()(const typelib_set_member& t) const
366
  {
367
    return hasher(t.s);
368
  }
369
};
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
370
}
371
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
372
static bool check_duplicates_in_interval(const char *set_or_name,
373
                                         const char *name, TYPELIB *typelib,
374
                                         const CHARSET_INFO * const cs,
375
                                         unsigned int *dup_val_count)
1 by brian
clean slate
376
{
377
  TYPELIB tmp= *typelib;
378
  const char **cur_value= typelib->type_names;
379
  unsigned int *cur_length= typelib->type_lengths;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
380
  *dup_val_count= 0;
381
1596.1.3 by Monty Taylor
Modified hash function into a function object so that Sun Studio would
382
  boost::unordered_set<typelib_set_member, typelib_set_member_hasher> interval_set;
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
383
384
  for ( ; tmp.count > 0; cur_value++, cur_length++)
1 by brian
clean slate
385
  {
386
    tmp.type_names++;
387
    tmp.type_lengths++;
388
    tmp.count--;
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
389
    if (interval_set.find(typelib_set_member(*cur_value, *cur_length, cs)) != interval_set.end())
1 by brian
clean slate
390
    {
391
      my_error(ER_DUPLICATED_VALUE_IN_TYPE, MYF(0),
392
               name,*cur_value,set_or_name);
393
      return 1;
394
    }
1579.3.10 by Stewart Smith
switch check_duplicates_in_interval() over to use a boost::unordered_set. Keep using the same comparison operator (whatever the charset/collation). Changes from O(N^2) to O(N) execution. Reduces a test case execution time for 2^16 enum elements from over 4 minutes to less than half a second. The 'real' fix is to get rid of TYPELIB of course.
395
    else
396
      interval_set.insert(typelib_set_member(*cur_value, *cur_length, cs));
1 by brian
clean slate
397
  }
398
  return 0;
399
}
400
401
402
/*
403
  Check TYPELIB (set or enum) max and total lengths
404
405
  SYNOPSIS
406
    calculate_interval_lengths()
407
    cs            charset+collation pair of the interval
408
    typelib       list of values for the column
409
    max_length    length of the longest item
410
    tot_length    sum of the item lengths
411
412
  DESCRIPTION
413
    After this function call:
414
    - ENUM uses max_length
415
    - SET uses tot_length.
416
417
  RETURN VALUES
418
    void
419
*/
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
420
static void calculate_interval_lengths(const CHARSET_INFO * const cs,
421
                                       TYPELIB *interval,
422
                                       uint32_t *max_length,
423
                                       uint32_t *tot_length)
1 by brian
clean slate
424
{
425
  const char **pos;
482 by Brian Aker
Remove uint.
426
  uint32_t *len;
1 by brian
clean slate
427
  *max_length= *tot_length= 0;
428
  for (pos= interval->type_names, len= interval->type_lengths;
429
       *pos ; pos++, len++)
430
  {
482 by Brian Aker
Remove uint.
431
    uint32_t length= cs->cset->numchars(cs, *pos, *pos + *len);
1 by brian
clean slate
432
    *tot_length+= length;
205 by Brian Aker
uint32 -> uin32_t
433
    set_if_bigger(*max_length, (uint32_t)length);
1 by brian
clean slate
434
  }
435
}
436
437
/*
438
  Prepare a create_table instance for packing
439
440
  SYNOPSIS
441
    prepare_create_field()
442
    sql_field     field to prepare for packing
443
    blob_columns  count for BLOBs
444
    timestamps    count for timestamps
445
446
  DESCRIPTION
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
447
    This function prepares a CreateField instance.
1 by brian
clean slate
448
    Fields such as pack_flag are valid after this call.
449
450
  RETURN VALUES
451
   0	ok
452
   1	Error
453
*/
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
454
int prepare_create_field(CreateField *sql_field,
482 by Brian Aker
Remove uint.
455
                         uint32_t *blob_columns,
1233.1.8 by Brian Aker
Final removal table_flag().
456
                         int *timestamps,
457
                         int *timestamps_with_niladic)
1 by brian
clean slate
458
{
459
  unsigned int dup_val_count;
460
461
  /*
462
    This code came from mysql_prepare_create_table.
463
    Indent preserved to make patching easier
464
  */
51.2.1 by Patrick Galbraith
Removed DBUG_PRINTs, DBUG_ASSERTs, DBUG_EXECUTE_IFs from
465
  assert(sql_field->charset);
1 by brian
clean slate
466
467
  switch (sql_field->sql_type) {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
468
  case DRIZZLE_TYPE_BLOB:
1119.9.2 by Jay Pipes
Phase I removal of FIELDFLAG_BLOB. This is a piece of poop.
469
    sql_field->length= 8; // Unireg field length
1 by brian
clean slate
470
    (*blob_columns)++;
471
    break;
2046.2.1 by Brian Aker
First pass on micro timestamp.
472
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
473
  case DRIZZLE_TYPE_ENUM:
2046.2.1 by Brian Aker
First pass on micro timestamp.
474
    {
475
      if (check_duplicates_in_interval("ENUM",
476
				       sql_field->field_name,
477
				       sql_field->interval,
478
				       sql_field->charset,
479
				       &dup_val_count))
480
      {
481
	return 1;
482
      }
483
    }
484
    break;
485
486
  case DRIZZLE_TYPE_MICROTIME:
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
487
  case DRIZZLE_TYPE_TIMESTAMP:
1 by brian
clean slate
488
    /* We should replace old TIMESTAMP fields with their newer analogs */
489
    if (sql_field->unireg_check == Field::TIMESTAMP_OLD_FIELD)
490
    {
491
      if (!*timestamps)
492
      {
493
        sql_field->unireg_check= Field::TIMESTAMP_DNUN_FIELD;
494
        (*timestamps_with_niladic)++;
495
      }
496
      else
1575 by Brian Aker
First part, remove pack flags.
497
      {
1 by brian
clean slate
498
        sql_field->unireg_check= Field::NONE;
1575 by Brian Aker
First part, remove pack flags.
499
      }
1 by brian
clean slate
500
    }
501
    else if (sql_field->unireg_check != Field::NONE)
2046.2.1 by Brian Aker
First pass on micro timestamp.
502
    {
1 by brian
clean slate
503
      (*timestamps_with_niladic)++;
2046.2.1 by Brian Aker
First pass on micro timestamp.
504
    }
1 by brian
clean slate
505
506
    (*timestamps)++;
2046.2.1 by Brian Aker
First pass on micro timestamp.
507
508
    break;
509
510
  case DRIZZLE_TYPE_BOOLEAN:
511
  case DRIZZLE_TYPE_DATE:  // Rest of string types
512
  case DRIZZLE_TYPE_DATETIME:
513
  case DRIZZLE_TYPE_DECIMAL:
514
  case DRIZZLE_TYPE_DOUBLE:
515
  case DRIZZLE_TYPE_LONG:
516
  case DRIZZLE_TYPE_LONGLONG:
517
  case DRIZZLE_TYPE_NULL:
518
  case DRIZZLE_TYPE_TIME:
519
  case DRIZZLE_TYPE_UUID:
520
  case DRIZZLE_TYPE_VARCHAR:
1 by brian
clean slate
521
    break;
522
  }
1575 by Brian Aker
First part, remove pack flags.
523
1046.1.10 by Brian Aker
Formatting around return (style)
524
  return 0;
1 by brian
clean slate
525
}
526
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
527
static int prepare_create_table(Session *session,
2064.2.2 by Brian Aker
Formattting, etc.
528
                                HA_CREATE_INFO *create_info,
529
                                message::Table &create_proto,
530
                                AlterInfo *alter_info,
531
                                bool tmp_table,
532
                                uint32_t *db_options,
533
                                KeyInfo **key_info_buffer,
534
                                uint32_t *key_count,
535
                                int select_field_count)
1 by brian
clean slate
536
{
537
  const char	*key_name;
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
538
  CreateField	*sql_field,*dup_field;
1 by brian
clean slate
539
  uint		field,null_fields,blob_columns,max_key_length;
540
  ulong		record_offset= 0;
1535 by Brian Aker
Rename of KEY to KeyInfo
541
  KeyInfo		*key_info;
1534 by Brian Aker
Remove of KeyPartInfo
542
  KeyPartInfo *key_part_info;
1 by brian
clean slate
543
  int		timestamps= 0, timestamps_with_niladic= 0;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
544
  int		dup_no;
1 by brian
clean slate
545
  int		select_field_pos,auto_increment=0;
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
546
  List_iterator<CreateField> it(alter_info->create_list);
547
  List_iterator<CreateField> it2(alter_info->create_list);
482 by Brian Aker
Remove uint.
548
  uint32_t total_uneven_bit_length= 0;
1 by brian
clean slate
549
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
550
  plugin::StorageEngine *engine= plugin::StorageEngine::findByName(create_proto.engine().name());
551
1 by brian
clean slate
552
  select_field_pos= alter_info->create_list.elements - select_field_count;
553
  null_fields=blob_columns=0;
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
554
  max_key_length= engine->max_key_length();
1 by brian
clean slate
555
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
556
  for (int32_t field_no=0; (sql_field=it++) ; field_no++)
1 by brian
clean slate
557
  {
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
558
    const CHARSET_INFO *save_cs;
1 by brian
clean slate
559
560
    /*
561
      Initialize length from its original value (number of characters),
562
      which was set in the parser. This is necessary if we're
563
      executing a prepared statement for the second time.
564
    */
565
    sql_field->length= sql_field->char_length;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
566
1 by brian
clean slate
567
    if (!sql_field->charset)
568
      sql_field->charset= create_info->default_table_charset;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
569
1 by brian
clean slate
570
    /*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
571
      table_charset is set in ALTER Table if we want change character set
1 by brian
clean slate
572
      for all varchar/char columns.
573
      But the table charset must not affect the BLOB fields, so don't
574
      allow to change my_charset_bin to somethig else.
575
    */
576
    if (create_info->table_charset && sql_field->charset != &my_charset_bin)
577
      sql_field->charset= create_info->table_charset;
578
579
    save_cs= sql_field->charset;
580
    if ((sql_field->flags & BINCMP_FLAG) &&
862 by Brian Aker
Remove charset directory code.
581
        !(sql_field->charset= get_charset_by_csname(sql_field->charset->csname, MY_CS_BINSORT)))
1 by brian
clean slate
582
    {
583
      char tmp[64];
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
584
      char *tmp_pos= tmp;
585
      strncpy(tmp_pos, save_cs->csname, sizeof(tmp)-4);
586
      tmp_pos+= strlen(tmp);
587
      strncpy(tmp_pos, STRING_WITH_LEN("_bin"));
1 by brian
clean slate
588
      my_error(ER_UNKNOWN_COLLATION, MYF(0), tmp);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
589
      return(true);
1 by brian
clean slate
590
    }
591
592
    /*
593
      Convert the default value from client character
594
      set into the column character set if necessary.
595
    */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
596
    if (sql_field->def &&
1 by brian
clean slate
597
        save_cs != sql_field->def->collation.collation &&
325 by Brian Aker
Remove SET
598
        (sql_field->sql_type == DRIZZLE_TYPE_ENUM))
1 by brian
clean slate
599
    {
600
      /*
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
601
        Starting from 5.1 we work here with a copy of CreateField
1 by brian
clean slate
602
        created by the caller, not with the instance that was
603
        originally created during parsing. It's OK to create
604
        a temporary item and initialize with it a member of the
605
        copy -- this item will be thrown away along with the copy
606
        at the end of execution, and thus not introduce a dangling
607
        pointer in the parsed tree of a prepared statement or a
608
        stored procedure statement.
609
      */
610
      sql_field->def= sql_field->def->safe_charset_converter(save_cs);
611
612
      if (sql_field->def == NULL)
613
      {
614
        /* Could not convert */
615
        my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->field_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
616
        return(true);
1 by brian
clean slate
617
      }
618
    }
619
325 by Brian Aker
Remove SET
620
    if (sql_field->sql_type == DRIZZLE_TYPE_ENUM)
1 by brian
clean slate
621
    {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
622
      size_t dummy;
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
623
      const CHARSET_INFO * const cs= sql_field->charset;
1 by brian
clean slate
624
      TYPELIB *interval= sql_field->interval;
625
626
      /*
627
        Create typelib from interval_list, and if necessary
628
        convert strings from client character set to the
629
        column character set.
630
      */
631
      if (!interval)
632
      {
633
        /*
634
          Create the typelib in runtime memory - we will free the
635
          occupied memory at the same time when we free this
636
          sql_field -- at the end of execution.
637
        */
520.1.22 by Brian Aker
Second pass of thd cleanup
638
        interval= sql_field->interval= typelib(session->mem_root,
1101.1.24 by Monty Taylor
Reverted my change to interval_list
639
                                               sql_field->interval_list);
1101.3.1 by Nathan Williams
Reverted CreateField::interval_list to a List<String>. Fixes memory leaks I introduced by converting it to a vector in branch listed below.
640
641
        List_iterator<String> int_it(sql_field->interval_list);
642
        String conv, *tmp;
1 by brian
clean slate
643
        char comma_buf[4];
481 by Brian Aker
Remove all of uchar.
644
        int comma_length= cs->cset->wc_mb(cs, ',', (unsigned char*) comma_buf,
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
645
                                          (unsigned char*) comma_buf +
1 by brian
clean slate
646
                                          sizeof(comma_buf));
51.2.1 by Patrick Galbraith
Removed DBUG_PRINTs, DBUG_ASSERTs, DBUG_EXECUTE_IFs from
647
        assert(comma_length > 0);
1052.2.1 by Nathan Williams
Converted Create_field::interval_list to a std::list<String*>.
648
1101.3.1 by Nathan Williams
Reverted CreateField::interval_list to a List<String>. Fixes memory leaks I introduced by converting it to a vector in branch listed below.
649
        for (uint32_t i= 0; (tmp= int_it++); i++)
1 by brian
clean slate
650
        {
1101.3.1 by Nathan Williams
Reverted CreateField::interval_list to a List<String>. Fixes memory leaks I introduced by converting it to a vector in branch listed below.
651
          uint32_t lengthsp;
1 by brian
clean slate
652
          if (String::needs_conversion(tmp->length(), tmp->charset(),
653
                                       cs, &dummy))
654
          {
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
655
            size_t cnv_errs;
1 by brian
clean slate
656
            conv.copy(tmp->ptr(), tmp->length(), tmp->charset(), cs, &cnv_errs);
1487 by Brian Aker
More updates for memory::Root
657
            interval->type_names[i]= session->mem_root->strmake_root(conv.ptr(), conv.length());
1 by brian
clean slate
658
            interval->type_lengths[i]= conv.length();
659
          }
660
661
          // Strip trailing spaces.
1101.3.1 by Nathan Williams
Reverted CreateField::interval_list to a List<String>. Fixes memory leaks I introduced by converting it to a vector in branch listed below.
662
          lengthsp= cs->cset->lengthsp(cs, interval->type_names[i],
663
                                       interval->type_lengths[i]);
1 by brian
clean slate
664
          interval->type_lengths[i]= lengthsp;
481 by Brian Aker
Remove all of uchar.
665
          ((unsigned char *)interval->type_names[i])[lengthsp]= '\0';
1 by brian
clean slate
666
        }
1101.1.24 by Monty Taylor
Reverted my change to interval_list
667
        sql_field->interval_list.empty(); // Don't need interval_list anymore
1 by brian
clean slate
668
      }
669
325 by Brian Aker
Remove SET
670
      /* DRIZZLE_TYPE_ENUM */
1 by brian
clean slate
671
      {
205 by Brian Aker
uint32 -> uin32_t
672
        uint32_t field_length;
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
673
        assert(sql_field->sql_type == DRIZZLE_TYPE_ENUM);
1 by brian
clean slate
674
        if (sql_field->def != NULL)
675
        {
676
          String str, *def= sql_field->def->val_str(&str);
677
          if (def == NULL) /* SQL "NULL" maps to NULL */
678
          {
679
            if ((sql_field->flags & NOT_NULL_FLAG) != 0)
680
            {
681
              my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->field_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
682
              return(true);
1 by brian
clean slate
683
            }
684
685
            /* else, the defaults yield the correct length for NULLs. */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
686
          }
1 by brian
clean slate
687
          else /* not NULL */
688
          {
689
            def->length(cs->cset->lengthsp(cs, def->ptr(), def->length()));
690
            if (find_type2(interval, def->ptr(), def->length(), cs) == 0) /* not found */
691
            {
692
              my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->field_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
693
              return(true);
1 by brian
clean slate
694
            }
695
          }
696
        }
1816.3.1 by Brian Aker
Convert sql_string to use size_t (this should clean up ICC warnings).
697
        uint32_t new_dummy;
698
        calculate_interval_lengths(cs, interval, &field_length, &new_dummy);
1 by brian
clean slate
699
        sql_field->length= field_length;
700
      }
937.2.6 by Stewart Smith
make set_if_bigger typesafe for C and C++. Fix up everywhere.
701
      set_if_smaller(sql_field->length, (uint32_t)MAX_FIELD_WIDTH-1);
1 by brian
clean slate
702
    }
703
704
    sql_field->create_length_to_internal_length();
520.1.22 by Brian Aker
Second pass of thd cleanup
705
    if (prepare_blob_field(session, sql_field))
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
706
      return(true);
1 by brian
clean slate
707
708
    if (!(sql_field->flags & NOT_NULL_FLAG))
709
      null_fields++;
710
711
    if (check_column_name(sql_field->field_name))
712
    {
713
      my_error(ER_WRONG_COLUMN_NAME, MYF(0), sql_field->field_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
714
      return(true);
1 by brian
clean slate
715
    }
716
717
    /* Check if we have used the same field name before */
718
    for (dup_no=0; (dup_field=it2++) != sql_field; dup_no++)
719
    {
720
      if (my_strcasecmp(system_charset_info,
721
                        sql_field->field_name,
722
                        dup_field->field_name) == 0)
723
      {
724
	/*
725
	  If this was a CREATE ... SELECT statement, accept a field
726
	  redefinition if we are changing a field in the SELECT part
727
	*/
728
	if (field_no < select_field_pos || dup_no >= select_field_pos)
729
	{
730
	  my_error(ER_DUP_FIELDNAME, MYF(0), sql_field->field_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
731
	  return(true);
1 by brian
clean slate
732
	}
733
	else
734
	{
735
	  /* Field redefined */
736
	  sql_field->def=		dup_field->def;
737
	  sql_field->sql_type=		dup_field->sql_type;
738
	  sql_field->charset=		(dup_field->charset ?
739
					 dup_field->charset :
740
					 create_info->default_table_charset);
741
	  sql_field->length=		dup_field->char_length;
742
          sql_field->pack_length=	dup_field->pack_length;
743
          sql_field->key_length=	dup_field->key_length;
744
	  sql_field->decimals=		dup_field->decimals;
745
	  sql_field->create_length_to_internal_length();
746
	  sql_field->unireg_check=	dup_field->unireg_check;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
747
          /*
1 by brian
clean slate
748
            We're making one field from two, the result field will have
749
            dup_field->flags as flags. If we've incremented null_fields
750
            because of sql_field->flags, decrement it back.
751
          */
752
          if (!(sql_field->flags & NOT_NULL_FLAG))
753
            null_fields--;
754
	  sql_field->flags=		dup_field->flags;
755
          sql_field->interval=          dup_field->interval;
756
	  it2.remove();			// Remove first (create) definition
757
	  select_field_pos--;
758
	  break;
759
	}
760
      }
761
    }
1308.2.11 by Jay Pipes
* Adds CREATE TABLE as a specific CreateTableStatement message in the
762
763
    /** @todo Get rid of this MyISAM-specific crap. */
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
764
    if (not create_proto.engine().name().compare("MyISAM") &&
1308.2.11 by Jay Pipes
* Adds CREATE TABLE as a specific CreateTableStatement message in the
765
        ((sql_field->flags & BLOB_FLAG) ||
1638.2.1 by Stewart Smith
remove unused row_type from table proto (although referenced in a bunch of places)
766
         (sql_field->sql_type == DRIZZLE_TYPE_VARCHAR)))
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
767
    {
1 by brian
clean slate
768
      (*db_options)|= HA_OPTION_PACK_RECORD;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
769
    }
770
1 by brian
clean slate
771
    it2.rewind();
772
  }
773
774
  /* record_offset will be increased with 'length-of-null-bits' later */
775
  record_offset= 0;
776
  null_fields+= total_uneven_bit_length;
777
778
  it.rewind();
779
  while ((sql_field=it++))
780
  {
51.2.1 by Patrick Galbraith
Removed DBUG_PRINTs, DBUG_ASSERTs, DBUG_EXECUTE_IFs from
781
    assert(sql_field->charset != 0);
1 by brian
clean slate
782
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
783
    if (prepare_create_field(sql_field, &blob_columns,
1233.1.8 by Brian Aker
Final removal table_flag().
784
			     &timestamps, &timestamps_with_niladic))
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
785
      return(true);
1 by brian
clean slate
786
    sql_field->offset= record_offset;
787
    if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
788
      auto_increment++;
789
  }
790
  if (timestamps_with_niladic > 1)
791
  {
792
    my_message(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS,
793
               ER(ER_TOO_MUCH_AUTO_TIMESTAMP_COLS), MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
794
    return(true);
1 by brian
clean slate
795
  }
796
  if (auto_increment > 1)
797
  {
798
    my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
799
    return(true);
1 by brian
clean slate
800
  }
801
  if (auto_increment &&
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
802
      (engine->check_flag(HTON_BIT_NO_AUTO_INCREMENT)))
1 by brian
clean slate
803
  {
804
    my_message(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT,
805
               ER(ER_TABLE_CANT_HANDLE_AUTO_INCREMENT), MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
806
    return(true);
1 by brian
clean slate
807
  }
808
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
809
  if (blob_columns && (engine->check_flag(HTON_BIT_NO_BLOBS)))
1 by brian
clean slate
810
  {
811
    my_message(ER_TABLE_CANT_HANDLE_BLOB, ER(ER_TABLE_CANT_HANDLE_BLOB),
812
               MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
813
    return(true);
1 by brian
clean slate
814
  }
815
816
  /* Create keys */
817
818
  List_iterator<Key> key_iterator(alter_info->key_list);
819
  List_iterator<Key> key_iterator2(alter_info->key_list);
482 by Brian Aker
Remove uint.
820
  uint32_t key_parts=0, fk_key_count=0;
1 by brian
clean slate
821
  bool primary_key=0,unique_key=0;
822
  Key *key, *key2;
482 by Brian Aker
Remove uint.
823
  uint32_t tmp, key_number;
1 by brian
clean slate
824
  /* special marker for keys to be ignored */
825
  static char ignore_key[1];
826
827
  /* Calculate number of key segements */
828
  *key_count= 0;
829
830
  while ((key=key_iterator++))
831
  {
832
    if (key->type == Key::FOREIGN_KEY)
833
    {
834
      fk_key_count++;
383.7.1 by Andrey Zhakov
Initial submit of code and tests
835
      if (((Foreign_key *)key)->validate(alter_info->create_list))
836
        return true;
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
837
1 by brian
clean slate
838
      Foreign_key *fk_key= (Foreign_key*) key;
1638.10.80 by Stewart Smith
fix storing and manipulating foreign keys in the proto around ALTER TABLE, CREATE TABLE and ALTER TABLE ADD/DROP FOREIGN KEY. We also (mostly) emulate the naming of innodb foreign keys in the upper layer.
839
840
      add_foreign_key_to_table_message(&create_proto,
841
                                       fk_key->name.str,
842
                                       fk_key->columns,
843
                                       fk_key->ref_table,
844
                                       fk_key->ref_columns,
845
                                       fk_key->delete_opt,
846
                                       fk_key->update_opt,
847
                                       fk_key->match_opt);
848
1 by brian
clean slate
849
      if (fk_key->ref_columns.elements &&
850
	  fk_key->ref_columns.elements != fk_key->columns.elements)
851
      {
852
        my_error(ER_WRONG_FK_DEF, MYF(0),
853
                 (fk_key->name.str ? fk_key->name.str :
854
                                     "foreign key without name"),
855
                 ER(ER_KEY_REF_DO_NOT_MATCH_TABLE_REF));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
856
	return(true);
1 by brian
clean slate
857
      }
858
      continue;
859
    }
860
    (*key_count)++;
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
861
    tmp= engine->max_key_parts();
1 by brian
clean slate
862
    if (key->columns.elements > tmp)
863
    {
864
      my_error(ER_TOO_MANY_KEY_PARTS,MYF(0),tmp);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
865
      return(true);
1 by brian
clean slate
866
    }
867
    if (check_identifier_name(&key->name, ER_TOO_LONG_IDENT))
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
868
      return(true);
1 by brian
clean slate
869
    key_iterator2.rewind ();
870
    if (key->type != Key::FOREIGN_KEY)
871
    {
872
      while ((key2 = key_iterator2++) != key)
873
      {
874
	/*
875
          foreign_key_prefix(key, key2) returns 0 if key or key2, or both, is
876
          'generated', and a generated key is a prefix of the other key.
877
          Then we do not need the generated shorter key.
878
        */
879
        if ((key2->type != Key::FOREIGN_KEY &&
880
             key2->name.str != ignore_key &&
881
             !foreign_key_prefix(key, key2)))
882
        {
1672.3.4 by Brian Aker
This change the style on a few TODO, and fixes an error path to correctly
883
          /* @todo issue warning message */
1 by brian
clean slate
884
          /* mark that the generated key should be ignored */
885
          if (!key2->generated ||
886
              (key->generated && key->columns.elements <
887
               key2->columns.elements))
888
            key->name.str= ignore_key;
889
          else
890
          {
891
            key2->name.str= ignore_key;
892
            key_parts-= key2->columns.elements;
893
            (*key_count)--;
894
          }
895
          break;
896
        }
897
      }
898
    }
899
    if (key->name.str != ignore_key)
900
      key_parts+=key->columns.elements;
901
    else
902
      (*key_count)--;
903
    if (key->name.str && !tmp_table && (key->type != Key::PRIMARY) &&
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
904
        is_primary_key_name(key->name.str))
1 by brian
clean slate
905
    {
906
      my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key->name.str);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
907
      return(true);
1 by brian
clean slate
908
    }
909
  }
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
910
  tmp= engine->max_keys();
1 by brian
clean slate
911
  if (*key_count > tmp)
912
  {
913
    my_error(ER_TOO_MANY_KEYS,MYF(0),tmp);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
914
    return(true);
1 by brian
clean slate
915
  }
916
1535 by Brian Aker
Rename of KEY to KeyInfo
917
  (*key_info_buffer)= key_info= (KeyInfo*) memory::sql_calloc(sizeof(KeyInfo) * (*key_count));
1534 by Brian Aker
Remove of KeyPartInfo
918
  key_part_info=(KeyPartInfo*) memory::sql_calloc(sizeof(KeyPartInfo)*key_parts);
1 by brian
clean slate
919
  if (!*key_info_buffer || ! key_part_info)
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
920
    return(true);				// Out of memory
1 by brian
clean slate
921
922
  key_iterator.rewind();
923
  key_number=0;
924
  for (; (key=key_iterator++) ; key_number++)
925
  {
482 by Brian Aker
Remove uint.
926
    uint32_t key_length=0;
1 by brian
clean slate
927
    Key_part_spec *column;
928
929
    if (key->name.str == ignore_key)
930
    {
931
      /* ignore redundant keys */
932
      do
933
	key=key_iterator++;
934
      while (key && key->name.str == ignore_key);
935
      if (!key)
936
	break;
937
    }
938
939
    switch (key->type) {
940
    case Key::MULTIPLE:
941
	key_info->flags= 0;
942
	break;
943
    case Key::FOREIGN_KEY:
944
      key_number--;				// Skip this key
945
      continue;
946
    default:
947
      key_info->flags = HA_NOSAME;
948
      break;
949
    }
950
    if (key->generated)
951
      key_info->flags|= HA_GENERATED_KEY;
952
206 by Brian Aker
Removed final uint dead types.
953
    key_info->key_parts=(uint8_t) key->columns.elements;
1 by brian
clean slate
954
    key_info->key_part=key_part_info;
955
    key_info->usable_key_parts= key_number;
956
    key_info->algorithm= key->key_create_info.algorithm;
957
482 by Brian Aker
Remove uint.
958
    uint32_t tmp_len= system_charset_info->cset->charpos(system_charset_info,
1 by brian
clean slate
959
                                           key->key_create_info.comment.str,
960
                                           key->key_create_info.comment.str +
961
                                           key->key_create_info.comment.length,
962
                                           INDEX_COMMENT_MAXLEN);
963
964
    if (tmp_len < key->key_create_info.comment.length)
965
    {
966
      my_error(ER_WRONG_STRING_LENGTH, MYF(0),
967
               key->key_create_info.comment.str,"INDEX COMMENT",
895 by Brian Aker
Completion (?) of uint conversion.
968
               (uint32_t) INDEX_COMMENT_MAXLEN);
1046.1.10 by Brian Aker
Formatting around return (style)
969
      return -1;
1 by brian
clean slate
970
    }
971
972
    key_info->comment.length= key->key_create_info.comment.length;
973
    if (key_info->comment.length > 0)
974
    {
975
      key_info->flags|= HA_USES_COMMENT;
976
      key_info->comment.str= key->key_create_info.comment.str;
977
    }
978
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
979
    message::Table::Field *protofield= NULL;
980
1 by brian
clean slate
981
    List_iterator<Key_part_spec> cols(key->columns), cols2(key->columns);
482 by Brian Aker
Remove uint.
982
    for (uint32_t column_nr=0 ; (column=cols++) ; column_nr++)
1 by brian
clean slate
983
    {
482 by Brian Aker
Remove uint.
984
      uint32_t length;
1 by brian
clean slate
985
      Key_part_spec *dup_column;
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
986
      int proto_field_nr= 0;
1 by brian
clean slate
987
988
      it.rewind();
989
      field=0;
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
990
      while ((sql_field=it++) && ++proto_field_nr &&
1 by brian
clean slate
991
	     my_strcasecmp(system_charset_info,
992
			   column->field_name.str,
993
			   sql_field->field_name))
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
994
      {
1 by brian
clean slate
995
	field++;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
996
      }
997
1 by brian
clean slate
998
      if (!sql_field)
999
      {
1000
	my_error(ER_KEY_COLUMN_DOES_NOT_EXITS, MYF(0), column->field_name.str);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1001
	return(true);
1 by brian
clean slate
1002
      }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1003
1 by brian
clean slate
1004
      while ((dup_column= cols2++) != column)
1005
      {
1006
        if (!my_strcasecmp(system_charset_info,
1233.1.3 by Brian Aker
Move more of the flags up to engine flags.
1007
                           column->field_name.str, dup_column->field_name.str))
1 by brian
clean slate
1008
	{
1009
	  my_printf_error(ER_DUP_FIELDNAME,
1010
			  ER(ER_DUP_FIELDNAME),MYF(0),
1011
			  column->field_name.str);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1012
	  return(true);
1 by brian
clean slate
1013
	}
1014
      }
1015
      cols2.rewind();
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
1016
1320.1.3 by Brian Aker
More reference cleanup.
1017
      if (create_proto.field_size() > 0)
1018
        protofield= create_proto.mutable_field(proto_field_nr - 1);
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
1019
1 by brian
clean slate
1020
      {
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1021
        column->length*= sql_field->charset->mbmaxlen;
1 by brian
clean slate
1022
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1023
        if (sql_field->sql_type == DRIZZLE_TYPE_BLOB)
1024
        {
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1025
          if (! (engine->check_flag(HTON_BIT_CAN_INDEX_BLOBS)))
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1026
          {
1027
            my_error(ER_BLOB_USED_AS_KEY, MYF(0), column->field_name.str);
1028
            return true;
1029
          }
1030
          if (! column->length)
1031
          {
1032
            my_error(ER_BLOB_KEY_WITHOUT_LENGTH, MYF(0), column->field_name.str);
1033
            return true;
1034
          }
1035
        }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1036
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1037
        if (! (sql_field->flags & NOT_NULL_FLAG))
1038
        {
1039
          if (key->type == Key::PRIMARY)
1040
          {
1041
            /* Implicitly set primary key fields to NOT NULL for ISO conf. */
1042
            sql_field->flags|= NOT_NULL_FLAG;
1 by brian
clean slate
1043
            null_fields--;
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
1044
1045
            if (protofield)
1046
            {
1047
              message::Table::Field::FieldConstraints *constraints;
1048
              constraints= protofield->mutable_constraints();
2064.2.1 by Brian Aker
Fixes naming conventions and issues around notnull being "true" by default.
1049
              constraints->set_is_notnull(true);
1215.1.2 by stewart at flamingspork
[patch 02/17] field NULL | NOT NULL in proto in parser. Only for CREATE TABLE. Change default in proto to reflect default in SQL.
1050
            }
1051
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1052
          }
1053
          else
1 by brian
clean slate
1054
          {
1055
            key_info->flags|= HA_NULL_PART_KEY;
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1056
            if (! (engine->check_flag(HTON_BIT_NULL_IN_KEY)))
1 by brian
clean slate
1057
            {
1058
              my_error(ER_NULL_COLUMN_IN_INDEX, MYF(0), column->field_name.str);
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1059
              return true;
1 by brian
clean slate
1060
            }
1061
          }
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1062
        }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1063
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1064
        if (MTYP_TYPENR(sql_field->unireg_check) == Field::NEXT_NUMBER)
1065
        {
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1066
          if (column_nr == 0 || (engine->check_flag(HTON_BIT_AUTO_PART_KEY)))
1119.9.10 by Jay Pipes
Removes FIELDFLAG_MAYBE_NULL and f_maybe_null() macro.
1067
            auto_increment--;			// Field is used
1068
        }
1 by brian
clean slate
1069
      }
1070
1071
      key_part_info->fieldnr= field;
206 by Brian Aker
Removed final uint dead types.
1072
      key_part_info->offset=  (uint16_t) sql_field->offset;
1575 by Brian Aker
First part, remove pack flags.
1073
      key_part_info->key_type= 0;
1 by brian
clean slate
1074
      length= sql_field->key_length;
1075
1076
      if (column->length)
1077
      {
1119.9.2 by Jay Pipes
Phase I removal of FIELDFLAG_BLOB. This is a piece of poop.
1078
	if (sql_field->sql_type == DRIZZLE_TYPE_BLOB)
1 by brian
clean slate
1079
	{
1080
	  if ((length=column->length) > max_key_length ||
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1081
	      length > engine->max_key_part_length())
1 by brian
clean slate
1082
	  {
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1083
	    length= min(max_key_length, engine->max_key_part_length());
1 by brian
clean slate
1084
	    if (key->type == Key::MULTIPLE)
1085
	    {
1086
	      /* not a critical problem */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1087
	      char warn_buff[DRIZZLE_ERRMSG_SIZE];
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1088
	      snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1089
                       length);
520.1.22 by Brian Aker
Second pass of thd cleanup
1090
	      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
1091
			   ER_TOO_LONG_KEY, warn_buff);
1092
              /* Align key length to multibyte char boundary */
1093
              length-= length % sql_field->charset->mbmaxlen;
1094
	    }
1095
	    else
1096
	    {
1097
	      my_error(ER_TOO_LONG_KEY,MYF(0),length);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1098
	      return(true);
1 by brian
clean slate
1099
	    }
1100
	  }
1101
	}
1102
	else if ((column->length > length ||
1119.9.11 by Jay Pipes
Removes f_is_packed() macro and FIELDFLAG_PACK
1103
            ! Field::type_can_have_key_part(sql_field->sql_type)))
1 by brian
clean slate
1104
	{
1105
	  my_message(ER_WRONG_SUB_KEY, ER(ER_WRONG_SUB_KEY), MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1106
	  return(true);
1 by brian
clean slate
1107
	}
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1108
	else if (! (engine->check_flag(HTON_BIT_NO_PREFIX_CHAR_KEYS)))
1233.1.7 by Brian Aker
Final table flag removal.
1109
        {
1 by brian
clean slate
1110
	  length=column->length;
1233.1.7 by Brian Aker
Final table flag removal.
1111
        }
1 by brian
clean slate
1112
      }
1113
      else if (length == 0)
1114
      {
1115
	my_error(ER_WRONG_KEY_COLUMN, MYF(0), column->field_name.str);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1116
	  return(true);
1 by brian
clean slate
1117
      }
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1118
      if (length > engine->max_key_part_length())
1 by brian
clean slate
1119
      {
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1120
        length= engine->max_key_part_length();
1 by brian
clean slate
1121
	if (key->type == Key::MULTIPLE)
1122
	{
1123
	  /* not a critical problem */
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1124
	  char warn_buff[DRIZZLE_ERRMSG_SIZE];
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1125
	  snprintf(warn_buff, sizeof(warn_buff), ER(ER_TOO_LONG_KEY),
1126
                   length);
520.1.22 by Brian Aker
Second pass of thd cleanup
1127
	  push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
1 by brian
clean slate
1128
		       ER_TOO_LONG_KEY, warn_buff);
1129
          /* Align key length to multibyte char boundary */
1130
          length-= length % sql_field->charset->mbmaxlen;
1131
	}
1132
	else
1133
	{
1134
	  my_error(ER_TOO_LONG_KEY,MYF(0),length);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1135
	  return(true);
1 by brian
clean slate
1136
	}
1137
      }
206 by Brian Aker
Removed final uint dead types.
1138
      key_part_info->length=(uint16_t) length;
1 by brian
clean slate
1139
      /* Use packed keys for long strings on the first column */
1140
      if (!((*db_options) & HA_OPTION_NO_PACK_KEYS) &&
1502.1.27 by Brian Aker
Remove dead pack keys code.
1141
          (length >= KEY_DEFAULT_PACK_LENGTH &&
1142
           (sql_field->sql_type == DRIZZLE_TYPE_VARCHAR ||
1143
            sql_field->sql_type == DRIZZLE_TYPE_BLOB)))
1 by brian
clean slate
1144
      {
1119.9.2 by Jay Pipes
Phase I removal of FIELDFLAG_BLOB. This is a piece of poop.
1145
        if ((column_nr == 0 && sql_field->sql_type == DRIZZLE_TYPE_BLOB) ||
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1146
            sql_field->sql_type == DRIZZLE_TYPE_VARCHAR)
1502.1.27 by Brian Aker
Remove dead pack keys code.
1147
        {
1119.9.2 by Jay Pipes
Phase I removal of FIELDFLAG_BLOB. This is a piece of poop.
1148
          key_info->flags|= HA_BINARY_PACK_KEY | HA_VAR_LENGTH_KEY;
1502.1.27 by Brian Aker
Remove dead pack keys code.
1149
        }
1119.9.2 by Jay Pipes
Phase I removal of FIELDFLAG_BLOB. This is a piece of poop.
1150
        else
1502.1.27 by Brian Aker
Remove dead pack keys code.
1151
        {
1119.9.2 by Jay Pipes
Phase I removal of FIELDFLAG_BLOB. This is a piece of poop.
1152
          key_info->flags|= HA_PACK_KEY;
1502.1.27 by Brian Aker
Remove dead pack keys code.
1153
        }
1 by brian
clean slate
1154
      }
1155
      /* Check if the key segment is partial, set the key flag accordingly */
1156
      if (length != sql_field->key_length)
1157
        key_info->flags|= HA_KEY_HAS_PART_KEY_SEG;
1158
1159
      key_length+=length;
1160
      key_part_info++;
1161
1162
      /* Create the key name based on the first column (if not given) */
1163
      if (column_nr == 0)
1164
      {
1165
	if (key->type == Key::PRIMARY)
1166
	{
1167
	  if (primary_key)
1168
	  {
1169
	    my_message(ER_MULTIPLE_PRI_KEY, ER(ER_MULTIPLE_PRI_KEY),
1170
                       MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1171
	    return(true);
1 by brian
clean slate
1172
	  }
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
1173
          static const char pkey_name[]= "PRIMARY";
1174
	  key_name=pkey_name;
1 by brian
clean slate
1175
	  primary_key=1;
1176
	}
1177
	else if (!(key_name= key->name.str))
1178
	  key_name=make_unique_key_name(sql_field->field_name,
1179
					*key_info_buffer, key_info);
1180
	if (check_if_keyname_exists(key_name, *key_info_buffer, key_info))
1181
	{
1182
	  my_error(ER_DUP_KEYNAME, MYF(0), key_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1183
	  return(true);
1 by brian
clean slate
1184
	}
1185
	key_info->name=(char*) key_name;
1186
      }
1187
    }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1188
1 by brian
clean slate
1189
    if (!key_info->name || check_column_name(key_info->name))
1190
    {
1191
      my_error(ER_WRONG_NAME_FOR_INDEX, MYF(0), key_info->name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1192
      return(true);
1 by brian
clean slate
1193
    }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1194
1 by brian
clean slate
1195
    if (!(key_info->flags & HA_NULL_PART_KEY))
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1196
    {
1 by brian
clean slate
1197
      unique_key=1;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1198
    }
1199
206 by Brian Aker
Removed final uint dead types.
1200
    key_info->key_length=(uint16_t) key_length;
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1201
1 by brian
clean slate
1202
    if (key_length > max_key_length)
1203
    {
1204
      my_error(ER_TOO_LONG_KEY,MYF(0),max_key_length);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1205
      return(true);
1 by brian
clean slate
1206
    }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1207
1 by brian
clean slate
1208
    key_info++;
1209
  }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1210
1 by brian
clean slate
1211
  if (!unique_key && !primary_key &&
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1212
      (engine->check_flag(HTON_BIT_REQUIRE_PRIMARY_KEY)))
1 by brian
clean slate
1213
  {
1214
    my_message(ER_REQUIRES_PRIMARY_KEY, ER(ER_REQUIRES_PRIMARY_KEY), MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1215
    return(true);
1 by brian
clean slate
1216
  }
2057.2.7 by Brian Aker
Remove need for committed type for microtime in proto.
1217
1 by brian
clean slate
1218
  if (auto_increment > 0)
1219
  {
1220
    my_message(ER_WRONG_AUTO_KEY, ER(ER_WRONG_AUTO_KEY), MYF(0));
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1221
    return(true);
1 by brian
clean slate
1222
  }
1223
  /* Sort keys in optimized order */
1535 by Brian Aker
Rename of KEY to KeyInfo
1224
  internal::my_qsort((unsigned char*) *key_info_buffer, *key_count, sizeof(KeyInfo),
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1225
	             (qsort_cmp) sort_keys);
1 by brian
clean slate
1226
1227
  /* Check fields. */
1228
  it.rewind();
1229
  while ((sql_field=it++))
1230
  {
1231
    Field::utype type= (Field::utype) MTYP_TYPENR(sql_field->unireg_check);
1232
520.1.22 by Brian Aker
Second pass of thd cleanup
1233
    if (session->variables.sql_mode & MODE_NO_ZERO_DATE &&
1 by brian
clean slate
1234
        !sql_field->def &&
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1235
        sql_field->sql_type == DRIZZLE_TYPE_TIMESTAMP &&
1 by brian
clean slate
1236
        (sql_field->flags & NOT_NULL_FLAG) &&
1237
        (type == Field::NONE || type == Field::TIMESTAMP_UN_FIELD))
1238
    {
1239
      /*
1240
        An error should be reported if:
1241
          - NO_ZERO_DATE SQL mode is active;
1242
          - there is no explicit DEFAULT clause (default column value);
1243
          - this is a TIMESTAMP column;
1244
          - the column is not NULL;
1245
          - this is not the DEFAULT CURRENT_TIMESTAMP column.
1246
1247
        In other words, an error should be reported if
1248
          - NO_ZERO_DATE SQL mode is active;
1249
          - the column definition is equivalent to
1250
            'column_name TIMESTAMP DEFAULT 0'.
1251
      */
1252
1253
      my_error(ER_INVALID_DEFAULT, MYF(0), sql_field->field_name);
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1254
      return(true);
1 by brian
clean slate
1255
    }
1256
  }
1257
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1258
  return(false);
1 by brian
clean slate
1259
}
1260
1261
/*
1262
  Extend long VARCHAR fields to blob & prepare field if it's a blob
1263
1264
  SYNOPSIS
1265
    prepare_blob_field()
1266
    sql_field		Field to check
1267
1268
  RETURN
1269
    0	ok
1270
    1	Error (sql_field can't be converted to blob)
1271
        In this case the error is given
1272
*/
1273
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
1274
static bool prepare_blob_field(Session *,
1052.2.3 by Nathan Williams
No actual code changes. Changed Create_field to CreateField to be consistent with coding standards.
1275
                               CreateField *sql_field)
1 by brian
clean slate
1276
{
1277
1278
  if (sql_field->length > MAX_FIELD_VARCHARLENGTH &&
1279
      !(sql_field->flags & BLOB_FLAG))
1280
  {
1281
    my_error(ER_TOO_BIG_FIELDLENGTH, MYF(0), sql_field->field_name,
1282
             MAX_FIELD_VARCHARLENGTH / sql_field->charset->mbmaxlen);
1046.1.10 by Brian Aker
Formatting around return (style)
1283
    return 1;
1 by brian
clean slate
1284
  }
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1285
1 by brian
clean slate
1286
  if ((sql_field->flags & BLOB_FLAG) && sql_field->length)
1287
  {
212.2.2 by Patrick Galbraith
Renamed FIELD_TYPE to DRIZZLE_TYPE
1288
    if (sql_field->sql_type == DRIZZLE_TYPE_BLOB)
1 by brian
clean slate
1289
    {
1290
      /* The user has given a length to the blob column */
1291
      sql_field->pack_length= calc_pack_length(sql_field->sql_type, 0);
1292
    }
1293
    sql_field->length= 0;
1294
  }
1046.1.10 by Brian Aker
Formatting around return (style)
1295
  return 0;
1 by brian
clean slate
1296
}
1297
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1298
static bool locked_create_event(Session *session,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
1299
                                const TableIdentifier &identifier,
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1300
                                HA_CREATE_INFO *create_info,
1301
                                message::Table &table_proto,
1302
                                AlterInfo *alter_info,
1303
                                bool is_if_not_exists,
1304
                                bool internal_tmp_table,
1305
                                uint db_options,
1306
                                uint key_count,
1535 by Brian Aker
Rename of KEY to KeyInfo
1307
                                KeyInfo *key_info_buffer)
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1308
{
1309
  bool error= true;
1310
1311
  {
1312
1313
    /*
1314
      @note if we are building a temp table we need to check to see if a temp table
1315
      already exists, otherwise we just need to find out if a normal table exists (aka it is fine
1316
      to create a table under a temporary table.
1317
    */
1318
    bool exists= 
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
1319
      plugin::StorageEngine::doesTableExist(*session, identifier, 
1320
                                            identifier.getType() != message::Table::STANDARD );
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1321
1322
    if (exists)
1323
    {
1324
      if (is_if_not_exists)
1325
      {
1326
        error= false;
1327
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1328
                            ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1329
                            identifier.getTableName().c_str());
1330
        create_info->table_existed= 1;		// Mark that table existed
1331
        return error;
1332
      }
1333
1954.2.1 by Brian Aker
getSQLPath() modified to take a string so that we can const the table
1334
      std::string path;
1335
      identifier.getSQLPath(path);
1336
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1337
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1338
      return error;
1339
    }
1340
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
1341
    if (identifier.getType() == message::Table::STANDARD) // We have a real table
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1342
    {
1343
      /*
1344
        We don't assert here, but check the result, because the table could be
1345
        in the table definition cache and in the same time the .frm could be
1346
        missing from the disk, in case of manual intervention which deletes
1347
        the .frm cursor. The user has to use FLUSH TABLES; to clear the cache.
1348
        Then she could create the table. This case is pretty obscure and
1349
        therefore we don't introduce a new error message only for it.
1350
      */
1351
      /*
1352
        @todo improve this error condition.
1353
      */
1938.4.4 by Brian Aker
Remove dead getShare() call which should have been a call on the cache
1354
      if (definition::Cache::singleton().find(identifier.getKey()))
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1355
      {
1954.2.1 by Brian Aker
getSQLPath() modified to take a string so that we can const the table
1356
        std::string path;
1357
        identifier.getSQLPath(path);
1358
        my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1359
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1360
        return error;
1361
      }
1362
    }
1363
  }
1364
1365
  session->set_proc_info("creating table");
1366
  create_info->table_existed= 0;		// Mark that table is created
1367
1368
  create_info->table_options= db_options;
1369
1372.1.3 by Brian Aker
Refactor for table message.
1370
  if (not rea_create_table(session, identifier,
1371
                           table_proto,
1372
                           create_info, alter_info->create_list,
1373
                           key_count, key_info_buffer))
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1374
  {
1375
    return error;
1376
  }
1377
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
1378
  if (identifier.getType() == message::Table::TEMPORARY)
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1379
  {
1380
    /* Open table and put in temporary table list */
1381
    if (not (session->open_temporary_table(identifier)))
1382
    {
1383
      (void) session->rm_temporary_table(identifier);
1384
      return error;
1385
    }
1386
  }
1387
1388
  /* 
1389
    We keep this behind the lock to make sure ordering is correct for a table.
1390
    This is a very unlikely problem where before we would write out to the
1391
    trans log, someone would do a delete/create operation.
1392
  */
1393
1394
  if (table_proto.type() == message::Table::STANDARD && not internal_tmp_table)
1395
  {
1396
    TransactionServices &transaction_services= TransactionServices::singleton();
1397
    transaction_services.createTable(session, table_proto);
1398
  }
1399
1400
  return false;
1401
}
1402
1 by brian
clean slate
1403
1404
/*
1039.1.11 by Brian Aker
Refactor function to make sense.
1405
  Ignore the name of this function... it locks :(
1406
1 by brian
clean slate
1407
  Create a table
1408
1409
  SYNOPSIS
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1410
    create_table_no_lock()
520.1.22 by Brian Aker
Second pass of thd cleanup
1411
    session			Thread object
1 by brian
clean slate
1412
    db			Database
1413
    table_name		Table name
1414
    create_info	        Create information (like MAX_ROWS)
1415
    fields		List of fields to create
1416
    keys		List of keys to create
1417
    internal_tmp_table  Set to 1 if this is an internal temporary table
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1418
			(From ALTER Table)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1419
    select_field_count
1 by brian
clean slate
1420
1421
  DESCRIPTION
1422
    If one creates a temporary table, this is automatically opened
1423
1424
    Note that this function assumes that caller already have taken
1425
    name-lock on table being created or used some other way to ensure
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1426
    that concurrent operations won't intervene. create_table()
1 by brian
clean slate
1427
    is a wrapper that can be used for this.
1428
1429
  RETURN VALUES
55 by brian
Update for using real bool types.
1430
    false OK
1431
    true  error
1 by brian
clean slate
1432
*/
1433
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1434
bool create_table_no_lock(Session *session,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
1435
                                const TableIdentifier &identifier,
1 by brian
clean slate
1436
                                HA_CREATE_INFO *create_info,
1320.1.1 by Brian Aker
Light cleanup for references.
1437
				message::Table &table_proto,
1126.3.3 by Jay Pipes
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.
1438
                                AlterInfo *alter_info,
1 by brian
clean slate
1439
                                bool internal_tmp_table,
1235.2.1 by Brian Aker
More identifier work.
1440
                                uint32_t select_field_count,
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1441
                                bool is_if_not_exists)
1 by brian
clean slate
1442
{
1443
  uint		db_options, key_count;
1535 by Brian Aker
Rename of KEY to KeyInfo
1444
  KeyInfo		*key_info_buffer;
55 by brian
Update for using real bool types.
1445
  bool		error= true;
1208.3.2 by brian
Update for Cursor renaming.
1446
1 by brian
clean slate
1447
  /* Check for duplicate fields and check type of table to create */
1320.1.8 by Brian Aker
Temporary fix for allowing engines to say "don't do this".
1448
  if (not alter_info->create_list.elements)
1 by brian
clean slate
1449
  {
1450
    my_message(ER_TABLE_MUST_HAVE_COLUMNS, ER(ER_TABLE_MUST_HAVE_COLUMNS),
1451
               MYF(0));
1039.1.11 by Brian Aker
Refactor function to make sense.
1452
    return true;
1 by brian
clean slate
1453
  }
1358.1.9 by Brian Aker
Update for std::string
1454
  assert(identifier.getTableName() == table_proto.name());
1 by brian
clean slate
1455
  db_options= create_info->table_options;
1320.1.15 by Brian Aker
Remove old need for Cursor in creating table.
1456
1415 by Brian Aker
Mass overhaul to use schema_identifier.
1457
  set_table_default_charset(create_info, identifier.getSchemaName().c_str());
1 by brian
clean slate
1458
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1459
  /* Build a Table object to pass down to the engine, and the do the actual create. */
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1460
  if (not prepare_create_table(session, create_info, table_proto, alter_info,
1874.3.2 by Brian Aker
Updating a few bits of the interface to be specific to table::Concurrent
1461
                                     internal_tmp_table,
1462
                                     &db_options,
1463
                                     &key_info_buffer, &key_count,
1464
                                     select_field_count))
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1465
  {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1466
    boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* CREATE TABLE (some confussion on naming, double check) */
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1467
    error= locked_create_event(session,
1468
                               identifier,
1469
                               create_info,
1470
                               table_proto,
1471
                               alter_info,
1472
                               is_if_not_exists,
1473
                               internal_tmp_table,
1474
                               db_options, key_count,
1475
                               key_info_buffer);
1476
  }
1477
1478
  session->set_proc_info("After create");
1479
1480
  return(error);
1481
}
1482
1483
/**
1484
  @note the following two methods implement create [temporary] table.
1485
*/
1486
static bool drizzle_create_table(Session *session,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
1487
                                 const TableIdentifier &identifier,
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1488
                                 HA_CREATE_INFO *create_info,
1489
                                 message::Table &table_proto,
1490
                                 AlterInfo *alter_info,
1491
                                 bool internal_tmp_table,
1492
                                 uint32_t select_field_count,
1493
                                 bool is_if_not_exists)
1494
{
1495
  Table *name_lock= NULL;
1496
  bool result;
1497
1498
  if (session->lock_table_name_if_not_cached(identifier, &name_lock))
1499
  {
1500
    result= true;
1501
  }
1502
  else if (name_lock == NULL)
1 by brian
clean slate
1503
  {
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1504
    if (is_if_not_exists)
1 by brian
clean slate
1505
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1506
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1 by brian
clean slate
1507
                          ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1358.1.9 by Brian Aker
Update for std::string
1508
                          identifier.getTableName().c_str());
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1509
      create_info->table_existed= 1;
1510
      result= false;
1511
    }
1512
    else
1513
    {
1954.2.1 by Brian Aker
getSQLPath() modified to take a string so that we can const the table
1514
      std::string path;
1515
      identifier.getSQLPath(path);
1516
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), path.c_str());
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1517
      result= true;
1518
    }
1519
  }
1520
  else
1521
  {
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1522
    result= create_table_no_lock(session,
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1523
                                       identifier,
1524
                                       create_info,
1525
                                       table_proto,
1526
                                       alter_info,
1527
                                       internal_tmp_table,
1528
                                       select_field_count,
1529
                                       is_if_not_exists);
1530
  }
1531
1532
  if (name_lock)
1533
  {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1534
    boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* Lock for removing name_lock during table create */
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1535
    session->unlink_open_table(name_lock);
1536
  }
1537
1538
  return(result);
1 by brian
clean slate
1539
}
1540
1541
1542
/*
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1543
  Database locking aware wrapper for create_table_no_lock(),
1 by brian
clean slate
1544
*/
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1545
bool create_table(Session *session,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
1546
                        const TableIdentifier &identifier,
1 by brian
clean slate
1547
                        HA_CREATE_INFO *create_info,
1320.1.2 by Brian Aker
More reference counting.
1548
			message::Table &table_proto,
1126.3.3 by Jay Pipes
Moves Alter_info out into its own header and source file, cleans up some related include mess in sql_lex.h, and renames Alter_info to AlterInfo.
1549
                        AlterInfo *alter_info,
1 by brian
clean slate
1550
                        bool internal_tmp_table,
1222.2.3 by Brian Aker
Remove a few more options, from options in HA_CREATE_INFO.
1551
                        uint32_t select_field_count,
1552
                        bool is_if_not_exists)
1 by brian
clean slate
1553
{
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
1554
  if (identifier.isTmp())
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1555
  {
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1556
    return create_table_no_lock(session,
1372.1.2 by Brian Aker
Rework to remove goto in the create table path.
1557
                                      identifier,
1558
                                      create_info,
1559
                                      table_proto,
1560
                                      alter_info,
1561
                                      internal_tmp_table,
1562
                                      select_field_count,
1563
                                      is_if_not_exists);
1564
  }
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
1565
1566
  return drizzle_create_table(session,
1567
                              identifier,
1568
                              create_info,
1569
                              table_proto,
1570
                              alter_info,
1571
                              internal_tmp_table,
1572
                              select_field_count,
1573
                              is_if_not_exists);
1 by brian
clean slate
1574
}
1575
1576
1577
/*
1578
** Give the key name after the first field with an optional '_#' after
1579
**/
1580
1581
static bool
1535 by Brian Aker
Rename of KEY to KeyInfo
1582
check_if_keyname_exists(const char *name, KeyInfo *start, KeyInfo *end)
1 by brian
clean slate
1583
{
1535 by Brian Aker
Rename of KEY to KeyInfo
1584
  for (KeyInfo *key=start ; key != end ; key++)
1 by brian
clean slate
1585
    if (!my_strcasecmp(system_charset_info,name,key->name))
1586
      return 1;
1587
  return 0;
1588
}
1589
1590
1591
static char *
1535 by Brian Aker
Rename of KEY to KeyInfo
1592
make_unique_key_name(const char *field_name,KeyInfo *start,KeyInfo *end)
1 by brian
clean slate
1593
{
1594
  char buff[MAX_FIELD_NAME],*buff_end;
1595
1596
  if (!check_if_keyname_exists(field_name,start,end) &&
590.1.1 by Stewart Smith
begin moving from global const char* primary_key_name to methods is_primary_key() and is_primary_key_name()
1597
      !is_primary_key_name(field_name))
1 by brian
clean slate
1598
    return (char*) field_name;			// Use fieldname
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
1599
1600
  buff_end= strncpy(buff, field_name, sizeof(buff)-4);
1601
  buff_end+= strlen(buff);
1 by brian
clean slate
1602
1603
  /*
1604
    Only 3 chars + '\0' left, so need to limit to 2 digit
1605
    This is ok as we can't have more than 100 keys anyway
1606
  */
482 by Brian Aker
Remove uint.
1607
  for (uint32_t i=2 ; i< 100; i++)
1 by brian
clean slate
1608
  {
1609
    *buff_end= '_';
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
1610
    internal::int10_to_str(i, buff_end+1, 10);
1 by brian
clean slate
1611
    if (!check_if_keyname_exists(buff,start,end))
1253.1.6 by Monty Taylor
Moved mem_root functions into drizzled::memory:: namespace.
1612
      return memory::sql_strdup(buff);
1 by brian
clean slate
1613
  }
1614
  return (char*) "not_specified";		// Should never happen
1615
}
1616
1617
1618
/****************************************************************************
1619
** Alter a table definition
1620
****************************************************************************/
1621
1622
/*
1623
  Rename a table.
1624
1625
  SYNOPSIS
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1626
    rename_table()
1578.4.11 by Brian Aker
PAss through the code removing current_session
1627
      session
1130.1.4 by Monty Taylor
Moved StorageEngine into plugin namespace.
1628
      base                      The plugin::StorageEngine handle.
1 by brian
clean slate
1629
      old_db                    The old database name.
1630
      old_name                  The old table name.
1631
      new_db                    The new database name.
1632
      new_name                  The new table name.
1633
1634
  RETURN
55 by brian
Update for using real bool types.
1635
    false   OK
1636
    true    Error
1 by brian
clean slate
1637
*/
1638
1639
bool
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1640
rename_table(Session &session,
1578.4.11 by Brian Aker
PAss through the code removing current_session
1641
                   plugin::StorageEngine *base,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
1642
                   const TableIdentifier &from,
1643
                   const TableIdentifier &to)
1 by brian
clean slate
1644
{
1039.1.11 by Brian Aker
Refactor function to make sense.
1645
  int error= 0;
1 by brian
clean slate
1646
1039.3.10 by Stewart Smith
move ha_rename_table to just be StorageEngine::renameTable with engines implementing renameTableImpl.
1647
  assert(base);
1 by brian
clean slate
1648
1415 by Brian Aker
Mass overhaul to use schema_identifier.
1649
  if (not plugin::StorageEngine::doesSchemaExist(to))
1650
  {
1651
    my_error(ER_NO_DB_ERROR, MYF(0), to.getSchemaName().c_str());
1652
    return true;
1653
  }
1654
1578.4.11 by Brian Aker
PAss through the code removing current_session
1655
  error= base->renameTable(session, from, to);
1039.3.11 by Stewart Smith
fix build error in mysql_rename_table: forgot to remove old dead code.
1656
1 by brian
clean slate
1657
  if (error == HA_ERR_WRONG_COMMAND)
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
1658
  {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1659
    my_error(ER_NOT_SUPPORTED_YET, MYF(0), "ALTER Table");
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
1660
  }
1 by brian
clean slate
1661
  else if (error)
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
1662
  {
1954.2.1 by Brian Aker
getSQLPath() modified to take a string so that we can const the table
1663
    std::string from_path;
1664
    std::string to_path;
1665
1666
    from.getSQLPath(from_path);
1667
    to.getSQLPath(to_path);
1668
1669
    const char *from_identifier= from.isTmp() ? "#sql-temporary" : from_path.c_str();
1670
    const char *to_identifier= to.isTmp() ? "#sql-temporary" : to_path.c_str();
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
1671
1672
    my_error(ER_ERROR_ON_RENAME, MYF(0), from_identifier, to_identifier, error);
1673
  }
1674
1675
  return error ? true : false; 
1 by brian
clean slate
1676
}
1677
1678
1679
/*
1680
  Force all other threads to stop using the table
1681
1682
  SYNOPSIS
1683
    wait_while_table_is_used()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1684
    session			Thread Cursor
1 by brian
clean slate
1685
    table		Table to remove from cache
1686
    function            HA_EXTRA_PREPARE_FOR_DROP if table is to be deleted
1687
                        HA_EXTRA_FORCE_REOPEN if table is not be used
1688
                        HA_EXTRA_PREPARE_FOR_RENAME if table is to be renamed
1689
  NOTES
1690
   When returning, the table will be unusable for other threads until
1691
   the table is closed.
1692
1693
  PREREQUISITES
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1694
    Lock on table::Cache::singleton().mutex()
1 by brian
clean slate
1695
    Win32 clients must also have a WRITE LOCK on the table !
1696
*/
1697
520.1.22 by Brian Aker
Second pass of thd cleanup
1698
void wait_while_table_is_used(Session *session, Table *table,
1 by brian
clean slate
1699
                              enum ha_extra_function function)
1700
{
1701
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1702
  safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle());
1 by brian
clean slate
1703
1208.3.2 by brian
Update for Cursor renaming.
1704
  table->cursor->extra(function);
1 by brian
clean slate
1705
  /* Mark all tables that are in use as 'old' */
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1706
  session->abortLock(table);	/* end threads waiting on lock */
1 by brian
clean slate
1707
1708
  /* Wait until all there are no other threads that has this table open */
1835.1.5 by Brian Aker
Cleans up some spots where we were using mutable but did not need too.
1709
  TableIdentifier identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName());
1877.2.8 by Brian Aker
Additional encapsulation
1710
  table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG);
1 by brian
clean slate
1711
}
1712
1713
/*
1714
  Close a cached table
1715
1716
  SYNOPSIS
1717
    close_cached_table()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1718
    session			Thread Cursor
1 by brian
clean slate
1719
    table		Table to remove from cache
1720
1721
  NOTES
1722
    Function ends by signaling threads waiting for the table to try to
1723
    reopen the table.
1724
1725
  PREREQUISITES
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1726
    Lock on table::Cache::singleton().mutex()
1 by brian
clean slate
1727
    Win32 clients must also have a WRITE LOCK on the table !
1728
*/
1729
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1730
void Session::close_cached_table(Table *table)
1 by brian
clean slate
1731
{
1732
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1733
  wait_while_table_is_used(this, table, HA_EXTRA_FORCE_REOPEN);
1 by brian
clean slate
1734
  /* Close lock if this is not got with LOCK TABLES */
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1735
  if (lock)
1 by brian
clean slate
1736
  {
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1737
    unlockTables(lock);
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1738
    lock= NULL;			// Start locked threads
1 by brian
clean slate
1739
  }
1740
  /* Close all copies of 'table'.  This also frees all LOCK TABLES lock */
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1741
  unlink_open_table(table);
1 by brian
clean slate
1742
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1743
  /* When lock on table::Cache::singleton().mutex() is freed other threads can continue */
1910.2.5 by Brian Aker
Merge in changes such that lock is now broken out into its own directory.
1744
  locking::broadcast_refresh();
1 by brian
clean slate
1745
}
1746
1747
/*
1748
  RETURN VALUES
55 by brian
Update for using real bool types.
1749
    false Message sent to net (admin operation went ok)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1750
    true  Message should be sent by caller
1 by brian
clean slate
1751
          (admin operation or network communication failed)
1752
*/
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
1753
static bool admin_table(Session* session, TableList* tables,
1 by brian
clean slate
1754
                              HA_CHECK_OPT* check_opt,
1755
                              const char *operator_name,
1756
                              thr_lock_type lock_type,
1757
                              bool open_for_modify,
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1758
                              int (Cursor::*operator_func)(Session *,
1 by brian
clean slate
1759
                                                            HA_CHECK_OPT *))
1760
{
327.2.4 by Brian Aker
Refactoring table.h
1761
  TableList *table;
846 by Brian Aker
Removing on typedeffed class.
1762
  Select_Lex *select= &session->lex->select_lex;
1 by brian
clean slate
1763
  List<Item> field_list;
1764
  Item *item;
520.1.22 by Brian Aker
Second pass of thd cleanup
1765
  LEX *lex= session->lex;
1 by brian
clean slate
1766
  int result_code= 0;
1273.1.2 by Jay Pipes
This patch does not change any algorithms or code paths,
1767
  TransactionServices &transaction_services= TransactionServices::singleton();
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
1768
  const CHARSET_INFO * const cs= system_charset_info;
1 by brian
clean slate
1769
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1770
  if (! session->endActiveTransaction())
1046.1.10 by Brian Aker
Formatting around return (style)
1771
    return 1;
1 by brian
clean slate
1772
  field_list.push_back(item = new Item_empty_string("Table",
1773
                                                    NAME_CHAR_LEN * 2,
1774
                                                    cs));
1775
  item->maybe_null = 1;
1776
  field_list.push_back(item = new Item_empty_string("Op", 10, cs));
1777
  item->maybe_null = 1;
1778
  field_list.push_back(item = new Item_empty_string("Msg_type", 10, cs));
1779
  item->maybe_null = 1;
1780
  field_list.push_back(item = new Item_empty_string("Msg_text", 255, cs));
1781
  item->maybe_null = 1;
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1782
  if (session->getClient()->sendFields(&field_list))
971.3.63 by Eric Day
Removed protocol field flags.
1783
    return true;
1 by brian
clean slate
1784
1785
  for (table= tables; table; table= table->next_local)
1786
  {
1787
    char table_name[NAME_LEN*2+2];
1788
    bool fatal_error=0;
1789
1874.1.2 by Brian Aker
Encapsulate table_name from table_list.
1790
    snprintf(table_name, sizeof(table_name), "%s.%s", table->getSchemaName(), table->getTableName());
1 by brian
clean slate
1791
    table->lock_type= lock_type;
1792
    /* open only one table from local list of command */
1793
    {
327.2.4 by Brian Aker
Refactoring table.h
1794
      TableList *save_next_global, *save_next_local;
1 by brian
clean slate
1795
      save_next_global= table->next_global;
1796
      table->next_global= 0;
1797
      save_next_local= table->next_local;
1798
      table->next_local= 0;
481 by Brian Aker
Remove all of uchar.
1799
      select->table_list.first= (unsigned char*)table;
1 by brian
clean slate
1800
      /*
1801
        Time zone tables and SP tables can be add to lex->query_tables list,
1802
        so it have to be prepared.
1672.3.4 by Brian Aker
This change the style on a few TODO, and fixes an error path to correctly
1803
        @todo Investigate if we can put extra tables into argument instead of using lex->query_tables
1 by brian
clean slate
1804
      */
1805
      lex->query_tables= table;
1806
      lex->query_tables_last= &table->next_global;
1807
      lex->query_tables_own_last= 0;
1237.6.2 by Brian Aker
Minor cleanup for dead code.
1808
      session->no_warnings_for_error= 0;
1 by brian
clean slate
1809
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1810
      session->openTablesLock(table);
520.1.22 by Brian Aker
Second pass of thd cleanup
1811
      session->no_warnings_for_error= 0;
1 by brian
clean slate
1812
      table->next_global= save_next_global;
1813
      table->next_local= save_next_local;
1814
    }
1815
1816
    /*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1817
      CHECK Table command is only command where VIEW allowed here and this
1 by brian
clean slate
1818
      command use only temporary teble method for VIEWs resolving => there
1819
      can't be VIEW tree substitition of join view => if opening table
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1820
      succeed then table->table will have real Table pointer as value (in
1 by brian
clean slate
1821
      case of join view substitution table->table can be 0, but here it is
1822
      impossible)
1823
    */
1824
    if (!table->table)
1825
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1826
      if (!session->warn_list.elements)
1827
        push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
1 by brian
clean slate
1828
                     ER_CHECK_NO_SUCH_TABLE, ER(ER_CHECK_NO_SUCH_TABLE));
1001.1.6 by Andrew Ettinger
Match style guide
1829
      result_code= HA_ADMIN_CORRUPT;
1 by brian
clean slate
1830
      goto send_result;
1831
    }
1832
1833
    if ((table->table->db_stat & HA_READ_ONLY) && open_for_modify)
1834
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1835
      char buff[FN_REFLEN + DRIZZLE_ERRMSG_SIZE];
482 by Brian Aker
Remove uint.
1836
      uint32_t length;
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1837
      session->getClient()->store(table_name);
1838
      session->getClient()->store(operator_name);
1839
      session->getClient()->store(STRING_WITH_LEN("error"));
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1840
      length= snprintf(buff, sizeof(buff), ER(ER_OPEN_AS_READONLY),
1841
                       table_name);
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1842
      session->getClient()->store(buff, length);
1405.3.5 by Jay Pipes
TransactionServices method names now meet code style guidelines.
1843
      transaction_services.autocommitOrRollback(session, false);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1844
      session->endTransaction(COMMIT);
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
1845
      session->close_thread_tables();
55 by brian
Update for using real bool types.
1846
      lex->reset_query_tables_list(false);
1 by brian
clean slate
1847
      table->table=0;				// For query cache
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1848
      if (session->getClient()->flush())
1 by brian
clean slate
1849
	goto err;
1850
      continue;
1851
    }
1852
1853
    /* Close all instances of the table to allow repair to rename files */
1578.2.3 by Brian Aker
Take version and encapsulate it in TableShare
1854
    if (lock_type == TL_WRITE && table->table->getShare()->getVersion())
1 by brian
clean slate
1855
    {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1856
      table::Cache::singleton().mutex().lock(); /* Lock type is TL_WRITE and we lock to repair the table */
1857
      const char *old_message=session->enter_cond(COND_refresh, table::Cache::singleton().mutex(),
1703.1.1 by Brian Aker
Update lock interface.
1858
                                                  "Waiting to get writelock");
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1859
      session->abortLock(table->table);
1835.1.5 by Brian Aker
Cleans up some spots where we were using mutable but did not need too.
1860
      TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1887.2.5 by Brian Aker
OSX found a reference to a function that was no longer declared.
1861
      table::Cache::singleton().removeTable(session, identifier, RTFC_WAIT_OTHER_THREAD_FLAG | RTFC_CHECK_KILLED_FLAG);
520.1.22 by Brian Aker
Second pass of thd cleanup
1862
      session->exit_cond(old_message);
1910.2.8 by Brian Aker
Enapsulate Kill.
1863
      if (session->getKilled())
1 by brian
clean slate
1864
	goto err;
1865
      open_for_modify= 0;
1866
    }
1867
1208.3.2 by brian
Update for Cursor renaming.
1868
    result_code = (table->table->cursor->*operator_func)(session, check_opt);
1001.1.3 by Andrew Ettinger
Revert bad spacing
1869
1 by brian
clean slate
1870
send_result:
1871
1872
    lex->cleanup_after_one_table_open();
520.1.22 by Brian Aker
Second pass of thd cleanup
1873
    session->clear_error();  // these errors shouldn't get client
1 by brian
clean slate
1874
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1875
      List_iterator_fast<DRIZZLE_ERROR> it(session->warn_list);
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
1876
      DRIZZLE_ERROR *err;
1 by brian
clean slate
1877
      while ((err= it++))
1878
      {
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1879
        session->getClient()->store(table_name);
1880
        session->getClient()->store(operator_name);
1881
        session->getClient()->store(warning_level_names[err->level].str,
971.6.1 by Eric Day
Renamed Protocol to Client, cleaned up some unnecessary methods along the way.
1882
                               warning_level_names[err->level].length);
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1883
        session->getClient()->store(err->msg);
1884
        if (session->getClient()->flush())
1 by brian
clean slate
1885
          goto err;
1886
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
1887
      drizzle_reset_errors(session, true);
1 by brian
clean slate
1888
    }
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1889
    session->getClient()->store(table_name);
1890
    session->getClient()->store(operator_name);
1 by brian
clean slate
1891
1892
    switch (result_code) {
1893
    case HA_ADMIN_NOT_IMPLEMENTED:
1001.1.3 by Andrew Ettinger
Revert bad spacing
1894
      {
1895
	char buf[ERRMSGSIZE+20];
1896
	uint32_t length=snprintf(buf, ERRMSGSIZE,
1897
                             ER(ER_CHECK_NOT_IMPLEMENTED), operator_name);
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1898
	session->getClient()->store(STRING_WITH_LEN("note"));
1899
	session->getClient()->store(buf, length);
1001.1.3 by Andrew Ettinger
Revert bad spacing
1900
      }
1901
      break;
1 by brian
clean slate
1902
1903
    case HA_ADMIN_OK:
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1904
      session->getClient()->store(STRING_WITH_LEN("status"));
1905
      session->getClient()->store(STRING_WITH_LEN("OK"));
1 by brian
clean slate
1906
      break;
1907
1908
    case HA_ADMIN_FAILED:
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1909
      session->getClient()->store(STRING_WITH_LEN("status"));
1910
      session->getClient()->store(STRING_WITH_LEN("Operation failed"));
1 by brian
clean slate
1911
      break;
1912
1913
    case HA_ADMIN_REJECT:
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1914
      session->getClient()->store(STRING_WITH_LEN("status"));
1915
      session->getClient()->store(STRING_WITH_LEN("Operation need committed state"));
55 by brian
Update for using real bool types.
1916
      open_for_modify= false;
1 by brian
clean slate
1917
      break;
1918
1919
    case HA_ADMIN_ALREADY_DONE:
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1920
      session->getClient()->store(STRING_WITH_LEN("status"));
1921
      session->getClient()->store(STRING_WITH_LEN("Table is already up to date"));
1 by brian
clean slate
1922
      break;
1923
1924
    case HA_ADMIN_CORRUPT:
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1925
      session->getClient()->store(STRING_WITH_LEN("error"));
1926
      session->getClient()->store(STRING_WITH_LEN("Corrupt"));
1 by brian
clean slate
1927
      fatal_error=1;
1928
      break;
1929
1930
    case HA_ADMIN_INVALID:
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1931
      session->getClient()->store(STRING_WITH_LEN("error"));
1932
      session->getClient()->store(STRING_WITH_LEN("Invalid argument"));
1 by brian
clean slate
1933
      break;
1934
1935
    default:				// Probably HA_ADMIN_INTERNAL_ERROR
1936
      {
1937
        char buf[ERRMSGSIZE+20];
482 by Brian Aker
Remove uint.
1938
        uint32_t length=snprintf(buf, ERRMSGSIZE,
338 by Monty Taylor
Tagged more strings.
1939
                             _("Unknown - internal error %d during operation"),
77.1.18 by Monty Taylor
Removed my_vsnprintf and my_snprintf.
1940
                             result_code);
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1941
        session->getClient()->store(STRING_WITH_LEN("error"));
1942
        session->getClient()->store(buf, length);
1 by brian
clean slate
1943
        fatal_error=1;
1944
        break;
1945
      }
1946
    }
1947
    if (table->table)
1948
    {
1949
      if (fatal_error)
1578.2.3 by Brian Aker
Take version and encapsulate it in TableShare
1950
      {
1951
        table->table->getMutableShare()->resetVersion();               // Force close of table
1952
      }
1 by brian
clean slate
1953
      else if (open_for_modify)
1954
      {
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
1955
        if (table->table->getShare()->getType())
1578.2.3 by Brian Aker
Take version and encapsulate it in TableShare
1956
        {
1208.3.2 by brian
Update for Cursor renaming.
1957
          table->table->cursor->info(HA_STATUS_CONST);
1578.2.3 by Brian Aker
Take version and encapsulate it in TableShare
1958
        }
1 by brian
clean slate
1959
        else
1960
        {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1961
          boost::unique_lock<boost::mutex> lock(table::Cache::singleton().mutex());
1835.1.5 by Brian Aker
Cleans up some spots where we were using mutable but did not need too.
1962
	  TableIdentifier identifier(table->table->getShare()->getSchemaName(), table->table->getShare()->getTableName());
1877.2.8 by Brian Aker
Additional encapsulation
1963
          table::Cache::singleton().removeTable(session, identifier, RTFC_NO_FLAG);
1 by brian
clean slate
1964
        }
1965
      }
1966
    }
1405.3.5 by Jay Pipes
TransactionServices method names now meet code style guidelines.
1967
    transaction_services.autocommitOrRollback(session, false);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1968
    session->endTransaction(COMMIT);
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
1969
    session->close_thread_tables();
1 by brian
clean slate
1970
    table->table=0;				// For query cache
2015.3.1 by Brian Aker
Encapsulate client call. Also remove the need to call current_session when
1971
    if (session->getClient()->flush())
1 by brian
clean slate
1972
      goto err;
1973
  }
1974
836 by Brian Aker
Fixed session call from function to method.
1975
  session->my_eof();
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1976
  return(false);
1 by brian
clean slate
1977
1978
err:
1405.3.5 by Jay Pipes
TransactionServices method names now meet code style guidelines.
1979
  transaction_services.autocommitOrRollback(session, true);
934.2.11 by Jay Pipes
Moves end_trans(), begin_trans(), end_active_trans() out of the parser module and adds startTransaction(), endTransaction(), and endActiveTransaction() member methods of Session object.
1980
  session->endTransaction(ROLLBACK);
1039.1.16 by Brian Aker
A lot of little cleanups (most based off lcov)
1981
  session->close_thread_tables();			// Shouldn't be needed
1 by brian
clean slate
1982
  if (table)
1983
    table->table=0;
51.1.8 by Jay Pipes
Resolving conflicts for a few files regarding FALSE=>false
1984
  return(true);
1 by brian
clean slate
1985
}
1986
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
1987
  /*
1988
    Create a new table by copying from source table
1989
1990
    Altough exclusive name-lock on target table protects us from concurrent
1991
    DML and DDL operations on it we still want to wrap .FRM creation and call
1992
    to plugin::StorageEngine::createTable() in critical section protected by
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1993
    table::Cache::singleton().mutex() in order to provide minimal atomicity against operations which
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
1994
    disregard name-locks, like I_S implementation, for example. This is a
1995
    temporary and should not be copied. Instead we should fix our code to
1996
    always honor name-locks.
1997
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
1998
    Also some engines (e.g. NDB cluster) require that table::Cache::singleton().mutex() should be held
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
1999
    during the call to plugin::StorageEngine::createTable().
2000
    See bug #28614 for more info.
2001
  */
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
2002
static bool create_table_wrapper(Session &session, const message::Table& create_table_proto,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
2003
                                 const TableIdentifier &destination_identifier,
2004
                                 const TableIdentifier &src_table,
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
2005
                                 bool is_engine_set)
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2006
{
2007
  int protoerr= EEXIST;
2008
  message::Table new_proto;
1938.4.2 by Brian Aker
Fix style issue around table for message (though this is imperfect,...)
2009
  message::table::shared_ptr src_proto;
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2010
2011
  protoerr= plugin::StorageEngine::getTableDefinition(session,
2012
                                                      src_table,
1354.1.1 by Brian Aker
Modify ptr to reference.
2013
                                                      src_proto);
1910.2.15 by Brian Aker
Update so that we use shared_ptr from the cache throughout more of the
2014
  new_proto.CopyFrom(*src_proto);
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2015
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
2016
  if (destination_identifier.isTmp())
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2017
  {
2018
    new_proto.set_type(message::Table::TEMPORARY);
2019
  }
2020
  else
2021
  {
2022
    new_proto.set_type(message::Table::STANDARD);
2023
  }
2024
2025
  if (is_engine_set)
2026
  {
1502.1.31 by Brian Aker
Merge engine options for schema/table.
2027
    new_proto.mutable_engine()->set_name(create_table_proto.engine().name());
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2028
  }
2029
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
2030
  { // We now do a selective copy of elements on to the new table.
2031
    new_proto.set_name(create_table_proto.name());
2032
    new_proto.set_schema(create_table_proto.schema());
2033
    new_proto.set_catalog(create_table_proto.catalog());
2034
  }
2035
1320.1.4 by Brian Aker
Move proto file write into createTable() (where it obviously belongs).
2036
  if (protoerr && protoerr != EEXIST)
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2037
  {
2038
    if (errno == ENOENT)
1358.1.9 by Brian Aker
Update for std::string
2039
      my_error(ER_BAD_DB_ERROR,MYF(0), destination_identifier.getSchemaName().c_str());
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2040
    else
1358.1.9 by Brian Aker
Update for std::string
2041
      my_error(ER_CANT_CREATE_FILE, MYF(0), destination_identifier.getPath().c_str(), errno);
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2042
2043
    return false;
2044
  }
2045
2046
  /*
2047
    As mysql_truncate don't work on a new table at this stage of
2048
    creation, instead create the table directly (for both normal
2049
    and temporary tables).
2050
  */
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2051
  int err= plugin::StorageEngine::createTable(session,
2052
                                              destination_identifier,
1537 by Brian Aker
Remove dead options/rename Option and remove the update that we no longer
2053
                                              new_proto);
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2054
1616 by Brian Aker
Discovery that create table like was not going through replication path
2055
  if (err == false && not destination_identifier.isTmp())
2056
  {
2057
    TransactionServices &transaction_services= TransactionServices::singleton();
2058
    transaction_services.createTable(&session, new_proto);
2059
  }
2060
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2061
  return err ? false : true;
2062
}
2063
1 by brian
clean slate
2064
/*
2065
  Create a table identical to the specified table
2066
2067
  SYNOPSIS
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
2068
    create_like_table()
520.1.22 by Brian Aker
Second pass of thd cleanup
2069
    session		Thread object
1 by brian
clean slate
2070
    table       Table list element for target table
2071
    src_table   Table list element for source table
2072
    create_info Create info
2073
2074
  RETURN VALUES
55 by brian
Update for using real bool types.
2075
    false OK
2076
    true  error
1 by brian
clean slate
2077
*/
2078
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
2079
bool create_like_table(Session* session,
1954.2.3 by Brian Aker
Update tableidentifier so that it is const in many callers.
2080
                             const TableIdentifier &destination_identifier,
1340.1.1 by Brian Aker
Length usage of identifier down the tree.
2081
                             TableList* table, TableList* src_table,
1389 by Brian Aker
Large reord in ALTER TABLE for RENAME.
2082
                             message::Table &create_table_proto,
1222.1.3 by Brian Aker
Remove used flag for engine.
2083
                             bool is_if_not_exists,
2084
                             bool is_engine_set)
1 by brian
clean slate
2085
{
55 by brian
Update for using real bool types.
2086
  bool res= true;
482 by Brian Aker
Remove uint.
2087
  uint32_t not_used;
1 by brian
clean slate
2088
2089
  /*
2090
    By opening source table we guarantee that it exists and no concurrent
2091
    DDL operation will mess with it. Later we also take an exclusive
1208.3.2 by brian
Update for Cursor renaming.
2092
    name-lock on target table name, which makes copying of .frm cursor,
1130.3.16 by Monty Taylor
Moved the last three methods from plugin/storage_engine.cc to be static methods on StorageEngine.
2093
    call to plugin::StorageEngine::createTable() and binlogging atomic
2094
    against concurrent DML and DDL operations on target table.
2095
    Thus by holding both these "locks" we ensure that our statement is
2096
    properly isolated from all concurrent operations which matter.
1 by brian
clean slate
2097
  */
1109.1.2 by Brian Aker
More from the table patch
2098
  if (session->open_tables_from_list(&src_table, &not_used))
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
2099
    return true;
1 by brian
clean slate
2100
1835.1.5 by Brian Aker
Cleans up some spots where we were using mutable but did not need too.
2101
  TableIdentifier src_identifier(src_table->table->getShare()->getSchemaName(),
2102
                                 src_table->table->getShare()->getTableName(), src_table->table->getShare()->getType());
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2103
2104
1223.4.6 by Brian Aker
Additional TableIdentifier usage.
2105
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2106
  /*
1 by brian
clean slate
2107
    Check that destination tables does not exist. Note that its name
2108
    was already checked when it was added to the table list.
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2109
2110
    For temporary tables we don't aim to grab locks.
1 by brian
clean slate
2111
  */
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2112
  bool table_exists= false;
1395.1.10 by Brian Aker
Trying to be consistent through interface on state of tmp.
2113
  if (destination_identifier.isTmp())
1 by brian
clean slate
2114
  {
1864.3.12 by Brian Aker
REmove an older use of find_temporary_table
2115
    if (session->find_temporary_table(destination_identifier))
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2116
    {
2117
      table_exists= true;
2118
    }
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2119
    else
2120
    {
2121
      bool was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
1616 by Brian Aker
Discovery that create table like was not going through replication path
2122
                                             src_identifier, is_engine_set);
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2123
      if (not was_created) // This is pretty paranoid, but we assume something might not clean up after itself
2124
      {
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
2125
        (void) session->rm_temporary_table(destination_identifier, true);
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2126
      }
2127
      else if (not session->open_temporary_table(destination_identifier))
2128
      {
2129
        // We created, but we can't open... also, a hack.
1608.2.1 by Brian Aker
Modified to table identifier to fix temporary table creation loss of file.
2130
        (void) session->rm_temporary_table(destination_identifier, true);
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2131
      }
2132
      else
2133
      {
2134
        res= false;
2135
      }
2136
    }
1 by brian
clean slate
2137
  }
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2138
  else // Standard table which will require locks.
1 by brian
clean slate
2139
  {
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2140
    Table *name_lock= 0;
2141
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
2142
    if (session->lock_table_name_if_not_cached(destination_identifier, &name_lock))
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2143
    {
2144
      if (name_lock)
2145
      {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
2146
        boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* unlink open tables for create table like*/
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2147
        session->unlink_open_table(name_lock);
2148
      }
2149
2150
      return res;
2151
    }
1309.2.17 by Brian Aker
Update to refactor out the inner code in create table.
2152
1309.1.27 by Brian Aker
Updating interface around doesTablExist()
2153
    if (not name_lock)
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2154
    {
2155
      table_exists= true;
2156
    }
2157
    else if (plugin::StorageEngine::doesTableExist(*session, destination_identifier))
2158
    {
2159
      table_exists= true;
2160
    }
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2161
    else // Otherwise we create the table
2162
    {
1718.1.1 by Brian Aker
Test scoped ptr in the tree.
2163
      bool was_created;
2164
      {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
2165
        boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* We lock for CREATE TABLE LIKE to copy table definition */
1718.1.1 by Brian Aker
Test scoped ptr in the tree.
2166
        was_created= create_table_wrapper(*session, create_table_proto, destination_identifier,
2167
                                               src_identifier, is_engine_set);
2168
      }
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2169
2170
      // So we blew the creation of the table, and we scramble to clean up
2171
      // anything that might have been created (read... it is a hack)
2172
      if (not was_created)
2173
      {
1954.2.4 by Brian Aker
Remove quick_rm_table().
2174
        plugin::StorageEngine::dropTable(*session, destination_identifier);
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2175
      } 
2176
      else
2177
      {
2178
        res= false;
2179
      }
2180
    }
2181
2182
    if (name_lock)
2183
    {
1938.4.10 by Brian Aker
Convert LOCK_open to lock in mutex
2184
      boost_unique_lock_t lock(table::Cache::singleton().mutex()); /* unlink open tables for create table like*/
1547 by Brian Aker
This fixes a bug where we did a full open for a temporary table for CREATE
2185
      session->unlink_open_table(name_lock);
2186
    }
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2187
  }
2188
2189
  if (table_exists)
2190
  {
2191
    if (is_if_not_exists)
2192
    {
2193
      char warn_buff[DRIZZLE_ERRMSG_SIZE];
2194
      snprintf(warn_buff, sizeof(warn_buff),
1874.1.2 by Brian Aker
Encapsulate table_name from table_list.
2195
               ER(ER_TABLE_EXISTS_ERROR), table->getTableName());
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2196
      push_warning(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
2197
                   ER_TABLE_EXISTS_ERROR,warn_buff);
2198
      res= false;
2199
    }
2200
    else
2201
    {
1874.1.2 by Brian Aker
Encapsulate table_name from table_list.
2202
      my_error(ER_TABLE_EXISTS_ERROR, MYF(0), table->getTableName());
1309.2.18 by Brian Aker
Remove a lot of the goto logic from createTable (this will allow me to
2203
    }
2204
  }
2205
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
2206
  return(res);
1 by brian
clean slate
2207
}
2208
2209
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
2210
bool analyze_table(Session* session, TableList* tables, HA_CHECK_OPT* check_opt)
1 by brian
clean slate
2211
{
2212
  thr_lock_type lock_type = TL_READ_NO_INSERT;
2213
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
2214
  return(admin_table(session, tables, check_opt,
1237.6.2 by Brian Aker
Minor cleanup for dead code.
2215
				"analyze", lock_type, true,
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
2216
				&Cursor::ha_analyze));
1 by brian
clean slate
2217
}
2218
2219
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
2220
bool check_table(Session* session, TableList* tables,HA_CHECK_OPT* check_opt)
1 by brian
clean slate
2221
{
2222
  thr_lock_type lock_type = TL_READ_NO_INSERT;
2223
2026.2.1 by Monty Taylor
Renamed things prefixed mysql_ or mysqld_
2224
  return(admin_table(session, tables, check_opt,
1 by brian
clean slate
2225
				"check", lock_type,
1237.6.2 by Brian Aker
Minor cleanup for dead code.
2226
				false,
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
2227
				&Cursor::ha_check));
1 by brian
clean slate
2228
}
2229
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
2230
} /* namespace drizzled */