~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000-2006 MySQL AB
2
1046 by Brian Aker
Merge Jay.
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
17
/* Basic functions needed by many modules */
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
18
#include <config.h>
1046.1.4 by Brian Aker
Move lazy read of refresh to outside of LOCK_open (we don't lock the refresh
19
#include <assert.h>
584.1.13 by Monty Taylor
Split out a little more code. Removed table_list.h from common_includes.
20
575.4.7 by Monty Taylor
More header cleanup.
21
#include <signal.h>
1 by brian
clean slate
22
481.1.15 by Monty Taylor
Removed time.h and sys/time.h from global.h.
23
#if TIME_WITH_SYS_TIME
24
# include <sys/time.h>
25
# include <time.h>
26
#else
27
# if HAVE_SYS_TIME_H
28
#  include <sys/time.h>
29
# else
30
#  include <time.h>
31
# endif
32
#endif
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
33
#include <drizzled/internal/my_pthread.h>
34
#include <drizzled/internal/thread_var.h>
575.4.7 by Monty Taylor
More header cleanup.
35
36
#include <drizzled/sql_select.h>
37
#include <drizzled/error.h>
38
#include <drizzled/gettext.h>
39
#include <drizzled/nested_join.h>
40
#include <drizzled/sql_base.h>
41
#include <drizzled/show.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
42
#include <drizzled/item/cmpfunc.h>
1039.5.31 by Jay Pipes
This patch does a few things:
43
#include <drizzled/replication_services.h>
670.2.4 by Monty Taylor
Removed more stuff from the headers.
44
#include <drizzled/check_stack_overrun.h>
45
#include <drizzled/lock.h>
1130.1.12 by Monty Taylor
Moved service stuff into plugin/
46
#include <drizzled/plugin/listen.h>
2154.2.18 by Brian Aker
Merge in all changes for include files.
47
#include <drizzled/cached_directory.h>
1999.4.9 by Brian Aker
Created EPOCH
48
#include <drizzled/field/epoch.h>
1130.3.1 by Monty Taylor
Moved multi_malloc into drizzled since it's not going away any time soon. Also,
49
#include <drizzled/field/null.h>
2154.2.18 by Brian Aker
Merge in all changes for include files.
50
#include <drizzled/sql_table.h>
2281.5.1 by Muhammad Umair
Merged charset declarations of global_charset_info.h and charset_info.h into charset.h header file.
51
#include <drizzled/charset.h>
2154.2.18 by Brian Aker
Merge in all changes for include files.
52
#include <drizzled/pthread_globals.h>
53
#include <drizzled/internal/iocache.h>
54
#include <drizzled/drizzled.h>
55
#include <drizzled/plugin/authorization.h>
56
#include <drizzled/table/temporary.h>
57
#include <drizzled/table/placeholder.h>
58
#include <drizzled/table/unused.h>
59
#include <drizzled/plugin/storage_engine.h>
60
#include <drizzled/session.h>
2198.1.2 by Olaf van der Spek
Refactor includes
61
#include <drizzled/item/subselect.h>
2234.1.4 by Olaf van der Spek
Refactor includes
62
#include <drizzled/sql_lex.h>
2241.3.14 by Olaf van der Spek
Refactor
63
#include <drizzled/catalog/local.h>
2263.3.11 by Olaf van der Spek
Open Tables
64
#include <drizzled/open_tables_state.h>
2272.1.1 by Olaf van der Spek
Refactor includes
65
#include <drizzled/table/cache.h>
2148.7.11 by Brian Aker
Remove some of the dependencies from session.h so that we make the include
66
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
67
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
68
2241.3.14 by Olaf van der Spek
Refactor
69
namespace drizzled {
1089.2.4 by David Shrewsbury
Put CachedDirectory in mysys namespace; added std namespace to sql_base.cc and default.cc to replace std::
70
1241.9.33 by Monty Taylor
Moved most of the global vars to set_var where they belong.
71
extern bool volatile shutdown_in_progress;
72
2318.2.16 by Olaf van der Spek
Refactor
73
void table_cache_free()
1 by brian
clean slate
74
{
2263.3.4 by Olaf van der Spek
Prefix global refresh_version
75
  g_refresh_version++;				// Force close of open tables
1109.1.4 by Brian Aker
More Table refactor
76
1877.2.3 by Brian Aker
Move unused under table
77
  table::getUnused().clear();
1877.2.5 by Brian Aker
Shift a bit more of the guts of cache to its own space.
78
  table::getCache().clear();
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
79
}
1 by brian
clean slate
80
81
/*
1208.3.2 by brian
Update for Cursor renaming.
82
  Close cursor handle, but leave the table in the table cache
1 by brian
clean slate
83
84
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
85
  close_handle_and_leave_table_as_lock()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
86
  table		Table Cursor
1 by brian
clean slate
87
88
  NOTES
1046 by Brian Aker
Merge Jay.
89
  By leaving the table in the table cache, it disallows any other thread
90
  to open the table
91
1910.2.8 by Brian Aker
Enapsulate Kill.
92
  session->getKilled() will be set if we run out of memory
1046 by Brian Aker
Merge Jay.
93
94
  If closing a MERGE child, the calling function has to take care for
95
  closing the parent too, if necessary.
1 by brian
clean slate
96
*/
97
98
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
99
void close_handle_and_leave_table_as_lock(Table *table)
1 by brian
clean slate
100
{
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
101
  assert(table->db_stat);
1619.1.1 by Brian Aker
Update for TableShare usage of key (remove a case where we had to build it).
102
  assert(table->getShare()->getType() == message::Table::STANDARD);
1 by brian
clean slate
103
104
  /*
105
    Make a local copy of the table share and free the current one.
106
    This has to be done to ensure that the table share is removed from
107
    the table defintion cache as soon as the last instance is removed
108
  */
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
109
  identifier::Table identifier(table->getShare()->getSchemaName(), table->getShare()->getTableName(), message::Table::INTERNAL);
2318.9.3 by Olaf van der Spek
Remove const_cast
110
  TableShare *share= new TableShare(identifier.getType(), identifier, identifier.getKey().vector(),  static_cast<uint32_t>(table->getShare()->getCacheKeySize()));
1 by brian
clean slate
111
1208.3.2 by brian
Update for Cursor renaming.
112
  table->cursor->close();
113
  table->db_stat= 0;                            // Mark cursor closed
2069.4.4 by Brian Aker
Create a shared form of the instance which is a bit more heavier weight then
114
  table::instance::release(table->getMutableShare());
1574 by Brian Aker
Rollup patch for hiding tableshare.
115
  table->setShare(share);
1 by brian
clean slate
116
}
117
118
119
/*****************************************************************************
120
 *	 Functions to free open table cache
121
 ****************************************************************************/
122
123
1109.1.4 by Brian Aker
More Table refactor
124
void Table::intern_close_table()
1 by brian
clean slate
125
{						// Free all structures
1109.1.4 by Brian Aker
More Table refactor
126
  free_io_cache();
1208.3.2 by brian
Update for Cursor renaming.
127
  if (cursor)                              // Not true if name lock
1502.1.13 by Brian Aker
Next bit of TableShare to c++.
128
  {
1502.1.3 by Brian Aker
Cleanup to use references.
129
    delete_table(true);			// close cursor
1502.1.13 by Brian Aker
Next bit of TableShare to c++.
130
  }
1 by brian
clean slate
131
}
132
133
/* Free resources allocated by filesort() and read_record() */
134
1109.1.4 by Brian Aker
More Table refactor
135
void Table::free_io_cache()
1 by brian
clean slate
136
{
1109.1.4 by Brian Aker
More Table refactor
137
  if (sort.io_cache)
1 by brian
clean slate
138
  {
1909.1.1 by Brian Aker
Encapsulation of IO_CACHE.
139
    sort.io_cache->close_cached_file();
2172.3.22 by Brian Aker
This patch adds safe_delete(). All of these locations in the code should be
140
    safe_delete(sort.io_cache);
1 by brian
clean slate
141
  }
142
}
143
144
145
/*
146
  Close all tables which aren't in use by any thread
147
1046.1.12 by Brian Aker
Simplify close_cached_tables() (it now always locks).
148
  @param session Thread context (may be NULL)
1 by brian
clean slate
149
  @param tables List of tables to remove from the cache
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
150
  @param have_lock If table::Cache::mutex() is locked
1 by brian
clean slate
151
  @param wait_for_refresh Wait for a impending flush
152
  @param wait_for_placeholders Wait for tables being reopened so that the GRL
1046 by Brian Aker
Merge Jay.
153
  won't proceed while write-locked tables are being reopened by other
154
  threads.
1 by brian
clean slate
155
520.1.21 by Brian Aker
THD -> Session rename
156
  @remark Session can be NULL, but then wait_for_refresh must be false
1046 by Brian Aker
Merge Jay.
157
  and tables must be NULL.
1 by brian
clean slate
158
*/
159
1109.1.4 by Brian Aker
More Table refactor
160
bool Session::close_cached_tables(TableList *tables, bool wait_for_refresh, bool wait_for_placeholders)
1 by brian
clean slate
161
{
1046.1.12 by Brian Aker
Simplify close_cached_tables() (it now always locks).
162
  bool result= false;
1109.1.4 by Brian Aker
More Table refactor
163
  Session *session= this;
1 by brian
clean slate
164
165
  {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
166
    boost::mutex::scoped_lock scopedLock(table::Cache::mutex()); /* Optionally lock for remove tables from open_cahe if not in use */
2281.4.4 by Olaf van der Spek
Delete write-only Session::some_tables_deleted
167
    if (not tables)
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
168
    {
2263.3.4 by Olaf van der Spek
Prefix global refresh_version
169
      g_refresh_version++;				// Force close of open tables
1877.2.3 by Brian Aker
Move unused under table
170
      table::getUnused().clear();
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
171
    }
172
    else
173
    {
174
      bool found= false;
175
      for (TableList *table= tables; table; table= table->next_local)
176
      {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
177
        if (table::Cache::removeTable(*session, identifier::Table(table->getSchemaName(), table->getTableName()), RTFC_OWNED_BY_Session_FLAG))
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
178
        {
179
          found= true;
180
        }
181
      }
182
      if (!found)
183
        wait_for_refresh= false;			// Nothing to wait for
184
    }
1109.1.4 by Brian Aker
More Table refactor
185
1 by brian
clean slate
186
    if (wait_for_refresh)
187
    {
188
      /*
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
189
        If there is any table that has a lower refresh_version, wait until
190
        this is closed (or this thread is killed) before returning
191
      */
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
192
      session->mysys_var->current_mutex= &table::Cache::mutex();
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
193
      session->mysys_var->current_cond= &COND_refresh;
194
      session->set_proc_info("Flushing tables");
195
196
      session->close_old_data_files();
197
198
      bool found= true;
199
      /* Wait until all threads has closed all the tables we had locked */
1910.2.8 by Brian Aker
Enapsulate Kill.
200
      while (found && ! session->getKilled())
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
201
      {
202
        found= false;
1877.2.6 by Brian Aker
Fixed header/etc.
203
        for (table::CacheMap::const_iterator iter= table::getCache().begin();
1877.2.5 by Brian Aker
Shift a bit more of the guts of cache to its own space.
204
             iter != table::getCache().end();
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
205
             iter++)
206
        {
2192.4.7 by Olaf van der Spek
Use "iter->" instead of "(*iter)."
207
          Table *table= iter->second;
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
208
          /* Avoid a self-deadlock. */
209
          if (table->in_use == session)
210
            continue;
211
          /*
212
            Note that we wait here only for tables which are actually open, and
213
            not for placeholders with Table::open_placeholder set. Waiting for
214
            latter will cause deadlock in the following scenario, for example:
215
216
            conn1-> lock table t1 write;
217
            conn2-> lock table t2 write;
218
            conn1-> flush tables;
219
            conn2-> flush tables;
220
221
            It also does not make sense to wait for those of placeholders that
222
            are employed by CREATE TABLE as in this case table simply does not
223
            exist yet.
224
          */
225
          if (table->needs_reopen_or_name_lock() && (table->db_stat ||
226
                                                     (table->open_placeholder && wait_for_placeholders)))
227
          {
228
            found= true;
2085.2.2 by Brian Aker
Check that we have the lock before making the call that requires that we
229
            COND_refresh.wait(scopedLock);
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
230
            break;
231
          }
232
        }
233
      }
234
      /*
235
        No other thread has the locked tables open; reopen them and get the
236
        old locks. This should always succeed (unless some external process
237
        has removed the tables)
238
      */
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
239
      result= session->reopen_tables();
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
240
241
      /* Set version for table */
2263.3.2 by Olaf van der Spek
Use open_tables
242
      for (Table *table= session->open_tables.open_tables_; table ; table= table->getNext())
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
243
      {
1 by brian
clean slate
244
        /*
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
245
          Preserve the version (0) of write locked tables so that a impending
246
          global read lock won't sneak in.
1 by brian
clean slate
247
        */
1798.3.4 by Brian Aker
Remove a if 0 block, capture a lock inside of {}
248
        if (table->reginfo.lock_type < TL_WRITE_ALLOW_WRITE)
249
          table->getMutableShare()->refreshVersion();
1 by brian
clean slate
250
      }
251
    }
252
  }
1046.1.12 by Brian Aker
Simplify close_cached_tables() (it now always locks).
253
1 by brian
clean slate
254
  if (wait_for_refresh)
255
  {
2275.3.4 by Olaf van der Spek
Thread
256
    boost::mutex::scoped_lock scopedLock(session->mysys_var->mutex);
520.1.22 by Brian Aker
Second pass of thd cleanup
257
    session->mysys_var->current_mutex= 0;
258
    session->mysys_var->current_cond= 0;
259
    session->set_proc_info(0);
1 by brian
clean slate
260
  }
1046.1.12 by Brian Aker
Simplify close_cached_tables() (it now always locks).
261
262
  return result;
1 by brian
clean slate
263
}
264
265
266
/**
2221.11.1 by Olaf van der Spek
Session::getLex()
267
  move one table to free list
1 by brian
clean slate
268
*/
269
2280.1.8 by Olaf van der Spek
Refactor
270
bool Open_tables_state::free_cached_table()
1 by brian
clean slate
271
{
2280.1.8 by Olaf van der Spek
Refactor
272
  table::Concurrent *table= static_cast<table::Concurrent *>(open_tables_);
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
273
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
274
  safe_mutex_assert_owner(table::Cache::mutex().native_handle());
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
275
  assert(table->key_read == 0);
2280.1.8 by Olaf van der Spek
Refactor
276
  assert(not table->cursor || table->cursor->inited == Cursor::NONE);
1 by brian
clean slate
277
2280.1.8 by Olaf van der Spek
Refactor
278
  open_tables_= table->getNext();
1 by brian
clean slate
279
280
  if (table->needs_reopen_or_name_lock() ||
2280.1.8 by Olaf van der Spek
Refactor
281
      version != g_refresh_version || !table->db_stat)
1 by brian
clean slate
282
  {
1877.2.5 by Brian Aker
Shift a bit more of the guts of cache to its own space.
283
    table::remove_table(table);
2280.1.8 by Olaf van der Spek
Refactor
284
    return true;
285
  }
286
  /*
287
    Open placeholders have Table::db_stat set to 0, so they should be
288
    handled by the first alternative.
289
  */
290
  assert(not table->open_placeholder);
291
292
  /* Free memory and reset for next loop */
293
  table->cursor->ha_reset();
294
  table->in_use= NULL;
295
296
  table::getUnused().link(table);
297
  return false;
1046.1.6 by Brian Aker
Formatting/style cleanup.
298
}
299
300
301
/**
302
  Auxiliary function to close all tables in the open_tables list.
303
304
  @param session Thread context.
305
306
  @remark It should not ordinarily be called directly.
307
*/
308
2280.1.10 by Olaf van der Spek
Remove unused param
309
void Open_tables_state::close_open_tables()
1046.1.6 by Brian Aker
Formatting/style cleanup.
310
{
311
  bool found_old_table= false;
312
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
313
  safe_mutex_assert_not_owner(table::Cache::mutex().native_handle());
1046.1.6 by Brian Aker
Formatting/style cleanup.
314
2275.3.4 by Olaf van der Spek
Thread
315
  boost::mutex::scoped_lock scoped_lock(table::Cache::mutex()); /* Close all open tables on Session */
1046.1.6 by Brian Aker
Formatting/style cleanup.
316
2280.1.8 by Olaf van der Spek
Refactor
317
  while (open_tables_)
1532.1.14 by Brian Aker
We no longer use alloc for placeholders (due to HASH I didn't use a
318
  {
2280.1.8 by Olaf van der Spek
Refactor
319
    found_old_table|= free_cached_table();
1532.1.14 by Brian Aker
We no longer use alloc for placeholders (due to HASH I didn't use a
320
  }
1046.1.6 by Brian Aker
Formatting/style cleanup.
321
  if (found_old_table)
322
  {
323
    /* Tell threads waiting for refresh that something has happened */
1910.2.5 by Brian Aker
Merge in changes such that lock is now broken out into its own directory.
324
    locking::broadcast_refresh();
1046.1.6 by Brian Aker
Formatting/style cleanup.
325
  }
1 by brian
clean slate
326
}
327
328
/*
329
  Find table in list.
330
331
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
332
  find_table_in_list()
333
  table		Pointer to table list
334
  offset		Offset to which list in table structure to use
335
  db_name		Data base name
336
  table_name		Table name
337
338
NOTES:
1109.1.5 by Brian Aker
More extraction from sql_base
339
This is called by find_table_in_global_list().
1046 by Brian Aker
Merge Jay.
340
341
RETURN VALUES
342
NULL	Table not found
343
#		Pointer to found table.
1 by brian
clean slate
344
*/
345
327.2.4 by Brian Aker
Refactoring table.h
346
TableList *find_table_in_list(TableList *table,
1046 by Brian Aker
Merge Jay.
347
                              TableList *TableList::*link,
348
                              const char *db_name,
349
                              const char *table_name)
1 by brian
clean slate
350
{
2456.1.4 by Olaf van der Spek
Refactor
351
  for (; table; table= table->*link)
1 by brian
clean slate
352
  {
2456.1.4 by Olaf van der Spek
Refactor
353
    if ((table->table == 0 || table->table->getShare()->getType() == message::Table::STANDARD)
354
        && not system_charset_info->strcasecmp(table->getSchemaName(), db_name)
355
        && not system_charset_info->strcasecmp(table->getTableName(), table_name))
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
356
    {
1 by brian
clean slate
357
      break;
2085.2.3 by Brian Aker
Fix strcasecmp issues (ie, check UTF-8).
358
    }
1 by brian
clean slate
359
  }
360
  return table;
361
}
362
363
364
/*
365
  Test that table is unique (It's only exists once in the table list)
366
367
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
368
  unique_table()
369
  session                   thread handle
370
  table                 table which should be checked
371
  table_list            list of tables
372
  check_alias           whether to check tables' aliases
373
374
NOTE: to exclude derived tables from check we use following mechanism:
375
a) during derived table processing set Session::derived_tables_processing
376
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
377
Session::derived_tables_processing set. (we can't use JOIN::execute
378
because for PS we perform only JOIN::prepare, but we can't set this
379
flag in JOIN::prepare if we are not sure that we are in derived table
380
processing loop, because multi-update call fix_fields() for some its
381
items (which mean JOIN::prepare for subqueries) before unique_table
382
call to detect which tables should be locked for write).
383
c) unique_table skip all tables which belong to SELECT with
384
SELECT::exclude_from_table_unique_test set.
385
Also SELECT::exclude_from_table_unique_test used to exclude from check
386
tables of main SELECT of multi-delete and multi-update
387
388
We also skip tables with TableList::prelocking_placeholder set,
389
because we want to allow SELECTs from them, and their modification
390
will rise the error anyway.
391
392
TODO: when we will have table/view change detection we can do this check
393
only once for PS/SP
394
395
RETURN
396
found duplicate
397
0 if table is unique
1 by brian
clean slate
398
*/
399
1220.1.13 by Brian Aker
Remove mysql_lock_have_duplicate() (we don't have merge, and our partition
400
TableList* unique_table(TableList *table, TableList *table_list,
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
401
                        bool check_alias)
1 by brian
clean slate
402
{
327.2.4 by Brian Aker
Refactoring table.h
403
  TableList *res;
1 by brian
clean slate
404
  const char *d_name, *t_name, *t_alias;
405
406
  /*
407
    If this function called for query which update table (INSERT/UPDATE/...)
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
408
    then we have in table->table pointer to Table object which we are
327.2.4 by Brian Aker
Refactoring table.h
409
    updating even if it is VIEW so we need TableList of this Table object
1 by brian
clean slate
410
    to get right names (even if lower_case_table_names used).
411
412
    If this function called for CREATE command that we have not opened table
327.2.4 by Brian Aker
Refactoring table.h
413
    (table->table equal to 0) and right names is in current TableList
1 by brian
clean slate
414
    object.
415
  */
416
  if (table->table)
417
  {
418
    /* temporary table is always unique */
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
419
    if (table->table && table->table->getShare()->getType() != message::Table::STANDARD)
1046.1.10 by Brian Aker
Formatting around return (style)
420
      return 0;
1 by brian
clean slate
421
    table= table->find_underlying_table(table->table);
422
    /*
327.2.4 by Brian Aker
Refactoring table.h
423
      as far as we have table->table we have to find real TableList of
1 by brian
clean slate
424
      it in underlying tables
425
    */
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
426
    assert(table);
1 by brian
clean slate
427
  }
1874.1.1 by Brian Aker
Encapsulate schema_name it table_list.
428
  d_name= table->getSchemaName();
1874.1.2 by Brian Aker
Encapsulate table_name from table_list.
429
  t_name= table->getTableName();
1 by brian
clean slate
430
  t_alias= table->alias;
431
432
  for (;;)
433
  {
1220.1.13 by Brian Aker
Remove mysql_lock_have_duplicate() (we don't have merge, and our partition
434
    if ((! (res= find_table_in_global_list(table_list, d_name, t_name))) ||
1 by brian
clean slate
435
        ((!res->table || res->table != table->table) &&
2456.1.4 by Olaf van der Spek
Refactor
436
         (!check_alias || !(files_charset_info->strcasecmp(t_alias, res->alias))) &&
1 by brian
clean slate
437
         res->select_lex && !res->select_lex->exclude_from_table_unique_test))
438
      break;
439
    /*
440
      If we found entry of this table or table of SELECT which already
441
      processed in derived table or top select of multi-update/multi-delete
442
      (exclude_from_table_unique_test) or prelocking placeholder.
443
    */
444
    table_list= res->next_global;
445
  }
2318.6.55 by Olaf van der Spek
Refactor
446
  return res;
1 by brian
clean slate
447
}
448
449
2087.4.1 by Brian Aker
Merge in schema identifier.
450
void Open_tables_state::doGetTableNames(const identifier::Schema &schema_identifier,
1923.1.2 by Brian Aker
Encapsulate temporary tables.
451
                                        std::set<std::string>& set_of_names)
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
452
{
1923.1.2 by Brian Aker
Encapsulate temporary tables.
453
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
454
  {
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
455
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
456
    {
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
457
      set_of_names.insert(table->getShare()->getTableName());
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
458
    }
459
  }
460
}
461
1923.1.2 by Brian Aker
Encapsulate temporary tables.
462
void Open_tables_state::doGetTableNames(CachedDirectory &,
2087.4.1 by Brian Aker
Merge in schema identifier.
463
                                        const identifier::Schema &schema_identifier,
1923.1.2 by Brian Aker
Encapsulate temporary tables.
464
                                        std::set<std::string> &set_of_names)
1387 by Brian Aker
Fix for cases where not all files are removed during a deletion of a schema.
465
{
1415 by Brian Aker
Mass overhaul to use schema_identifier.
466
  doGetTableNames(schema_identifier, set_of_names);
1387 by Brian Aker
Fix for cases where not all files are removed during a deletion of a schema.
467
}
468
2087.4.1 by Brian Aker
Merge in schema identifier.
469
void Open_tables_state::doGetTableIdentifiers(const identifier::Schema &schema_identifier,
2252.1.9 by Olaf van der Spek
Common fwd
470
                                              identifier::table::vector &set_of_identifiers)
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
471
{
1923.1.2 by Brian Aker
Encapsulate temporary tables.
472
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
473
  {
1532.1.15 by Brian Aker
Partial encapsulation of TableShare from Table.
474
    if (schema_identifier.compare(table->getShare()->getSchemaName()))
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
475
    {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
476
      set_of_identifiers.push_back(identifier::Table(table->getShare()->getSchemaName(),
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
477
                                                   table->getShare()->getTableName(),
478
                                                   table->getShare()->getPath()));
479
    }
480
  }
481
}
482
1923.1.2 by Brian Aker
Encapsulate temporary tables.
483
void Open_tables_state::doGetTableIdentifiers(CachedDirectory &,
2087.4.1 by Brian Aker
Merge in schema identifier.
484
                                              const identifier::Schema &schema_identifier,
2252.1.9 by Olaf van der Spek
Common fwd
485
                                              identifier::table::vector &set_of_identifiers)
1429.1.3 by Brian Aker
Merge in work for fetching a list of table identifiers.
486
{
487
  doGetTableIdentifiers(schema_identifier, set_of_identifiers);
488
}
489
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
490
bool Open_tables_state::doDoesTableExist(const identifier::Table &identifier)
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
491
{
1923.1.2 by Brian Aker
Encapsulate temporary tables.
492
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
493
  {
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
494
    if (table->getShare()->getType() == message::Table::TEMPORARY)
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
495
    {
1669.2.2 by Brian Aker
Remove compare and need to keep around lower_table.
496
      if (identifier.getKey() == table->getShare()->getCacheKey())
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
497
      {
1415 by Brian Aker
Mass overhaul to use schema_identifier.
498
        return true;
1358.1.1 by Brian Aker
Fixes regression in performance from Exists patch.
499
      }
500
    }
501
  }
502
503
  return false;
504
}
505
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
506
int Open_tables_state::doGetTableDefinition(const identifier::Table &identifier,
2134.1.6 by Brian Aker
Merge in encapsulation of the table message from the main class.
507
                                            message::Table &table_proto)
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
508
{
1923.1.2 by Brian Aker
Encapsulate temporary tables.
509
  for (Table *table= getTemporaryTables() ; table ; table= table->getNext())
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
510
  {
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
511
    if (table->getShare()->getType() == message::Table::TEMPORARY)
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
512
    {
1669.2.2 by Brian Aker
Remove compare and need to keep around lower_table.
513
      if (identifier.getKey() == table->getShare()->getCacheKey())
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
514
      {
2134.1.6 by Brian Aker
Merge in encapsulation of the table message from the main class.
515
        table_proto.CopyFrom(*(table->getShare()->getTableMessage()));
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
516
1415 by Brian Aker
Mass overhaul to use schema_identifier.
517
        return EEXIST;
1273.19.10 by Brian Aker
Add support for listing temporay tables from show commands.
518
      }
519
    }
520
  }
521
522
  return ENOENT;
523
}
524
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
525
Table *Open_tables_state::find_temporary_table(const identifier::Table &identifier)
1369 by Brian Aker
Small interface bits.
526
{
1608 by Brian Aker
This encapsulates prev/next.
527
  for (Table *table= temporary_tables ; table ; table= table->getNext())
1371 by Brian Aker
Small corrections.
528
  {
1619.1.1 by Brian Aker
Update for TableShare usage of key (remove a case where we had to build it).
529
    if (identifier.getKey() == table->getShare()->getCacheKey())
1371 by Brian Aker
Small corrections.
530
      return table;
531
  }
532
533
  return NULL;                               // Not a temporary table
1369 by Brian Aker
Small interface bits.
534
}
535
1 by brian
clean slate
536
537
/**
538
  Drop a temporary table.
539
520.1.22 by Brian Aker
Second pass of thd cleanup
540
  Try to locate the table in the list of session->temporary_tables.
1 by brian
clean slate
541
  If the table is found:
1046 by Brian Aker
Merge Jay.
542
  - if the table is being used by some outer statement, fail.
543
  - if the table is in session->locked_tables, unlock it and
544
  remove it from the list of locked tables. Currently only transactional
545
  temporary tables are present in the locked_tables list.
546
  - Close the temporary table, remove its .FRM
547
  - remove the table from the list of temporary tables
1 by brian
clean slate
548
549
  This function is used to drop user temporary tables, as well as
550
  internal tables created in CREATE TEMPORARY TABLE ... SELECT
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
551
  or ALTER Table. Even though part of the work done by this function
1 by brian
clean slate
552
  is redundant when the table is internal, as long as we
553
  link both internal and user temporary tables into the same
520.1.22 by Brian Aker
Second pass of thd cleanup
554
  session->temporary_tables list, it's impossible to tell here whether
1 by brian
clean slate
555
  we're dealing with an internal or a user temporary table.
556
557
  @retval  0  the table was found and dropped successfully.
558
  @retval  1  the table was not found in the list of temporary tables
1046 by Brian Aker
Merge Jay.
559
  of this thread
1 by brian
clean slate
560
  @retval -1  the table is in use by a outer query
561
*/
562
2263.3.7 by Olaf van der Spek
Keep functions in Open_tables_state
563
int Open_tables_state::drop_temporary_table(const drizzled::identifier::Table &identifier)
1864.3.13 by Brian Aker
Usage of find_temporary_table() to identifier.
564
{
2263.3.7 by Olaf van der Spek
Keep functions in Open_tables_state
565
  Table* table= find_temporary_table(identifier);
566
  if (not table)
1864.3.13 by Brian Aker
Usage of find_temporary_table() to identifier.
567
    return 1;
568
569
  /* Table might be in use by some outer statement. */
2263.3.10 by Olaf van der Spek
Open Tables
570
  if (table->query_id && table->query_id != session_.getQueryId())
1864.3.13 by Brian Aker
Usage of find_temporary_table() to identifier.
571
  {
572
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
573
    return -1;
574
  }
575
  close_temporary_table(table);
576
  return 0;
577
}
578
1 by brian
clean slate
579
580
/**
1046 by Brian Aker
Merge Jay.
581
  Remove all instances of table from thread's open list and
582
  table cache.
583
584
  @param  session     Thread context
585
  @param  find    Table to remove
1859.2.8 by Brian Aker
Remove memcpy (though really this is just a shell, since it is still a
586
1938.4.1 by Brian Aker
Fix style on naming of shared_ptr for tableshare.
587
  @note because we risk the chance of deleting the share, we can't assume that it will exist past, this should be modified once we can use a TableShare::shared_ptr here.
1 by brian
clean slate
588
*/
589
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
590
void Session::unlink_open_table(Table *find)
1 by brian
clean slate
591
{
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
592
  const identifier::Table::Key find_key(find->getShare()->getCacheKey());
1859.2.8 by Brian Aker
Remove memcpy (though really this is just a shell, since it is still a
593
  Table **prev;
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
594
  safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1 by brian
clean slate
595
596
  /*
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
597
    Note that we need to hold table::Cache::mutex() while changing the
1 by brian
clean slate
598
    open_tables list. Another thread may work on it.
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
599
    (See: table::Cache::removeTable(), wait_completed_table())
1 by brian
clean slate
600
    Closing a MERGE child before the parent would be fatal if the
601
    other thread tries to abort the MERGE lock in between.
602
  */
2263.3.2 by Olaf van der Spek
Use open_tables
603
  for (prev= &open_tables.open_tables_; *prev; )
1 by brian
clean slate
604
  {
1859.2.8 by Brian Aker
Remove memcpy (though really this is just a shell, since it is still a
605
    Table *list= *prev;
1 by brian
clean slate
606
1859.2.8 by Brian Aker
Remove memcpy (though really this is just a shell, since it is still a
607
    if (list->getShare()->getCacheKey() == find_key)
1 by brian
clean slate
608
    {
609
      /* Remove table from open_tables list. */
1608 by Brian Aker
This encapsulates prev/next.
610
      *prev= list->getNext();
1532.1.14 by Brian Aker
We no longer use alloc for placeholders (due to HASH I didn't use a
611
1 by brian
clean slate
612
      /* Close table. */
1887.2.1 by Brian Aker
Merge in refactoring tree. This breaks out TableShare into cache and
613
      table::remove_table(static_cast<table::Concurrent *>(list));
1 by brian
clean slate
614
    }
615
    else
616
    {
617
      /* Step to next entry in open_tables list. */
1608 by Brian Aker
This encapsulates prev/next.
618
      prev= list->getNextPtr();
1 by brian
clean slate
619
    }
620
  }
621
622
  // Notify any 'refresh' threads
1910.2.5 by Brian Aker
Merge in changes such that lock is now broken out into its own directory.
623
  locking::broadcast_refresh();
1 by brian
clean slate
624
}
625
626
627
/**
1046 by Brian Aker
Merge Jay.
628
  Auxiliary routine which closes and drops open table.
629
630
  @param  session         Thread handle
631
  @param  table       Table object for table to be dropped
632
  @param  db_name     Name of database for this table
633
  @param  table_name  Name of this table
634
635
  @note This routine assumes that table to be closed is open only
636
  by calling thread so we needn't wait until other threads
637
  will close the table. Also unless called under implicit or
638
  explicit LOCK TABLES mode it assumes that table to be
639
  dropped is already unlocked. In the former case it will
640
  also remove lock on the table. But one should not rely on
641
  this behaviour as it may change in future.
642
  Currently, however, this function is never called for a
643
  table that was locked with LOCK TABLES.
1 by brian
clean slate
644
*/
645
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
646
void Session::drop_open_table(Table *table, const identifier::Table &identifier)
1 by brian
clean slate
647
{
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
648
  if (table->getShare()->getType())
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
649
  {
2263.3.10 by Olaf van der Spek
Open Tables
650
    open_tables.close_temporary_table(table);
1237.6.12 by Brian Aker
Adding patch for engine methods for definition files.
651
  }
1 by brian
clean slate
652
  else
653
  {
2275.3.4 by Olaf van der Spek
Thread
654
    boost::mutex::scoped_lock scoped_lock(table::Cache::mutex()); /* Close and drop a table (AUX routine) */
1 by brian
clean slate
655
    /*
656
      unlink_open_table() also tells threads waiting for refresh or close
657
      that something has happened.
658
    */
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
659
    unlink_open_table(table);
2068.7.1 by Brian Aker
First pass through error correction in SE interface for drop table.
660
    (void)plugin::StorageEngine::dropTable(*this, identifier);
1 by brian
clean slate
661
  }
662
}
663
664
665
/*
1046 by Brian Aker
Merge Jay.
666
  Wait for condition but allow the user to send a kill to mysqld
1 by brian
clean slate
667
1046 by Brian Aker
Merge Jay.
668
  SYNOPSIS
669
  wait_for_condition()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
670
  session	Thread Cursor
1046 by Brian Aker
Merge Jay.
671
  mutex	mutex that is currently hold that is associated with condition
672
  Will be unlocked on return
673
  cond	Condition to wait for
1 by brian
clean slate
674
*/
675
1812.3.5 by Brian Aker
Move to boost condition_any
676
void Session::wait_for_condition(boost::mutex &mutex, boost::condition_variable_any &cond)
1 by brian
clean slate
677
{
678
  /* Wait until the current table is up to date */
1054.1.10 by Brian Aker
Move open_table() to session.
679
  const char *saved_proc_info;
1786.2.1 by Brian Aker
Current boost work (more conversion).
680
  mysys_var->current_mutex= &mutex;
681
  mysys_var->current_cond= &cond;
1054.1.10 by Brian Aker
Move open_table() to session.
682
  saved_proc_info= get_proc_info();
683
  set_proc_info("Waiting for table");
1798.3.5 by Brian Aker
Scope wait_for_condition().
684
  {
685
    /*
686
      We must unlock mutex first to avoid deadlock becasue conditions are
687
      sent to this thread by doing locks in the following order:
688
      lock(mysys_var->mutex)
689
      lock(mysys_var->current_mutex)
690
691
      One by effect of this that one can only use wait_for_condition with
692
      condition variables that are guranteed to not disapper (freed) even if this
693
      mutex is unlocked
694
    */
2275.3.4 by Olaf van der Spek
Thread
695
    boost::mutex::scoped_lock scopedLock(mutex, boost::adopt_lock_t());
1910.2.8 by Brian Aker
Enapsulate Kill.
696
    if (not getKilled())
1798.3.5 by Brian Aker
Scope wait_for_condition().
697
    {
698
      cond.wait(scopedLock);
699
    }
700
  }
2275.3.4 by Olaf van der Spek
Thread
701
  boost::mutex::scoped_lock mysys_scopedLock(mysys_var->mutex);
1054.1.10 by Brian Aker
Move open_table() to session.
702
  mysys_var->current_mutex= 0;
703
  mysys_var->current_cond= 0;
704
  set_proc_info(saved_proc_info);
1 by brian
clean slate
705
}
706
707
708
/**
1046 by Brian Aker
Merge Jay.
709
  Create and insert into table cache placeholder for table
710
  which will prevent its opening (or creation) (a.k.a lock
711
  table name).
712
713
  @param session         Thread context
714
  @param key         Table cache key for name to be locked
715
  @param key_length  Table cache key length
716
717
  @return Pointer to Table object used for name locking or 0 in
718
  case of failure.
1 by brian
clean slate
719
*/
720
2263.3.13 by Olaf van der Spek
Table Cache
721
table::Placeholder& Session::table_cache_insert_placeholder(const drizzled::identifier::Table &arg)
1 by brian
clean slate
722
{
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
723
  safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1 by brian
clean slate
724
725
  /*
726
    Create a table entry with the right key and with an old refresh version
727
  */
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
728
  identifier::Table identifier(arg.getSchemaName(), arg.getTableName(), message::Table::INTERNAL);
2263.3.13 by Olaf van der Spek
Table Cache
729
  table::Placeholder* table= new table::Placeholder(this, identifier);
2263.3.15 by Olaf van der Spek
Fix
730
  table::Cache::insert(table);
2263.3.13 by Olaf van der Spek
Table Cache
731
  return *table;
1 by brian
clean slate
732
}
733
734
735
/**
1046 by Brian Aker
Merge Jay.
736
  Obtain an exclusive name lock on the table if it is not cached
737
  in the table cache.
738
739
  @param      session         Thread context
740
  @param      db          Name of database
741
  @param      table_name  Name of table
742
  @param[out] table       Out parameter which is either:
743
  - set to NULL if table cache contains record for
744
  the table or
745
  - set to point to the Table instance used for
746
  name-locking.
747
748
  @note This function takes into account all records for table in table
749
  cache, even placeholders used for name-locking. This means that
750
  'table' parameter can be set to NULL for some situations when
751
  table does not really exist.
752
753
  @retval  true   Error occured (OOM)
754
  @retval  false  Success. 'table' parameter set according to above rules.
1 by brian
clean slate
755
*/
2263.9.1 by Olaf van der Spek
Table Cache
756
Table* Session::lock_table_name_if_not_cached(const identifier::Table &identifier)
1358.1.9 by Brian Aker
Update for std::string
757
{
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
758
  const identifier::Table::Key &key(identifier.getKey());
2275.3.4 by Olaf van der Spek
Thread
759
  boost::mutex::scoped_lock scope_lock(table::Cache::mutex()); /* Obtain a name lock even though table is not in cache (like for create table)  */
2192.5.1 by Olaf van der Spek
Use find_ptr
760
  if (find_ptr(table::getCache(), key))
2263.9.1 by Olaf van der Spek
Table Cache
761
    return NULL;
762
  Table& table= table_cache_insert_placeholder(identifier);
763
  table.open_placeholder= true;
764
  table.setNext(open_tables.open_tables_);
765
  open_tables.open_tables_= &table;
766
  return &table;
1 by brian
clean slate
767
}
768
769
/*
770
  Open a table.
771
772
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
773
  open_table()
774
  session                 Thread context.
775
  table_list          Open first table in list.
776
  refresh      INOUT  Pointer to memory that will be set to 1 if
777
  we need to close all tables and reopen them.
778
  If this is a NULL pointer, then the table is not
779
  put in the thread-open-list.
780
  flags               Bitmap of flags to modify how open works:
781
  DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
782
  someone has done a flush or namelock on it.
783
  No version number checking is done.
784
  DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
785
  table not the base table or view.
1 by brian
clean slate
786
787
  IMPLEMENTATION
1046 by Brian Aker
Merge Jay.
788
  Uses a cache of open tables to find a table not in use.
1 by brian
clean slate
789
1046 by Brian Aker
Merge Jay.
790
  If table list element for the table to be opened has "create" flag
791
  set and table does not exist, this function will automatically insert
792
  a placeholder for exclusive name lock into the open tables cache and
793
  will return the Table instance that corresponds to this placeholder.
1 by brian
clean slate
794
795
  RETURN
1046 by Brian Aker
Merge Jay.
796
  NULL  Open failed.  If refresh is set then one should close
797
  all other tables and retry the open.
798
#     Success. Pointer to Table object for open table.
1 by brian
clean slate
799
*/
800
801
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
802
Table *Session::openTable(TableList *table_list, bool *refresh, uint32_t flags)
1 by brian
clean slate
803
{
1220.1.7 by Brian Aker
Cleanup interface for open session.
804
  Table *table;
1039.1.4 by Brian Aker
Modified alias to being const.
805
  const char *alias= table_list->alias;
1 by brian
clean slate
806
520.1.22 by Brian Aker
Second pass of thd cleanup
807
  /* Parsing of partitioning information from .frm needs session->lex set up. */
2227.4.8 by Olaf van der Spek
Session::lex()
808
  assert(lex().is_lex_started);
1 by brian
clean slate
809
810
  /* find a unused table in the open table cache */
811
  if (refresh)
2440.4.1 by Brian Aker
Enable syslog and format sql_select
812
  {
1046.1.5 by Brian Aker
Codestyle cleanup.
813
    *refresh= false;
2440.4.1 by Brian Aker
Enable syslog and format sql_select
814
  }
1 by brian
clean slate
815
816
  /* an open table operation needs a lot of the stack space */
1054.1.10 by Brian Aker
Move open_table() to session.
817
  if (check_stack_overrun(this, STACK_MIN_SIZE_FOR_OPEN, (unsigned char *)&alias))
2440.4.1 by Brian Aker
Enable syslog and format sql_select
818
  {
1046.1.5 by Brian Aker
Codestyle cleanup.
819
    return NULL;
2440.4.1 by Brian Aker
Enable syslog and format sql_select
820
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
821
1910.2.8 by Brian Aker
Enapsulate Kill.
822
  if (getKilled())
2440.4.1 by Brian Aker
Enable syslog and format sql_select
823
  {
1046.1.5 by Brian Aker
Codestyle cleanup.
824
    return NULL;
2440.4.1 by Brian Aker
Enable syslog and format sql_select
825
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
826
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
827
  identifier::Table identifier(table_list->getSchemaName(), table_list->getTableName());
828
  const identifier::Table::Key &key(identifier.getKey());
1877.2.4 by Brian Aker
Shift over to cache definition in table/cache
829
  table::CacheRange ppp;
1 by brian
clean slate
830
831
  /*
832
    Unless requested otherwise, try to resolve this table in the list
833
    of temporary tables of this thread. In MySQL temporary tables
834
    are always thread-local and "shadow" possible base tables with the
835
    same name. This block implements the behaviour.
1046.1.5 by Brian Aker
Codestyle cleanup.
836
    TODO -> move this block into a separate function.
1 by brian
clean slate
837
  */
1798.3.2 by Brian Aker
Remove goto for reset.
838
  bool reset= false;
2263.3.2 by Olaf van der Spek
Use open_tables
839
  for (table= open_tables.getTemporaryTables(); table ; table=table->getNext())
1 by brian
clean slate
840
  {
1619.1.1 by Brian Aker
Update for TableShare usage of key (remove a case where we had to build it).
841
    if (table->getShare()->getCacheKey() == key)
1 by brian
clean slate
842
    {
1046.1.4 by Brian Aker
Move lazy read of refresh to outside of LOCK_open (we don't lock the refresh
843
      /*
844
        We're trying to use the same temporary table twice in a query.
845
        Right now we don't support this because a temporary table
846
        is always represented by only one Table object in Session, and
847
        it can not be cloned. Emit an error for an unsupported behaviour.
848
      */
849
      if (table->query_id)
1 by brian
clean slate
850
      {
1669.2.6 by Brian Aker
First pass through encapsulating getAlias() from table.
851
        my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1046.1.5 by Brian Aker
Codestyle cleanup.
852
        return NULL;
1 by brian
clean slate
853
      }
1273.1.1 by Jay Pipes
* Changes Session::warn_id to Session::warn_query_id
854
      table->query_id= getQueryId();
1798.3.2 by Brian Aker
Remove goto for reset.
855
      reset= true;
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
856
      break;
1798.3.2 by Brian Aker
Remove goto for reset.
857
    }
858
  }
859
860
  if (not reset)
861
  {
862
    if (flags & DRIZZLE_OPEN_TEMPORARY_ONLY)
863
    {
2140.1.3 by Brian Aker
Merge in error message fix for just one type of error for unknown table.
864
      my_error(ER_TABLE_UNKNOWN, identifier);
1798.3.2 by Brian Aker
Remove goto for reset.
865
      return NULL;
866
    }
867
1 by brian
clean slate
868
    /*
1798.3.2 by Brian Aker
Remove goto for reset.
869
      If it's the first table from a list of tables used in a query,
870
      remember refresh_version (the version of open_cache state).
871
      If the version changes while we're opening the remaining tables,
872
      we will have to back off, close all the tables opened-so-far,
873
      and try to reopen them.
874
875
      Note-> refresh_version is currently changed only during FLUSH TABLES.
1 by brian
clean slate
876
    */
2263.3.2 by Olaf van der Spek
Use open_tables
877
    if (!open_tables.open_tables_)
1798.3.2 by Brian Aker
Remove goto for reset.
878
    {
2263.3.4 by Olaf van der Spek
Prefix global refresh_version
879
      open_tables.version= g_refresh_version;
1798.3.2 by Brian Aker
Remove goto for reset.
880
    }
2263.3.4 by Olaf van der Spek
Prefix global refresh_version
881
    else if ((open_tables.version != g_refresh_version) &&
1798.3.2 by Brian Aker
Remove goto for reset.
882
             ! (flags & DRIZZLE_LOCK_IGNORE_FLUSH))
883
    {
884
      /* Someone did a refresh while thread was opening tables */
1 by brian
clean slate
885
      if (refresh)
1046.1.5 by Brian Aker
Codestyle cleanup.
886
        *refresh= true;
1798.3.2 by Brian Aker
Remove goto for reset.
887
1046.1.5 by Brian Aker
Codestyle cleanup.
888
      return NULL;
1 by brian
clean slate
889
    }
1798.3.2 by Brian Aker
Remove goto for reset.
890
891
    /*
892
      Non pre-locked/LOCK TABLES mode, and the table is not temporary:
893
      this is the normal use case.
894
      Now we should:
895
      - try to find the table in the table cache.
896
      - if one of the discovered Table instances is name-locked
897
      (table->getShare()->version == 0) back off -- we have to wait
898
      until no one holds a name lock on the table.
899
      - if there is no such Table in the name cache, read the table definition
900
      and insert it into the cache.
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
901
      We perform all of the above under table::Cache::mutex() which currently protects
1798.3.2 by Brian Aker
Remove goto for reset.
902
      the open cache (also known as table cache) and table definitions stored
903
      on disk.
904
    */
905
906
    {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
907
      boost::mutex::scoped_lock scopedLock(table::Cache::mutex());
1798.3.2 by Brian Aker
Remove goto for reset.
908
909
      /*
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
910
        Actually try to find the table in the open_cache.
911
        The cache may contain several "Table" instances for the same
912
        physical table. The instances that are currently "in use" by
913
        some thread have their "in_use" member != NULL.
914
        There is no good reason for having more than one entry in the
915
        hash for the same physical table, except that we use this as
916
        an implicit "pending locks queue" - see
917
        wait_for_locked_table_names for details.
1798.3.2 by Brian Aker
Remove goto for reset.
918
      */
1877.2.5 by Brian Aker
Shift a bit more of the guts of cache to its own space.
919
      ppp= table::getCache().equal_range(key);
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
920
921
      table= NULL;
2241.3.7 by Olaf van der Spek
Refactor
922
      for (table::CacheMap::const_iterator iter= ppp.first; iter != ppp.second; ++iter, table= NULL)
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
923
      {
2192.4.7 by Olaf van der Spek
Use "iter->" instead of "(*iter)."
924
        table= iter->second;
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
925
926
        if (not table->in_use)
927
          break;
928
        /*
929
          Here we flush tables marked for flush.
930
          Normally, table->getShare()->version contains the value of
931
          refresh_version from the moment when this table was
932
          (re-)opened and added to the cache.
933
          If since then we did (or just started) FLUSH TABLES
934
          statement, refresh_version has been increased.
935
          For "name-locked" Table instances, table->getShare()->version is set
936
          to 0 (see lock_table_name for details).
937
          In case there is a pending FLUSH TABLES or a name lock, we
938
          need to back off and re-start opening tables.
939
          If we do not back off now, we may dead lock in case of lock
940
          order mismatch with some other thread:
941
          c1-> name lock t1; -- sort of exclusive lock
942
          c2-> open t2;      -- sort of shared lock
943
          c1-> name lock t2; -- blocks
944
          c2-> open t1; -- blocks
945
        */
946
        if (table->needs_reopen_or_name_lock())
947
        {
948
          if (flags & DRIZZLE_LOCK_IGNORE_FLUSH)
949
          {
950
            /* Force close at once after usage */
2263.3.2 by Olaf van der Spek
Use open_tables
951
            open_tables.version= table->getShare()->getVersion();
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
952
            continue;
953
          }
954
955
          /* Avoid self-deadlocks by detecting self-dependencies. */
956
          if (table->open_placeholder && table->in_use == this)
1798.3.2 by Brian Aker
Remove goto for reset.
957
          {
1835.1.4 by Brian Aker
Update to remove more of the getMutableShare() uses.
958
            my_error(ER_UPDATE_TABLE_USED, MYF(0), table->getShare()->getTableName());
1798.3.2 by Brian Aker
Remove goto for reset.
959
            return NULL;
960
          }
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
961
962
          /*
963
            Back off, part 1: mark the table as "unused" for the
964
            purpose of name-locking by setting table->db_stat to 0. Do
965
            that only for the tables in this thread that have an old
966
            table->getShare()->version (this is an optimization (?)).
967
            table->db_stat == 0 signals wait_for_locked_table_names
968
            that the tables in question are not used any more. See
969
            table_is_used call for details.
970
          */
971
          close_old_data_files(false, false);
972
973
          /*
974
            Back-off part 2: try to avoid "busy waiting" on the table:
975
            if the table is in use by some other thread, we suspend
976
            and wait till the operation is complete: when any
977
            operation that juggles with table->getShare()->version completes,
978
            it broadcasts COND_refresh condition variable.
979
            If 'old' table we met is in use by current thread we return
980
            without waiting since in this situation it's this thread
981
            which is responsible for broadcasting on COND_refresh
982
            (and this was done already in Session::close_old_data_files()).
983
            Good example of such situation is when we have statement
984
            that needs two instances of table and FLUSH TABLES comes
985
            after we open first instance but before we open second
986
            instance.
987
          */
988
          if (table->in_use != this)
989
          {
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
990
            /* wait_for_conditionwill unlock table::Cache::mutex() for us */
991
            wait_for_condition(table::Cache::mutex(), COND_refresh);
2140.1.10 by Brian Aker
Switch to scoped_lock
992
            scopedLock.release();
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
993
          }
994
          else
995
          {
2140.1.10 by Brian Aker
Switch to scoped_lock
996
            scopedLock.unlock();
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
997
          }
2140.1.10 by Brian Aker
Switch to scoped_lock
998
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
999
          /*
1000
            There is a refresh in progress for this table.
1001
            Signal the caller that it has to try again.
1002
          */
1003
          if (refresh)
1004
            *refresh= true;
2140.1.10 by Brian Aker
Switch to scoped_lock
1005
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1006
          return NULL;
1007
        }
1008
      }
2140.1.10 by Brian Aker
Switch to scoped_lock
1009
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1010
      if (table)
1011
      {
1887.2.1 by Brian Aker
Merge in refactoring tree. This breaks out TableShare into cache and
1012
        table::getUnused().unlink(static_cast<table::Concurrent *>(table));
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1013
        table->in_use= this;
1014
      }
1015
      else
1016
      {
1017
        /* Insert a new Table instance into the open cache */
1018
        /* Free cache if too big */
1877.2.3 by Brian Aker
Move unused under table
1019
        table::getUnused().cull();
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1020
1021
        if (table_list->isCreate())
1022
        {
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1023
          identifier::Table  lock_table_identifier(table_list->getSchemaName(), table_list->getTableName(), message::Table::STANDARD);
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1024
1025
          if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1026
          {
1027
            /*
1028
              Table to be created, so we need to create placeholder in table-cache.
1029
            */
2263.3.13 by Olaf van der Spek
Table Cache
1030
            table= &table_cache_insert_placeholder(lock_table_identifier);
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1031
            /*
1032
              Link placeholder to the open tables list so it will be automatically
1033
              removed once tables are closed. Also mark it so it won't be ignored
1034
              by other trying to take name-lock.
1035
            */
1036
            table->open_placeholder= true;
2263.3.2 by Olaf van der Spek
Use open_tables
1037
            table->setNext(open_tables.open_tables_);
1038
            open_tables.open_tables_= table;
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1039
1040
            return table ;
1041
          }
1042
          /* Table exists. Let us try to open it. */
1043
        }
1044
1045
        /* make a new table */
1046
        {
1859.2.6 by Brian Aker
Merge in a bit more strictness around table type.
1047
          table::Concurrent *new_table= new table::Concurrent;
1048
          table= new_table;
2263.3.13 by Olaf van der Spek
Table Cache
1049
          if (new_table->open_unireg_entry(this, alias, identifier))
1859.2.6 by Brian Aker
Merge in a bit more strictness around table type.
1050
          {
1051
            delete new_table;
1052
            return NULL;
1053
          }
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1054
          (void)table::Cache::insert(new_table);
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1055
        }
1056
      }
2140.1.10 by Brian Aker
Switch to scoped_lock
1057
    }
1798.3.6 by Brian Aker
This is just a refactor for {} so I can follow a different merge a bit
1058
1798.3.2 by Brian Aker
Remove goto for reset.
1059
    if (refresh)
1060
    {
2263.3.2 by Olaf van der Spek
Use open_tables
1061
      table->setNext(open_tables.open_tables_); /* Link into simple list */
1062
      open_tables.open_tables_= table;
1798.3.2 by Brian Aker
Remove goto for reset.
1063
    }
1064
    table->reginfo.lock_type= TL_READ; /* Assume read */
1065
1066
  }
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
1067
  assert(table->getShare()->getTableCount() > 0 || table->getShare()->getType() != message::Table::STANDARD);
1 by brian
clean slate
1068
1069
  /* Fix alias if table name changes */
1669.2.6 by Brian Aker
First pass through encapsulating getAlias() from table.
1070
  if (strcmp(table->getAlias(), alias))
1 by brian
clean slate
1071
  {
1864.4.4 by Brian Aker
We now handle the free of the alias inside of table.
1072
    table->setAlias(alias);
1 by brian
clean slate
1073
  }
1046.1.5 by Brian Aker
Codestyle cleanup.
1074
1 by brian
clean slate
1075
  /* These variables are also set in reopen_table() */
2263.3.2 by Olaf van der Spek
Use open_tables
1076
  table->tablenr= open_tables.current_tablenr++;
1046.1.5 by Brian Aker
Codestyle cleanup.
1077
  table->used_fields= 0;
1078
  table->const_table= 0;
274 by Brian Aker
my_bool conversion in Table
1079
  table->null_row= false;
1080
  table->maybe_null= false;
1081
  table->force_index= false;
1 by brian
clean slate
1082
  table->status=STATUS_NO_RECORD;
1672.3.5 by Brian Aker
This replaces the allocation we do for insert/update.
1083
  table->insert_values.clear();
1 by brian
clean slate
1084
  /* Catch wrong handling of the auto_increment_field_not_null. */
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1085
  assert(!table->auto_increment_field_not_null);
55 by brian
Update for using real bool types.
1086
  table->auto_increment_field_not_null= false;
1 by brian
clean slate
1087
  if (table->timestamp_field)
1607 by Brian Aker
Cursor now fully handles the update to timestamp for the engine.
1088
  {
1 by brian
clean slate
1089
    table->timestamp_field_type= table->timestamp_field->get_auto_set_type();
1607 by Brian Aker
Cursor now fully handles the update to timestamp for the engine.
1090
  }
1 by brian
clean slate
1091
  table->pos_in_table_list= table_list;
1092
  table->clear_column_bitmaps();
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1093
  assert(table->key_read == 0);
1046.1.5 by Brian Aker
Codestyle cleanup.
1094
1095
  return table;
1 by brian
clean slate
1096
}
1097
1098
1099
/**
1046 by Brian Aker
Merge Jay.
1100
  Close all instances of a table open by this thread and replace
1101
  them with exclusive name-locks.
1102
1103
  @param session        Thread context
1104
  @param db         Database name for the table to be closed
1105
  @param table_name Name of the table to be closed
1106
1107
  @note This function assumes that if we are not under LOCK TABLES,
1108
  then there is only one table open and locked. This means that
1109
  the function probably has to be adjusted before it can be used
1110
  anywhere outside ALTER Table.
1111
1112
  @note Must not use TableShare::table_name/db of the table being closed,
1113
  the strings are used in a loop even after the share may be freed.
1 by brian
clean slate
1114
*/
1115
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1116
void Session::close_data_files_and_morph_locks(const identifier::Table &identifier)
1395.1.2 by Brian Aker
More logic pulling from ALTER TABLE
1117
{
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1118
  safe_mutex_assert_owner(table::Cache::mutex().native_handle()); /* Adjust locks at the end of ALTER TABLEL */
1 by brian
clean slate
1119
2263.3.2 by Olaf van der Spek
Use open_tables
1120
  if (open_tables.lock)
1 by brian
clean slate
1121
  {
1122
    /*
1123
      If we are not under LOCK TABLES we should have only one table
1124
      open and locked so it makes sense to remove the lock at once.
1125
    */
2263.3.2 by Olaf van der Spek
Use open_tables
1126
    unlockTables(open_tables.lock);
1127
    open_tables.lock= 0;
1 by brian
clean slate
1128
  }
1129
1130
  /*
1131
    Note that open table list may contain a name-lock placeholder
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1132
    for target table name if we process ALTER Table ... RENAME.
1 by brian
clean slate
1133
    So loop below makes sense even if we are not under LOCK TABLES.
1134
  */
2263.3.2 by Olaf van der Spek
Use open_tables
1135
  for (Table *table= open_tables.open_tables_; table ; table=table->getNext())
1 by brian
clean slate
1136
  {
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
1137
    if (table->getShare()->getCacheKey() == identifier.getKey())
1 by brian
clean slate
1138
    {
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1139
      table->open_placeholder= true;
1 by brian
clean slate
1140
      close_handle_and_leave_table_as_lock(table);
1141
    }
1142
  }
1143
}
1144
1145
1146
/**
1046 by Brian Aker
Merge Jay.
1147
  Reopen all tables with closed data files.
1148
1149
  @param session         Thread context
1150
  @param get_locks   Should we get locks after reopening tables ?
1151
  @param mark_share_as_old  Mark share as old to protect from a impending
1152
  global read lock.
1153
1154
  @note Since this function can't properly handle prelocking and
1155
  create placeholders it should be used in very special
1156
  situations like FLUSH TABLES or ALTER Table. In general
1157
  case one should just repeat open_tables()/lock_tables()
1158
  combination when one needs tables to be reopened (for
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1159
  example see openTablesLock()).
1046 by Brian Aker
Merge Jay.
1160
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1161
  @note One should have lock on table::Cache::mutex() when calling this.
1046 by Brian Aker
Merge Jay.
1162
1163
  @return false in case of success, true - otherwise.
1 by brian
clean slate
1164
*/
1165
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1166
bool Session::reopen_tables()
1 by brian
clean slate
1167
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1168
  Table *table,*next,**prev;
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1169
  Table **tables= 0;			// For locks
1170
  Table **tables_ptr= 0;			// For locks
2064.1.2 by Brian Aker
Remove not_used bit for lock.
1171
  bool error= false;
482 by Brian Aker
Remove uint.
1172
  const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
1046 by Brian Aker
Merge Jay.
1173
    DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
1174
    DRIZZLE_LOCK_IGNORE_FLUSH;
1 by brian
clean slate
1175
2263.3.2 by Olaf van der Spek
Use open_tables
1176
  if (open_tables.open_tables_ == NULL)
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1177
    return false;
1 by brian
clean slate
1178
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1179
  safe_mutex_assert_owner(table::Cache::mutex().native_handle());
1 by brian
clean slate
1180
  {
1181
    /*
1182
      The ptr is checked later
1183
      Do not handle locks of MERGE children.
1184
    */
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1185
    uint32_t opens= 0;
1046.1.13 by Brian Aker
Remove malloc() of table.
1186
2263.3.2 by Olaf van der Spek
Use open_tables
1187
    for (table= open_tables.open_tables_; table ; table=table->getNext())
1502.1.1 by Brian Aker
This is a small update to remove reopen hack (this also moves us one more
1188
    {
1 by brian
clean slate
1189
      opens++;
1502.1.1 by Brian Aker
This is a small update to remove reopen hack (this also moves us one more
1190
    }
1046.1.13 by Brian Aker
Remove malloc() of table.
1191
    tables= new Table *[opens];
1 by brian
clean slate
1192
  }
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1193
1 by brian
clean slate
1194
  tables_ptr =tables;
1195
2263.3.2 by Olaf van der Spek
Use open_tables
1196
  prev= &open_tables.open_tables_;
1197
  for (table= open_tables.open_tables_; table ; table=next)
1 by brian
clean slate
1198
  {
1608 by Brian Aker
This encapsulates prev/next.
1199
    next= table->getNext();
1502.1.1 by Brian Aker
This is a small update to remove reopen hack (this also moves us one more
1200
1669.2.6 by Brian Aker
First pass through encapsulating getAlias() from table.
1201
    my_error(ER_CANT_REOPEN_TABLE, MYF(0), table->getAlias());
1887.2.1 by Brian Aker
Merge in refactoring tree. This breaks out TableShare into cache and
1202
    table::remove_table(static_cast<table::Concurrent *>(table));
1502.1.1 by Brian Aker
This is a small update to remove reopen hack (this also moves us one more
1203
    error= 1;
1 by brian
clean slate
1204
  }
1205
  *prev=0;
2064.1.2 by Brian Aker
Remove not_used bit for lock.
1206
1 by brian
clean slate
1207
  if (tables != tables_ptr)			// Should we get back old locks
1208
  {
1209
    /*
1210
      We should always get these locks. Anyway, we must not go into
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1211
      wait_for_tables() as it tries to acquire table::Cache::mutex(), which is
1 by brian
clean slate
1212
      already locked.
1213
    */
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1214
2281.4.4 by Olaf van der Spek
Delete write-only Session::some_tables_deleted
1215
    if (not lockTables(tables, (uint32_t) (tables_ptr - tables), flags))
1 by brian
clean slate
1216
    {
1217
      /*
1218
        This case should only happen if there is a bug in the reopen logic.
1219
        Need to issue error message to have a reply for the application.
1220
        Not exactly what happened though, but close enough.
1221
      */
1222
      my_error(ER_LOCK_DEADLOCK, MYF(0));
1223
      error=1;
1224
    }
1225
  }
1046.1.13 by Brian Aker
Remove malloc() of table.
1226
2385.1.10 by Olaf van der Spek
Update .yy
1227
  delete[] tables;
1046.1.13 by Brian Aker
Remove malloc() of table.
1228
1910.2.5 by Brian Aker
Merge in changes such that lock is now broken out into its own directory.
1229
  locking::broadcast_refresh();
1046.1.13 by Brian Aker
Remove malloc() of table.
1230
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1231
  return error;
1 by brian
clean slate
1232
}
1233
1234
1235
/**
1046 by Brian Aker
Merge Jay.
1236
  Close handlers for tables in list, but leave the Table structure
1237
  intact so that we can re-open these quickly.
1 by brian
clean slate
1238
1046 by Brian Aker
Merge Jay.
1239
  @param session           Thread context
1240
  @param table         Head of the list of Table objects
1241
  @param morph_locks   true  - remove locks which we have on tables being closed
1242
  but ensure that no DML or DDL will sneak in before
1243
  we will re-open the table (i.e. temporarily morph
1244
  our table-level locks into name-locks).
1245
  false - otherwise
1246
  @param send_refresh  Should we awake waiters even if we didn't close any tables?
1 by brian
clean slate
1247
*/
1248
1046.1.11 by Brian Aker
Refactor call close_old_data_files to Session object.
1249
void Session::close_old_data_files(bool morph_locks, bool send_refresh)
1 by brian
clean slate
1250
{
1251
  bool found= send_refresh;
1252
2263.3.2 by Olaf van der Spek
Use open_tables
1253
  Table *table= open_tables.open_tables_;
1046.1.11 by Brian Aker
Refactor call close_old_data_files to Session object.
1254
1608 by Brian Aker
This encapsulates prev/next.
1255
  for (; table ; table=table->getNext())
1 by brian
clean slate
1256
  {
1257
    /*
1258
      Reopen marked for flush.
1259
    */
1260
    if (table->needs_reopen_or_name_lock())
1261
    {
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1262
      found= true;
1 by brian
clean slate
1263
      if (table->db_stat)
1264
      {
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
1265
        if (morph_locks)
1266
        {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1267
          Table *ulcktbl= table;
1 by brian
clean slate
1268
          if (ulcktbl->lock_count)
1269
          {
1270
            /*
1271
              Wake up threads waiting for table-level lock on this table
1272
              so they won't sneak in when we will temporarily remove our
1273
              lock on it. This will also give them a chance to close their
1274
              instances of this table.
1275
            */
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1276
            abortLock(ulcktbl);
1277
            removeLock(ulcktbl);
1 by brian
clean slate
1278
            ulcktbl->lock_count= 0;
1279
          }
1280
          if ((ulcktbl != table) && ulcktbl->db_stat)
1281
          {
1282
            /*
1283
              Close the parent too. Note that parent can come later in
1284
              the list of tables. It will then be noticed as closed and
1285
              as a placeholder. When this happens, do not clear the
1286
              placeholder flag. See the branch below ("***").
1287
            */
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1288
            ulcktbl->open_placeholder= true;
1 by brian
clean slate
1289
            close_handle_and_leave_table_as_lock(ulcktbl);
1290
          }
1291
          /*
1292
            We want to protect the table from concurrent DDL operations
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1293
            (like RENAME Table) until we will re-open and re-lock it.
1 by brian
clean slate
1294
          */
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1295
          table->open_placeholder= true;
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
1296
        }
1 by brian
clean slate
1297
        close_handle_and_leave_table_as_lock(table);
1298
      }
1299
      else if (table->open_placeholder && !morph_locks)
1300
      {
1301
        /*
1302
          We come here only in close-for-back-off scenario. So we have to
1303
          "close" create placeholder here to avoid deadlocks (for example,
1304
          in case of concurrent execution of CREATE TABLE t1 SELECT * FROM t2
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1305
          and RENAME Table t2 TO t1). In close-for-re-open scenario we will
1 by brian
clean slate
1306
          probably want to let it stay.
1307
1308
          Note "***": We must not enter this branch if the placeholder
1309
          flag has been set because of a former close through a child.
1310
          See above the comment that refers to this note.
1311
        */
1046.1.14 by Brian Aker
More redactoring of all lock issue code that is session bound, to be a
1312
        table->open_placeholder= false;
1 by brian
clean slate
1313
      }
1314
    }
1315
  }
1316
  if (found)
1910.2.5 by Brian Aker
Merge in changes such that lock is now broken out into its own directory.
1317
    locking::broadcast_refresh();
1 by brian
clean slate
1318
}
1319
1320
1321
/*
1322
  drop tables from locked list
1323
1324
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
1325
  drop_locked_tables()
1326
  session			Thread thandler
1327
  db			Database
1328
  table_name		Table name
1 by brian
clean slate
1329
1330
  INFORMATION
1046 by Brian Aker
Merge Jay.
1331
  This is only called on drop tables
1 by brian
clean slate
1332
1046 by Brian Aker
Merge Jay.
1333
  The Table object for the dropped table is unlocked but still kept around
1334
  as a name lock, which means that the table will be available for other
1335
  thread as soon as we call unlock_table_names().
1336
  If there is multiple copies of the table locked, all copies except
1337
  the first, which acts as a name lock, is removed.
1 by brian
clean slate
1338
1339
  RETURN
1046 by Brian Aker
Merge Jay.
1340
#    If table existed, return table
1341
0	 Table was not locked
1 by brian
clean slate
1342
*/
1343
1344
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1345
Table *drop_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1 by brian
clean slate
1346
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1347
  Table *table,*next,**prev, *found= 0;
2263.3.2 by Olaf van der Spek
Use open_tables
1348
  prev= &session->open_tables.open_tables_;
1 by brian
clean slate
1349
1350
  /*
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1351
    Note that we need to hold table::Cache::mutex() while changing the
1 by brian
clean slate
1352
    open_tables list. Another thread may work on it.
2275.3.1 by Olaf van der Spek
Remove table::Cache::singleton()
1353
    (See: table::Cache::removeTable(), wait_completed_table())
1 by brian
clean slate
1354
    Closing a MERGE child before the parent would be fatal if the
1355
    other thread tries to abort the MERGE lock in between.
1356
  */
2263.3.2 by Olaf van der Spek
Use open_tables
1357
  for (table= session->open_tables.open_tables_; table ; table=next)
1 by brian
clean slate
1358
  {
1608 by Brian Aker
This encapsulates prev/next.
1359
    next=table->getNext();
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
1360
    if (table->getShare()->getCacheKey() == identifier.getKey())
1 by brian
clean slate
1361
    {
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1362
      session->removeLock(table);
1 by brian
clean slate
1363
1364
      if (!found)
1365
      {
1366
        found= table;
1367
        /* Close engine table, but keep object around as a name lock */
1368
        if (table->db_stat)
1369
        {
1370
          table->db_stat= 0;
1208.3.2 by brian
Update for Cursor renaming.
1371
          table->cursor->close();
1 by brian
clean slate
1372
        }
1373
      }
1374
      else
1375
      {
1376
        /* We already have a name lock, remove copy */
1887.2.1 by Brian Aker
Merge in refactoring tree. This breaks out TableShare into cache and
1377
        table::remove_table(static_cast<table::Concurrent *>(table));
1 by brian
clean slate
1378
      }
1379
    }
1380
    else
1381
    {
1382
      *prev=table;
1608 by Brian Aker
This encapsulates prev/next.
1383
      prev= table->getNextPtr();
1 by brian
clean slate
1384
    }
1385
  }
1386
  *prev=0;
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1387
1 by brian
clean slate
1388
  if (found)
1910.2.5 by Brian Aker
Merge in changes such that lock is now broken out into its own directory.
1389
    locking::broadcast_refresh();
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1390
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1391
  return found;
1 by brian
clean slate
1392
}
1393
1394
1395
/*
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1396
  If we have the table open, which only happens when a LOCK Table has been
1 by brian
clean slate
1397
  done on the table, change the lock type to a lock that will abort all
1398
  other threads trying to get the lock.
1399
*/
1400
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1401
void abort_locked_tables(Session *session, const drizzled::identifier::Table &identifier)
1 by brian
clean slate
1402
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1403
  Table *table;
2263.3.2 by Olaf van der Spek
Use open_tables
1404
  for (table= session->open_tables.open_tables_; table ; table= table->getNext())
1 by brian
clean slate
1405
  {
1669 by Brian Aker
This patch turns the table_cache into boost::unordered_multimap.
1406
    if (table->getShare()->getCacheKey() == identifier.getKey())
1 by brian
clean slate
1407
    {
1408
      /* If MERGE child, forward lock handling to parent. */
1910.2.7 by Brian Aker
Rename lock methods to be style + well make sense.
1409
      session->abortLock(table);
2085.2.1 by Brian Aker
Fix in style/scope issue/removed dead options to method.
1410
      assert(0);
1 by brian
clean slate
1411
      break;
1412
    }
1413
  }
1414
}
1415
1416
1417
/*
1418
  Open all tables in list
1419
1420
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
1421
  open_tables()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1422
  session - thread Cursor
1046 by Brian Aker
Merge Jay.
1423
  start - list of tables in/out
1424
  counter - number of opened tables will be return using this parameter
1425
  flags   - bitmap of flags to modify how the tables will be open:
1426
  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
1427
  done a flush or namelock on it.
1 by brian
clean slate
1428
1429
  NOTE
1046 by Brian Aker
Merge Jay.
1430
  Unless we are already in prelocked mode, this function will also precache
1431
  all SP/SFs explicitly or implicitly (via views and triggers) used by the
1432
  query and add tables needed for their execution to table list. If resulting
1433
  tables list will be non empty it will mark query as requiring precaching.
1434
  Prelocked mode will be enabled for such query during lock_tables() call.
1 by brian
clean slate
1435
1046 by Brian Aker
Merge Jay.
1436
  If query for which we are opening tables is already marked as requiring
1437
  prelocking it won't do such precaching and will simply reuse table list
1438
  which is already built.
1 by brian
clean slate
1439
1440
  RETURN
1046 by Brian Aker
Merge Jay.
1441
  0  - OK
1442
  -1 - error
1 by brian
clean slate
1443
*/
1444
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1445
int Session::open_tables_from_list(TableList **start, uint32_t *counter, uint32_t flags)
1 by brian
clean slate
1446
{
327.2.4 by Brian Aker
Refactoring table.h
1447
  TableList *tables= NULL;
1 by brian
clean slate
1448
  bool refresh;
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1449
  int result= 0;
1 by brian
clean slate
1450
  /* Also used for indicating that prelocking is need */
1451
  bool safe_to_ignore_table;
1452
2263.3.2 by Olaf van der Spek
Use open_tables
1453
  open_tables.current_tablenr= 0;
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
1454
restart:
1 by brian
clean slate
1455
  *counter= 0;
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1456
  set_proc_info("Opening tables");
1 by brian
clean slate
1457
1458
  /*
1459
    For every table in the list of tables to open, try to find or open
1460
    a table.
1461
  */
1462
  for (tables= *start; tables ;tables= tables->next_global)
1463
  {
55 by brian
Update for using real bool types.
1464
    safe_to_ignore_table= false;
1 by brian
clean slate
1465
1466
    /*
1467
      Ignore placeholders for derived tables. After derived tables
1468
      processing, link to created temporary table will be put here.
1469
      If this is derived table for view then we still want to process
1470
      routines used by this view.
1046 by Brian Aker
Merge Jay.
1471
    */
1 by brian
clean slate
1472
    if (tables->derived)
1473
    {
1474
      continue;
1475
    }
1476
    (*counter)++;
1477
1478
    /*
1317.2.5 by Monty Taylor
Prevent the user from seeing or attempting to access tables that he is not
1479
     * Is the user authorized to see this table? Do this before we check
1480
     * to see if it exists so that an unauthorized user cannot phish for
1481
     * table/schema information via error messages
1482
     */
2087.4.2 by Brian Aker
Modify TableIdentifier to fit with the rest of the identifiers.
1483
    identifier::Table the_table(tables->getSchemaName(), tables->getTableName());
2159.2.7 by Brian Aker
Merge in shared ptr modification for auth (namely we don't take the hit for
1484
    if (not plugin::Authorization::isAuthorized(*user(), the_table))
1317.2.5 by Monty Taylor
Prevent the user from seeing or attempting to access tables that he is not
1485
    {
1486
      result= -1;                               // Fatal error
1487
      break;
1488
    }
1489
1490
1491
    /*
1 by brian
clean slate
1492
      Not a placeholder: must be a base table or a view, and the table is
1493
      not opened yet. Try to open the table.
1494
    */
1034.1.4 by Brian Aker
Fixed TableList to correctly construct (and removed memset() calls in
1495
    if (tables->table == NULL)
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1496
      tables->table= openTable(tables, &refresh, flags);
1 by brian
clean slate
1497
1034.1.4 by Brian Aker
Fixed TableList to correctly construct (and removed memset() calls in
1498
    if (tables->table == NULL)
1 by brian
clean slate
1499
    {
1500
      if (refresh)				// Refresh in progress
1501
      {
1502
        /*
1503
          We have met name-locked or old version of table. Now we have
1504
          to close all tables which are not up to date. We also have to
1505
          throw away set of prelocked tables (and thus close tables from
1506
          this set that were open by now) since it possible that one of
1507
          tables which determined its content was changed.
1508
1509
          Instead of implementing complex/non-robust logic mentioned
1510
          above we simply close and then reopen all tables.
1511
1512
          In order to prepare for recalculation of set of prelocked tables
1513
          we pretend that we have finished calculation which we were doing
1514
          currently.
1515
        */
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1516
        close_tables_for_reopen(start);
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
1517
        goto restart;
1 by brian
clean slate
1518
      }
1519
1520
      if (safe_to_ignore_table)
1521
        continue;
1522
1523
      result= -1;				// Fatal error
1524
      break;
1525
    }
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1526
    if (tables->lock_type != TL_UNLOCK)
1 by brian
clean slate
1527
    {
1528
      if (tables->lock_type == TL_WRITE_DEFAULT)
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1529
        tables->table->reginfo.lock_type= update_lock_default;
1608.2.3 by Brian Aker
Encapsulate type for TableShare.
1530
      else if (tables->table->getShare()->getType() == message::Table::STANDARD)
1 by brian
clean slate
1531
        tables->table->reginfo.lock_type= tables->lock_type;
1532
    }
1533
  }
1534
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1535
  set_proc_info(0);
1 by brian
clean slate
1536
1537
  if (result && tables)
1538
  {
1539
    /*
1540
      Some functions determine success as (tables->table != NULL).
520.1.22 by Brian Aker
Second pass of thd cleanup
1541
      tables->table is in session->open_tables.
1 by brian
clean slate
1542
    */
1543
    tables->table= NULL;
1544
  }
1054.1.9 by Brian Aker
This is a large number of refactors against the Session class for its
1545
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1546
  return(result);
1 by brian
clean slate
1547
}
1548
1549
1550
/*
1551
  Open and lock one table
1552
1553
  SYNOPSIS
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
1554
  openTableLock()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1555
  session			Thread Cursor
1046 by Brian Aker
Merge Jay.
1556
  table_list		Table to open is first table in this list
1557
  lock_type		Lock to use for open
1558
  lock_flags          Flags passed to mysql_lock_table
1 by brian
clean slate
1559
1560
  NOTE
1046 by Brian Aker
Merge Jay.
1561
  This function don't do anything like SP/SF/views/triggers analysis done
1562
  in open_tables(). It is intended for opening of only one concrete table.
1563
  And used only in special contexts.
1 by brian
clean slate
1564
1565
  RETURN VALUES
1046 by Brian Aker
Merge Jay.
1566
  table		Opened table
1567
  0			Error
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1568
1046 by Brian Aker
Merge Jay.
1569
  If ok, the following are also set:
1570
  table_list->lock_type 	lock_type
1571
  table_list->table		table
1 by brian
clean slate
1572
*/
1573
1109.1.3 by Brian Aker
Move names around a bit (to align similar methods)
1574
Table *Session::openTableLock(TableList *table_list, thr_lock_type lock_type)
1 by brian
clean slate
1575
{
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
1576
  Table *table;
1 by brian
clean slate
1577
  bool refresh;
1578
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1579
  set_proc_info("Opening table");
2263.3.2 by Olaf van der Spek
Use open_tables
1580
  open_tables.current_tablenr= 0;
2060.3.1 by Brian Aker
First pass, remove retry.
1581
  while (!(table= openTable(table_list, &refresh)) && refresh) ;
1 by brian
clean slate
1582
1583
  if (table)
1584
  {
1585
    table_list->lock_type= lock_type;
1586
    table_list->table=	   table;
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1587
2263.3.2 by Olaf van der Spek
Use open_tables
1588
    assert(open_tables.lock == 0);	// You must lock everything at once
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1589
    if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
2060.3.1 by Brian Aker
First pass, remove retry.
1590
    {
2263.3.2 by Olaf van der Spek
Use open_tables
1591
      if (not (open_tables.lock= lockTables(&table_list->table, 1, 0)))
2064.1.2 by Brian Aker
Remove not_used bit for lock.
1592
        table= NULL;
2060.3.1 by Brian Aker
First pass, remove retry.
1593
    }
1 by brian
clean slate
1594
  }
1595
1054.1.11 by Brian Aker
Remove dead lock.cc commands.
1596
  set_proc_info(0);
1597
1598
  return table;
1 by brian
clean slate
1599
}
1600
1601
/*
1602
  Lock all tables in list
1603
1604
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
1605
  lock_tables()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1606
  session			Thread Cursor
1046 by Brian Aker
Merge Jay.
1607
  tables		Tables to lock
1608
  count		Number of opened tables
1609
  need_reopen         Out parameter which if true indicates that some
1610
  tables were dropped or altered during this call
1611
  and therefore invoker should reopen tables and
1612
  try to lock them once again (in this case
1613
  lock_tables() will also return error).
1 by brian
clean slate
1614
1615
  NOTES
1046 by Brian Aker
Merge Jay.
1616
  You can't call lock_tables twice, as this would break the dead-lock-free
1617
  handling thr_lock gives us.  You most always get all needed locks at
1618
  once.
1 by brian
clean slate
1619
1046 by Brian Aker
Merge Jay.
1620
  If query for which we are calling this function marked as requring
1621
  prelocking, this function will do implicit LOCK TABLES and change
1622
  session::prelocked_mode accordingly.
1 by brian
clean slate
1623
1624
  RETURN VALUES
1046 by Brian Aker
Merge Jay.
1625
  0	ok
1626
  -1	Error
1 by brian
clean slate
1627
*/
1628
1109.1.5 by Brian Aker
More extraction from sql_base
1629
int Session::lock_tables(TableList *tables, uint32_t count, bool *need_reopen)
1 by brian
clean slate
1630
{
1631
  /*
1632
    We can't meet statement requiring prelocking if we already
1633
    in prelocked mode.
1634
  */
55 by brian
Update for using real bool types.
1635
  *need_reopen= false;
1 by brian
clean slate
1636
1034.1.4 by Brian Aker
Fixed TableList to correctly construct (and removed memset() calls in
1637
  if (tables == NULL)
600 by Brian Aker
Removing more options around changing replication
1638
    return 0;
1 by brian
clean slate
1639
2318.6.59 by Olaf van der Spek
Refactor
1640
  assert(not open_tables.lock);	// You must lock everything at once
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1641
2318.6.30 by Olaf van der Spek
Refactor
1642
  Table** start;
2318.6.59 by Olaf van der Spek
Refactor
1643
  Table** ptr=start= new (mem) Table*[count];
2318.6.30 by Olaf van der Spek
Refactor
1644
  for (TableList* table= tables; table; table= table->next_global)
1 by brian
clean slate
1645
  {
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1646
    if (!table->placeholder())
1647
      *(ptr++)= table->table;
1 by brian
clean slate
1648
  }
2318.6.59 by Olaf van der Spek
Refactor
1649
  if (not (open_tables.lock= lockTables(start, (uint32_t) (ptr - start), DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN)))
1 by brian
clean slate
1650
  {
1054.1.8 by Brian Aker
Remove lock_tables list from session.
1651
    return -1;
1 by brian
clean slate
1652
  }
600 by Brian Aker
Removing more options around changing replication
1653
  return 0;
1 by brian
clean slate
1654
}
1655
1656
1657
/*
1658
  Open a single table without table caching and don't set it in open_list
1659
1660
  SYNPOSIS
1046 by Brian Aker
Merge Jay.
1661
  open_temporary_table()
1662
  session		  Thread object
1663
  path	  Path (without .frm)
1664
  db		  database
1665
  table_name	  Table name
1666
  link_in_list  1 if table should be linked into session->temporary_tables
1667
1668
NOTES:
1669
Used by alter_table to open a temporary table and when creating
1670
a temporary table with CREATE TEMPORARY ...
1671
1672
RETURN
1673
0  Error
1674
#  Table object
1 by brian
clean slate
1675
*/
1676
2263.3.3 by Olaf van der Spek
Use open_tables
1677
Table* Session::open_temporary_table(const identifier::Table &identifier, bool link_in_list)
1 by brian
clean slate
1678
{
1618 by Brian Aker
This is a rollup set of patches for modifications to TableIdentifier to have
1679
  assert(identifier.isTmp());
2318.9.3 by Olaf van der Spek
Remove const_cast
1680
  table::Temporary *new_tmp_table= new table::Temporary(identifier.getType(), identifier, 
1681
    identifier.getPath().c_str(), static_cast<uint32_t>(identifier.getPath().length()));
1039.1.10 by Brian Aker
Minor formating, change of one name to make grep easier :)
1682
  /*
1683
    First open the share, and then open the table from the share we just opened.
1684
  */
2318.9.3 by Olaf van der Spek
Remove const_cast
1685
  if (new_tmp_table->getMutableShare()->open_table_def(*this, identifier) ||
1686
      new_tmp_table->getMutableShare()->open_table_from_share(this, identifier, identifier.getTableName().c_str(),
1687
        (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE | HA_GET_INDEX), ha_open_options, *new_tmp_table))
1 by brian
clean slate
1688
  {
1689
    /* No need to lock share->mutex as this is not needed for tmp tables */
1864.4.1 by Brian Aker
Move share creation into temporary table (there still exists and issue with
1690
    delete new_tmp_table->getMutableShare();
1672.3.4 by Brian Aker
This change the style on a few TODO, and fixes an error path to correctly
1691
    delete new_tmp_table;
2318.9.3 by Olaf van der Spek
Remove const_cast
1692
    return NULL;
1 by brian
clean slate
1693
  }
1694
1109.1.1 by Brian Aker
Applying refactor of tmp table bits back to session. (this all needs to be
1695
  new_tmp_table->reginfo.lock_type= TL_WRITE;	 // Simulate locked
1 by brian
clean slate
1696
1697
  if (link_in_list)
1698
  {
1699
    /* growing temp list at the head */
2263.3.3 by Olaf van der Spek
Use open_tables
1700
    new_tmp_table->setNext(open_tables.temporary_tables);
1608 by Brian Aker
This encapsulates prev/next.
1701
    if (new_tmp_table->getNext())
1702
    {
1703
      new_tmp_table->getNext()->setPrev(new_tmp_table);
1704
    }
2263.3.3 by Olaf van der Spek
Use open_tables
1705
    open_tables.temporary_tables= new_tmp_table;
1706
    open_tables.temporary_tables->setPrev(0);
1109.1.1 by Brian Aker
Applying refactor of tmp table bits back to session. (this all needs to be
1707
  }
1708
  new_tmp_table->pos_in_table_list= 0;
1709
1710
  return new_tmp_table;
1 by brian
clean slate
1711
}
1712
1713
1714
/*****************************************************************************
1046 by Brian Aker
Merge Jay.
1715
 * The following find_field_in_XXX procedures implement the core of the
1716
 * name resolution functionality. The entry point to resolve a column name in a
1717
 * list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
1718
 * for each table reference. In turn, depending on the type of table reference,
1719
 * 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
1720
 * below specific for the type of table reference.
1721
 ******************************************************************************/
1 by brian
clean slate
1722
1723
/* Special Field pointers as return values of find_field_in_XXX functions. */
1724
Field *not_found_field= (Field*) 0x1;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1725
Field *view_ref_found= (Field*) 0x2;
1 by brian
clean slate
1726
520.1.22 by Brian Aker
Second pass of thd cleanup
1727
static void update_field_dependencies(Session *session, Field *field, Table *table)
1 by brian
clean slate
1728
{
520.1.22 by Brian Aker
Second pass of thd cleanup
1729
  if (session->mark_used_columns != MARK_COLUMNS_NONE)
1 by brian
clean slate
1730
  {
1802.16.8 by Padraig O'Sullivan
Removal of all MyBitmap from the code base. Compiles but test failures exist now.
1731
    boost::dynamic_bitset<> *current_bitmap= NULL;
1 by brian
clean slate
1732
1733
    /*
1734
      We always want to register the used keys, as the column bitmap may have
1735
      been set for all fields (for example for view).
1736
    */
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1737
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
1738
    table->covering_keys&= field->part_of_key;
1739
    table->merge_keys|= field->part_of_key;
1 by brian
clean slate
1740
520.1.22 by Brian Aker
Second pass of thd cleanup
1741
    if (session->mark_used_columns == MARK_COLUMNS_READ)
1 by brian
clean slate
1742
    {
1743
      current_bitmap= table->read_set;
1744
    }
1745
    else
1746
    {
1747
      current_bitmap= table->write_set;
1748
    }
1749
1999.4.2 by Brian Aker
Encapsulate the field's position.
1750
    //if (current_bitmap->testAndSet(field->position()))
1751
    if (current_bitmap->test(field->position()))
1 by brian
clean slate
1752
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
1753
      if (session->mark_used_columns == MARK_COLUMNS_WRITE)
1754
        session->dup_field= field;
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1755
      return;
1 by brian
clean slate
1756
    }
1757
    table->used_fields++;
1758
  }
1759
}
1760
1761
1762
/*
1763
  Find field by name in a NATURAL/USING join table reference.
1764
1765
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
1766
  find_field_in_natural_join()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1767
  session			 [in]  thread Cursor
1046 by Brian Aker
Merge Jay.
1768
  table_ref            [in]  table reference to search
1769
  name		 [in]  name of field
1770
  length		 [in]  length of name
1771
  ref                  [in/out] if 'name' is resolved to a view field, ref is
1772
  set to point to the found view field
1773
  register_tree_change [in]  true if ref is not stack variable and we
1774
  need register changes in item tree
1775
  actual_table         [out] the original table reference where the field
1776
  belongs - differs from 'table_list' only for
1777
  NATURAL/USING joins
1 by brian
clean slate
1778
1779
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
1780
  Search for a field among the result fields of a NATURAL/USING join.
1781
  Notice that this procedure is called only for non-qualified field
1782
  names. In the case of qualified fields, we search directly the base
1783
  tables of a natural join.
1 by brian
clean slate
1784
1785
  RETURN
1046 by Brian Aker
Merge Jay.
1786
  NULL        if the field was not found
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
1787
  PTR         Pointer to the found Field
1 by brian
clean slate
1788
*/
1789
1790
static Field *
779.3.1 by Monty Taylor
More cleanup.
1791
find_field_in_natural_join(Session *session, TableList *table_ref,
1792
                           const char *name, uint32_t , Item **,
1793
                           bool, TableList **actual_table)
1 by brian
clean slate
1794
{
2456.1.4 by Olaf van der Spek
Refactor
1795
  List<Natural_join_column>::iterator field_it(table_ref->join_columns->begin());
1 by brian
clean slate
1796
  Natural_join_column *nj_col, *curr_nj_col;
1797
  Field *found_field;
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1798
1799
  assert(table_ref->is_natural_join && table_ref->join_columns);
1800
  assert(*actual_table == NULL);
1 by brian
clean slate
1801
2456.1.4 by Olaf van der Spek
Refactor
1802
  for (nj_col= NULL, curr_nj_col= field_it++; curr_nj_col; curr_nj_col= field_it++)
1 by brian
clean slate
1803
  {
2456.1.4 by Olaf van der Spek
Refactor
1804
    if (!system_charset_info->strcasecmp(curr_nj_col->name(), name))
1 by brian
clean slate
1805
    {
1806
      if (nj_col)
1807
      {
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
1808
        my_error(ER_NON_UNIQ_ERROR, MYF(0), name, session->where());
1019.1.6 by Brian Aker
A number of random cleanups.
1809
        return NULL;
1 by brian
clean slate
1810
      }
1811
      nj_col= curr_nj_col;
1812
    }
1813
  }
1814
  if (!nj_col)
1019.1.6 by Brian Aker
A number of random cleanups.
1815
    return NULL;
1 by brian
clean slate
1816
  {
1817
    /* This is a base table. */
1660.1.3 by Brian Aker
Encapsulate Table in field
1818
    assert(nj_col->table_ref->table == nj_col->table_field->getTable());
1 by brian
clean slate
1819
    found_field= nj_col->table_field;
520.1.22 by Brian Aker
Second pass of thd cleanup
1820
    update_field_dependencies(session, found_field, nj_col->table_ref->table);
1 by brian
clean slate
1821
  }
1822
1823
  *actual_table= nj_col->table_ref;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
1824
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1825
  return(found_field);
1 by brian
clean slate
1826
}
1827
1828
1829
/*
1830
  Find field by name in a base table or a view with temp table algorithm.
1831
1832
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
1833
  find_field_in_table()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1834
  session				thread Cursor
1046 by Brian Aker
Merge Jay.
1835
  table			table where to search for the field
1836
  name			name of field
1837
  length			length of name
1838
  allow_rowid			do allow finding of "_rowid" field?
1839
  cached_field_index_ptr	cached position in field list (used to speedup
1840
  lookup for fields in prepared tables)
1 by brian
clean slate
1841
1842
  RETURN
1046 by Brian Aker
Merge Jay.
1843
  0	field is not found
1844
#	pointer to field
1 by brian
clean slate
1845
*/
1846
1847
Field *
520.1.22 by Brian Aker
Second pass of thd cleanup
1848
find_field_in_table(Session *session, Table *table, const char *name, uint32_t length,
482 by Brian Aker
Remove uint.
1849
                    bool allow_rowid, uint32_t *cached_field_index_ptr)
1 by brian
clean slate
1850
{
1851
  Field **field_ptr, *field;
482 by Brian Aker
Remove uint.
1852
  uint32_t cached_field_index= *cached_field_index_ptr;
1 by brian
clean slate
1853
1854
  /* We assume here that table->field < NO_CACHED_FIELD_INDEX = UINT_MAX */
2456.1.4 by Olaf van der Spek
Refactor
1855
  if (cached_field_index < table->getShare()->sizeFields() 
1856
    && not system_charset_info->strcasecmp(table->getField(cached_field_index)->field_name, name))
1578.2.14 by Brian Aker
Additional pass through to remove raw field access.
1857
  {
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
1858
    field_ptr= table->getFields() + cached_field_index;
1578.2.14 by Brian Aker
Additional pass through to remove raw field access.
1859
  }
1669.3.1 by Brian Aker
Remove usage of my_hash in table_share.
1860
  else if (table->getShare()->getNamedFieldSize())
1 by brian
clean slate
1861
  {
1669.3.1 by Brian Aker
Remove usage of my_hash in table_share.
1862
    field_ptr= table->getMutableShare()->getNamedField(std::string(name, length));
1 by brian
clean slate
1863
    if (field_ptr)
1864
    {
1865
      /*
1000.1.3 by Brian Aker
Renamed TABLE_SHARE to TableShare
1866
        field_ptr points to field in TableShare. Convert it to the matching
1 by brian
clean slate
1867
        field in table
1868
      */
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
1869
      field_ptr= (table->getFields() + table->getShare()->positionFields(field_ptr));
1 by brian
clean slate
1870
    }
1871
  }
1872
  else
1873
  {
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
1874
    if (!(field_ptr= table->getFields()))
2456.1.4 by Olaf van der Spek
Refactor
1875
      return NULL;
1 by brian
clean slate
1876
    for (; *field_ptr; ++field_ptr)
2456.1.4 by Olaf van der Spek
Refactor
1877
      if (not system_charset_info->strcasecmp((*field_ptr)->field_name, name))
1 by brian
clean slate
1878
        break;
1879
  }
1880
1881
  if (field_ptr && *field_ptr)
1882
  {
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
1883
    *cached_field_index_ptr= field_ptr - table->getFields();
1 by brian
clean slate
1884
    field= *field_ptr;
1885
  }
1886
  else
1887
  {
1888
    if (!allow_rowid ||
2456.1.4 by Olaf van der Spek
Refactor
1889
        system_charset_info->strcasecmp(name, "_rowid") ||
1574 by Brian Aker
Rollup patch for hiding tableshare.
1890
        table->getShare()->rowid_field_offset == 0)
2456.1.4 by Olaf van der Spek
Refactor
1891
      return NULL;
1578.2.16 by Brian Aker
Merge in change to getTable() to private the field objects.
1892
    field= table->getField(table->getShare()->rowid_field_offset-1);
1 by brian
clean slate
1893
  }
1894
520.1.22 by Brian Aker
Second pass of thd cleanup
1895
  update_field_dependencies(session, field, table);
1 by brian
clean slate
1896
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
1897
  return field;
1 by brian
clean slate
1898
}
1899
1900
1901
/*
1902
  Find field in a table reference.
1903
1904
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
1905
  find_field_in_table_ref()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
1906
  session			   [in]  thread Cursor
1046 by Brian Aker
Merge Jay.
1907
  table_list		   [in]  table reference to search
1908
  name		   [in]  name of field
1909
  length		   [in]  field length of name
1910
  item_name              [in]  name of item if it will be created (VIEW)
1911
  db_name                [in]  optional database name that qualifies the
1912
  table_name             [in]  optional table name that qualifies the field
1913
  ref		       [in/out] if 'name' is resolved to a view field, ref
1914
  is set to point to the found view field
1915
  allow_rowid		   [in]  do allow finding of "_rowid" field?
1916
  cached_field_index_ptr [in]  cached position in field list (used to
1917
  speedup lookup for fields in prepared tables)
1918
  register_tree_change   [in]  true if ref is not stack variable and we
1919
  need register changes in item tree
1920
  actual_table           [out] the original table reference where the field
1921
  belongs - differs from 'table_list' only for
1922
  NATURAL_USING joins.
1 by brian
clean slate
1923
1924
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
1925
  Find a field in a table reference depending on the type of table
1926
  reference. There are three types of table references with respect
1927
  to the representation of their result columns:
1928
  - an array of Field_translator objects for MERGE views and some
1929
  information_schema tables,
1930
  - an array of Field objects (and possibly a name hash) for stored
1931
  tables,
1932
  - a list of Natural_join_column objects for NATURAL/USING joins.
1933
  This procedure detects the type of the table reference 'table_list'
1934
  and calls the corresponding search routine.
1 by brian
clean slate
1935
1936
  RETURN
1046 by Brian Aker
Merge Jay.
1937
  0			field is not found
1938
  view_ref_found	found value in VIEW (real result is in *ref)
1939
#			pointer to field
1 by brian
clean slate
1940
*/
1941
1942
Field *
520.1.22 by Brian Aker
Second pass of thd cleanup
1943
find_field_in_table_ref(Session *session, TableList *table_list,
482 by Brian Aker
Remove uint.
1944
                        const char *name, uint32_t length,
1 by brian
clean slate
1945
                        const char *item_name, const char *db_name,
1946
                        const char *table_name, Item **ref,
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
1947
                        bool allow_rowid,
482 by Brian Aker
Remove uint.
1948
                        uint32_t *cached_field_index_ptr,
327.2.4 by Brian Aker
Refactoring table.h
1949
                        bool register_tree_change, TableList **actual_table)
1 by brian
clean slate
1950
{
327.2.5 by Brian Aker
Refactoring show command
1951
  Field *fld= NULL;
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1952
1953
  assert(table_list->alias);
1954
  assert(name);
1955
  assert(item_name);
1 by brian
clean slate
1956
1957
  /*
1958
    Check that the table and database that qualify the current field name
1959
    are the same as the table reference we are going to search for the field.
1960
1961
    Exclude from the test below nested joins because the columns in a
1962
    nested join generally originate from different tables. Nested joins
1963
    also have no table name, except when a nested join is a merge view
1964
    or an information schema table.
1965
1966
    We include explicitly table references with a 'field_translation' table,
1967
    because if there are views over natural joins we don't want to search
1968
    inside the view, but we want to search directly in the view columns
1969
    which are represented as a 'field_translation'.
1970
1532.1.2 by Brian Aker
Remove need for memset on Table object.
1971
    TODO-> Ensure that table_name, db_name and tables->db always points to something !
1 by brian
clean slate
1972
  */
1973
  if (/* Exclude nested joins. */
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
1974
      (!table_list->getNestedJoin()) &&
1046 by Brian Aker
Merge Jay.
1975
      /* Include merge views and information schema tables. */
1 by brian
clean slate
1976
      /*
1977
        Test if the field qualifiers match the table reference we plan
1978
        to search.
1979
      */
1980
      table_name && table_name[0] &&
2456.1.4 by Olaf van der Spek
Refactor
1981
      (table_alias_charset->strcasecmp(table_list->alias, table_name) ||
1874.1.1 by Brian Aker
Encapsulate schema_name it table_list.
1982
       (db_name && db_name[0] && table_list->getSchemaName() && table_list->getSchemaName()[0] &&
1983
        strcmp(db_name, table_list->getSchemaName()))))
1046.1.10 by Brian Aker
Formatting around return (style)
1984
    return 0;
1 by brian
clean slate
1985
1986
  *actual_table= NULL;
1987
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
1988
  if (!table_list->getNestedJoin())
1 by brian
clean slate
1989
  {
1990
    /* 'table_list' is a stored table. */
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
1991
    assert(table_list->table);
520.1.22 by Brian Aker
Second pass of thd cleanup
1992
    if ((fld= find_field_in_table(session, table_list->table, name, length,
1 by brian
clean slate
1993
                                  allow_rowid,
1994
                                  cached_field_index_ptr)))
1995
      *actual_table= table_list;
1996
  }
1997
  else
1998
  {
1999
    /*
2000
      'table_list' is a NATURAL/USING join, or an operand of such join that
2001
      is a nested join itself.
2002
2003
      If the field name we search for is qualified, then search for the field
2004
      in the table references used by NATURAL/USING the join.
2005
    */
2006
    if (table_name && table_name[0])
2007
    {
2183.2.12 by Olaf van der Spek
Use List::begin()
2008
      List<TableList>::iterator it(table_list->getNestedJoin()->join_list.begin());
327.2.4 by Brian Aker
Refactoring table.h
2009
      TableList *table;
1 by brian
clean slate
2010
      while ((table= it++))
2011
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
2012
        if ((fld= find_field_in_table_ref(session, table, name, length, item_name,
1 by brian
clean slate
2013
                                          db_name, table_name, ref,
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
2014
                                          allow_rowid,
1 by brian
clean slate
2015
                                          cached_field_index_ptr,
2016
                                          register_tree_change, actual_table)))
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
2017
          return fld;
1 by brian
clean slate
2018
      }
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
2019
      return NULL;
1 by brian
clean slate
2020
    }
2021
    /*
2022
      Non-qualified field, search directly in the result columns of the
2023
      natural join. The condition of the outer IF is true for the top-most
2024
      natural join, thus if the field is not qualified, we will search
2025
      directly the top-most NATURAL/USING join.
2026
    */
520.1.22 by Brian Aker
Second pass of thd cleanup
2027
    fld= find_field_in_natural_join(session, table_list, name, length, ref,
1 by brian
clean slate
2028
                                    register_tree_change, actual_table);
2029
  }
2030
2031
  if (fld)
2032
  {
1046 by Brian Aker
Merge Jay.
2033
    if (session->mark_used_columns != MARK_COLUMNS_NONE)
2034
    {
2035
      /*
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
2036
        Get rw_set correct for this field so that the Cursor
1046 by Brian Aker
Merge Jay.
2037
        knows that this field is involved in the query and gets
2038
        retrieved/updated
2039
      */
2040
      Field *field_to_set= NULL;
2041
      if (fld == view_ref_found)
1 by brian
clean slate
2042
      {
1046 by Brian Aker
Merge Jay.
2043
        Item *it= (*ref)->real_item();
2044
        if (it->type() == Item::FIELD_ITEM)
2045
          field_to_set= ((Item_field*)it)->field;
1 by brian
clean slate
2046
        else
2047
        {
520.1.22 by Brian Aker
Second pass of thd cleanup
2048
          if (session->mark_used_columns == MARK_COLUMNS_READ)
1046 by Brian Aker
Merge Jay.
2049
            it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
1 by brian
clean slate
2050
        }
2051
      }
1046 by Brian Aker
Merge Jay.
2052
      else
2053
        field_to_set= fld;
2054
      if (field_to_set)
2055
      {
1660.1.3 by Brian Aker
Encapsulate Table in field
2056
        Table *table= field_to_set->getTable();
1046 by Brian Aker
Merge Jay.
2057
        if (session->mark_used_columns == MARK_COLUMNS_READ)
1999.4.2 by Brian Aker
Encapsulate the field's position.
2058
          table->setReadSet(field_to_set->position());
1046 by Brian Aker
Merge Jay.
2059
        else
1999.4.2 by Brian Aker
Encapsulate the field's position.
2060
          table->setWriteSet(field_to_set->position());
1046 by Brian Aker
Merge Jay.
2061
      }
2062
    }
1 by brian
clean slate
2063
  }
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2064
  return(fld);
1 by brian
clean slate
2065
}
2066
2067
2068
/*
2069
  Find field in table list.
2070
2071
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2072
  find_field_in_tables()
2073
  session			  pointer to current thread structure
2074
  item		  field item that should be found
2075
  first_table           list of tables to be searched for item
2076
  last_table            end of the list of tables to search for item. If NULL
2077
  then search to the end of the list 'first_table'.
2078
  ref			  if 'item' is resolved to a view field, ref is set to
2079
  point to the found view field
2080
  report_error	  Degree of error reporting:
2081
  - IGNORE_ERRORS then do not report any error
2082
  - IGNORE_EXCEPT_NON_UNIQUE report only non-unique
2083
  fields, suppress all other errors
2084
  - REPORT_EXCEPT_NON_UNIQUE report all other errors
2085
  except when non-unique fields were found
2086
  - REPORT_ALL_ERRORS
2087
  register_tree_change  true if ref is not a stack variable and we
2088
  to need register changes in item tree
1 by brian
clean slate
2089
2090
  RETURN VALUES
1046 by Brian Aker
Merge Jay.
2091
  0			If error: the found field is not unique, or there are
2092
  no sufficient access priviliges for the found field,
2093
  or the field is qualified with non-existing table.
2094
  not_found_field	The function was called with report_error ==
2095
  (IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
2096
  field was not found.
2097
  view_ref_found	View field is found, item passed through ref parameter
2098
  found field         If a item was resolved to some field
1 by brian
clean slate
2099
*/
2100
2101
Field *
520.1.22 by Brian Aker
Second pass of thd cleanup
2102
find_field_in_tables(Session *session, Item_ident *item,
327.2.4 by Brian Aker
Refactoring table.h
2103
                     TableList *first_table, TableList *last_table,
1046 by Brian Aker
Merge Jay.
2104
                     Item **ref, find_item_error_report_type report_error,
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
2105
                     bool register_tree_change)
1 by brian
clean slate
2106
{
2107
  Field *found=0;
2108
  const char *db= item->db_name;
2109
  const char *table_name= item->table_name;
2110
  const char *name= item->field_name;
895 by Brian Aker
Completion (?) of uint conversion.
2111
  uint32_t length=(uint32_t) strlen(name);
1 by brian
clean slate
2112
  char name_buff[NAME_LEN+1];
327.2.4 by Brian Aker
Refactoring table.h
2113
  TableList *cur_table= first_table;
2114
  TableList *actual_table;
1 by brian
clean slate
2115
  bool allow_rowid;
2116
2117
  if (!table_name || !table_name[0])
2118
  {
2119
    table_name= 0;                              // For easier test
2120
    db= 0;
2121
  }
2122
2123
  allow_rowid= table_name || (cur_table && !cur_table->next_local);
2124
2125
  if (item->cached_table)
2126
  {
2127
    /*
2128
      This shortcut is used by prepared statements. We assume that
327.2.4 by Brian Aker
Refactoring table.h
2129
      TableList *first_table is not changed during query execution (which
1 by brian
clean slate
2130
      is true for all queries except RENAME but luckily RENAME doesn't
2131
      use fields...) so we can rely on reusing pointer to its member.
2132
      With this optimization we also miss case when addition of one more
2133
      field makes some prepared query ambiguous and so erroneous, but we
2134
      accept this trade off.
2135
    */
327.2.4 by Brian Aker
Refactoring table.h
2136
    TableList *table_ref= item->cached_table;
1 by brian
clean slate
2137
    /*
2138
      The condition (table_ref->view == NULL) ensures that we will call
2139
      find_field_in_table even in the case of information schema tables
2140
      when table_ref->field_translation != NULL.
1046 by Brian Aker
Merge Jay.
2141
    */
1 by brian
clean slate
2142
    if (table_ref->table)
520.1.22 by Brian Aker
Second pass of thd cleanup
2143
      found= find_field_in_table(session, table_ref->table, name, length,
55 by brian
Update for using real bool types.
2144
                                 true, &(item->cached_field_index));
1 by brian
clean slate
2145
    else
520.1.22 by Brian Aker
Second pass of thd cleanup
2146
      found= find_field_in_table_ref(session, table_ref, name, length, item->name,
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
2147
                                     NULL, NULL, ref,
55 by brian
Update for using real bool types.
2148
                                     true, &(item->cached_field_index),
1 by brian
clean slate
2149
                                     register_tree_change,
2150
                                     &actual_table);
2151
    if (found)
2152
    {
2153
      /*
2154
        Only views fields should be marked as dependent, not an underlying
2155
        fields.
2156
      */
2157
      {
2227.4.8 by Olaf van der Spek
Session::lex()
2158
        Select_Lex *current_sel= session->lex().current_select;
846 by Brian Aker
Removing on typedeffed class.
2159
        Select_Lex *last_select= table_ref->select_lex;
1 by brian
clean slate
2160
        /*
2161
          If the field was an outer referencee, mark all selects using this
2162
          sub query as dependent on the outer query
2163
        */
2164
        if (current_sel != last_select)
520.1.22 by Brian Aker
Second pass of thd cleanup
2165
          mark_select_range_as_dependent(session, last_select, current_sel,
1 by brian
clean slate
2166
                                         found, *ref, item);
2167
      }
2168
      return found;
2169
    }
2170
  }
2171
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
2172
  if (db)
1 by brian
clean slate
2173
  {
2174
    /*
2175
      convert database to lower case for comparison.
2176
      We can't do this in Item_field as this would change the
2177
      'name' of the item which may be used in the select list
2178
    */
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
2179
    strncpy(name_buff, db, sizeof(name_buff)-1);
2456.1.4 by Olaf van der Spek
Refactor
2180
    files_charset_info->casedn_str(name_buff);
1 by brian
clean slate
2181
    db= name_buff;
2182
  }
2183
2184
  if (last_table)
2185
    last_table= last_table->next_name_resolution_table;
2186
2187
  for (; cur_table != last_table ;
2188
       cur_table= cur_table->next_name_resolution_table)
2189
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
2190
    Field *cur_field= find_field_in_table_ref(session, cur_table, name, length,
1 by brian
clean slate
2191
                                              item->name, db, table_name, ref,
2192
                                              allow_rowid,
2193
                                              &(item->cached_field_index),
2194
                                              register_tree_change,
2195
                                              &actual_table);
2196
    if (cur_field)
2197
    {
2198
      /*
2199
        Store the original table of the field, which may be different from
2200
        cur_table in the case of NATURAL/USING join.
2201
      */
1019.1.2 by Brian Aker
Removed dead code from table_list.
2202
      item->cached_table= found ?  0 : actual_table;
1 by brian
clean slate
2203
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2204
      assert(session->where());
1 by brian
clean slate
2205
      /*
2206
        If we found a fully qualified field we return it directly as it can't
2207
        have duplicates.
1046 by Brian Aker
Merge Jay.
2208
      */
1 by brian
clean slate
2209
      if (db)
2210
        return cur_field;
2211
2212
      if (found)
2213
      {
2214
        if (report_error == REPORT_ALL_ERRORS ||
2215
            report_error == IGNORE_EXCEPT_NON_UNIQUE)
2216
          my_error(ER_NON_UNIQ_ERROR, MYF(0),
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2217
                   table_name ? item->full_name() : name, session->where());
1 by brian
clean slate
2218
        return (Field*) 0;
2219
      }
2220
      found= cur_field;
2221
    }
2222
  }
2223
2224
  if (found)
2225
    return found;
2226
2227
  /*
2228
    If the field was qualified and there were no tables to search, issue
2229
    an error that an unknown table was given. The situation is detected
2230
    as follows: if there were no tables we wouldn't go through the loop
2231
    and cur_table wouldn't be updated by the loop increment part, so it
2232
    will be equal to the first table.
2233
  */
2234
  if (table_name && (cur_table == first_table) &&
2235
      (report_error == REPORT_ALL_ERRORS ||
2236
       report_error == REPORT_EXCEPT_NON_UNIQUE))
2237
  {
2238
    char buff[NAME_LEN*2+1];
2239
    if (db && db[0])
2240
    {
534 by Monty Taylor
Removed stxnmov. Also deleted strstr which had already been removed.
2241
      /* We're in an error condition, two extra strlen's aren't going
2242
       * to kill us */
2243
      assert(strlen(db) <= NAME_LEN);
2244
      assert(strlen(table_name) <= NAME_LEN);
2245
      strcpy(buff, db);
2246
      strcat(buff,".");
2247
      strcat(buff, table_name);
1 by brian
clean slate
2248
      table_name=buff;
2249
    }
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2250
    my_error(ER_UNKNOWN_TABLE, MYF(0), table_name, session->where());
1 by brian
clean slate
2251
  }
2252
  else
2253
  {
2254
    if (report_error == REPORT_ALL_ERRORS ||
2255
        report_error == REPORT_EXCEPT_NON_UNIQUE)
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2256
      my_error(ER_BAD_FIELD_ERROR, MYF(0), item->full_name(), session->where());
1 by brian
clean slate
2257
    else
2258
      found= not_found_field;
2259
  }
2260
  return found;
2261
}
2262
2263
2264
/*
2265
  Find Item in list of items (find_field_in_tables analog)
2266
2267
  TODO
1046 by Brian Aker
Merge Jay.
2268
  is it better return only counter?
1 by brian
clean slate
2269
2270
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2271
  find_item_in_list()
2272
  find			Item to find
2273
  items			List of items
2274
  counter			To return number of found item
2275
  report_error
2276
  REPORT_ALL_ERRORS		report errors, return 0 if error
2277
  REPORT_EXCEPT_NOT_FOUND	Do not report 'not found' error and
2278
  return not_found_item, report other errors,
2279
  return 0
2280
  IGNORE_ERRORS		Do not report errors, return 0 if error
2281
  resolution                  Set to the resolution type if the item is found
2282
  (it says whether the item is resolved
2283
  against an alias name,
2284
  or as a field name without alias,
2285
  or as a field hidden by alias,
2286
  or ignoring alias)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2287
1 by brian
clean slate
2288
  RETURN VALUES
1046 by Brian Aker
Merge Jay.
2289
  0			Item is not found or item is not unique,
2290
  error message is reported
2291
  not_found_item	Function was called with
2292
  report_error == REPORT_EXCEPT_NOT_FOUND and
2293
  item was not found. No error message was reported
2294
  found field
1 by brian
clean slate
2295
*/
2296
2297
/* Special Item pointer to serve as a return value from find_item_in_list(). */
2298
Item **not_found_item= (Item**) 0x1;
2299
2300
2301
Item **
1578.6.3 by Brian Aker
More current_session removal.
2302
find_item_in_list(Session *session,
2303
                  Item *find, List<Item> &items, uint32_t *counter,
1 by brian
clean slate
2304
                  find_item_error_report_type report_error,
2305
                  enum_resolution_type *resolution)
2306
{
2183.2.12 by Olaf van der Spek
Use List::begin()
2307
  List<Item>::iterator li(items.begin());
1 by brian
clean slate
2308
  Item **found=0, **found_unaliased= 0, *item;
2309
  const char *db_name=0;
2310
  const char *field_name=0;
2311
  const char *table_name=0;
2312
  bool found_unaliased_non_uniq= 0;
2313
  /*
2314
    true if the item that we search for is a valid name reference
2315
    (and not an item that happens to have a name).
2316
  */
2317
  bool is_ref_by_name= 0;
482 by Brian Aker
Remove uint.
2318
  uint32_t unaliased_counter= 0;
1 by brian
clean slate
2319
2320
  *resolution= NOT_RESOLVED;
2321
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2322
  is_ref_by_name= (find->type() == Item::FIELD_ITEM  ||
1 by brian
clean slate
2323
                   find->type() == Item::REF_ITEM);
2324
  if (is_ref_by_name)
2325
  {
2326
    field_name= ((Item_ident*) find)->field_name;
2327
    table_name= ((Item_ident*) find)->table_name;
2328
    db_name=    ((Item_ident*) find)->db_name;
2329
  }
2330
482 by Brian Aker
Remove uint.
2331
  for (uint32_t i= 0; (item=li++); i++)
1 by brian
clean slate
2332
  {
2333
    if (field_name && item->real_item()->type() == Item::FIELD_ITEM)
2334
    {
2335
      Item_ident *item_field= (Item_ident*) item;
2336
2337
      /*
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
2338
        In case of group_concat() with ORDER BY condition in the QUERY
2339
        item_field can be field of temporary table without item name
2340
        (if this field created from expression argument of group_concat()),
2341
        => we have to check presence of name before compare
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2342
      */
1 by brian
clean slate
2343
      if (!item_field->name)
2344
        continue;
2345
2346
      if (table_name)
2347
      {
2348
        /*
2349
          If table name is specified we should find field 'field_name' in
2350
          table 'table_name'. According to SQL-standard we should ignore
2351
          aliases in this case.
2352
2353
          Since we should NOT prefer fields from the select list over
2354
          other fields from the tables participating in this select in
2355
          case of ambiguity we have to do extra check outside this function.
2356
2357
          We use strcmp for table names and database names as these may be
2358
          case sensitive. In cases where they are not case sensitive, they
2359
          are always in lower case.
2360
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
2361
          item_field->field_name and item_field->table_name can be 0x0 if
2362
          item is not fix_field()'ed yet.
1 by brian
clean slate
2363
        */
2364
        if (item_field->field_name && item_field->table_name &&
2456.1.4 by Olaf van der Spek
Refactor
2365
            not system_charset_info->strcasecmp(item_field->field_name, field_name) &&
2366
            not table_alias_charset->strcasecmp(item_field->table_name, table_name) &&
2367
            (!db_name || (item_field->db_name && !strcmp(item_field->db_name, db_name))))
1 by brian
clean slate
2368
        {
2369
          if (found_unaliased)
2370
          {
2371
            if ((*found_unaliased)->eq(item, 0))
2372
              continue;
2373
            /*
2374
              Two matching fields in select list.
2375
              We already can bail out because we are searching through
2376
              unaliased names only and will have duplicate error anyway.
2377
            */
2378
            if (report_error != IGNORE_ERRORS)
2379
              my_error(ER_NON_UNIQ_ERROR, MYF(0),
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2380
                       find->full_name(), session->where());
1 by brian
clean slate
2381
            return (Item**) 0;
2382
          }
2383
          found_unaliased= li.ref();
2384
          unaliased_counter= i;
2385
          *resolution= RESOLVED_IGNORING_ALIAS;
2386
          if (db_name)
2387
            break;                              // Perfect match
2388
        }
2389
      }
2390
      else
2391
      {
2456.1.4 by Olaf van der Spek
Refactor
2392
        int fname_cmp= system_charset_info->strcasecmp(item_field->field_name, field_name);
2393
        if (not system_charset_info->strcasecmp(item_field->name, field_name))
1 by brian
clean slate
2394
        {
2395
          /*
2396
            If table name was not given we should scan through aliases
2397
            and non-aliased fields first. We are also checking unaliased
2398
            name of the field in then next  else-if, to be able to find
2399
            instantly field (hidden by alias) if no suitable alias or
2400
            non-aliased field was found.
2401
          */
2402
          if (found)
2403
          {
2404
            if ((*found)->eq(item, 0))
2405
              continue;                           // Same field twice
2406
            if (report_error != IGNORE_ERRORS)
2407
              my_error(ER_NON_UNIQ_ERROR, MYF(0),
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2408
                       find->full_name(), session->where());
1 by brian
clean slate
2409
            return (Item**) 0;
2410
          }
2411
          found= li.ref();
2412
          *counter= i;
2456.1.4 by Olaf van der Spek
Refactor
2413
          *resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS : RESOLVED_WITH_NO_ALIAS;
1 by brian
clean slate
2414
        }
2415
        else if (!fname_cmp)
2416
        {
2417
          /*
2418
            We will use non-aliased field or react on such ambiguities only if
2419
            we won't be able to find aliased field.
2420
            Again if we have ambiguity with field outside of select list
2421
            we should prefer fields from select list.
2422
          */
2423
          if (found_unaliased)
2424
          {
2425
            if ((*found_unaliased)->eq(item, 0))
2426
              continue;                           // Same field twice
2427
            found_unaliased_non_uniq= 1;
2428
          }
2429
          found_unaliased= li.ref();
2430
          unaliased_counter= i;
2431
        }
2432
      }
2433
    }
2434
    else if (!table_name)
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2435
    {
1 by brian
clean slate
2436
      if (is_ref_by_name && find->name && item->name &&
2456.1.4 by Olaf van der Spek
Refactor
2437
          not system_charset_info->strcasecmp(item->name,find->name))
1 by brian
clean slate
2438
      {
2439
        found= li.ref();
2440
        *counter= i;
2441
        *resolution= RESOLVED_AGAINST_ALIAS;
2442
        break;
2443
      }
2444
      else if (find->eq(item,0))
2445
      {
2446
        found= li.ref();
2447
        *counter= i;
2448
        *resolution= RESOLVED_IGNORING_ALIAS;
2449
        break;
2450
      }
2451
    }
2452
  }
2453
  if (!found)
2454
  {
2455
    if (found_unaliased_non_uniq)
2456
    {
2457
      if (report_error != IGNORE_ERRORS)
2458
        my_error(ER_NON_UNIQ_ERROR, MYF(0),
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2459
                 find->full_name(), session->where());
1 by brian
clean slate
2460
      return (Item **) 0;
2461
    }
2462
    if (found_unaliased)
2463
    {
2464
      found= found_unaliased;
2465
      *counter= unaliased_counter;
2466
      *resolution= RESOLVED_BEHIND_ALIAS;
2467
    }
2468
  }
2469
  if (found)
2470
    return found;
2471
  if (report_error != REPORT_EXCEPT_NOT_FOUND)
2472
  {
2473
    if (report_error == REPORT_ALL_ERRORS)
2474
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2475
               find->full_name(), session->where());
1 by brian
clean slate
2476
    return (Item **) 0;
2477
  }
2478
  else
2479
    return (Item **) not_found_item;
2480
}
2481
2482
2483
/*
2484
  Test if a string is a member of a list of strings.
2485
2486
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2487
  test_if_string_in_list()
2488
  find      the string to look for
2489
  str_list  a list of strings to be searched
1 by brian
clean slate
2490
2491
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
2492
  Sequentially search a list of strings for a string, and test whether
2493
  the list contains the same string.
1 by brian
clean slate
2494
2495
  RETURN
1046 by Brian Aker
Merge Jay.
2496
  true  if find is in str_list
2497
  false otherwise
1 by brian
clean slate
2498
*/
2499
2500
static bool
2501
test_if_string_in_list(const char *find, List<String> *str_list)
2502
{
2183.2.12 by Olaf van der Spek
Use List::begin()
2503
  List<String>::iterator str_list_it(str_list->begin());
1 by brian
clean slate
2504
  String *curr_str;
2505
  size_t find_length= strlen(find);
2506
  while ((curr_str= str_list_it++))
2507
  {
2508
    if (find_length != curr_str->length())
2509
      continue;
2456.1.4 by Olaf van der Spek
Refactor
2510
    if (not system_charset_info->strcasecmp(find, curr_str->ptr()))
55 by brian
Update for using real bool types.
2511
      return true;
1 by brian
clean slate
2512
  }
55 by brian
Update for using real bool types.
2513
  return false;
1 by brian
clean slate
2514
}
2515
2516
2517
/*
2518
  Create a new name resolution context for an item so that it is
2519
  being resolved in a specific table reference.
2520
2521
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2522
  set_new_item_local_context()
2523
  session        pointer to current thread
2524
  item       item for which new context is created and set
2525
  table_ref  table ref where an item showld be resolved
1 by brian
clean slate
2526
2527
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
2528
  Create a new name resolution context for an item, so that the item
2529
  is resolved only the supplied 'table_ref'.
1 by brian
clean slate
2530
2531
  RETURN
1046 by Brian Aker
Merge Jay.
2532
  false  if all OK
2533
  true   otherwise
1 by brian
clean slate
2534
*/
2535
2318.6.90 by Olaf van der Spek
Refactor
2536
static void set_new_item_local_context(Session *session, Item_ident *item, TableList *table_ref)
1 by brian
clean slate
2537
{
2318.6.90 by Olaf van der Spek
Refactor
2538
  Name_resolution_context* context= new (session->mem_root) Name_resolution_context;
1 by brian
clean slate
2539
  context->init();
2318.6.90 by Olaf van der Spek
Refactor
2540
  context->first_name_resolution_table= context->last_name_resolution_table= table_ref;
1 by brian
clean slate
2541
  item->context= context;
2542
}
2543
2544
2545
/*
2546
  Find and mark the common columns of two table references.
2547
2548
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2549
  mark_common_columns()
2550
  session                [in] current thread
2551
  table_ref_1        [in] the first (left) join operand
2552
  table_ref_2        [in] the second (right) join operand
2553
  using_fields       [in] if the join is JOIN...USING - the join columns,
2554
  if NATURAL join, then NULL
2555
  found_using_fields [out] number of fields from the USING clause that were
2556
  found among the common fields
1 by brian
clean slate
2557
2558
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
2559
  The procedure finds the common columns of two relations (either
2560
  tables or intermediate join results), and adds an equi-join condition
2561
  to the ON clause of 'table_ref_2' for each pair of matching columns.
2562
  If some of table_ref_XXX represents a base table or view, then we
2563
  create new 'Natural_join_column' instances for each column
2564
  reference and store them in the 'join_columns' of the table
2565
  reference.
1 by brian
clean slate
2566
2567
  IMPLEMENTATION
1046 by Brian Aker
Merge Jay.
2568
  The procedure assumes that store_natural_using_join_columns() was
2569
  called for the previous level of NATURAL/USING joins.
1 by brian
clean slate
2570
2571
  RETURN
1046 by Brian Aker
Merge Jay.
2572
  true   error when some common column is non-unique, or out of memory
2573
  false  OK
1 by brian
clean slate
2574
*/
2575
2576
static bool
520.1.22 by Brian Aker
Second pass of thd cleanup
2577
mark_common_columns(Session *session, TableList *table_ref_1, TableList *table_ref_2,
482 by Brian Aker
Remove uint.
2578
                    List<String> *using_fields, uint32_t *found_using_fields)
1 by brian
clean slate
2579
{
2580
  Field_iterator_table_ref it_1, it_2;
2581
  Natural_join_column *nj_col_1, *nj_col_2;
55 by brian
Update for using real bool types.
2582
  bool first_outer_loop= true;
1 by brian
clean slate
2583
  /*
2584
    Leaf table references to which new natural join columns are added
2585
    if the leaves are != NULL.
2586
  */
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
2587
  TableList *leaf_1= (table_ref_1->getNestedJoin() &&
2588
                      ! table_ref_1->is_natural_join) ?
1046 by Brian Aker
Merge Jay.
2589
    NULL : table_ref_1;
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
2590
  TableList *leaf_2= (table_ref_2->getNestedJoin() &&
2591
                      ! table_ref_2->is_natural_join) ?
1046 by Brian Aker
Merge Jay.
2592
    NULL : table_ref_2;
1 by brian
clean slate
2593
2594
  *found_using_fields= 0;
2595
2596
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
2597
  {
55 by brian
Update for using real bool types.
2598
    bool found= false;
1 by brian
clean slate
2599
    const char *field_name_1;
2600
    /* true if field_name_1 is a member of using_fields */
2601
    bool is_using_column_1;
2602
    if (!(nj_col_1= it_1.get_or_create_column_ref(leaf_1)))
2318.6.92 by Olaf van der Spek
Refactor
2603
      return true;
1 by brian
clean slate
2604
    field_name_1= nj_col_1->name();
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2605
    is_using_column_1= using_fields &&
1 by brian
clean slate
2606
      test_if_string_in_list(field_name_1, using_fields);
2607
2608
    /*
2609
      Find a field with the same name in table_ref_2.
2610
2611
      Note that for the second loop, it_2.set() will iterate over
2612
      table_ref_2->join_columns and not generate any new elements or
2613
      lists.
2614
    */
2615
    nj_col_2= NULL;
2616
    for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
2617
    {
2618
      Natural_join_column *cur_nj_col_2;
2619
      const char *cur_field_name_2;
2620
      if (!(cur_nj_col_2= it_2.get_or_create_column_ref(leaf_2)))
2318.6.92 by Olaf van der Spek
Refactor
2621
        return true;
1 by brian
clean slate
2622
      cur_field_name_2= cur_nj_col_2->name();
2623
2624
      /*
2625
        Compare the two columns and check for duplicate common fields.
2626
        A common field is duplicate either if it was already found in
55 by brian
Update for using real bool types.
2627
        table_ref_2 (then found == true), or if a field in table_ref_2
1 by brian
clean slate
2628
        was already matched by some previous field in table_ref_1
55 by brian
Update for using real bool types.
2629
        (then cur_nj_col_2->is_common == true).
1 by brian
clean slate
2630
        Note that it is too early to check the columns outside of the
2631
        USING list for ambiguity because they are not actually "referenced"
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
2632
        here. These columns must be checked only on unqualified reference
1 by brian
clean slate
2633
        by name (e.g. in SELECT list).
2634
      */
2456.1.4 by Olaf van der Spek
Refactor
2635
      if (!system_charset_info->strcasecmp(field_name_1, cur_field_name_2))
1 by brian
clean slate
2636
      {
2637
        if (cur_nj_col_2->is_common ||
2638
            (found && (!using_fields || is_using_column_1)))
2639
        {
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
2640
          my_error(ER_NON_UNIQ_ERROR, MYF(0), field_name_1, session->where());
2318.6.92 by Olaf van der Spek
Refactor
2641
          return true;
1 by brian
clean slate
2642
        }
2643
        nj_col_2= cur_nj_col_2;
55 by brian
Update for using real bool types.
2644
        found= true;
1 by brian
clean slate
2645
      }
2646
    }
2647
    if (first_outer_loop && leaf_2)
2648
    {
2649
      /*
2650
        Make sure that the next inner loop "knows" that all columns
2651
        are materialized already.
2652
      */
55 by brian
Update for using real bool types.
2653
      leaf_2->is_join_columns_complete= true;
2654
      first_outer_loop= false;
1 by brian
clean slate
2655
    }
2656
    if (!found)
2657
      continue;                                 // No matching field
2658
2659
    /*
2660
      field_1 and field_2 have the same names. Check if they are in the USING
2661
      clause (if present), mark them as common fields, and add a new
2662
      equi-join condition to the ON clause.
2663
    */
2664
    if (nj_col_2 && (!using_fields ||is_using_column_1))
2665
    {
520.1.22 by Brian Aker
Second pass of thd cleanup
2666
      Item *item_1=   nj_col_1->create_item(session);
2667
      Item *item_2=   nj_col_2->create_item(session);
1 by brian
clean slate
2668
      Field *field_1= nj_col_1->field();
2669
      Field *field_2= nj_col_2->field();
2318.6.90 by Olaf van der Spek
Refactor
2670
 
1 by brian
clean slate
2671
      if (!item_1 || !item_2)
2318.6.92 by Olaf van der Spek
Refactor
2672
        return true; // out of memory
1 by brian
clean slate
2673
2674
      /*
2675
        In the case of no_wrap_view_item == 0, the created items must be
2676
        of sub-classes of Item_ident.
2677
      */
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2678
      assert(item_1->type() == Item::FIELD_ITEM ||
1046 by Brian Aker
Merge Jay.
2679
             item_1->type() == Item::REF_ITEM);
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2680
      assert(item_2->type() == Item::FIELD_ITEM ||
1046 by Brian Aker
Merge Jay.
2681
             item_2->type() == Item::REF_ITEM);
1 by brian
clean slate
2682
2683
      /*
2684
        We need to cast item_1,2 to Item_ident, because we need to hook name
2685
        resolution contexts specific to each item.
2686
      */
2318.6.90 by Olaf van der Spek
Refactor
2687
      Item_ident* item_ident_1= (Item_ident*) item_1;
2688
      Item_ident* item_ident_2= (Item_ident*) item_2;
1 by brian
clean slate
2689
      /*
2690
        Create and hook special name resolution contexts to each item in the
2691
        new join condition . We need this to both speed-up subsequent name
2692
        resolution of these items, and to enable proper name resolution of
2693
        the items during the execute phase of PS.
2694
      */
2318.6.90 by Olaf van der Spek
Refactor
2695
      set_new_item_local_context(session, item_ident_1, nj_col_1->table_ref);
2696
      set_new_item_local_context(session, item_ident_2, nj_col_2->table_ref);
1 by brian
clean slate
2697
2318.6.90 by Olaf van der Spek
Refactor
2698
      Item_func_eq* eq_cond= new Item_func_eq(item_ident_1, item_ident_2);
1 by brian
clean slate
2699
2700
      /*
2701
        Add the new equi-join condition to the ON clause. Notice that
2702
        fix_fields() is applied to all ON conditions in setup_conds()
2703
        so we don't do it here.
1046 by Brian Aker
Merge Jay.
2704
      */
1 by brian
clean slate
2705
      add_join_on((table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
2706
                   table_ref_1 : table_ref_2),
2707
                  eq_cond);
2708
55 by brian
Update for using real bool types.
2709
      nj_col_1->is_common= nj_col_2->is_common= true;
1 by brian
clean slate
2710
2711
      if (field_1)
2712
      {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2713
        Table *table_1= nj_col_1->table_ref->table;
1 by brian
clean slate
2714
        /* Mark field_1 used for table cache. */
1999.4.2 by Brian Aker
Encapsulate the field's position.
2715
        table_1->setReadSet(field_1->position());
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
2716
        table_1->covering_keys&= field_1->part_of_key;
2717
        table_1->merge_keys|= field_1->part_of_key;
1 by brian
clean slate
2718
      }
2719
      if (field_2)
2720
      {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
2721
        Table *table_2= nj_col_2->table_ref->table;
1 by brian
clean slate
2722
        /* Mark field_2 used for table cache. */
1999.4.2 by Brian Aker
Encapsulate the field's position.
2723
        table_2->setReadSet(field_2->position());
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
2724
        table_2->covering_keys&= field_2->part_of_key;
2725
        table_2->merge_keys|= field_2->part_of_key;
1 by brian
clean slate
2726
      }
2727
2728
      if (using_fields != NULL)
2729
        ++(*found_using_fields);
2730
    }
2731
  }
2732
  if (leaf_1)
55 by brian
Update for using real bool types.
2733
    leaf_1->is_join_columns_complete= true;
1 by brian
clean slate
2734
2735
  /*
2736
    Everything is OK.
2737
    Notice that at this point there may be some column names in the USING
2738
    clause that are not among the common columns. This is an SQL error and
2739
    we check for this error in store_natural_using_join_columns() when
2740
    (found_using_fields < length(join_using_fields)).
2741
  */
2318.6.90 by Olaf van der Spek
Refactor
2742
  return false;
1 by brian
clean slate
2743
}
2744
2745
2746
2747
/*
2748
  Materialize and store the row type of NATURAL/USING join.
2749
2750
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2751
  store_natural_using_join_columns()
2752
  session                current thread
2753
  natural_using_join the table reference of the NATURAL/USING join
2754
  table_ref_1        the first (left) operand (of a NATURAL/USING join).
2755
  table_ref_2        the second (right) operand (of a NATURAL/USING join).
2756
  using_fields       if the join is JOIN...USING - the join columns,
2757
  if NATURAL join, then NULL
2758
  found_using_fields number of fields from the USING clause that were
2759
  found among the common fields
1 by brian
clean slate
2760
2761
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
2762
  Iterate over the columns of both join operands and sort and store
2763
  all columns into the 'join_columns' list of natural_using_join
2764
  where the list is formed by three parts:
2765
part1: The coalesced columns of table_ref_1 and table_ref_2,
2766
sorted according to the column order of the first table.
2767
part2: The other columns of the first table, in the order in
2768
which they were defined in CREATE TABLE.
2769
part3: The other columns of the second table, in the order in
2770
which they were defined in CREATE TABLE.
2771
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
2772
2773
IMPLEMENTATION
2774
The procedure assumes that mark_common_columns() has been called
2775
for the join that is being processed.
2776
2777
RETURN
2778
true    error: Some common column is ambiguous
2779
false   OK
1 by brian
clean slate
2780
*/
2781
2782
static bool
1578.6.3 by Brian Aker
More current_session removal.
2783
store_natural_using_join_columns(Session *session,
327.2.4 by Brian Aker
Refactoring table.h
2784
                                 TableList *natural_using_join,
2785
                                 TableList *table_ref_1,
2786
                                 TableList *table_ref_2,
1 by brian
clean slate
2787
                                 List<String> *using_fields,
482 by Brian Aker
Remove uint.
2788
                                 uint32_t found_using_fields)
1 by brian
clean slate
2789
{
2790
  Field_iterator_table_ref it_1, it_2;
2791
  Natural_join_column *nj_col_1, *nj_col_2;
2792
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
2793
  assert(!natural_using_join->join_columns);
1 by brian
clean slate
2794
2318.6.90 by Olaf van der Spek
Refactor
2795
  List<Natural_join_column>* non_join_columns= new List<Natural_join_column>;
2796
  natural_using_join->join_columns= new List<Natural_join_column>;
1 by brian
clean slate
2797
2798
  /* Append the columns of the first join operand. */
2799
  for (it_1.set(table_ref_1); !it_1.end_of_fields(); it_1.next())
2800
  {
2801
    nj_col_1= it_1.get_natural_column_ref();
2802
    if (nj_col_1->is_common)
2803
    {
2804
      natural_using_join->join_columns->push_back(nj_col_1);
2805
      /* Reset the common columns for the next call to mark_common_columns. */
55 by brian
Update for using real bool types.
2806
      nj_col_1->is_common= false;
1 by brian
clean slate
2807
    }
2808
    else
2809
      non_join_columns->push_back(nj_col_1);
2810
  }
2811
2812
  /*
2813
    Check that all columns in the USING clause are among the common
2814
    columns. If this is not the case, report the first one that was
2815
    not found in an error.
2816
  */
2183.2.19 by Olaf van der Spek
Use List::size()
2817
  if (using_fields && found_using_fields < using_fields->size())
1 by brian
clean slate
2818
  {
2183.2.12 by Olaf van der Spek
Use List::begin()
2819
    List<String>::iterator using_fields_it(using_fields->begin());
2318.6.90 by Olaf van der Spek
Refactor
2820
    while (String* using_field_name= using_fields_it++)
1 by brian
clean slate
2821
    {
2822
      const char *using_field_name_ptr= using_field_name->c_ptr();
2318.6.90 by Olaf van der Spek
Refactor
2823
      List<Natural_join_column>::iterator  it(natural_using_join->join_columns->begin());
1 by brian
clean slate
2824
      for (;;)
2825
      {
2826
        /* If reached the end of fields, and none was found, report error. */
2318.6.90 by Olaf van der Spek
Refactor
2827
        Natural_join_column* common_field= it++;
2828
        if (not common_field)
1 by brian
clean slate
2829
        {
2318.6.90 by Olaf van der Spek
Refactor
2830
          my_error(ER_BAD_FIELD_ERROR, MYF(0), using_field_name_ptr, session->where());
2318.6.92 by Olaf van der Spek
Refactor
2831
          return true;
1 by brian
clean slate
2832
        }
2456.1.4 by Olaf van der Spek
Refactor
2833
        if (!system_charset_info->strcasecmp(common_field->name(), using_field_name_ptr))
1 by brian
clean slate
2834
          break;                                // Found match
2835
      }
2836
    }
2837
  }
2838
2839
  /* Append the non-equi-join columns of the second join operand. */
2840
  for (it_2.set(table_ref_2); !it_2.end_of_fields(); it_2.next())
2841
  {
2842
    nj_col_2= it_2.get_natural_column_ref();
2843
    if (!nj_col_2->is_common)
2844
      non_join_columns->push_back(nj_col_2);
2845
    else
2846
    {
2847
      /* Reset the common columns for the next call to mark_common_columns. */
55 by brian
Update for using real bool types.
2848
      nj_col_2->is_common= false;
1 by brian
clean slate
2849
    }
2850
  }
2851
2183.2.19 by Olaf van der Spek
Use List::size()
2852
  if (non_join_columns->size() > 0)
1 by brian
clean slate
2853
    natural_using_join->join_columns->concat(non_join_columns);
55 by brian
Update for using real bool types.
2854
  natural_using_join->is_join_columns_complete= true;
1 by brian
clean slate
2855
2318.6.90 by Olaf van der Spek
Refactor
2856
  return false;
1 by brian
clean slate
2857
}
2858
2859
2860
/*
2861
  Precompute and store the row types of the top-most NATURAL/USING joins.
2862
2863
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
2864
  store_top_level_join_columns()
2865
  session            current thread
2866
  table_ref      nested join or table in a FROM clause
2867
  left_neighbor  neighbor table reference to the left of table_ref at the
2868
  same level in the join tree
2869
  right_neighbor neighbor table reference to the right of table_ref at the
2870
  same level in the join tree
1 by brian
clean slate
2871
2872
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
2873
  The procedure performs a post-order traversal of a nested join tree
2874
  and materializes the row types of NATURAL/USING joins in a
2875
  bottom-up manner until it reaches the TableList elements that
2876
  represent the top-most NATURAL/USING joins. The procedure should be
2877
  applied to each element of Select_Lex::top_join_list (i.e. to each
2878
  top-level element of the FROM clause).
1 by brian
clean slate
2879
2880
  IMPLEMENTATION
1046 by Brian Aker
Merge Jay.
2881
  Notice that the table references in the list nested_join->join_list
2882
  are in reverse order, thus when we iterate over it, we are moving
2883
  from the right to the left in the FROM clause.
1 by brian
clean slate
2884
2885
  RETURN
1046 by Brian Aker
Merge Jay.
2886
  true   Error
2887
  false  OK
1 by brian
clean slate
2888
*/
2889
2890
static bool
520.1.22 by Brian Aker
Second pass of thd cleanup
2891
store_top_level_join_columns(Session *session, TableList *table_ref,
327.2.4 by Brian Aker
Refactoring table.h
2892
                             TableList *left_neighbor,
2893
                             TableList *right_neighbor)
1 by brian
clean slate
2894
{
2895
  /* Call the procedure recursively for each nested table reference. */
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
2896
  if (table_ref->getNestedJoin())
1 by brian
clean slate
2897
  {
2183.2.12 by Olaf van der Spek
Use List::begin()
2898
    List<TableList>::iterator nested_it(table_ref->getNestedJoin()->join_list.begin());
327.2.4 by Brian Aker
Refactoring table.h
2899
    TableList *same_level_left_neighbor= nested_it++;
2900
    TableList *same_level_right_neighbor= NULL;
1 by brian
clean slate
2901
    /* Left/right-most neighbors, possibly at higher levels in the join tree. */
327.2.4 by Brian Aker
Refactoring table.h
2902
    TableList *real_left_neighbor, *real_right_neighbor;
1 by brian
clean slate
2903
2904
    while (same_level_left_neighbor)
2905
    {
327.2.4 by Brian Aker
Refactoring table.h
2906
      TableList *cur_table_ref= same_level_left_neighbor;
1 by brian
clean slate
2907
      same_level_left_neighbor= nested_it++;
2908
      /*
2909
        The order of RIGHT JOIN operands is reversed in 'join list' to
2910
        transform it into a LEFT JOIN. However, in this procedure we need
2911
        the join operands in their lexical order, so below we reverse the
2912
        join operands. Notice that this happens only in the first loop,
2913
        and not in the second one, as in the second loop
2914
        same_level_left_neighbor == NULL.
2915
        This is the correct behavior, because the second loop sets
2916
        cur_table_ref reference correctly after the join operands are
2917
        swapped in the first loop.
2918
      */
2919
      if (same_level_left_neighbor &&
2920
          cur_table_ref->outer_join & JOIN_TYPE_RIGHT)
2921
      {
2922
        /* This can happen only for JOIN ... ON. */
2183.2.19 by Olaf van der Spek
Use List::size()
2923
        assert(table_ref->getNestedJoin()->join_list.size() == 2);
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
2924
        std::swap(same_level_left_neighbor, cur_table_ref);
1 by brian
clean slate
2925
      }
2926
2927
      /*
2928
        Pick the parent's left and right neighbors if there are no immediate
2929
        neighbors at the same level.
2930
      */
2931
      real_left_neighbor=  (same_level_left_neighbor) ?
1046 by Brian Aker
Merge Jay.
2932
        same_level_left_neighbor : left_neighbor;
1 by brian
clean slate
2933
      real_right_neighbor= (same_level_right_neighbor) ?
1046 by Brian Aker
Merge Jay.
2934
        same_level_right_neighbor : right_neighbor;
1 by brian
clean slate
2935
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
2936
      if (cur_table_ref->getNestedJoin() &&
2318.6.92 by Olaf van der Spek
Refactor
2937
          store_top_level_join_columns(session, cur_table_ref, real_left_neighbor, real_right_neighbor))
2938
        return true;
1 by brian
clean slate
2939
      same_level_right_neighbor= cur_table_ref;
2940
    }
2941
  }
2942
2943
  /*
2944
    If this is a NATURAL/USING join, materialize its result columns and
2945
    convert to a JOIN ... ON.
2946
  */
2947
  if (table_ref->is_natural_join)
2948
  {
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
2949
    assert(table_ref->getNestedJoin() &&
2183.2.19 by Olaf van der Spek
Use List::size()
2950
           table_ref->getNestedJoin()->join_list.size() == 2);
2183.2.12 by Olaf van der Spek
Use List::begin()
2951
    List<TableList>::iterator operand_it(table_ref->getNestedJoin()->join_list.begin());
1 by brian
clean slate
2952
    /*
2953
      Notice that the order of join operands depends on whether table_ref
2954
      represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
2955
      in inverted order.
1046 by Brian Aker
Merge Jay.
2956
    */
327.2.4 by Brian Aker
Refactoring table.h
2957
    TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
2958
    TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
1 by brian
clean slate
2959
    List<String> *using_fields= table_ref->join_using_fields;
482 by Brian Aker
Remove uint.
2960
    uint32_t found_using_fields;
1 by brian
clean slate
2961
2962
    /*
2963
      The two join operands were interchanged in the parser, change the order
2964
      back for 'mark_common_columns'.
2965
    */
2966
    if (table_ref_2->outer_join & JOIN_TYPE_RIGHT)
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
2967
      std::swap(table_ref_1, table_ref_2);
520.1.22 by Brian Aker
Second pass of thd cleanup
2968
    if (mark_common_columns(session, table_ref_1, table_ref_2,
1 by brian
clean slate
2969
                            using_fields, &found_using_fields))
2318.6.92 by Olaf van der Spek
Refactor
2970
      return true;
1 by brian
clean slate
2971
2972
    /*
2973
      Swap the join operands back, so that we pick the columns of the second
2974
      one as the coalesced columns. In this way the coalesced columns are the
2975
      same as of an equivalent LEFT JOIN.
2976
    */
2977
    if (table_ref_1->outer_join & JOIN_TYPE_RIGHT)
322.2.2 by Mats Kindahl
Hiding THD::proc_info field and providing a setter and getter.
2978
      std::swap(table_ref_1, table_ref_2);
520.1.22 by Brian Aker
Second pass of thd cleanup
2979
    if (store_natural_using_join_columns(session, table_ref, table_ref_1,
1 by brian
clean slate
2980
                                         table_ref_2, using_fields,
2981
                                         found_using_fields))
2318.6.92 by Olaf van der Spek
Refactor
2982
      return true;
1 by brian
clean slate
2983
2984
    /*
2985
      Change NATURAL JOIN to JOIN ... ON. We do this for both operands
2986
      because either one of them or the other is the one with the
2987
      natural join flag because RIGHT joins are transformed into LEFT,
2988
      and the two tables may be reordered.
2989
    */
2990
    table_ref_1->natural_join= table_ref_2->natural_join= NULL;
2991
55 by brian
Update for using real bool types.
2992
    /* Add a true condition to outer joins that have no common columns. */
1 by brian
clean slate
2993
    if (table_ref_2->outer_join &&
2994
        !table_ref_1->on_expr && !table_ref_2->on_expr)
152 by Brian Aker
longlong replacement
2995
      table_ref_2->on_expr= new Item_int((int64_t) 1,1);   /* Always true. */
1 by brian
clean slate
2996
2997
    /* Change this table reference to become a leaf for name resolution. */
2998
    if (left_neighbor)
2999
    {
327.2.4 by Brian Aker
Refactoring table.h
3000
      TableList *last_leaf_on_the_left;
1 by brian
clean slate
3001
      last_leaf_on_the_left= left_neighbor->last_leaf_for_name_resolution();
3002
      last_leaf_on_the_left->next_name_resolution_table= table_ref;
3003
    }
3004
    if (right_neighbor)
3005
    {
327.2.4 by Brian Aker
Refactoring table.h
3006
      TableList *first_leaf_on_the_right;
1 by brian
clean slate
3007
      first_leaf_on_the_right= right_neighbor->first_leaf_for_name_resolution();
3008
      table_ref->next_name_resolution_table= first_leaf_on_the_right;
3009
    }
3010
    else
3011
      table_ref->next_name_resolution_table= NULL;
3012
  }
2318.6.90 by Olaf van der Spek
Refactor
3013
  return false;
1 by brian
clean slate
3014
}
3015
3016
3017
/*
3018
  Compute and store the row types of the top-most NATURAL/USING joins
3019
  in a FROM clause.
3020
3021
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3022
  setup_natural_join_row_types()
3023
  session          current thread
3024
  from_clause  list of top-level table references in a FROM clause
1 by brian
clean slate
3025
3026
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
3027
  Apply the procedure 'store_top_level_join_columns' to each of the
3028
  top-level table referencs of the FROM clause. Adjust the list of tables
3029
  for name resolution - context->first_name_resolution_table to the
3030
  top-most, lef-most NATURAL/USING join.
1 by brian
clean slate
3031
3032
  IMPLEMENTATION
1046 by Brian Aker
Merge Jay.
3033
  Notice that the table references in 'from_clause' are in reverse
3034
  order, thus when we iterate over it, we are moving from the right
3035
  to the left in the FROM clause.
1 by brian
clean slate
3036
3037
  RETURN
1046 by Brian Aker
Merge Jay.
3038
  true   Error
3039
  false  OK
1 by brian
clean slate
3040
*/
520.1.22 by Brian Aker
Second pass of thd cleanup
3041
static bool setup_natural_join_row_types(Session *session,
327.2.4 by Brian Aker
Refactoring table.h
3042
                                         List<TableList> *from_clause,
1 by brian
clean slate
3043
                                         Name_resolution_context *context)
3044
{
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3045
  session->setWhere("from clause");
2183.2.19 by Olaf van der Spek
Use List::size()
3046
  if (from_clause->size() == 0)
55 by brian
Update for using real bool types.
3047
    return false; /* We come here in the case of UNIONs. */
1 by brian
clean slate
3048
2183.2.12 by Olaf van der Spek
Use List::begin()
3049
  List<TableList>::iterator table_ref_it(from_clause->begin());
327.2.4 by Brian Aker
Refactoring table.h
3050
  TableList *table_ref; /* Current table reference. */
1 by brian
clean slate
3051
  /* Table reference to the left of the current. */
327.2.4 by Brian Aker
Refactoring table.h
3052
  TableList *left_neighbor;
1 by brian
clean slate
3053
  /* Table reference to the right of the current. */
327.2.4 by Brian Aker
Refactoring table.h
3054
  TableList *right_neighbor= NULL;
1 by brian
clean slate
3055
3056
  /* Note that tables in the list are in reversed order */
3057
  for (left_neighbor= table_ref_it++; left_neighbor ; )
3058
  {
3059
    table_ref= left_neighbor;
3060
    left_neighbor= table_ref_it++;
2318.6.90 by Olaf van der Spek
Refactor
3061
    if (store_top_level_join_columns(session, table_ref, left_neighbor, right_neighbor))
404 by Brian Aker
Removed dead variable
3062
      return true;
3063
    if (left_neighbor)
1 by brian
clean slate
3064
    {
404 by Brian Aker
Removed dead variable
3065
      TableList *first_leaf_on_the_right;
3066
      first_leaf_on_the_right= table_ref->first_leaf_for_name_resolution();
3067
      left_neighbor->next_name_resolution_table= first_leaf_on_the_right;
1 by brian
clean slate
3068
    }
3069
    right_neighbor= table_ref;
3070
  }
3071
3072
  /*
3073
    Store the top-most, left-most NATURAL/USING join, so that we start
3074
    the search from that one instead of context->table_list. At this point
3075
    right_neighbor points to the left-most top-level table reference in the
3076
    FROM clause.
3077
  */
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3078
  assert(right_neighbor);
1 by brian
clean slate
3079
  context->first_name_resolution_table=
3080
    right_neighbor->first_leaf_for_name_resolution();
3081
55 by brian
Update for using real bool types.
3082
  return false;
1 by brian
clean slate
3083
}
3084
3085
3086
/****************************************************************************
1046 by Brian Aker
Merge Jay.
3087
 ** Expand all '*' in given fields
3088
 ****************************************************************************/
1 by brian
clean slate
3089
1034.1.7 by Brian Aker
Remove dead bits to the end of functions.
3090
int setup_wild(Session *session, List<Item> &fields,
77.1.45 by Monty Taylor
Warning fixes.
3091
               List<Item> *sum_func_list,
482 by Brian Aker
Remove uint.
3092
               uint32_t wild_num)
1 by brian
clean slate
3093
{
3094
  if (!wild_num)
1034.1.7 by Brian Aker
Remove dead bits to the end of functions.
3095
    return 0;
1 by brian
clean slate
3096
3097
  Item *item;
2183.2.12 by Olaf van der Spek
Use List::begin()
3098
  List<Item>::iterator it(fields.begin());
1 by brian
clean slate
3099
2227.4.8 by Olaf van der Spek
Session::lex()
3100
  session->lex().current_select->cur_pos_in_select_list= 0;
1 by brian
clean slate
3101
  while (wild_num && (item= it++))
3102
  {
3103
    if (item->type() == Item::FIELD_ITEM &&
3104
        ((Item_field*) item)->field_name &&
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
3105
        ((Item_field*) item)->field_name[0] == '*' &&
3106
        !((Item_field*) item)->field)
1 by brian
clean slate
3107
    {
2183.2.19 by Olaf van der Spek
Use List::size()
3108
      uint32_t elem= fields.size();
1 by brian
clean slate
3109
      bool any_privileges= ((Item_field *) item)->any_privileges;
2227.4.8 by Olaf van der Spek
Session::lex()
3110
      Item_subselect *subsel= session->lex().current_select->master_unit()->item;
1 by brian
clean slate
3111
      if (subsel &&
3112
          subsel->substype() == Item_subselect::EXISTS_SUBS)
3113
      {
3114
        /*
3115
          It is EXISTS(SELECT * ...) and we can replace * by any constant.
3116
3117
          Item_int do not need fix_fields() because it is basic constant.
3118
        */
152 by Brian Aker
longlong replacement
3119
        it.replace(new Item_int("Not_used", (int64_t) 1,
1 by brian
clean slate
3120
                                MY_INT64_NUM_DECIMAL_DIGITS));
3121
      }
520.1.22 by Brian Aker
Second pass of thd cleanup
3122
      else if (insert_fields(session, ((Item_field*) item)->context,
1 by brian
clean slate
3123
                             ((Item_field*) item)->db_name,
3124
                             ((Item_field*) item)->table_name, &it,
3125
                             any_privileges))
3126
      {
1046.1.10 by Brian Aker
Formatting around return (style)
3127
        return -1;
1 by brian
clean slate
3128
      }
3129
      if (sum_func_list)
3130
      {
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
3131
        /*
3132
          sum_func_list is a list that has the fields list as a tail.
3133
          Because of this we have to update the element count also for this
3134
          list after expanding the '*' entry.
3135
        */
2183.2.20 by Olaf van der Spek
Use List::size()
3136
        sum_func_list->set_size(sum_func_list->size() + fields.size() - elem);
1 by brian
clean slate
3137
      }
3138
      wild_num--;
3139
    }
3140
    else
2227.4.8 by Olaf van der Spek
Session::lex()
3141
      session->lex().current_select->cur_pos_in_select_list++;
1 by brian
clean slate
3142
  }
2227.4.8 by Olaf van der Spek
Session::lex()
3143
  session->lex().current_select->cur_pos_in_select_list= UNDEF_POS;
1113.1.1 by Brian Aker
Dead code removal around LCOV finds.
3144
1046.1.10 by Brian Aker
Formatting around return (style)
3145
  return 0;
1 by brian
clean slate
3146
}
3147
3148
/****************************************************************************
1046 by Brian Aker
Merge Jay.
3149
 ** Check that all given fields exists and fill struct with current data
3150
 ****************************************************************************/
1 by brian
clean slate
3151
520.1.22 by Brian Aker
Second pass of thd cleanup
3152
bool setup_fields(Session *session, Item **ref_pointer_array,
1 by brian
clean slate
3153
                  List<Item> &fields, enum_mark_columns mark_used_columns,
3154
                  List<Item> *sum_func_list, bool allow_sum_func)
3155
{
2318.6.90 by Olaf van der Spek
Refactor
3156
  Item *item;
520.1.22 by Brian Aker
Second pass of thd cleanup
3157
  enum_mark_columns save_mark_used_columns= session->mark_used_columns;
2227.4.8 by Olaf van der Spek
Session::lex()
3158
  nesting_map save_allow_sum_func= session->lex().allow_sum_func;
2183.2.12 by Olaf van der Spek
Use List::begin()
3159
  List<Item>::iterator it(fields.begin());
1 by brian
clean slate
3160
  bool save_is_item_list_lookup;
3161
520.1.22 by Brian Aker
Second pass of thd cleanup
3162
  session->mark_used_columns= mark_used_columns;
1 by brian
clean slate
3163
  if (allow_sum_func)
2227.4.8 by Olaf van der Spek
Session::lex()
3164
    session->lex().allow_sum_func|= 1 << session->lex().current_select->nest_level;
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3165
  session->setWhere(Session::DEFAULT_WHERE);
2227.4.8 by Olaf van der Spek
Session::lex()
3166
  save_is_item_list_lookup= session->lex().current_select->is_item_list_lookup;
3167
  session->lex().current_select->is_item_list_lookup= 0;
1 by brian
clean slate
3168
3169
  /*
3170
    To prevent fail on forward lookup we fill it with zerows,
3171
    then if we got pointer on zero after find_item_in_list we will know
3172
    that it is forward lookup.
3173
3174
    There is other way to solve problem: fill array with pointers to list,
3175
    but it will be slower.
3176
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3177
    TODO-> remove it when (if) we made one list for allfields and ref_pointer_array
1 by brian
clean slate
3178
  */
3179
  if (ref_pointer_array)
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3180
  {
2183.2.19 by Olaf van der Spek
Use List::size()
3181
    memset(ref_pointer_array, 0, sizeof(Item *) * fields.size());
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3182
  }
1 by brian
clean slate
3183
3184
  Item **ref= ref_pointer_array;
2227.4.8 by Olaf van der Spek
Session::lex()
3185
  session->lex().current_select->cur_pos_in_select_list= 0;
1 by brian
clean slate
3186
  while ((item= it++))
3187
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
3188
    if ((!item->fixed && item->fix_fields(session, it.ref())) || (item= *(it.ref()))->check_cols(1))
1 by brian
clean slate
3189
    {
2227.4.8 by Olaf van der Spek
Session::lex()
3190
      session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3191
      session->lex().allow_sum_func= save_allow_sum_func;
520.1.22 by Brian Aker
Second pass of thd cleanup
3192
      session->mark_used_columns= save_mark_used_columns;
971.6.11 by Eric Day
Removed purecov messages.
3193
      return true;
1 by brian
clean slate
3194
    }
3195
    if (ref)
3196
      *(ref++)= item;
3197
    if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
1046 by Brian Aker
Merge Jay.
3198
        sum_func_list)
520.1.22 by Brian Aker
Second pass of thd cleanup
3199
      item->split_sum_func(session, ref_pointer_array, *sum_func_list);
3200
    session->used_tables|= item->used_tables();
2227.4.8 by Olaf van der Spek
Session::lex()
3201
    session->lex().current_select->cur_pos_in_select_list++;
1 by brian
clean slate
3202
  }
2227.4.8 by Olaf van der Spek
Session::lex()
3203
  session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
3204
  session->lex().current_select->cur_pos_in_select_list= UNDEF_POS;
1 by brian
clean slate
3205
2227.4.8 by Olaf van der Spek
Session::lex()
3206
  session->lex().allow_sum_func= save_allow_sum_func;
520.1.22 by Brian Aker
Second pass of thd cleanup
3207
  session->mark_used_columns= save_mark_used_columns;
3208
  return(test(session->is_error()));
1 by brian
clean slate
3209
}
3210
3211
3212
/*
3213
  make list of leaves of join table tree
3214
3215
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3216
  make_leaves_list()
3217
  list    pointer to pointer on list first element
3218
  tables  table list
1 by brian
clean slate
3219
3220
  RETURN pointer on pointer to next_leaf of last element
3221
*/
3222
1085.1.2 by Monty Taylor
Fixed -Wmissing-declarations
3223
static TableList **make_leaves_list(TableList **list, TableList *tables)
1 by brian
clean slate
3224
{
327.2.4 by Brian Aker
Refactoring table.h
3225
  for (TableList *table= tables; table; table= table->next_local)
1 by brian
clean slate
3226
  {
3227
    {
3228
      *list= table;
3229
      list= &table->next_leaf;
3230
    }
3231
  }
3232
  return list;
3233
}
3234
3235
/*
3236
  prepare tables
3237
3238
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3239
  setup_tables()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
3240
  session		  Thread Cursor
1046 by Brian Aker
Merge Jay.
3241
  context       name resolution contest to setup table list there
3242
  from_clause   Top-level list of table references in the FROM clause
3243
  tables	  Table list (select_lex->table_list)
3244
  leaves        List of join table leaves list (select_lex->leaf_tables)
3245
  refresh       It is onle refresh for subquery
3246
  select_insert It is SELECT ... INSERT command
1 by brian
clean slate
3247
3248
  NOTE
1046 by Brian Aker
Merge Jay.
3249
  Check also that the 'used keys' and 'ignored keys' exists and set up the
3250
  table structure accordingly.
3251
  Create a list of leaf tables. For queries with NATURAL/USING JOINs,
3252
  compute the row types of the top most natural/using join table references
3253
  and link these into a list of table references for name resolution.
1 by brian
clean slate
3254
1046 by Brian Aker
Merge Jay.
3255
  This has to be called for all tables that are used by items, as otherwise
3256
  table->map is not set and all Item_field will be regarded as const items.
1 by brian
clean slate
3257
3258
  RETURN
1046 by Brian Aker
Merge Jay.
3259
  false ok;  In this case *map will includes the chosen index
3260
  true  error
1 by brian
clean slate
3261
*/
3262
520.1.22 by Brian Aker
Second pass of thd cleanup
3263
bool setup_tables(Session *session, Name_resolution_context *context,
327.2.4 by Brian Aker
Refactoring table.h
3264
                  List<TableList> *from_clause, TableList *tables,
3265
                  TableList **leaves, bool select_insert)
1 by brian
clean slate
3266
{
2385.2.4 by Olaf van der Spek
cppcheck
3267
  assert((select_insert && !tables->next_name_resolution_table) || !tables ||
1046 by Brian Aker
Merge Jay.
3268
          (context->table_list && context->first_name_resolution_table));
1 by brian
clean slate
3269
  /*
3270
    this is used for INSERT ... SELECT.
3271
    For select we setup tables except first (and its underlying tables)
3272
  */
2385.2.4 by Olaf van der Spek
cppcheck
3273
  TableList *first_select_table= select_insert ? tables->next_local : NULL;
1034.1.4 by Brian Aker
Fixed TableList to correctly construct (and removed memset() calls in
3274
2385.2.4 by Olaf van der Spek
cppcheck
3275
  if (not *leaves)
1 by brian
clean slate
3276
    make_leaves_list(leaves, tables);
3277
2385.2.4 by Olaf van der Spek
cppcheck
3278
  uint32_t tablenr= 0;
3279
  for (TableList* table_list= *leaves; table_list; table_list= table_list->next_leaf, tablenr++)
1 by brian
clean slate
3280
  {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3281
    Table *table= table_list->table;
1 by brian
clean slate
3282
    table->pos_in_table_list= table_list;
3283
    if (first_select_table &&
3284
        table_list->top_table() == first_select_table)
3285
    {
3286
      /* new counting for SELECT of INSERT ... SELECT command */
3287
      first_select_table= 0;
3288
      tablenr= 0;
3289
    }
934.1.1 by Brian Aker
Moved two functions in classes.
3290
    table->setup_table_map(table_list, tablenr);
1 by brian
clean slate
3291
    if (table_list->process_index_hints(table))
1046.1.10 by Brian Aker
Formatting around return (style)
3292
      return 1;
1 by brian
clean slate
3293
  }
3294
  if (tablenr > MAX_TABLES)
3295
  {
3296
    my_error(ER_TOO_MANY_TABLES,MYF(0),MAX_TABLES);
1046.1.10 by Brian Aker
Formatting around return (style)
3297
    return 1;
1 by brian
clean slate
3298
  }
3299
3300
  /* Precompute and store the row types of NATURAL/USING joins. */
520.1.22 by Brian Aker
Second pass of thd cleanup
3301
  if (setup_natural_join_row_types(session, from_clause, context))
1046.1.10 by Brian Aker
Formatting around return (style)
3302
    return 1;
1 by brian
clean slate
3303
1046.1.10 by Brian Aker
Formatting around return (style)
3304
  return 0;
1 by brian
clean slate
3305
}
3306
3307
3308
/*
3309
  prepare tables and check access for the view tables
3310
3311
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3312
  setup_tables_and_check_view_access()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
3313
  session		  Thread Cursor
1046 by Brian Aker
Merge Jay.
3314
  context       name resolution contest to setup table list there
3315
  from_clause   Top-level list of table references in the FROM clause
3316
  tables	  Table list (select_lex->table_list)
3317
  conds	  Condition of current SELECT (can be changed by VIEW)
3318
  leaves        List of join table leaves list (select_lex->leaf_tables)
3319
  refresh       It is onle refresh for subquery
3320
  select_insert It is SELECT ... INSERT command
3321
  want_access   what access is needed
1 by brian
clean slate
3322
3323
  NOTE
1046 by Brian Aker
Merge Jay.
3324
  a wrapper for check_tables that will also check the resulting
3325
  table leaves list for access to all the tables that belong to a view
1 by brian
clean slate
3326
3327
  RETURN
1046 by Brian Aker
Merge Jay.
3328
  false ok;  In this case *map will include the chosen index
3329
  true  error
1 by brian
clean slate
3330
*/
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3331
bool setup_tables_and_check_access(Session *session,
1 by brian
clean slate
3332
                                   Name_resolution_context *context,
327.2.4 by Brian Aker
Refactoring table.h
3333
                                   List<TableList> *from_clause,
3334
                                   TableList *tables,
3335
                                   TableList **leaves,
1 by brian
clean slate
3336
                                   bool select_insert)
3337
{
327.2.4 by Brian Aker
Refactoring table.h
3338
  TableList *leaves_tmp= NULL;
1 by brian
clean slate
3339
2385.2.4 by Olaf van der Spek
cppcheck
3340
  if (setup_tables(session, context, from_clause, tables, &leaves_tmp, select_insert))
55 by brian
Update for using real bool types.
3341
    return true;
1 by brian
clean slate
3342
3343
  if (leaves)
3344
    *leaves= leaves_tmp;
3345
55 by brian
Update for using real bool types.
3346
  return false;
1 by brian
clean slate
3347
}
3348
3349
3350
/*
3351
  Drops in all fields instead of current '*' field
3352
3353
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3354
  insert_fields()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
3355
  session			Thread Cursor
1046 by Brian Aker
Merge Jay.
3356
  context             Context for name resolution
3357
  db_name		Database name in case of 'database_name.table_name.*'
3358
  table_name		Table name in case of 'table_name.*'
3359
  it			Pointer to '*'
3360
  any_privileges	0 If we should ensure that we have SELECT privileges
3361
  for all columns
3362
  1 If any privilege is ok
1 by brian
clean slate
3363
  RETURN
1046 by Brian Aker
Merge Jay.
3364
  0	ok     'it' is updated to point at last inserted
3365
  1	error.  Error message is generated but not sent to client
1 by brian
clean slate
3366
*/
3367
3368
bool
520.1.22 by Brian Aker
Second pass of thd cleanup
3369
insert_fields(Session *session, Name_resolution_context *context, const char *db_name,
2179.1.2 by Olaf van der Spek
x
3370
              const char *table_name, List<Item>::iterator *it,
779.1.27 by Monty Taylor
Got rid of __attribute__((unused)) and the like from the .cc files.
3371
              bool )
1 by brian
clean slate
3372
{
3373
  Field_iterator_table_ref field_iterator;
3374
  bool found;
3375
  char name_buff[NAME_LEN+1];
3376
1039.1.5 by Brian Aker
Remove lower case filename bits (aka we just lock into the most compatible
3377
  if (db_name)
1 by brian
clean slate
3378
  {
3379
    /*
3380
      convert database to lower case for comparison
3381
      We can't do this in Item_field as this would change the
3382
      'name' of the item which may be used in the select list
3383
    */
629.5.3 by Toru Maesaka
Third pass of replacing MySQL's strmake() with libc calls
3384
    strncpy(name_buff, db_name, sizeof(name_buff)-1);
2456.1.4 by Olaf van der Spek
Refactor
3385
    files_charset_info->casedn_str(name_buff);
1 by brian
clean slate
3386
    db_name= name_buff;
3387
  }
3388
55 by brian
Update for using real bool types.
3389
  found= false;
1 by brian
clean slate
3390
3391
  /*
3392
    If table names are qualified, then loop over all tables used in the query,
3393
    else treat natural joins as leaves and do not iterate over their underlying
3394
    tables.
3395
  */
327.2.4 by Brian Aker
Refactoring table.h
3396
  for (TableList *tables= (table_name ? context->table_list :
1046 by Brian Aker
Merge Jay.
3397
                           context->first_name_resolution_table);
1 by brian
clean slate
3398
       tables;
3399
       tables= (table_name ? tables->next_local :
3400
                tables->next_name_resolution_table)
1046 by Brian Aker
Merge Jay.
3401
      )
1 by brian
clean slate
3402
  {
3403
    Field *field;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3404
    Table *table= tables->table;
1 by brian
clean slate
3405
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3406
    assert(tables->is_leaf_for_name_resolution());
1 by brian
clean slate
3407
2456.1.4 by Olaf van der Spek
Refactor
3408
    if ((table_name && table_alias_charset->strcasecmp(table_name, tables->alias)) ||
3409
        (db_name && system_charset_info->strcasecmp(tables->getSchemaName(),db_name)))
1 by brian
clean slate
3410
      continue;
3411
3412
    /*
3413
      Update the tables used in the query based on the referenced fields. For
3414
      views and natural joins this update is performed inside the loop below.
3415
    */
3416
    if (table)
520.1.22 by Brian Aker
Second pass of thd cleanup
3417
      session->used_tables|= table->map;
1 by brian
clean slate
3418
3419
    /*
3420
      Initialize a generic field iterator for the current table reference.
3421
      Notice that it is guaranteed that this iterator will iterate over the
3422
      fields of a single table reference, because 'tables' is a leaf (for
3423
      name resolution purposes).
3424
    */
3425
    field_iterator.set(tables);
3426
3427
    for (; !field_iterator.end_of_fields(); field_iterator.next())
3428
    {
2385.2.4 by Olaf van der Spek
cppcheck
3429
      Item *item= field_iterator.create_item(session);
3430
      if (not item)
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
3431
        return true;
1 by brian
clean slate
3432
2385.2.4 by Olaf van der Spek
cppcheck
3433
      if (not found)
1 by brian
clean slate
3434
      {
55 by brian
Update for using real bool types.
3435
        found= true;
1 by brian
clean slate
3436
        it->replace(item); /* Replace '*' with the first found item. */
3437
      }
3438
      else
3439
        it->after(item);   /* Add 'item' to the SELECT list. */
3440
3441
      if ((field= field_iterator.field()))
3442
      {
3443
        /* Mark fields as used to allow storage engine to optimze access */
1999.4.2 by Brian Aker
Encapsulate the field's position.
3444
        field->getTable()->setReadSet(field->position());
1 by brian
clean slate
3445
        if (table)
3446
        {
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
3447
          table->covering_keys&= field->part_of_key;
3448
          table->merge_keys|= field->part_of_key;
1 by brian
clean slate
3449
        }
3450
        if (tables->is_natural_join)
3451
        {
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3452
          Table *field_table;
1 by brian
clean slate
3453
          /*
3454
            In this case we are sure that the column ref will not be created
3455
            because it was already created and stored with the natural join.
3456
          */
3457
          Natural_join_column *nj_col;
3458
          if (!(nj_col= field_iterator.get_natural_column_ref()))
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
3459
            return true;
51.1.48 by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE
3460
          assert(nj_col->table_field);
1 by brian
clean slate
3461
          field_table= nj_col->table_ref->table;
3462
          if (field_table)
3463
          {
520.1.22 by Brian Aker
Second pass of thd cleanup
3464
            session->used_tables|= field_table->map;
1005.2.6 by Monty Taylor
Re-added bitset<> as a replacement for Bitmap<>
3465
            field_table->covering_keys&= field->part_of_key;
3466
            field_table->merge_keys|= field->part_of_key;
1 by brian
clean slate
3467
            field_table->used_fields++;
3468
          }
3469
        }
3470
      }
3471
      else
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
3472
      {
520.1.22 by Brian Aker
Second pass of thd cleanup
3473
        session->used_tables|= item->used_tables();
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
3474
      }
3475
2227.4.8 by Olaf van der Spek
Session::lex()
3476
      session->lex().current_select->cur_pos_in_select_list++;
1 by brian
clean slate
3477
    }
3478
    /*
3479
      In case of stored tables, all fields are considered as used,
3480
      while in the case of views, the fields considered as used are the
3481
      ones marked in setup_tables during fix_fields of view columns.
3482
      For NATURAL joins, used_tables is updated in the IF above.
3483
    */
3484
    if (table)
1578.2.10 by Brian Aker
keys and fields partial encapsulation.
3485
      table->used_fields= table->getShare()->sizeFields();
1 by brian
clean slate
3486
  }
3487
  if (found)
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
3488
    return false;
1 by brian
clean slate
3489
3490
  /*
1273.19.1 by Brian Aker
Update for show fields.
3491
    @TODO in the case when we skipped all columns because there was a
3492
    qualified '*', and all columns were coalesced, we have to give a more
3493
    meaningful message than ER_BAD_TABLE_ERROR.
1 by brian
clean slate
3494
  */
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
3495
  if (not table_name)
3496
  {
1 by brian
clean slate
3497
    my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
3498
  }
1 by brian
clean slate
3499
  else
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
3500
  {
1 by brian
clean slate
3501
    my_error(ER_BAD_TABLE_ERROR, MYF(0), table_name);
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
3502
  }
1 by brian
clean slate
3503
1046.1.9 by Brian Aker
Remove caller that wasn't correctly locking, and reverted code to 5.1
3504
  return true;
1 by brian
clean slate
3505
}
3506
3507
3508
/*
3509
  Fix all conditions and outer join expressions.
3510
3511
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3512
  setup_conds()
1183.1.2 by Brian Aker
Rename of handler to Cursor. You would not believe how long I have wanted
3513
  session     thread Cursor
1046 by Brian Aker
Merge Jay.
3514
  tables  list of tables for name resolving (select_lex->table_list)
3515
  leaves  list of leaves of join table tree (select_lex->leaf_tables)
3516
  conds   WHERE clause
1 by brian
clean slate
3517
3518
  DESCRIPTION
1046 by Brian Aker
Merge Jay.
3519
  TODO
1 by brian
clean slate
3520
3521
  RETURN
1046 by Brian Aker
Merge Jay.
3522
  true  if some error occured (e.g. out of memory)
3523
  false if all is OK
1 by brian
clean slate
3524
*/
3525
1109.1.5 by Brian Aker
More extraction from sql_base
3526
int Session::setup_conds(TableList *leaves, COND **conds)
1 by brian
clean slate
3527
{
1109.1.5 by Brian Aker
More extraction from sql_base
3528
  Session *session= this;
2227.4.8 by Olaf van der Spek
Session::lex()
3529
  Select_Lex *select_lex= session->lex().current_select;
327.2.4 by Brian Aker
Refactoring table.h
3530
  TableList *table= NULL;	// For HP compilers
520.1.22 by Brian Aker
Second pass of thd cleanup
3531
  void *save_session_marker= session->session_marker;
1 by brian
clean slate
3532
  /*
846 by Brian Aker
Removing on typedeffed class.
3533
    it_is_update set to true when tables of primary Select_Lex (Select_Lex
1 by brian
clean slate
3534
    which belong to LEX, i.e. most up SELECT) will be updated by
3535
    INSERT/UPDATE/LOAD
1046.1.10 by Brian Aker
Formatting around return (style)
3536
    NOTE-> using this condition helps to prevent call of prepare_check_option()
3537
    from subquery of VIEW, because tables of subquery belongs to VIEW
3538
    (see condition before prepare_check_option() call)
1 by brian
clean slate
3539
  */
3540
  bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
3541
  select_lex->is_item_list_lookup= 0;
3542
520.1.22 by Brian Aker
Second pass of thd cleanup
3543
  session->mark_used_columns= MARK_COLUMNS_READ;
1 by brian
clean slate
3544
  select_lex->cond_count= 0;
3545
  select_lex->between_count= 0;
3546
  select_lex->max_equal_elems= 0;
3547
520.1.22 by Brian Aker
Second pass of thd cleanup
3548
  session->session_marker= (void*)1;
1 by brian
clean slate
3549
  if (*conds)
3550
  {
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3551
    session->setWhere("where clause");
520.1.22 by Brian Aker
Second pass of thd cleanup
3552
    if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
1046 by Brian Aker
Merge Jay.
3553
        (*conds)->check_cols(1))
1 by brian
clean slate
3554
      goto err_no_arena;
3555
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
3556
  session->session_marker= save_session_marker;
1 by brian
clean slate
3557
3558
  /*
3559
    Apply fix_fields() to all ON clauses at all levels of nesting,
3560
    including the ones inside view definitions.
3561
  */
3562
  for (table= leaves; table; table= table->next_leaf)
3563
  {
327.2.4 by Brian Aker
Refactoring table.h
3564
    TableList *embedded; /* The table at the current level of nesting. */
3565
    TableList *embedding= table; /* The parent nested table reference. */
1 by brian
clean slate
3566
    do
3567
    {
3568
      embedded= embedding;
3569
      if (embedded->on_expr)
3570
      {
3571
        /* Make a join an a expression */
520.1.22 by Brian Aker
Second pass of thd cleanup
3572
        session->session_marker= (void*)embedded;
2114.4.10 by Brian Aker
Remove current_session from a couple of locations, encapsulate where in
3573
        session->setWhere("on clause");
520.1.22 by Brian Aker
Second pass of thd cleanup
3574
        if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(session, &embedded->on_expr)) ||
1039.2.8 by Jay Pipes
Yet more indentation and style cleanup
3575
            embedded->on_expr->check_cols(1))
3576
          goto err_no_arena;
1 by brian
clean slate
3577
        select_lex->cond_count++;
3578
      }
1637.2.7 by Vijay Samuel
Merge encapsulate TableList-2.
3579
      embedding= embedded->getEmbedding();
1 by brian
clean slate
3580
    }
3581
    while (embedding &&
2183.2.21 by Olaf van der Spek
Use List::front()
3582
           &embedding->getNestedJoin()->join_list.front() == embedded);
1 by brian
clean slate
3583
3584
  }
520.1.22 by Brian Aker
Second pass of thd cleanup
3585
  session->session_marker= save_session_marker;
1 by brian
clean slate
3586
2227.4.8 by Olaf van der Spek
Session::lex()
3587
  session->lex().current_select->is_item_list_lookup= save_is_item_list_lookup;
520.1.22 by Brian Aker
Second pass of thd cleanup
3588
  return(test(session->is_error()));
1 by brian
clean slate
3589
3590
err_no_arena:
3591
  select_lex->is_item_list_lookup= save_is_item_list_lookup;
1046.1.10 by Brian Aker
Formatting around return (style)
3592
3593
  return 1;
1 by brian
clean slate
3594
}
3595
3596
3597
/******************************************************************************
1046 by Brian Aker
Merge Jay.
3598
 ** Fill a record with data (for INSERT or UPDATE)
3599
 ** Returns : 1 if some field has wrong type
3600
 ******************************************************************************/
1 by brian
clean slate
3601
3602
3603
/*
3604
  Fill fields with given items.
3605
3606
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3607
  fill_record()
3608
  fields        Item_fields list to be filled
3609
  values        values to fill with
3610
  ignore_errors true if we should ignore errors
1 by brian
clean slate
3611
3612
  NOTE
1046 by Brian Aker
Merge Jay.
3613
  fill_record() may set table->auto_increment_field_not_null and a
3614
  caller should make sure that it is reset after their last call to this
3615
  function.
1 by brian
clean slate
3616
3617
  RETURN
1046 by Brian Aker
Merge Jay.
3618
  false   OK
3619
  true    error occured
1 by brian
clean slate
3620
*/
3621
3622
bool
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3623
fill_record(Session *session, List<Item> &fields, List<Item> &values, bool ignore_errors)
1 by brian
clean slate
3624
{
2183.2.12 by Olaf van der Spek
Use List::begin()
3625
  List<Item>::iterator f(fields.begin());
3626
  List<Item>::iterator v(values.begin());
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3627
  Item *value;
1 by brian
clean slate
3628
  Item_field *field;
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3629
  Table *table;
1 by brian
clean slate
3630
3631
  /*
3632
    Reset the table->auto_increment_field_not_null as it is valid for
3633
    only one row.
3634
  */
2183.2.19 by Olaf van der Spek
Use List::size()
3635
  if (fields.size())
1 by brian
clean slate
3636
  {
3637
    /*
3638
      On INSERT or UPDATE fields are checked to be from the same table,
3639
      thus we safely can take table from the first field.
3640
    */
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3641
    field= static_cast<Item_field *>(f++);
1660.1.3 by Brian Aker
Encapsulate Table in field
3642
    table= field->field->getTable();
55 by brian
Update for using real bool types.
3643
    table->auto_increment_field_not_null= false;
2183.2.7 by Olaf van der Spek
x
3644
    f= fields.begin();
1 by brian
clean slate
3645
  }
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3646
3647
  while ((field= static_cast<Item_field *>(f++)))
1 by brian
clean slate
3648
  {
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3649
    value= v++;
3650
1 by brian
clean slate
3651
    Field *rfield= field->field;
1660.1.3 by Brian Aker
Encapsulate Table in field
3652
    table= rfield->getTable();
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3653
1 by brian
clean slate
3654
    if (rfield == table->next_number_field)
55 by brian
Update for using real bool types.
3655
      table->auto_increment_field_not_null= true;
1 by brian
clean slate
3656
    if ((value->save_in_field(rfield, 0) < 0) && !ignore_errors)
3657
    {
3658
      my_message(ER_UNKNOWN_ERROR, ER(ER_UNKNOWN_ERROR), MYF(0));
1859.2.13 by Brian Aker
Remove a number of goto'. Also I removed the std::cerr that slipped in on
3659
      if (table)
3660
        table->auto_increment_field_not_null= false;
3661
3662
      return true;
1 by brian
clean slate
3663
    }
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3664
  }
3665
3666
  return session->is_error();
1 by brian
clean slate
3667
}
3668
3669
3670
/*
3671
  Fill field buffer with values from Field list
3672
3673
  SYNOPSIS
1046 by Brian Aker
Merge Jay.
3674
  fill_record()
3675
  ptr           pointer on pointer to record
3676
  values        list of fields
3677
  ignore_errors true if we should ignore errors
1 by brian
clean slate
3678
3679
  NOTE
1046 by Brian Aker
Merge Jay.
3680
  fill_record() may set table->auto_increment_field_not_null and a
3681
  caller should make sure that it is reset after their last call to this
3682
  function.
1 by brian
clean slate
3683
3684
  RETURN
1046 by Brian Aker
Merge Jay.
3685
  false   OK
3686
  true    error occured
1 by brian
clean slate
3687
*/
3688
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3689
bool fill_record(Session *session, Field **ptr, List<Item> &values, bool)
1 by brian
clean slate
3690
{
2183.2.12 by Olaf van der Spek
Use List::begin()
3691
  List<Item>::iterator v(values.begin());
1 by brian
clean slate
3692
  Item *value;
327.1.5 by Brian Aker
Refactor around classes. TABLE_LIST has been factored out of table.h
3693
  Table *table= 0;
1 by brian
clean slate
3694
  Field *field;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
3695
1 by brian
clean slate
3696
  /*
3697
    Reset the table->auto_increment_field_not_null as it is valid for
3698
    only one row.
3699
  */
3700
  if (*ptr)
3701
  {
3702
    /*
3703
      On INSERT or UPDATE fields are checked to be from the same table,
3704
      thus we safely can take table from the first field.
3705
    */
1660.1.3 by Brian Aker
Encapsulate Table in field
3706
    table= (*ptr)->getTable();
55 by brian
Update for using real bool types.
3707
    table->auto_increment_field_not_null= false;
1 by brian
clean slate
3708
  }
2069.2.1 by Brian Aker
Fix issue with now() not storing fractional seconds in timestamp(6)
3709
520.1.22 by Brian Aker
Second pass of thd cleanup
3710
  while ((field = *ptr++) && ! session->is_error())
1 by brian
clean slate
3711
  {
3712
    value=v++;
1660.1.3 by Brian Aker
Encapsulate Table in field
3713
    table= field->getTable();
2069.2.1 by Brian Aker
Fix issue with now() not storing fractional seconds in timestamp(6)
3714
1 by brian
clean slate
3715
    if (field == table->next_number_field)
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
3716
    {
55 by brian
Update for using real bool types.
3717
      table->auto_increment_field_not_null= true;
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
3718
    }
2069.2.1 by Brian Aker
Fix issue with now() not storing fractional seconds in timestamp(6)
3719
1 by brian
clean slate
3720
    if (value->save_in_field(field, 0) < 0)
1859.2.13 by Brian Aker
Remove a number of goto'. Also I removed the std::cerr that slipped in on
3721
    {
3722
      if (table)
3723
        table->auto_increment_field_not_null= false;
3724
3725
      return true;
3726
    }
1235.1.10 by Brian Aker
Cleaned up the update call (I noticed that there were still update view code
3727
  }
3728
520.1.22 by Brian Aker
Second pass of thd cleanup
3729
  return(session->is_error());
1 by brian
clean slate
3730
}
3731
3732
2302.1.3 by Olaf van der Spek
New / make_shared doesn't return NULL
3733
void drizzle_rm_tmp_tables()
1 by brian
clean slate
3734
{
1556.1.1 by Brian Aker
Updates for moving temporary directory.
3735
  assert(drizzle_tmpdir.size());
2039.6.3 by Brian Aker
Update for session to have a catalog object.
3736
  Session::shared_ptr session= Session::make_shared(plugin::Listen::getNullClient(), catalog::local());
3737
  session->thread_stack= (char*) session.get();
971.3.64 by Eric Day
Cleaned up Scheduler plugin, moved more code to the schedular plugins, reworked some functions to be methods in Session, removed some dead code.
3738
  session->storeGlobals();
1556.1.1 by Brian Aker
Updates for moving temporary directory.
3739
  plugin::StorageEngine::removeLostTemporaryTables(*session, drizzle_tmpdir.c_str());
1 by brian
clean slate
3740
}
3741
3742
/**
3743
  @} (end of group Data_Dictionary)
3744
*/
575.4.7 by Monty Taylor
More header cleanup.
3745
3746
void kill_drizzle(void)
3747
{
3748
  pthread_kill(signal_thread, SIGTERM);
2440.4.2 by Brian Aker
Fix level_t to be more inline with syslog
3749
  shutdown_in_progress= true;			// Safety if kill didn't work
575.4.7 by Monty Taylor
More header cleanup.
3750
}
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
3751
3752
} /* namespace drizzled */