~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2009-06-01 04:09:48 UTC
  • mfrom: (1039.2.9 working)
  • Revision ID: brian@gaz-20090601040948-ke10l7ndpzd6oupw
Merge Jay.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* Copyright (C) 2000-2006 MySQL AB
2
2
 
3
 
   This program is free software; you can redistribute it and/or modify
4
 
   it under the terms of the GNU General Public License as published by
5
 
   the Free Software Foundation; version 2 of the License.
6
 
 
7
 
   This program is distributed in the hope that it will be useful,
8
 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 
   GNU General Public License for more details.
11
 
 
12
 
   You should have received a copy of the GNU General Public License
13
 
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
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
 
14
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
 
17
17
/* Basic functions needed by many modules */
66
66
 
67
67
 
68
68
extern "C" unsigned char *table_cache_key(const unsigned char *record, size_t *length,
69
 
                                  bool )
 
69
                                          bool )
70
70
{
71
71
  Table *entry=(Table*) record;
72
72
  *length= entry->s->table_cache_key.length;
78
78
{
79
79
  return hash_init(&open_cache, &my_charset_bin,
80
80
                   (size_t) table_cache_size+16,
81
 
                   0, 0, table_cache_key,
82
 
                   free_cache_entry, 0);
 
81
                   0, 0, table_cache_key,
 
82
                   free_cache_entry, 0);
83
83
}
84
84
 
85
85
void table_cache_free(void)
102
102
  Create a table cache key
103
103
 
104
104
  SYNOPSIS
105
 
    create_table_def_key()
106
 
    key                 Create key here (must be of size MAX_DBKEY_LENGTH)
107
 
    table_list          Table definition
108
 
 
109
 
 IMPLEMENTATION
110
 
    The table cache_key is created from:
111
 
    db_name + \0
112
 
    table_name + \0
113
 
 
114
 
    if the table is a tmp table, we add the following to make each tmp table
115
 
    unique on the slave:
116
 
 
117
 
    4 bytes for master thread id
118
 
    4 bytes pseudo thread id
 
105
  create_table_def_key()
 
106
  key                   Create key here (must be of size MAX_DBKEY_LENGTH)
 
107
  table_list            Table definition
 
108
 
 
109
  IMPLEMENTATION
 
110
  The table cache_key is created from:
 
111
  db_name + \0
 
112
  table_name + \0
 
113
 
 
114
  if the table is a tmp table, we add the following to make each tmp table
 
115
  unique on the slave:
 
116
 
 
117
  4 bytes for master thread id
 
118
  4 bytes pseudo thread id
119
119
 
120
120
  RETURN
121
 
    Length of key
 
121
  Length of key
122
122
*/
123
123
 
124
124
uint32_t create_table_def_key(char *key, TableList *table_list)
127
127
  char *key_pos= key;
128
128
  key_pos= strcpy(key_pos, table_list->db) + strlen(table_list->db);
129
129
  key_pos= strcpy(key_pos+1, table_list->table_name) +
130
 
                  strlen(table_list->table_name);
 
130
    strlen(table_list->table_name);
131
131
  key_length= (uint32_t)(key_pos-key)+1;
132
132
 
133
133
  return key_length;
137
137
 
138
138
/*****************************************************************************
139
139
  Functions to handle table definition cach (TableShare)
140
 
*****************************************************************************/
 
140
 *****************************************************************************/
141
141
 
142
142
extern "C" unsigned char *table_def_key(const unsigned char *record, size_t *length,
143
 
                                bool )
 
143
                                        bool )
144
144
{
145
145
  TableShare *entry=(TableShare*) record;
146
146
  *length= entry->table_cache_key.length;
160
160
  pthread_mutex_init(&LOCK_table_share, MY_MUTEX_INIT_FAST);
161
161
 
162
162
  return hash_init(&table_def_cache, &my_charset_bin, (size_t)table_def_size,
163
 
                   0, 0, table_def_key,
164
 
                   (hash_free_key) table_def_free_entry, 0);
 
163
                   0, 0, table_def_key,
 
164
                   (hash_free_key) table_def_free_entry, 0);
165
165
}
166
166
 
167
167
 
194
194
  error                 out: Error code from open_table_def()
195
195
 
196
196
  IMPLEMENTATION
197
 
    Get a table definition from the table definition cache.
198
 
    If it doesn't exist, create a new from the table definition file.
 
197
  Get a table definition from the table definition cache.
 
198
  If it doesn't exist, create a new from the table definition file.
199
199
 
200
200
  NOTES
201
 
    We must have wrlock on LOCK_open when we come here
202
 
    (To be changed later)
 
201
  We must have wrlock on LOCK_open when we come here
 
202
  (To be changed later)
203
203
 
204
204
  RETURN
205
 
   0  Error
206
 
   #  Share for table
 
205
  0  Error
 
206
#  Share for table
207
207
*/
208
208
 
209
209
TableShare *get_table_share(Session *session, TableList *table_list, char *key,
210
 
                             uint32_t key_length, uint32_t, int *error)
 
210
                            uint32_t key_length, uint32_t, int *error)
211
211
{
212
212
  TableShare *share;
213
213
 
215
215
 
216
216
  /* Read table definition from cache */
217
217
  if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
218
 
                                         key_length)))
 
218
                                        key_length)))
219
219
    goto found;
220
220
 
221
221
  if (!(share= alloc_table_share(table_list, key, key_length)))
246
246
 
247
247
found:
248
248
  /*
249
 
     We found an existing table definition. Return it if we didn't get
250
 
     an error when reading the table definition from file.
 
249
    We found an existing table definition. Return it if we didn't get
 
250
    an error when reading the table definition from file.
251
251
  */
252
252
 
253
253
  /* We must do a lock to ensure that the structure is initialized */
310
310
 
311
311
 
312
312
/*
313
 
   Mark that we are not using table share anymore.
314
 
 
315
 
   SYNOPSIS
316
 
     release_table_share()
317
 
     share              Table share
318
 
 
319
 
   IMPLEMENTATION
320
 
     If ref_count goes to zero and (we have done a refresh or if we have
321
 
     already too many open table shares) then delete the definition.
322
 
 
323
 
     If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
324
 
     that the table is deleted or the thread is killed.
 
313
  Mark that we are not using table share anymore.
 
314
 
 
315
  SYNOPSIS
 
316
  release_table_share()
 
317
  share         Table share
 
318
 
 
319
  IMPLEMENTATION
 
320
  If ref_count goes to zero and (we have done a refresh or if we have
 
321
  already too many open table shares) then delete the definition.
 
322
 
 
323
  If type == RELEASE_WAIT_FOR_DROP then don't return until we get a signal
 
324
  that the table is deleted or the thread is killed.
325
325
*/
326
326
 
327
327
void release_table_share(TableShare *share)
347
347
  Check if table definition exits in cache
348
348
 
349
349
  SYNOPSIS
350
 
    get_cached_table_share()
351
 
    db                  Database name
352
 
    table_name          Table name
 
350
  get_cached_table_share()
 
351
  db                    Database name
 
352
  table_name            Table name
353
353
 
354
354
  RETURN
355
 
    0  Not cached
356
 
    #  TableShare for table
 
355
  0  Not cached
 
356
#  TableShare for table
357
357
*/
358
358
 
359
359
TableShare *get_cached_table_share(const char *db, const char *table_name)
374
374
  Close file handle, but leave the table in the table cache
375
375
 
376
376
  SYNOPSIS
377
 
    close_handle_and_leave_table_as_lock()
378
 
    table               Table handler
 
377
  close_handle_and_leave_table_as_lock()
 
378
  table         Table handler
379
379
 
380
380
  NOTES
381
 
    By leaving the table in the table cache, it disallows any other thread
382
 
    to open the table
383
 
 
384
 
    session->killed will be set if we run out of memory
385
 
 
386
 
    If closing a MERGE child, the calling function has to take care for
387
 
    closing the parent too, if necessary.
 
381
  By leaving the table in the table cache, it disallows any other thread
 
382
  to open the table
 
383
 
 
384
  session->killed will be set if we run out of memory
 
385
 
 
386
  If closing a MERGE child, the calling function has to take care for
 
387
  closing the parent too, if necessary.
388
388
*/
389
389
 
390
390
 
427
427
  Create a list for all open tables matching SQL expression
428
428
 
429
429
  SYNOPSIS
430
 
    list_open_tables()
431
 
    wild                SQL like expression
 
430
  list_open_tables()
 
431
  wild          SQL like expression
432
432
 
433
433
  NOTES
434
 
    One gets only a list of tables for which one has any kind of privilege.
435
 
    db and table names are allocated in result struct, so one doesn't need
436
 
    a lock on LOCK_open when traversing the return list.
 
434
  One gets only a list of tables for which one has any kind of privilege.
 
435
  db and table names are allocated in result struct, so one doesn't need
 
436
  a lock on LOCK_open when traversing the return list.
437
437
 
438
438
  RETURN VALUES
439
 
    NULL        Error (Probably OOM)
440
 
    #           Pointer to list of names of open tables.
 
439
  NULL  Error (Probably OOM)
 
440
#               Pointer to list of names of open tables.
441
441
*/
442
442
 
443
443
OPEN_TableList *list_open_tables(const char *db, const char *wild)
470
470
    for (table= open_list  ; table ; table=table->next)
471
471
    {
472
472
      if (!strcmp(table->table, share->table_name.str) &&
473
 
          !strcmp(table->db,    share->db.str))
 
473
          !strcmp(table->db,    share->db.str))
474
474
      {
475
 
        if (entry->in_use)
476
 
          table->in_use++;
477
 
        if (entry->locked_by_name)
478
 
          table->locked++;
479
 
        break;
 
475
        if (entry->in_use)
 
476
          table->in_use++;
 
477
        if (entry->locked_by_name)
 
478
          table->locked++;
 
479
        break;
480
480
      }
481
481
    }
482
482
    if (table)
483
483
      continue;
484
484
    if (!(*start_list = (OPEN_TableList *)
485
 
          sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
 
485
          sql_alloc(sizeof(**start_list)+share->table_cache_key.length)))
486
486
    {
487
487
      open_list=0;                              // Out of memory
488
488
      break;
489
489
    }
490
490
    strcpy((*start_list)->table=
491
491
           strcpy(((*start_list)->db= (char*) ((*start_list)+1)),
492
 
           share->db.str)+share->db.length+1,
 
492
                  share->db.str)+share->db.length+1,
493
493
           share->table_name.str);
494
494
    (*start_list)->in_use= entry->in_use ? 1 : 0;
495
495
    (*start_list)->locked= entry->locked_by_name ? 1 : 0;
517
517
  Remove table from the open table cache
518
518
 
519
519
  SYNOPSIS
520
 
    free_cache_entry()
521
 
    entry               Table to remove
 
520
  free_cache_entry()
 
521
  entry         Table to remove
522
522
 
523
523
  NOTE
524
 
    We need to have a lock on LOCK_open when calling this
 
524
  We need to have a lock on LOCK_open when calling this
525
525
*/
526
526
 
527
527
void free_cache_entry(void *entry)
536
536
    {
537
537
      unused_tables=unused_tables->next;
538
538
      if (table == unused_tables)
539
 
        unused_tables=0;
 
539
        unused_tables=0;
540
540
    }
541
541
  }
542
542
  free(table);
565
565
  @param have_lock If LOCK_open is locked
566
566
  @param wait_for_refresh Wait for a impending flush
567
567
  @param wait_for_placeholders Wait for tables being reopened so that the GRL
568
 
         won't proceed while write-locked tables are being reopened by other
569
 
         threads.
 
568
  won't proceed while write-locked tables are being reopened by other
 
569
  threads.
570
570
 
571
571
  @remark Session can be NULL, but then wait_for_refresh must be false
572
 
          and tables must be NULL.
 
572
  and tables must be NULL.
573
573
*/
574
574
 
575
575
bool close_cached_tables(Session *session, TableList *tables, bool have_lock,
587
587
    {
588
588
#ifdef EXTRA_DEBUG
589
589
      if (hash_delete(&open_cache,(unsigned char*) unused_tables))
590
 
        printf("Warning: Couldn't delete open table from hash\n");
 
590
        printf("Warning: Couldn't delete open table from hash\n");
591
591
#else
592
592
      hash_delete(&open_cache,(unsigned char*) unused_tables);
593
593
#endif
646
646
    {
647
647
      if (remove_table_from_cache(session, table->db, table->table_name,
648
648
                                  RTFC_OWNED_BY_Session_FLAG))
649
 
        found=1;
 
649
        found=1;
650
650
    }
651
651
    if (!found)
652
652
      wait_for_refresh=0;                       // Nothing to wait for
671
671
      found=0;
672
672
      for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
673
673
      {
674
 
        Table *table=(Table*) hash_element(&open_cache,idx);
 
674
        Table *table=(Table*) hash_element(&open_cache,idx);
675
675
        /* Avoid a self-deadlock. */
676
676
        if (table->in_use == session)
677
677
          continue;
680
680
          not for placeholders with Table::open_placeholder set. Waiting for
681
681
          latter will cause deadlock in the following scenario, for example:
682
682
 
683
 
          conn1: lock table t1 write;
684
 
          conn2: lock table t2 write;
685
 
          conn1: flush tables;
686
 
          conn2: flush tables;
 
683
conn1: lock table t1 write;
 
684
conn2: lock table t2 write;
 
685
conn1: flush tables;
 
686
conn2: flush tables;
687
687
 
688
 
          It also does not make sense to wait for those of placeholders that
689
 
          are employed by CREATE TABLE as in this case table simply does not
690
 
          exist yet.
 
688
It also does not make sense to wait for those of placeholders that
 
689
are employed by CREATE TABLE as in this case table simply does not
 
690
exist yet.
691
691
        */
692
 
        if (table->needs_reopen_or_name_lock() && (table->db_stat ||
693
 
            (table->open_placeholder && wait_for_placeholders)))
694
 
        {
695
 
          found=1;
696
 
          pthread_cond_wait(&COND_refresh,&LOCK_open);
697
 
          break;
698
 
        }
 
692
        if (table->needs_reopen_or_name_lock() && (table->db_stat ||
 
693
                                                   (table->open_placeholder && wait_for_placeholders)))
 
694
        {
 
695
          found=1;
 
696
          pthread_cond_wait(&COND_refresh,&LOCK_open);
 
697
          break;
 
698
        }
699
699
      }
700
700
    }
701
701
    /*
771
771
    tmp.next_local= tables;
772
772
 
773
773
    tables= (TableList *) memdup_root(session->mem_root, (char*)&tmp,
774
 
                                       sizeof(TableList));
 
774
                                      sizeof(TableList));
775
775
  }
776
776
 
777
777
  if (tables)
872
872
  Find table in list.
873
873
 
874
874
  SYNOPSIS
875
 
    find_table_in_list()
876
 
    table               Pointer to table list
877
 
    offset              Offset to which list in table structure to use
878
 
    db_name             Data base name
879
 
    table_name          Table name
880
 
 
881
 
  NOTES:
882
 
    This is called by find_table_in_local_list() and
883
 
    find_table_in_global_list().
884
 
 
885
 
  RETURN VALUES
886
 
    NULL        Table not found
887
 
    #           Pointer to found table.
 
875
  find_table_in_list()
 
876
  table         Pointer to table list
 
877
  offset                Offset to which list in table structure to use
 
878
  db_name               Data base name
 
879
  table_name            Table name
 
880
 
 
881
NOTES:
 
882
This is called by find_table_in_local_list() and
 
883
find_table_in_global_list().
 
884
 
 
885
RETURN VALUES
 
886
NULL    Table not found
 
887
#               Pointer to found table.
888
888
*/
889
889
 
890
890
TableList *find_table_in_list(TableList *table,
891
 
                               TableList *TableList::*link,
892
 
                               const char *db_name,
893
 
                               const char *table_name)
 
891
                              TableList *TableList::*link,
 
892
                              const char *db_name,
 
893
                              const char *table_name)
894
894
{
895
895
  for (; table; table= table->*link )
896
896
  {
907
907
  Test that table is unique (It's only exists once in the table list)
908
908
 
909
909
  SYNOPSIS
910
 
    unique_table()
911
 
    session                   thread handle
912
 
    table                 table which should be checked
913
 
    table_list            list of tables
914
 
    check_alias           whether to check tables' aliases
915
 
 
916
 
  NOTE: to exclude derived tables from check we use following mechanism:
917
 
    a) during derived table processing set Session::derived_tables_processing
918
 
    b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
919
 
       Session::derived_tables_processing set. (we can't use JOIN::execute
920
 
       because for PS we perform only JOIN::prepare, but we can't set this
921
 
       flag in JOIN::prepare if we are not sure that we are in derived table
922
 
       processing loop, because multi-update call fix_fields() for some its
923
 
       items (which mean JOIN::prepare for subqueries) before unique_table
924
 
       call to detect which tables should be locked for write).
925
 
    c) unique_table skip all tables which belong to SELECT with
926
 
       SELECT::exclude_from_table_unique_test set.
927
 
    Also SELECT::exclude_from_table_unique_test used to exclude from check
928
 
    tables of main SELECT of multi-delete and multi-update
929
 
 
930
 
    We also skip tables with TableList::prelocking_placeholder set,
931
 
    because we want to allow SELECTs from them, and their modification
932
 
    will rise the error anyway.
933
 
 
934
 
    TODO: when we will have table/view change detection we can do this check
935
 
          only once for PS/SP
936
 
 
937
 
  RETURN
938
 
    found duplicate
939
 
    0 if table is unique
 
910
  unique_table()
 
911
  session                   thread handle
 
912
  table                 table which should be checked
 
913
  table_list            list of tables
 
914
  check_alias           whether to check tables' aliases
 
915
 
 
916
NOTE: to exclude derived tables from check we use following mechanism:
 
917
a) during derived table processing set Session::derived_tables_processing
 
918
b) JOIN::prepare set SELECT::exclude_from_table_unique_test if
 
919
Session::derived_tables_processing set. (we can't use JOIN::execute
 
920
because for PS we perform only JOIN::prepare, but we can't set this
 
921
flag in JOIN::prepare if we are not sure that we are in derived table
 
922
processing loop, because multi-update call fix_fields() for some its
 
923
items (which mean JOIN::prepare for subqueries) before unique_table
 
924
call to detect which tables should be locked for write).
 
925
c) unique_table skip all tables which belong to SELECT with
 
926
SELECT::exclude_from_table_unique_test set.
 
927
Also SELECT::exclude_from_table_unique_test used to exclude from check
 
928
tables of main SELECT of multi-delete and multi-update
 
929
 
 
930
We also skip tables with TableList::prelocking_placeholder set,
 
931
because we want to allow SELECTs from them, and their modification
 
932
will rise the error anyway.
 
933
 
 
934
TODO: when we will have table/view change detection we can do this check
 
935
only once for PS/SP
 
936
 
 
937
RETURN
 
938
found duplicate
 
939
0 if table is unique
940
940
*/
941
941
 
942
942
TableList* unique_table(Session *session, TableList *table, TableList *table_list,
943
 
                         bool check_alias)
 
943
                        bool check_alias)
944
944
{
945
945
  TableList *res;
946
946
  const char *d_name, *t_name, *t_alias;
995
995
  prevent some update operation
996
996
 
997
997
  SYNOPSIS
998
 
    update_non_unique_table_error()
999
 
    update      table which we try to update
1000
 
    operation   name of update operation
1001
 
    duplicate   duplicate table which we found
 
998
  update_non_unique_table_error()
 
999
  update      table which we try to update
 
1000
  operation   name of update operation
 
1001
  duplicate   duplicate table which we found
1002
1002
 
1003
 
  NOTE:
1004
 
    here we hide view underlying tables if we have them
 
1003
NOTE:
 
1004
here we hide view underlying tables if we have them
1005
1005
*/
1006
1006
 
1007
1007
void update_non_unique_table_error(TableList *update,
1032
1032
  for (table=session->temporary_tables ; table ; table= table->next)
1033
1033
  {
1034
1034
    if (table->s->table_cache_key.length == key_length &&
1035
 
        !memcmp(table->s->table_cache_key.str, key, key_length))
 
1035
        !memcmp(table->s->table_cache_key.str, key, key_length))
1036
1036
      return(table);
1037
1037
  }
1038
1038
  return(0);                               // Not a temporary table
1044
1044
 
1045
1045
  Try to locate the table in the list of session->temporary_tables.
1046
1046
  If the table is found:
1047
 
   - if the table is being used by some outer statement, fail.
1048
 
   - if the table is in session->locked_tables, unlock it and
1049
 
     remove it from the list of locked tables. Currently only transactional
1050
 
     temporary tables are present in the locked_tables list.
1051
 
   - Close the temporary table, remove its .FRM
1052
 
   - remove the table from the list of temporary tables
 
1047
  - if the table is being used by some outer statement, fail.
 
1048
  - if the table is in session->locked_tables, unlock it and
 
1049
  remove it from the list of locked tables. Currently only transactional
 
1050
  temporary tables are present in the locked_tables list.
 
1051
  - Close the temporary table, remove its .FRM
 
1052
  - remove the table from the list of temporary tables
1053
1053
 
1054
1054
  This function is used to drop user temporary tables, as well as
1055
1055
  internal tables created in CREATE TEMPORARY TABLE ... SELECT
1061
1061
 
1062
1062
  @retval  0  the table was found and dropped successfully.
1063
1063
  @retval  1  the table was not found in the list of temporary tables
1064
 
              of this thread
 
1064
  of this thread
1065
1065
  @retval -1  the table is in use by a outer query
1066
1066
*/
1067
1067
 
1123
1123
  Close and delete a temporary table
1124
1124
 
1125
1125
  NOTE
1126
 
    This dosn't unlink table from session->temporary
1127
 
    If this is needed, use close_temporary_table()
 
1126
  This dosn't unlink table from session->temporary
 
1127
  If this is needed, use close_temporary_table()
1128
1128
*/
1129
1129
 
1130
1130
void close_temporary(Table *table, bool free_share, bool delete_table)
1173
1173
}
1174
1174
 
1175
1175
 
1176
 
        /* move table first in unused links */
 
1176
/* move table first in unused links */
1177
1177
 
1178
1178
static void relink_unused(Table *table)
1179
1179
{
1191
1191
 
1192
1192
 
1193
1193
/**
1194
 
    Remove all instances of table from thread's open list and
1195
 
    table cache.
1196
 
 
1197
 
    @param  session     Thread context
1198
 
    @param  find    Table to remove
1199
 
    @param  unlock  true  - free all locks on tables removed that are
1200
 
                            done with LOCK TABLES
1201
 
                    false - otherwise
1202
 
 
1203
 
    @note When unlock parameter is false or current thread doesn't have
1204
 
          any tables locked with LOCK TABLES, tables are assumed to be
1205
 
          not locked (for example already unlocked).
 
1194
  Remove all instances of table from thread's open list and
 
1195
  table cache.
 
1196
 
 
1197
  @param  session     Thread context
 
1198
  @param  find    Table to remove
 
1199
  @param  unlock  true  - free all locks on tables removed that are
 
1200
  done with LOCK TABLES
 
1201
  false - otherwise
 
1202
 
 
1203
  @note When unlock parameter is false or current thread doesn't have
 
1204
  any tables locked with LOCK TABLES, tables are assumed to be
 
1205
  not locked (for example already unlocked).
1206
1206
*/
1207
1207
 
1208
1208
void unlink_open_table(Session *session, Table *find, bool unlock)
1226
1226
    list= *prev;
1227
1227
 
1228
1228
    if (list->s->table_cache_key.length == key_length &&
1229
 
        !memcmp(list->s->table_cache_key.str, key, key_length))
 
1229
        !memcmp(list->s->table_cache_key.str, key, key_length))
1230
1230
    {
1231
1231
      if (unlock && session->locked_tables)
1232
1232
        mysql_lock_remove(session, session->locked_tables, list, true);
1250
1250
 
1251
1251
 
1252
1252
/**
1253
 
    Auxiliary routine which closes and drops open table.
1254
 
 
1255
 
    @param  session         Thread handle
1256
 
    @param  table       Table object for table to be dropped
1257
 
    @param  db_name     Name of database for this table
1258
 
    @param  table_name  Name of this table
1259
 
 
1260
 
    @note This routine assumes that table to be closed is open only
1261
 
          by calling thread so we needn't wait until other threads
1262
 
          will close the table. Also unless called under implicit or
1263
 
          explicit LOCK TABLES mode it assumes that table to be
1264
 
          dropped is already unlocked. In the former case it will
1265
 
          also remove lock on the table. But one should not rely on
1266
 
          this behaviour as it may change in future.
1267
 
          Currently, however, this function is never called for a
1268
 
          table that was locked with LOCK TABLES.
 
1253
  Auxiliary routine which closes and drops open table.
 
1254
 
 
1255
  @param  session         Thread handle
 
1256
  @param  table       Table object for table to be dropped
 
1257
  @param  db_name     Name of database for this table
 
1258
  @param  table_name  Name of this table
 
1259
 
 
1260
  @note This routine assumes that table to be closed is open only
 
1261
  by calling thread so we needn't wait until other threads
 
1262
  will close the table. Also unless called under implicit or
 
1263
  explicit LOCK TABLES mode it assumes that table to be
 
1264
  dropped is already unlocked. In the former case it will
 
1265
  also remove lock on the table. But one should not rely on
 
1266
  this behaviour as it may change in future.
 
1267
  Currently, however, this function is never called for a
 
1268
  table that was locked with LOCK TABLES.
1269
1269
*/
1270
1270
 
1271
1271
void drop_open_table(Session *session, Table *table, const char *db_name,
1289
1289
 
1290
1290
 
1291
1291
/*
1292
 
   Wait for condition but allow the user to send a kill to mysqld
 
1292
  Wait for condition but allow the user to send a kill to mysqld
1293
1293
 
1294
 
   SYNOPSIS
1295
 
     wait_for_condition()
1296
 
     session    Thread handler
1297
 
     mutex      mutex that is currently hold that is associated with condition
1298
 
                Will be unlocked on return
1299
 
     cond       Condition to wait for
 
1294
  SYNOPSIS
 
1295
  wait_for_condition()
 
1296
  session       Thread handler
 
1297
  mutex mutex that is currently hold that is associated with condition
 
1298
  Will be unlocked on return
 
1299
  cond  Condition to wait for
1300
1300
*/
1301
1301
 
1302
1302
void wait_for_condition(Session *session, pthread_mutex_t *mutex, pthread_cond_t *cond)
1368
1368
  Open table which is already name-locked by this thread.
1369
1369
 
1370
1370
  SYNOPSIS
1371
 
    reopen_name_locked_table()
1372
 
      session         Thread handle
1373
 
      table_list  TableList object for table to be open, TableList::table
1374
 
                  member should point to Table object which was used for
1375
 
                  name-locking.
1376
 
      link_in     true  - if Table object for table to be opened should be
1377
 
                          linked into Session::open_tables list.
1378
 
                  false - placeholder used for name-locking is already in
1379
 
                          this list so we only need to preserve Table::next
1380
 
                          pointer.
 
1371
  reopen_name_locked_table()
 
1372
  session         Thread handle
 
1373
  table_list  TableList object for table to be open, TableList::table
 
1374
  member should point to Table object which was used for
 
1375
  name-locking.
 
1376
  link_in     true  - if Table object for table to be opened should be
 
1377
  linked into Session::open_tables list.
 
1378
  false - placeholder used for name-locking is already in
 
1379
  this list so we only need to preserve Table::next
 
1380
  pointer.
1381
1381
 
1382
1382
  NOTE
1383
 
    This function assumes that its caller already acquired LOCK_open mutex.
 
1383
  This function assumes that its caller already acquired LOCK_open mutex.
1384
1384
 
1385
1385
  RETURN VALUE
1386
 
    false - Success
1387
 
    true  - Error
 
1386
  false - Success
 
1387
  true  - Error
1388
1388
*/
1389
1389
 
1390
1390
bool reopen_name_locked_table(Session* session, TableList* table_list, bool link_in)
1454
1454
 
1455
1455
 
1456
1456
/**
1457
 
    Create and insert into table cache placeholder for table
1458
 
    which will prevent its opening (or creation) (a.k.a lock
1459
 
    table name).
1460
 
 
1461
 
    @param session         Thread context
1462
 
    @param key         Table cache key for name to be locked
1463
 
    @param key_length  Table cache key length
1464
 
 
1465
 
    @return Pointer to Table object used for name locking or 0 in
1466
 
            case of failure.
 
1457
  Create and insert into table cache placeholder for table
 
1458
  which will prevent its opening (or creation) (a.k.a lock
 
1459
  table name).
 
1460
 
 
1461
  @param session         Thread context
 
1462
  @param key         Table cache key for name to be locked
 
1463
  @param key_length  Table cache key length
 
1464
 
 
1465
  @return Pointer to Table object used for name locking or 0 in
 
1466
  case of failure.
1467
1467
*/
1468
1468
 
1469
1469
Table *table_cache_insert_placeholder(Session *session, const char *key,
1504
1504
 
1505
1505
 
1506
1506
/**
1507
 
    Obtain an exclusive name lock on the table if it is not cached
1508
 
    in the table cache.
1509
 
 
1510
 
    @param      session         Thread context
1511
 
    @param      db          Name of database
1512
 
    @param      table_name  Name of table
1513
 
    @param[out] table       Out parameter which is either:
1514
 
                            - set to NULL if table cache contains record for
1515
 
                              the table or
1516
 
                            - set to point to the Table instance used for
1517
 
                              name-locking.
1518
 
 
1519
 
    @note This function takes into account all records for table in table
1520
 
          cache, even placeholders used for name-locking. This means that
1521
 
          'table' parameter can be set to NULL for some situations when
1522
 
          table does not really exist.
1523
 
 
1524
 
    @retval  true   Error occured (OOM)
1525
 
    @retval  false  Success. 'table' parameter set according to above rules.
 
1507
  Obtain an exclusive name lock on the table if it is not cached
 
1508
  in the table cache.
 
1509
 
 
1510
  @param      session         Thread context
 
1511
  @param      db          Name of database
 
1512
  @param      table_name  Name of table
 
1513
  @param[out] table       Out parameter which is either:
 
1514
  - set to NULL if table cache contains record for
 
1515
  the table or
 
1516
  - set to point to the Table instance used for
 
1517
  name-locking.
 
1518
 
 
1519
  @note This function takes into account all records for table in table
 
1520
  cache, even placeholders used for name-locking. This means that
 
1521
  'table' parameter can be set to NULL for some situations when
 
1522
  table does not really exist.
 
1523
 
 
1524
  @retval  true   Error occured (OOM)
 
1525
  @retval  false  Success. 'table' parameter set according to above rules.
1526
1526
*/
1527
1527
 
1528
1528
bool lock_table_name_if_not_cached(Session *session, const char *db,
1560
1560
  Open a table.
1561
1561
 
1562
1562
  SYNOPSIS
1563
 
    open_table()
1564
 
    session                 Thread context.
1565
 
    table_list          Open first table in list.
1566
 
    refresh      INOUT  Pointer to memory that will be set to 1 if
1567
 
                        we need to close all tables and reopen them.
1568
 
                        If this is a NULL pointer, then the table is not
1569
 
                        put in the thread-open-list.
1570
 
    flags               Bitmap of flags to modify how open works:
1571
 
                          DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
1572
 
                          someone has done a flush or namelock on it.
1573
 
                          No version number checking is done.
1574
 
                          DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
1575
 
                          table not the base table or view.
 
1563
  open_table()
 
1564
  session                 Thread context.
 
1565
  table_list          Open first table in list.
 
1566
  refresh      INOUT  Pointer to memory that will be set to 1 if
 
1567
  we need to close all tables and reopen them.
 
1568
  If this is a NULL pointer, then the table is not
 
1569
  put in the thread-open-list.
 
1570
  flags               Bitmap of flags to modify how open works:
 
1571
  DRIZZLE_LOCK_IGNORE_FLUSH - Open table even if
 
1572
  someone has done a flush or namelock on it.
 
1573
  No version number checking is done.
 
1574
  DRIZZLE_OPEN_TEMPORARY_ONLY - Open only temporary
 
1575
  table not the base table or view.
1576
1576
 
1577
1577
  IMPLEMENTATION
1578
 
    Uses a cache of open tables to find a table not in use.
 
1578
  Uses a cache of open tables to find a table not in use.
1579
1579
 
1580
 
    If table list element for the table to be opened has "create" flag
1581
 
    set and table does not exist, this function will automatically insert
1582
 
    a placeholder for exclusive name lock into the open tables cache and
1583
 
    will return the Table instance that corresponds to this placeholder.
 
1580
  If table list element for the table to be opened has "create" flag
 
1581
  set and table does not exist, this function will automatically insert
 
1582
  a placeholder for exclusive name lock into the open tables cache and
 
1583
  will return the Table instance that corresponds to this placeholder.
1584
1584
 
1585
1585
  RETURN
1586
 
    NULL  Open failed.  If refresh is set then one should close
1587
 
          all other tables and retry the open.
1588
 
    #     Success. Pointer to Table object for open table.
 
1586
  NULL  Open failed.  If refresh is set then one should close
 
1587
  all other tables and retry the open.
 
1588
#     Success. Pointer to Table object for open table.
1589
1589
*/
1590
1590
 
1591
1591
 
1618
1618
    of temporary tables of this thread. In MySQL temporary tables
1619
1619
    are always thread-local and "shadow" possible base tables with the
1620
1620
    same name. This block implements the behaviour.
1621
 
    TODO: move this block into a separate function.
 
1621
TODO: move this block into a separate function.
1622
1622
  */
1623
1623
  {
1624
1624
    for (table= session->temporary_tables; table ; table=table->next)
1653
1653
    mode, let's try to find the requested table in the list of pre-opened
1654
1654
    and locked tables. If the table is not there, return an error - we can't
1655
1655
    open not pre-opened tables in pre-locked/LOCK TABLES mode.
1656
 
    TODO: move this block into a separate function.
 
1656
TODO: move this block into a separate function.
1657
1657
  */
1658
1658
  if (session->locked_tables)
1659
1659
  { // Using table locks
1740
1740
    Now we should:
1741
1741
    - try to find the table in the table cache.
1742
1742
    - if one of the discovered Table instances is name-locked
1743
 
      (table->s->version == 0) or some thread has started FLUSH TABLES
1744
 
      (refresh_version > table->s->version), back off -- we have to wait
1745
 
      until no one holds a name lock on the table.
 
1743
    (table->s->version == 0) or some thread has started FLUSH TABLES
 
1744
    (refresh_version > table->s->version), back off -- we have to wait
 
1745
    until no one holds a name lock on the table.
1746
1746
    - if there is no such Table in the name cache, read the table definition
1747
1747
    and insert it into the cache.
1748
1748
    We perform all of the above under LOCK_open which currently protects
1758
1758
    If the version changes while we're opening the remaining tables,
1759
1759
    we will have to back off, close all the tables opened-so-far,
1760
1760
    and try to reopen them.
1761
 
    Note: refresh_version is currently changed only during FLUSH TABLES.
 
1761
Note: refresh_version is currently changed only during FLUSH TABLES.
1762
1762
  */
1763
1763
  if (!session->open_tables)
1764
1764
    session->version=refresh_version;
1801
1801
      need to back off and re-start opening tables.
1802
1802
      If we do not back off now, we may dead lock in case of lock
1803
1803
      order mismatch with some other thread:
1804
 
      c1: name lock t1; -- sort of exclusive lock
1805
 
      c2: open t2;      -- sort of shared lock
1806
 
      c1: name lock t2; -- blocks
1807
 
      c2: open t1; -- blocks
 
1804
c1: name lock t1; -- sort of exclusive lock
 
1805
c2: open t2;      -- sort of shared lock
 
1806
c1: name lock t2; -- blocks
 
1807
c2: open t1; -- blocks
1808
1808
    */
1809
1809
    if (table->needs_reopen_or_name_lock())
1810
1810
    {
1894
1894
    if (table_list->create)
1895
1895
    {
1896
1896
      if (ha_table_exists_in_engine(session, table_list->db,
1897
 
                                   table_list->table_name)
 
1897
                                    table_list->table_name)
1898
1898
          != HA_ERR_TABLE_EXIST)
1899
1899
      {
1900
1900
        /*
1945
1945
  }
1946
1946
  table->reginfo.lock_type=TL_READ; /* Assume read */
1947
1947
 
1948
 
 reset:
 
1948
reset:
1949
1949
  assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
1950
1950
 
1951
1951
  if (session->lex->need_correct_ident())
2003
2003
  Reopen an table because the definition has changed.
2004
2004
 
2005
2005
  SYNOPSIS
2006
 
    reopen_table()
2007
 
    table       Table object
 
2006
  reopen_table()
 
2007
  table Table object
2008
2008
 
2009
2009
  NOTES
2010
 
   The data file for the table is already closed and the share is released
2011
 
   The table has a 'dummy' share that mainly contains database and table name.
 
2010
  The data file for the table is already closed and the share is released
 
2011
  The table has a 'dummy' share that mainly contains database and table name.
2012
2012
 
2013
 
 RETURN
2014
 
   0  ok
2015
 
   1  error. The old table object is not changed.
 
2013
  RETURN
 
2014
  0  ok
 
2015
  1  error. The old table object is not changed.
2016
2016
*/
2017
2017
 
2018
2018
bool reopen_table(Table *table)
2030
2030
#ifdef EXTRA_DEBUG
2031
2031
  if (table->db_stat)
2032
2032
    errmsg_printf(ERRMSG_LVL_ERROR, _("Table %s had a open data handler in reopen_table"),
2033
 
                    table->alias);
 
2033
                  table->alias);
2034
2034
#endif
2035
2035
  table_list.db=         table->s->db.str;
2036
2036
  table_list.table_name= table->s->table_name.str;
2040
2040
    return(1);                             // Thread was killed
2041
2041
 
2042
2042
  if (open_unireg_entry(session, &tmp, &table_list,
2043
 
                        table->alias,
 
2043
                        table->alias,
2044
2044
                        table->s->table_cache_key.str,
2045
2045
                        table->s->table_cache_key.length))
2046
2046
    goto end;
2088
2088
  broadcast_refresh();
2089
2089
  error=0;
2090
2090
 
2091
 
 end:
 
2091
end:
2092
2092
  return(error);
2093
2093
}
2094
2094
 
2095
2095
 
2096
2096
/**
2097
 
    Close all instances of a table open by this thread and replace
2098
 
    them with exclusive name-locks.
2099
 
 
2100
 
    @param session        Thread context
2101
 
    @param db         Database name for the table to be closed
2102
 
    @param table_name Name of the table to be closed
2103
 
 
2104
 
    @note This function assumes that if we are not under LOCK TABLES,
2105
 
          then there is only one table open and locked. This means that
2106
 
          the function probably has to be adjusted before it can be used
2107
 
          anywhere outside ALTER Table.
2108
 
 
2109
 
    @note Must not use TableShare::table_name/db of the table being closed,
2110
 
          the strings are used in a loop even after the share may be freed.
 
2097
  Close all instances of a table open by this thread and replace
 
2098
  them with exclusive name-locks.
 
2099
 
 
2100
  @param session        Thread context
 
2101
  @param db         Database name for the table to be closed
 
2102
  @param table_name Name of the table to be closed
 
2103
 
 
2104
  @note This function assumes that if we are not under LOCK TABLES,
 
2105
  then there is only one table open and locked. This means that
 
2106
  the function probably has to be adjusted before it can be used
 
2107
  anywhere outside ALTER Table.
 
2108
 
 
2109
  @note Must not use TableShare::table_name/db of the table being closed,
 
2110
  the strings are used in a loop even after the share may be freed.
2111
2111
*/
2112
2112
 
2113
2113
void close_data_files_and_morph_locks(Session *session, const char *db,
2135
2135
  for (table=session->open_tables; table ; table=table->next)
2136
2136
  {
2137
2137
    if (!strcmp(table->s->table_name.str, table_name) &&
2138
 
        !strcmp(table->s->db.str, db))
 
2138
        !strcmp(table->s->db.str, db))
2139
2139
    {
2140
2140
      if (session->locked_tables)
2141
2141
      {
2150
2150
 
2151
2151
 
2152
2152
/**
2153
 
    Reopen all tables with closed data files.
2154
 
 
2155
 
    @param session         Thread context
2156
 
    @param get_locks   Should we get locks after reopening tables ?
2157
 
    @param mark_share_as_old  Mark share as old to protect from a impending
2158
 
                              global read lock.
2159
 
 
2160
 
    @note Since this function can't properly handle prelocking and
2161
 
          create placeholders it should be used in very special
2162
 
          situations like FLUSH TABLES or ALTER Table. In general
2163
 
          case one should just repeat open_tables()/lock_tables()
2164
 
          combination when one needs tables to be reopened (for
2165
 
          example see open_and_lock_tables()).
2166
 
 
2167
 
    @note One should have lock on LOCK_open when calling this.
2168
 
 
2169
 
    @return false in case of success, true - otherwise.
 
2153
  Reopen all tables with closed data files.
 
2154
 
 
2155
  @param session         Thread context
 
2156
  @param get_locks   Should we get locks after reopening tables ?
 
2157
  @param mark_share_as_old  Mark share as old to protect from a impending
 
2158
  global read lock.
 
2159
 
 
2160
  @note Since this function can't properly handle prelocking and
 
2161
  create placeholders it should be used in very special
 
2162
  situations like FLUSH TABLES or ALTER Table. In general
 
2163
  case one should just repeat open_tables()/lock_tables()
 
2164
  combination when one needs tables to be reopened (for
 
2165
  example see open_and_lock_tables()).
 
2166
 
 
2167
  @note One should have lock on LOCK_open when calling this.
 
2168
 
 
2169
  @return false in case of success, true - otherwise.
2170
2170
*/
2171
2171
 
2172
2172
bool reopen_tables(Session *session, bool get_locks, bool mark_share_as_old)
2175
2175
  Table **tables,**tables_ptr;                  // For locks
2176
2176
  bool error=0, not_used;
2177
2177
  const uint32_t flags= DRIZZLE_LOCK_NOTIFY_IF_NEED_REOPEN |
2178
 
                    DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
2179
 
                    DRIZZLE_LOCK_IGNORE_FLUSH;
 
2178
    DRIZZLE_LOCK_IGNORE_GLOBAL_READ_LOCK |
 
2179
    DRIZZLE_LOCK_IGNORE_FLUSH;
2180
2180
 
2181
2181
  if (!session->open_tables)
2182
2182
    return(0);
2214
2214
      prev= &table->next;
2215
2215
      /* Do not handle locks of MERGE children. */
2216
2216
      if (get_locks && !db_stat)
2217
 
        *tables_ptr++= table;                   // need new lock on this
 
2217
        *tables_ptr++= table;                   // need new lock on this
2218
2218
      if (mark_share_as_old)
2219
2219
      {
2220
 
        table->s->version=0;
2221
 
        table->open_placeholder= 0;
 
2220
        table->s->version=0;
 
2221
        table->open_placeholder= 0;
2222
2222
      }
2223
2223
    }
2224
2224
  }
2258
2258
 
2259
2259
 
2260
2260
/**
2261
 
    Close handlers for tables in list, but leave the Table structure
2262
 
    intact so that we can re-open these quickly.
 
2261
  Close handlers for tables in list, but leave the Table structure
 
2262
  intact so that we can re-open these quickly.
2263
2263
 
2264
 
    @param session           Thread context
2265
 
    @param table         Head of the list of Table objects
2266
 
    @param morph_locks   true  - remove locks which we have on tables being closed
2267
 
                                 but ensure that no DML or DDL will sneak in before
2268
 
                                 we will re-open the table (i.e. temporarily morph
2269
 
                                 our table-level locks into name-locks).
2270
 
                         false - otherwise
2271
 
    @param send_refresh  Should we awake waiters even if we didn't close any tables?
 
2264
  @param session           Thread context
 
2265
  @param table         Head of the list of Table objects
 
2266
  @param morph_locks   true  - remove locks which we have on tables being closed
 
2267
  but ensure that no DML or DDL will sneak in before
 
2268
  we will re-open the table (i.e. temporarily morph
 
2269
  our table-level locks into name-locks).
 
2270
  false - otherwise
 
2271
  @param send_refresh  Should we awake waiters even if we didn't close any tables?
2272
2272
*/
2273
2273
 
2274
2274
static void close_old_data_files(Session *session, Table *table, bool morph_locks,
2286
2286
      found=1;
2287
2287
      if (table->db_stat)
2288
2288
      {
2289
 
        if (morph_locks)
2290
 
        {
 
2289
        if (morph_locks)
 
2290
        {
2291
2291
          Table *ulcktbl= table;
2292
2292
          if (ulcktbl->lock_count)
2293
2293
          {
2316
2316
            We want to protect the table from concurrent DDL operations
2317
2317
            (like RENAME Table) until we will re-open and re-lock it.
2318
2318
          */
2319
 
          table->open_placeholder= 1;
2320
 
        }
 
2319
          table->open_placeholder= 1;
 
2320
        }
2321
2321
        close_handle_and_leave_table_as_lock(table);
2322
2322
      }
2323
2323
      else if (table->open_placeholder && !morph_locks)
2358
2358
 
2359
2359
    HASH_SEARCH_STATE state;
2360
2360
    for (Table *search= (Table*) hash_first(&open_cache, (unsigned char*) key,
2361
 
                                             key_length, &state);
2362
 
         search ;
 
2361
                                            key_length, &state);
 
2362
         search ;
2363
2363
         search= (Table*) hash_next(&open_cache, (unsigned char*) key,
2364
2364
                                    key_length, &state))
2365
2365
    {
2416
2416
  drop tables from locked list
2417
2417
 
2418
2418
  SYNOPSIS
2419
 
    drop_locked_tables()
2420
 
    session                     Thread thandler
2421
 
    db                  Database
2422
 
    table_name          Table name
 
2419
  drop_locked_tables()
 
2420
  session                       Thread thandler
 
2421
  db                    Database
 
2422
  table_name            Table name
2423
2423
 
2424
2424
  INFORMATION
2425
 
    This is only called on drop tables
 
2425
  This is only called on drop tables
2426
2426
 
2427
 
    The Table object for the dropped table is unlocked but still kept around
2428
 
    as a name lock, which means that the table will be available for other
2429
 
    thread as soon as we call unlock_table_names().
2430
 
    If there is multiple copies of the table locked, all copies except
2431
 
    the first, which acts as a name lock, is removed.
 
2427
  The Table object for the dropped table is unlocked but still kept around
 
2428
  as a name lock, which means that the table will be available for other
 
2429
  thread as soon as we call unlock_table_names().
 
2430
  If there is multiple copies of the table locked, all copies except
 
2431
  the first, which acts as a name lock, is removed.
2432
2432
 
2433
2433
  RETURN
2434
 
    #    If table existed, return table
2435
 
    0    Table was not locked
 
2434
#    If table existed, return table
 
2435
0        Table was not locked
2436
2436
*/
2437
2437
 
2438
2438
 
2452
2452
  {
2453
2453
    next=table->next;
2454
2454
    if (!strcmp(table->s->table_name.str, table_name) &&
2455
 
        !strcmp(table->s->db.str, db))
 
2455
        !strcmp(table->s->db.str, db))
2456
2456
    {
2457
2457
      mysql_lock_remove(session, session->locked_tables, table, true);
2458
2458
 
2502
2502
  for (table= session->open_tables; table ; table= table->next)
2503
2503
  {
2504
2504
    if (!strcmp(table->s->table_name.str, table_name) &&
2505
 
        !strcmp(table->s->db.str, db))
 
2505
        !strcmp(table->s->db.str, db))
2506
2506
    {
2507
2507
      /* If MERGE child, forward lock handling to parent. */
2508
2508
      mysql_lock_abort(session, table, true);
2515
2515
  Load a table definition from file and open unireg table
2516
2516
 
2517
2517
  SYNOPSIS
2518
 
    open_unireg_entry()
2519
 
    session                     Thread handle
2520
 
    entry               Store open table definition here
2521
 
    table_list          TableList with db, table_name
2522
 
    alias               Alias name
2523
 
    cache_key           Key for share_cache
2524
 
    cache_key_length    length of cache_key
 
2518
  open_unireg_entry()
 
2519
  session                       Thread handle
 
2520
  entry         Store open table definition here
 
2521
  table_list            TableList with db, table_name
 
2522
  alias         Alias name
 
2523
  cache_key             Key for share_cache
 
2524
  cache_key_length      length of cache_key
2525
2525
 
2526
2526
  NOTES
2527
 
   Extra argument for open is taken from session->open_options
2528
 
   One must have a lock on LOCK_open when calling this function
 
2527
  Extra argument for open is taken from session->open_options
 
2528
  One must have a lock on LOCK_open when calling this function
2529
2529
 
2530
2530
  RETURN
2531
 
    0   ok
2532
 
    #   Error
 
2531
  0     ok
 
2532
#       Error
2533
2533
*/
2534
2534
 
2535
2535
static int open_unireg_entry(Session *session, Table *entry, TableList *table_list,
2550
2550
 
2551
2551
  while ((error= open_table_from_share(session, share, alias,
2552
2552
                                       (uint32_t) (HA_OPEN_KEYFILE |
2553
 
                                               HA_OPEN_RNDFILE |
2554
 
                                               HA_GET_INDEX |
2555
 
                                               HA_TRY_READ_ONLY),
 
2553
                                                   HA_OPEN_RNDFILE |
 
2554
                                                   HA_GET_INDEX |
 
2555
                                                   HA_TRY_READ_ONLY),
2556
2556
                                       (EXTRA_RECORD),
2557
2557
                                       session->open_options, entry, OTM_OPEN)))
2558
2558
  {
2563
2563
        goto err;
2564
2564
 
2565
2565
      /*
2566
 
        TODO:
2567
 
        Here we should wait until all threads has released the table.
2568
 
        For now we do one retry. This may cause a deadlock if there
2569
 
        is other threads waiting for other tables used by this thread.
 
2566
TODO:
 
2567
Here we should wait until all threads has released the table.
 
2568
For now we do one retry. This may cause a deadlock if there
 
2569
is other threads waiting for other tables used by this thread.
2570
2570
 
2571
 
        Proper fix would be to if the second retry failed:
2572
 
        - Mark that table def changed
2573
 
        - Return from open table
2574
 
        - Close all tables used by this thread
2575
 
        - Start waiting that the share is released
2576
 
        - Retry by opening all tables again
 
2571
Proper fix would be to if the second retry failed:
 
2572
- Mark that table def changed
 
2573
- Return from open table
 
2574
- Close all tables used by this thread
 
2575
- Start waiting that the share is released
 
2576
- Retry by opening all tables again
2577
2577
      */
2578
2578
 
2579
2579
      /*
2595
2595
    }
2596
2596
    if (!entry->s || !entry->s->crashed)
2597
2597
      goto err;
2598
 
     // Code below is for repairing a crashed file
2599
 
     if ((error= lock_table_name(session, table_list, true)))
2600
 
     {
2601
 
       if (error < 0)
2602
 
        goto err;
2603
 
       if (wait_for_locked_table_names(session, table_list))
2604
 
       {
2605
 
        unlock_table_name(table_list);
2606
 
        goto err;
2607
 
       }
2608
 
     }
2609
 
     pthread_mutex_unlock(&LOCK_open);
2610
 
     session->clear_error();                            // Clear error message
2611
 
     error= 0;
2612
 
     if (open_table_from_share(session, share, alias,
2613
 
                               (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
2614
 
                                       HA_GET_INDEX |
2615
 
                                       HA_TRY_READ_ONLY),
2616
 
                               EXTRA_RECORD,
2617
 
                               ha_open_options | HA_OPEN_FOR_REPAIR,
2618
 
                               entry, OTM_OPEN) || ! entry->file ||
 
2598
    // Code below is for repairing a crashed file
 
2599
    if ((error= lock_table_name(session, table_list, true)))
 
2600
    {
 
2601
      if (error < 0)
 
2602
        goto err;
 
2603
      if (wait_for_locked_table_names(session, table_list))
 
2604
      {
 
2605
        unlock_table_name(table_list);
 
2606
        goto err;
 
2607
      }
 
2608
    }
 
2609
    pthread_mutex_unlock(&LOCK_open);
 
2610
    session->clear_error();                             // Clear error message
 
2611
    error= 0;
 
2612
    if (open_table_from_share(session, share, alias,
 
2613
                              (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
 
2614
                                          HA_GET_INDEX |
 
2615
                                          HA_TRY_READ_ONLY),
 
2616
                              EXTRA_RECORD,
 
2617
                              ha_open_options | HA_OPEN_FOR_REPAIR,
 
2618
                              entry, OTM_OPEN) || ! entry->file ||
2619
2619
        (entry->file->is_crashed() && entry->file->ha_check_and_repair(session)))
2620
 
     {
2621
 
       /* Give right error message */
2622
 
       session->clear_error();
2623
 
       my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
2624
 
       errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
2625
 
                       share->table_name.str);
2626
 
       if (entry->file)
2627
 
        entry->closefrm(false);
2628
 
       error=1;
2629
 
     }
2630
 
     else
2631
 
       session->clear_error();                  // Clear error message
2632
 
     pthread_mutex_lock(&LOCK_open);
2633
 
     unlock_table_name(table_list);
 
2620
    {
 
2621
      /* Give right error message */
 
2622
      session->clear_error();
 
2623
      my_error(ER_NOT_KEYFILE, MYF(0), share->table_name.str, my_errno);
 
2624
      errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
 
2625
                    share->table_name.str);
 
2626
      if (entry->file)
 
2627
        entry->closefrm(false);
 
2628
      error=1;
 
2629
    }
 
2630
    else
 
2631
      session->clear_error();                   // Clear error message
 
2632
    pthread_mutex_lock(&LOCK_open);
 
2633
    unlock_table_name(table_list);
2634
2634
 
2635
 
     if (error)
2636
 
       goto err;
2637
 
     break;
2638
 
   }
 
2635
    if (error)
 
2636
      goto err;
 
2637
    break;
 
2638
  }
2639
2639
 
2640
2640
  /*
2641
2641
    If we are here, there was no fatal error (but error may be still
2685
2685
  Open all tables in list
2686
2686
 
2687
2687
  SYNOPSIS
2688
 
    open_tables()
2689
 
    session - thread handler
2690
 
    start - list of tables in/out
2691
 
    counter - number of opened tables will be return using this parameter
2692
 
    flags   - bitmap of flags to modify how the tables will be open:
2693
 
              DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
2694
 
              done a flush or namelock on it.
 
2688
  open_tables()
 
2689
  session - thread handler
 
2690
  start - list of tables in/out
 
2691
  counter - number of opened tables will be return using this parameter
 
2692
  flags   - bitmap of flags to modify how the tables will be open:
 
2693
  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
2694
  done a flush or namelock on it.
2695
2695
 
2696
2696
  NOTE
2697
 
    Unless we are already in prelocked mode, this function will also precache
2698
 
    all SP/SFs explicitly or implicitly (via views and triggers) used by the
2699
 
    query and add tables needed for their execution to table list. If resulting
2700
 
    tables list will be non empty it will mark query as requiring precaching.
2701
 
    Prelocked mode will be enabled for such query during lock_tables() call.
 
2697
  Unless we are already in prelocked mode, this function will also precache
 
2698
  all SP/SFs explicitly or implicitly (via views and triggers) used by the
 
2699
  query and add tables needed for their execution to table list. If resulting
 
2700
  tables list will be non empty it will mark query as requiring precaching.
 
2701
  Prelocked mode will be enabled for such query during lock_tables() call.
2702
2702
 
2703
 
    If query for which we are opening tables is already marked as requiring
2704
 
    prelocking it won't do such precaching and will simply reuse table list
2705
 
    which is already built.
 
2703
  If query for which we are opening tables is already marked as requiring
 
2704
  prelocking it won't do such precaching and will simply reuse table list
 
2705
  which is already built.
2706
2706
 
2707
2707
  RETURN
2708
 
    0  - OK
2709
 
    -1 - error
 
2708
  0  - OK
 
2709
  -1 - error
2710
2710
*/
2711
2711
 
2712
2712
int open_tables(Session *session, TableList **start, uint32_t *counter, uint32_t flags)
2720
2720
 
2721
2721
  /*
2722
2722
    temporary mem_root for new .frm parsing.
2723
 
    TODO: variables for size
 
2723
TODO: variables for size
2724
2724
  */
2725
2725
  init_sql_alloc(&new_frm_mem, 8024, 8024);
2726
2726
 
2727
2727
  session->current_tablenr= 0;
2728
 
 restart:
 
2728
restart:
2729
2729
  *counter= 0;
2730
2730
  session->set_proc_info("Opening tables");
2731
2731
 
2742
2742
      processing, link to created temporary table will be put here.
2743
2743
      If this is derived table for view then we still want to process
2744
2744
      routines used by this view.
2745
 
     */
 
2745
    */
2746
2746
    if (tables->derived)
2747
2747
    {
2748
2748
      continue;
2789
2789
          currently.
2790
2790
        */
2791
2791
        close_tables_for_reopen(session, start);
2792
 
        goto restart;
 
2792
        goto restart;
2793
2793
      }
2794
2794
 
2795
2795
      if (safe_to_ignore_table)
2826
2826
  Check that lock is ok for tables; Call start stmt if ok
2827
2827
 
2828
2828
  SYNOPSIS
2829
 
    check_lock_and_start_stmt()
2830
 
    session                     Thread handle
2831
 
    table_list          Table to check
2832
 
    lock_type           Lock used for table
 
2829
  check_lock_and_start_stmt()
 
2830
  session                       Thread handle
 
2831
  table_list            Table to check
 
2832
  lock_type             Lock used for table
2833
2833
 
2834
2834
  RETURN VALUES
2835
2835
  0     ok
2837
2837
*/
2838
2838
 
2839
2839
static bool check_lock_and_start_stmt(Session *session, Table *table,
2840
 
                                      thr_lock_type lock_type)
 
2840
                                      thr_lock_type lock_type)
2841
2841
{
2842
2842
  int error;
2843
2843
 
2864
2864
  @param[in]    lock_type       lock to use for table
2865
2865
 
2866
2866
  @return       table
2867
 
    @retval     != NULL         OK, opened table returned
2868
 
    @retval     NULL            Error
2869
 
 
2870
 
  @note
2871
 
    If ok, the following are also set:
2872
 
      table_list->lock_type     lock_type
2873
 
      table_list->table         table
2874
 
 
2875
 
  @note
2876
 
    If table_l is a list, not a single table, the list is temporarily
2877
 
    broken.
 
2867
  @retval     != NULL         OK, opened table returned
 
2868
  @retval     NULL            Error
 
2869
 
 
2870
  @note
 
2871
  If ok, the following are also set:
 
2872
  table_list->lock_type         lock_type
 
2873
  table_list->table             table
 
2874
 
 
2875
  @note
 
2876
  If table_l is a list, not a single table, the list is temporarily
 
2877
  broken.
2878
2878
 
2879
2879
  @detail
2880
 
    This function is meant as a replacement for open_ltable() when
2881
 
    MERGE tables can be opened. open_ltable() cannot open MERGE tables.
 
2880
  This function is meant as a replacement for open_ltable() when
 
2881
  MERGE tables can be opened. open_ltable() cannot open MERGE tables.
2882
2882
 
2883
 
    There may be more differences between open_n_lock_single_table() and
2884
 
    open_ltable(). One known difference is that open_ltable() does
2885
 
    neither call decide_logging_format() nor handle some other logging
2886
 
    and locking issues because it does not call lock_tables().
 
2883
  There may be more differences between open_n_lock_single_table() and
 
2884
  open_ltable(). One known difference is that open_ltable() does
 
2885
  neither call decide_logging_format() nor handle some other logging
 
2886
  and locking issues because it does not call lock_tables().
2887
2887
*/
2888
2888
 
2889
2889
Table *open_n_lock_single_table(Session *session, TableList *table_l,
2914
2914
  Open and lock one table
2915
2915
 
2916
2916
  SYNOPSIS
2917
 
    open_ltable()
2918
 
    session                     Thread handler
2919
 
    table_list          Table to open is first table in this list
2920
 
    lock_type           Lock to use for open
2921
 
    lock_flags          Flags passed to mysql_lock_table
 
2917
  open_ltable()
 
2918
  session                       Thread handler
 
2919
  table_list            Table to open is first table in this list
 
2920
  lock_type             Lock to use for open
 
2921
  lock_flags          Flags passed to mysql_lock_table
2922
2922
 
2923
2923
  NOTE
2924
 
    This function don't do anything like SP/SF/views/triggers analysis done
2925
 
    in open_tables(). It is intended for opening of only one concrete table.
2926
 
    And used only in special contexts.
 
2924
  This function don't do anything like SP/SF/views/triggers analysis done
 
2925
  in open_tables(). It is intended for opening of only one concrete table.
 
2926
  And used only in special contexts.
2927
2927
 
2928
2928
  RETURN VALUES
2929
 
    table               Opened table
2930
 
    0                   Error
 
2929
  table         Opened table
 
2930
  0                     Error
2931
2931
 
2932
 
    If ok, the following are also set:
2933
 
      table_list->lock_type     lock_type
2934
 
      table_list->table         table
 
2932
  If ok, the following are also set:
 
2933
  table_list->lock_type         lock_type
 
2934
  table_list->table             table
2935
2935
*/
2936
2936
 
2937
2937
Table *open_ltable(Session *session, TableList *table_list, thr_lock_type lock_type,
2953
2953
    if (session->locked_tables)
2954
2954
    {
2955
2955
      if (check_lock_and_start_stmt(session, table, lock_type))
2956
 
        table= 0;
 
2956
        table= 0;
2957
2957
    }
2958
2958
    else
2959
2959
    {
2960
2960
      assert(session->lock == 0);       // You must lock everything at once
2961
2961
      if ((table->reginfo.lock_type= lock_type) != TL_UNLOCK)
2962
 
        if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
2963
 
                                            lock_flags, &refresh)))
2964
 
          table= 0;
 
2962
        if (! (session->lock= mysql_lock_tables(session, &table_list->table, 1,
 
2963
                                                lock_flags, &refresh)))
 
2964
          table= 0;
2965
2965
    }
2966
2966
  }
2967
2967
 
2974
2974
  Open all tables in list, locks them and optionally process derived tables.
2975
2975
 
2976
2976
  SYNOPSIS
2977
 
    open_and_lock_tables_derived()
2978
 
    session             - thread handler
2979
 
    tables      - list of tables for open&locking
2980
 
    derived     - if to handle derived tables
 
2977
  open_and_lock_tables_derived()
 
2978
  session               - thread handler
 
2979
  tables        - list of tables for open&locking
 
2980
  derived     - if to handle derived tables
2981
2981
 
2982
2982
  RETURN
2983
 
    false - ok
2984
 
    true  - error
2985
 
 
2986
 
  NOTE
2987
 
    The lock will automaticaly be freed by close_thread_tables()
2988
 
 
2989
 
  NOTE
2990
 
    There are two convenience functions:
2991
 
    - simple_open_n_lock_tables(session, tables)  without derived handling
2992
 
    - open_and_lock_tables(session, tables)       with derived handling
2993
 
    Both inline functions call open_and_lock_tables_derived() with
2994
 
    the third argument set appropriately.
 
2983
  false - ok
 
2984
  true  - error
 
2985
 
 
2986
  NOTE
 
2987
  The lock will automaticaly be freed by close_thread_tables()
 
2988
 
 
2989
  NOTE
 
2990
  There are two convenience functions:
 
2991
  - simple_open_n_lock_tables(session, tables)  without derived handling
 
2992
  - open_and_lock_tables(session, tables)       with derived handling
 
2993
  Both inline functions call open_and_lock_tables_derived() with
 
2994
  the third argument set appropriately.
2995
2995
*/
2996
2996
 
2997
2997
int open_and_lock_tables_derived(Session *session, TableList *tables, bool derived)
3023
3023
  Open all tables in list and process derived tables
3024
3024
 
3025
3025
  SYNOPSIS
3026
 
    open_normal_and_derived_tables
3027
 
    session             - thread handler
3028
 
    tables      - list of tables for open
3029
 
    flags       - bitmap of flags to modify how the tables will be open:
3030
 
                  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
3031
 
                  done a flush or namelock on it.
 
3026
  open_normal_and_derived_tables
 
3027
  session               - thread handler
 
3028
  tables        - list of tables for open
 
3029
  flags       - bitmap of flags to modify how the tables will be open:
 
3030
  DRIZZLE_LOCK_IGNORE_FLUSH - open table even if someone has
 
3031
  done a flush or namelock on it.
3032
3032
 
3033
3033
  RETURN
3034
 
    false - ok
3035
 
    true  - error
 
3034
  false - ok
 
3035
  true  - error
3036
3036
 
3037
3037
  NOTE
3038
 
    This is to be used on prepare stage when you don't read any
3039
 
    data from the tables.
 
3038
  This is to be used on prepare stage when you don't read any
 
3039
  data from the tables.
3040
3040
*/
3041
3041
 
3042
3042
bool open_normal_and_derived_tables(Session *session, TableList *tables, uint32_t flags)
3053
3053
  Lock all tables in list
3054
3054
 
3055
3055
  SYNOPSIS
3056
 
    lock_tables()
3057
 
    session                     Thread handler
3058
 
    tables              Tables to lock
3059
 
    count               Number of opened tables
3060
 
    need_reopen         Out parameter which if true indicates that some
3061
 
                        tables were dropped or altered during this call
3062
 
                        and therefore invoker should reopen tables and
3063
 
                        try to lock them once again (in this case
3064
 
                        lock_tables() will also return error).
 
3056
  lock_tables()
 
3057
  session                       Thread handler
 
3058
  tables                Tables to lock
 
3059
  count         Number of opened tables
 
3060
  need_reopen         Out parameter which if true indicates that some
 
3061
  tables were dropped or altered during this call
 
3062
  and therefore invoker should reopen tables and
 
3063
  try to lock them once again (in this case
 
3064
  lock_tables() will also return error).
3065
3065
 
3066
3066
  NOTES
3067
 
    You can't call lock_tables twice, as this would break the dead-lock-free
3068
 
    handling thr_lock gives us.  You most always get all needed locks at
3069
 
    once.
 
3067
  You can't call lock_tables twice, as this would break the dead-lock-free
 
3068
  handling thr_lock gives us.  You most always get all needed locks at
 
3069
  once.
3070
3070
 
3071
 
    If query for which we are calling this function marked as requring
3072
 
    prelocking, this function will do implicit LOCK TABLES and change
3073
 
    session::prelocked_mode accordingly.
 
3071
  If query for which we are calling this function marked as requring
 
3072
  prelocking, this function will do implicit LOCK TABLES and change
 
3073
  session::prelocked_mode accordingly.
3074
3074
 
3075
3075
  RETURN VALUES
3076
 
   0    ok
3077
 
   -1   Error
 
3076
  0     ok
 
3077
  -1    Error
3078
3078
*/
3079
3079
 
3080
3080
int lock_tables(Session *session, TableList *tables, uint32_t count, bool *need_reopen)
3101
3101
    for (table= tables; table; table= table->next_global)
3102
3102
    {
3103
3103
      if (!table->placeholder())
3104
 
        *(ptr++)= table->table;
 
3104
        *(ptr++)= table->table;
3105
3105
    }
3106
3106
 
3107
3107
    if (!(session->lock= mysql_lock_tables(session, start, (uint32_t) (ptr - start),
3108
 
                                       lock_flag, need_reopen)))
 
3108
                                           lock_flag, need_reopen)))
3109
3109
    {
3110
3110
      return(-1);
3111
3111
    }
3126
3126
         table= table->next_global)
3127
3127
    {
3128
3128
      if (!table->placeholder() &&
3129
 
          check_lock_and_start_stmt(session, table->table, table->lock_type))
 
3129
          check_lock_and_start_stmt(session, table->table, table->lock_type))
3130
3130
      {
3131
 
        return(-1);
 
3131
        return(-1);
3132
3132
      }
3133
3133
    }
3134
3134
  }
3142
3142
  prelocked tables.
3143
3143
 
3144
3144
  SYNOPSIS
3145
 
    close_tables_for_reopen()
3146
 
      session    in     Thread context
3147
 
      tables in/out List of tables which we were trying to open and lock
 
3145
  close_tables_for_reopen()
 
3146
  session    in     Thread context
 
3147
  tables in/out List of tables which we were trying to open and lock
3148
3148
 
3149
3149
*/
3150
3150
 
3167
3167
  Open a single table without table caching and don't set it in open_list
3168
3168
 
3169
3169
  SYNPOSIS
3170
 
    open_temporary_table()
3171
 
    session               Thread object
3172
 
    path          Path (without .frm)
3173
 
    db            database
3174
 
    table_name    Table name
3175
 
    link_in_list  1 if table should be linked into session->temporary_tables
3176
 
 
3177
 
 NOTES:
3178
 
    Used by alter_table to open a temporary table and when creating
3179
 
    a temporary table with CREATE TEMPORARY ...
3180
 
 
3181
 
 RETURN
3182
 
   0  Error
3183
 
   #  Table object
 
3170
  open_temporary_table()
 
3171
  session                 Thread object
 
3172
  path    Path (without .frm)
 
3173
  db              database
 
3174
  table_name      Table name
 
3175
  link_in_list  1 if table should be linked into session->temporary_tables
 
3176
 
 
3177
NOTES:
 
3178
Used by alter_table to open a temporary table and when creating
 
3179
a temporary table with CREATE TEMPORARY ...
 
3180
 
 
3181
RETURN
 
3182
0  Error
 
3183
#  Table object
3184
3184
*/
3185
3185
 
3186
3186
Table *open_temporary_table(Session *session, const char *path, const char *db,
3187
 
                            const char *table_name, bool link_in_list,
 
3187
                            const char *table_name, bool link_in_list,
3188
3188
                            open_table_mode open_mode)
3189
3189
{
3190
3190
  Table *tmp_table;
3217
3217
      open_table_from_share(session, share, table_name,
3218
3218
                            (open_mode == OTM_ALTER) ? 0 :
3219
3219
                            (uint32_t) (HA_OPEN_KEYFILE | HA_OPEN_RNDFILE |
3220
 
                                    HA_GET_INDEX),
 
3220
                                        HA_GET_INDEX),
3221
3221
                            (open_mode == OTM_ALTER) ?
3222
 
                              (EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
 
3222
                            (EXTRA_RECORD | OPEN_FRM_FILE_ONLY)
3223
3223
                            : (EXTRA_RECORD),
3224
3224
                            ha_open_options,
3225
3225
                            tmp_table, open_mode))
3234
3234
  if (open_mode == OTM_ALTER)
3235
3235
  {
3236
3236
    /*
3237
 
       Temporary table has been created with frm_only
3238
 
       and has not been created in any storage engine
 
3237
      Temporary table has been created with frm_only
 
3238
      and has not been created in any storage engine
3239
3239
    */
3240
3240
    share->tmp_table= TMP_TABLE_FRM_FILE_ONLY;
3241
3241
  }
3271
3271
  {
3272
3272
    error=1;
3273
3273
    errmsg_printf(ERRMSG_LVL_WARN, _("Could not remove temporary table: '%s', error: %d"),
3274
 
                      path, my_errno);
 
3274
                  path, my_errno);
3275
3275
  }
3276
3276
  delete file;
3277
3277
  return(error);
3279
3279
 
3280
3280
 
3281
3281
/*****************************************************************************
3282
 
* The following find_field_in_XXX procedures implement the core of the
3283
 
* name resolution functionality. The entry point to resolve a column name in a
3284
 
* list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
3285
 
* for each table reference. In turn, depending on the type of table reference,
3286
 
* 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
3287
 
* below specific for the type of table reference.
3288
 
******************************************************************************/
 
3282
 * The following find_field_in_XXX procedures implement the core of the
 
3283
 * name resolution functionality. The entry point to resolve a column name in a
 
3284
 * list of tables is 'find_field_in_tables'. It calls 'find_field_in_table_ref'
 
3285
 * for each table reference. In turn, depending on the type of table reference,
 
3286
 * 'find_field_in_table_ref' calls one of the 'find_field_in_XXX' procedures
 
3287
 * below specific for the type of table reference.
 
3288
 ******************************************************************************/
3289
3289
 
3290
3290
/* Special Field pointers as return values of find_field_in_XXX functions. */
3291
3291
Field *not_found_field= (Field*) 0x1;
3338
3338
  Find field by name in a NATURAL/USING join table reference.
3339
3339
 
3340
3340
  SYNOPSIS
3341
 
    find_field_in_natural_join()
3342
 
    session                      [in]  thread handler
3343
 
    table_ref            [in]  table reference to search
3344
 
    name                 [in]  name of field
3345
 
    length               [in]  length of name
3346
 
    ref                  [in/out] if 'name' is resolved to a view field, ref is
3347
 
                               set to point to the found view field
3348
 
    register_tree_change [in]  true if ref is not stack variable and we
3349
 
                               need register changes in item tree
3350
 
    actual_table         [out] the original table reference where the field
3351
 
                               belongs - differs from 'table_list' only for
3352
 
                               NATURAL/USING joins
 
3341
  find_field_in_natural_join()
 
3342
  session                        [in]  thread handler
 
3343
  table_ref            [in]  table reference to search
 
3344
  name           [in]  name of field
 
3345
  length                 [in]  length of name
 
3346
  ref                  [in/out] if 'name' is resolved to a view field, ref is
 
3347
  set to point to the found view field
 
3348
  register_tree_change [in]  true if ref is not stack variable and we
 
3349
  need register changes in item tree
 
3350
  actual_table         [out] the original table reference where the field
 
3351
  belongs - differs from 'table_list' only for
 
3352
  NATURAL/USING joins
3353
3353
 
3354
3354
  DESCRIPTION
3355
 
    Search for a field among the result fields of a NATURAL/USING join.
3356
 
    Notice that this procedure is called only for non-qualified field
3357
 
    names. In the case of qualified fields, we search directly the base
3358
 
    tables of a natural join.
 
3355
  Search for a field among the result fields of a NATURAL/USING join.
 
3356
  Notice that this procedure is called only for non-qualified field
 
3357
  names. In the case of qualified fields, we search directly the base
 
3358
  tables of a natural join.
3359
3359
 
3360
3360
  RETURN
3361
 
    NULL        if the field was not found
3362
 
    WRONG_GRANT if no access rights to the found field
3363
 
    #           Pointer to the found Field
 
3361
  NULL        if the field was not found
 
3362
  WRONG_GRANT if no access rights to the found field
 
3363
#           Pointer to the found Field
3364
3364
*/
3365
3365
 
3366
3366
static Field *
3408
3408
  Find field by name in a base table or a view with temp table algorithm.
3409
3409
 
3410
3410
  SYNOPSIS
3411
 
    find_field_in_table()
3412
 
    session                             thread handler
3413
 
    table                       table where to search for the field
3414
 
    name                        name of field
3415
 
    length                      length of name
3416
 
    allow_rowid                 do allow finding of "_rowid" field?
3417
 
    cached_field_index_ptr      cached position in field list (used to speedup
3418
 
                                lookup for fields in prepared tables)
 
3411
  find_field_in_table()
 
3412
  session                               thread handler
 
3413
  table                 table where to search for the field
 
3414
  name                  name of field
 
3415
  length                        length of name
 
3416
  allow_rowid                   do allow finding of "_rowid" field?
 
3417
  cached_field_index_ptr        cached position in field list (used to speedup
 
3418
  lookup for fields in prepared tables)
3419
3419
 
3420
3420
  RETURN
3421
 
    0   field is not found
3422
 
    #   pointer to field
 
3421
  0     field is not found
 
3422
#       pointer to field
3423
3423
*/
3424
3424
 
3425
3425
Field *
3480
3480
  Find field in a table reference.
3481
3481
 
3482
3482
  SYNOPSIS
3483
 
    find_field_in_table_ref()
3484
 
    session                        [in]  thread handler
3485
 
    table_list             [in]  table reference to search
3486
 
    name                   [in]  name of field
3487
 
    length                 [in]  field length of name
3488
 
    item_name              [in]  name of item if it will be created (VIEW)
3489
 
    db_name                [in]  optional database name that qualifies the
3490
 
    table_name             [in]  optional table name that qualifies the field
3491
 
    ref                [in/out] if 'name' is resolved to a view field, ref
3492
 
                                 is set to point to the found view field
3493
 
    check_privileges       [in]  check privileges
3494
 
    allow_rowid            [in]  do allow finding of "_rowid" field?
3495
 
    cached_field_index_ptr [in]  cached position in field list (used to
3496
 
                                 speedup lookup for fields in prepared tables)
3497
 
    register_tree_change   [in]  true if ref is not stack variable and we
3498
 
                                 need register changes in item tree
3499
 
    actual_table           [out] the original table reference where the field
3500
 
                                 belongs - differs from 'table_list' only for
3501
 
                                 NATURAL_USING joins.
 
3483
  find_field_in_table_ref()
 
3484
  session                          [in]  thread handler
 
3485
  table_list               [in]  table reference to search
 
3486
  name             [in]  name of field
 
3487
  length                   [in]  field length of name
 
3488
  item_name              [in]  name of item if it will be created (VIEW)
 
3489
  db_name                [in]  optional database name that qualifies the
 
3490
  table_name             [in]  optional table name that qualifies the field
 
3491
  ref                  [in/out] if 'name' is resolved to a view field, ref
 
3492
  is set to point to the found view field
 
3493
  check_privileges       [in]  check privileges
 
3494
  allow_rowid              [in]  do allow finding of "_rowid" field?
 
3495
  cached_field_index_ptr [in]  cached position in field list (used to
 
3496
  speedup lookup for fields in prepared tables)
 
3497
  register_tree_change   [in]  true if ref is not stack variable and we
 
3498
  need register changes in item tree
 
3499
  actual_table           [out] the original table reference where the field
 
3500
  belongs - differs from 'table_list' only for
 
3501
  NATURAL_USING joins.
3502
3502
 
3503
3503
  DESCRIPTION
3504
 
    Find a field in a table reference depending on the type of table
3505
 
    reference. There are three types of table references with respect
3506
 
    to the representation of their result columns:
3507
 
    - an array of Field_translator objects for MERGE views and some
3508
 
      information_schema tables,
3509
 
    - an array of Field objects (and possibly a name hash) for stored
3510
 
      tables,
3511
 
    - a list of Natural_join_column objects for NATURAL/USING joins.
3512
 
    This procedure detects the type of the table reference 'table_list'
3513
 
    and calls the corresponding search routine.
 
3504
  Find a field in a table reference depending on the type of table
 
3505
  reference. There are three types of table references with respect
 
3506
  to the representation of their result columns:
 
3507
  - an array of Field_translator objects for MERGE views and some
 
3508
  information_schema tables,
 
3509
  - an array of Field objects (and possibly a name hash) for stored
 
3510
  tables,
 
3511
  - a list of Natural_join_column objects for NATURAL/USING joins.
 
3512
  This procedure detects the type of the table reference 'table_list'
 
3513
  and calls the corresponding search routine.
3514
3514
 
3515
3515
  RETURN
3516
 
    0                   field is not found
3517
 
    view_ref_found      found value in VIEW (real result is in *ref)
3518
 
    #                   pointer to field
 
3516
  0                     field is not found
 
3517
  view_ref_found        found value in VIEW (real result is in *ref)
 
3518
#                       pointer to field
3519
3519
*/
3520
3520
 
3521
3521
Field *
3547
3547
    inside the view, but we want to search directly in the view columns
3548
3548
    which are represented as a 'field_translation'.
3549
3549
 
3550
 
    TODO: Ensure that table_name, db_name and tables->db always points to
3551
 
          something !
 
3550
TODO: Ensure that table_name, db_name and tables->db always points to
 
3551
something !
3552
3552
  */
3553
3553
  if (/* Exclude nested joins. */
3554
3554
      (!table_list->nested_join) &&
3555
 
       /* Include merge views and information schema tables. */
 
3555
      /* Include merge views and information schema tables. */
3556
3556
      /*
3557
3557
        Test if the field qualifiers match the table reference we plan
3558
3558
        to search.
3610
3610
 
3611
3611
  if (fld)
3612
3612
  {
3613
 
      if (session->mark_used_columns != MARK_COLUMNS_NONE)
 
3613
    if (session->mark_used_columns != MARK_COLUMNS_NONE)
 
3614
    {
 
3615
      /*
 
3616
        Get rw_set correct for this field so that the handler
 
3617
        knows that this field is involved in the query and gets
 
3618
        retrieved/updated
 
3619
      */
 
3620
      Field *field_to_set= NULL;
 
3621
      if (fld == view_ref_found)
3614
3622
      {
3615
 
        /*
3616
 
          Get rw_set correct for this field so that the handler
3617
 
          knows that this field is involved in the query and gets
3618
 
          retrieved/updated
3619
 
         */
3620
 
        Field *field_to_set= NULL;
3621
 
        if (fld == view_ref_found)
3622
 
        {
3623
 
          Item *it= (*ref)->real_item();
3624
 
          if (it->type() == Item::FIELD_ITEM)
3625
 
            field_to_set= ((Item_field*)it)->field;
3626
 
          else
3627
 
          {
3628
 
            if (session->mark_used_columns == MARK_COLUMNS_READ)
3629
 
              it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3630
 
          }
3631
 
        }
 
3623
        Item *it= (*ref)->real_item();
 
3624
        if (it->type() == Item::FIELD_ITEM)
 
3625
          field_to_set= ((Item_field*)it)->field;
3632
3626
        else
3633
 
          field_to_set= fld;
3634
 
        if (field_to_set)
3635
3627
        {
3636
 
          Table *table= field_to_set->table;
3637
3628
          if (session->mark_used_columns == MARK_COLUMNS_READ)
3638
 
            table->setReadSet(field_to_set->field_index);
3639
 
          else
3640
 
            table->setWriteSet(field_to_set->field_index);
 
3629
            it->walk(&Item::register_field_in_read_map, 1, (unsigned char *) 0);
3641
3630
        }
3642
3631
      }
 
3632
      else
 
3633
        field_to_set= fld;
 
3634
      if (field_to_set)
 
3635
      {
 
3636
        Table *table= field_to_set->table;
 
3637
        if (session->mark_used_columns == MARK_COLUMNS_READ)
 
3638
          table->setReadSet(field_to_set->field_index);
 
3639
        else
 
3640
          table->setWriteSet(field_to_set->field_index);
 
3641
      }
 
3642
    }
3643
3643
  }
3644
3644
  return(fld);
3645
3645
}
3656
3656
  name                          Name of field searched for
3657
3657
 
3658
3658
  RETURN
3659
 
    0                   field is not found
3660
 
    #                   pointer to field
 
3659
  0                   field is not found
 
3660
#                   pointer to field
3661
3661
*/
3662
3662
 
3663
3663
Field *find_field_in_table_sef(Table *table, const char *name)
3695
3695
  Find field in table list.
3696
3696
 
3697
3697
  SYNOPSIS
3698
 
    find_field_in_tables()
3699
 
    session                       pointer to current thread structure
3700
 
    item                  field item that should be found
3701
 
    first_table           list of tables to be searched for item
3702
 
    last_table            end of the list of tables to search for item. If NULL
3703
 
                          then search to the end of the list 'first_table'.
3704
 
    ref                   if 'item' is resolved to a view field, ref is set to
3705
 
                          point to the found view field
3706
 
    report_error          Degree of error reporting:
3707
 
                          - IGNORE_ERRORS then do not report any error
3708
 
                          - IGNORE_EXCEPT_NON_UNIQUE report only non-unique
3709
 
                            fields, suppress all other errors
3710
 
                          - REPORT_EXCEPT_NON_UNIQUE report all other errors
3711
 
                            except when non-unique fields were found
3712
 
                          - REPORT_ALL_ERRORS
3713
 
    check_privileges      need to check privileges
3714
 
    register_tree_change  true if ref is not a stack variable and we
3715
 
                          to need register changes in item tree
 
3698
  find_field_in_tables()
 
3699
  session                         pointer to current thread structure
 
3700
  item            field item that should be found
 
3701
  first_table           list of tables to be searched for item
 
3702
  last_table            end of the list of tables to search for item. If NULL
 
3703
  then search to the end of the list 'first_table'.
 
3704
  ref                     if 'item' is resolved to a view field, ref is set to
 
3705
  point to the found view field
 
3706
  report_error    Degree of error reporting:
 
3707
  - IGNORE_ERRORS then do not report any error
 
3708
  - IGNORE_EXCEPT_NON_UNIQUE report only non-unique
 
3709
  fields, suppress all other errors
 
3710
  - REPORT_EXCEPT_NON_UNIQUE report all other errors
 
3711
  except when non-unique fields were found
 
3712
  - REPORT_ALL_ERRORS
 
3713
  check_privileges      need to check privileges
 
3714
  register_tree_change  true if ref is not a stack variable and we
 
3715
  to need register changes in item tree
3716
3716
 
3717
3717
  RETURN VALUES
3718
 
    0                   If error: the found field is not unique, or there are
3719
 
                        no sufficient access priviliges for the found field,
3720
 
                        or the field is qualified with non-existing table.
3721
 
    not_found_field     The function was called with report_error ==
3722
 
                        (IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
3723
 
                        field was not found.
3724
 
    view_ref_found      View field is found, item passed through ref parameter
3725
 
    found field         If a item was resolved to some field
 
3718
  0                     If error: the found field is not unique, or there are
 
3719
  no sufficient access priviliges for the found field,
 
3720
  or the field is qualified with non-existing table.
 
3721
  not_found_field       The function was called with report_error ==
 
3722
  (IGNORE_ERRORS || IGNORE_EXCEPT_NON_UNIQUE) and a
 
3723
  field was not found.
 
3724
  view_ref_found        View field is found, item passed through ref parameter
 
3725
  found field         If a item was resolved to some field
3726
3726
*/
3727
3727
 
3728
3728
Field *
3729
3729
find_field_in_tables(Session *session, Item_ident *item,
3730
3730
                     TableList *first_table, TableList *last_table,
3731
 
                     Item **ref, find_item_error_report_type report_error,
 
3731
                     Item **ref, find_item_error_report_type report_error,
3732
3732
                     bool check_privileges, bool register_tree_change)
3733
3733
{
3734
3734
  Field *found=0;
3765
3765
      The condition (table_ref->view == NULL) ensures that we will call
3766
3766
      find_field_in_table even in the case of information schema tables
3767
3767
      when table_ref->field_translation != NULL.
3768
 
      */
 
3768
    */
3769
3769
    if (table_ref->table)
3770
3770
      found= find_field_in_table(session, table_ref->table, name, length,
3771
3771
                                 true, &(item->cached_field_index));
3778
3778
    if (found)
3779
3779
    {
3780
3780
      if (found == WRONG_GRANT)
3781
 
        return (Field*) 0;
 
3781
        return (Field*) 0;
3782
3782
 
3783
3783
      /*
3784
3784
        Only views fields should be marked as dependent, not an underlying
3861
3861
      /*
3862
3862
        If we found a fully qualified field we return it directly as it can't
3863
3863
        have duplicates.
3864
 
       */
 
3864
      */
3865
3865
      if (db)
3866
3866
        return cur_field;
3867
3867
 
3921
3921
  Find Item in list of items (find_field_in_tables analog)
3922
3922
 
3923
3923
  TODO
3924
 
    is it better return only counter?
 
3924
  is it better return only counter?
3925
3925
 
3926
3926
  SYNOPSIS
3927
 
    find_item_in_list()
3928
 
    find                        Item to find
3929
 
    items                       List of items
3930
 
    counter                     To return number of found item
3931
 
    report_error
3932
 
      REPORT_ALL_ERRORS         report errors, return 0 if error
3933
 
      REPORT_EXCEPT_NOT_FOUND   Do not report 'not found' error and
3934
 
                                return not_found_item, report other errors,
3935
 
                                return 0
3936
 
      IGNORE_ERRORS             Do not report errors, return 0 if error
3937
 
    resolution                  Set to the resolution type if the item is found
3938
 
                                (it says whether the item is resolved
3939
 
                                 against an alias name,
3940
 
                                 or as a field name without alias,
3941
 
                                 or as a field hidden by alias,
3942
 
                                 or ignoring alias)
 
3927
  find_item_in_list()
 
3928
  find                  Item to find
 
3929
  items                 List of items
 
3930
  counter                       To return number of found item
 
3931
  report_error
 
3932
  REPORT_ALL_ERRORS             report errors, return 0 if error
 
3933
  REPORT_EXCEPT_NOT_FOUND       Do not report 'not found' error and
 
3934
  return not_found_item, report other errors,
 
3935
  return 0
 
3936
  IGNORE_ERRORS         Do not report errors, return 0 if error
 
3937
  resolution                  Set to the resolution type if the item is found
 
3938
  (it says whether the item is resolved
 
3939
  against an alias name,
 
3940
  or as a field name without alias,
 
3941
  or as a field hidden by alias,
 
3942
  or ignoring alias)
3943
3943
 
3944
3944
  RETURN VALUES
3945
 
    0                   Item is not found or item is not unique,
3946
 
                        error message is reported
3947
 
    not_found_item      Function was called with
3948
 
                        report_error == REPORT_EXCEPT_NOT_FOUND and
3949
 
                        item was not found. No error message was reported
3950
 
                        found field
 
3945
  0                     Item is not found or item is not unique,
 
3946
  error message is reported
 
3947
  not_found_item        Function was called with
 
3948
  report_error == REPORT_EXCEPT_NOT_FOUND and
 
3949
  item was not found. No error message was reported
 
3950
  found field
3951
3951
*/
3952
3952
 
3953
3953
/* Special Item pointer to serve as a return value from find_item_in_list(). */
3990
3990
      Item_ident *item_field= (Item_ident*) item;
3991
3991
 
3992
3992
      /*
3993
 
        In case of group_concat() with ORDER BY condition in the QUERY
3994
 
        item_field can be field of temporary table without item name
3995
 
        (if this field created from expression argument of group_concat()),
3996
 
        => we have to check presence of name before compare
 
3993
        In case of group_concat() with ORDER BY condition in the QUERY
 
3994
        item_field can be field of temporary table without item name
 
3995
        (if this field created from expression argument of group_concat()),
 
3996
        => we have to check presence of name before compare
3997
3997
      */
3998
3998
      if (!item_field->name)
3999
3999
        continue;
4013
4013
          case sensitive. In cases where they are not case sensitive, they
4014
4014
          are always in lower case.
4015
4015
 
4016
 
          item_field->field_name and item_field->table_name can be 0x0 if
4017
 
          item is not fix_field()'ed yet.
 
4016
          item_field->field_name and item_field->table_name can be 0x0 if
 
4017
          item is not fix_field()'ed yet.
4018
4018
        */
4019
4019
        if (item_field->field_name && item_field->table_name &&
4020
 
            !my_strcasecmp(system_charset_info, item_field->field_name,
 
4020
            !my_strcasecmp(system_charset_info, item_field->field_name,
4021
4021
                           field_name) &&
4022
4022
            !my_strcasecmp(table_alias_charset, item_field->table_name,
4023
4023
                           table_name) &&
4072
4072
          found= li.ref();
4073
4073
          *counter= i;
4074
4074
          *resolution= fname_cmp ? RESOLVED_AGAINST_ALIAS:
4075
 
                                   RESOLVED_WITH_NO_ALIAS;
 
4075
            RESOLVED_WITH_NO_ALIAS;
4076
4076
        }
4077
4077
        else if (!fname_cmp)
4078
4078
        {
4096
4096
    else if (!table_name)
4097
4097
    {
4098
4098
      if (is_ref_by_name && find->name && item->name &&
4099
 
          !my_strcasecmp(system_charset_info,item->name,find->name))
 
4099
          !my_strcasecmp(system_charset_info,item->name,find->name))
4100
4100
      {
4101
4101
        found= li.ref();
4102
4102
        *counter= i;
4146
4146
  Test if a string is a member of a list of strings.
4147
4147
 
4148
4148
  SYNOPSIS
4149
 
    test_if_string_in_list()
4150
 
    find      the string to look for
4151
 
    str_list  a list of strings to be searched
 
4149
  test_if_string_in_list()
 
4150
  find      the string to look for
 
4151
  str_list  a list of strings to be searched
4152
4152
 
4153
4153
  DESCRIPTION
4154
 
    Sequentially search a list of strings for a string, and test whether
4155
 
    the list contains the same string.
 
4154
  Sequentially search a list of strings for a string, and test whether
 
4155
  the list contains the same string.
4156
4156
 
4157
4157
  RETURN
4158
 
    true  if find is in str_list
4159
 
    false otherwise
 
4158
  true  if find is in str_list
 
4159
  false otherwise
4160
4160
*/
4161
4161
 
4162
4162
static bool
4181
4181
  being resolved in a specific table reference.
4182
4182
 
4183
4183
  SYNOPSIS
4184
 
    set_new_item_local_context()
4185
 
    session        pointer to current thread
4186
 
    item       item for which new context is created and set
4187
 
    table_ref  table ref where an item showld be resolved
 
4184
  set_new_item_local_context()
 
4185
  session        pointer to current thread
 
4186
  item       item for which new context is created and set
 
4187
  table_ref  table ref where an item showld be resolved
4188
4188
 
4189
4189
  DESCRIPTION
4190
 
    Create a new name resolution context for an item, so that the item
4191
 
    is resolved only the supplied 'table_ref'.
 
4190
  Create a new name resolution context for an item, so that the item
 
4191
  is resolved only the supplied 'table_ref'.
4192
4192
 
4193
4193
  RETURN
4194
 
    false  if all OK
4195
 
    true   otherwise
 
4194
  false  if all OK
 
4195
  true   otherwise
4196
4196
*/
4197
4197
 
4198
4198
static bool
4213
4213
  Find and mark the common columns of two table references.
4214
4214
 
4215
4215
  SYNOPSIS
4216
 
    mark_common_columns()
4217
 
    session                [in] current thread
4218
 
    table_ref_1        [in] the first (left) join operand
4219
 
    table_ref_2        [in] the second (right) join operand
4220
 
    using_fields       [in] if the join is JOIN...USING - the join columns,
4221
 
                            if NATURAL join, then NULL
4222
 
    found_using_fields [out] number of fields from the USING clause that were
4223
 
                             found among the common fields
 
4216
  mark_common_columns()
 
4217
  session                [in] current thread
 
4218
  table_ref_1        [in] the first (left) join operand
 
4219
  table_ref_2        [in] the second (right) join operand
 
4220
  using_fields       [in] if the join is JOIN...USING - the join columns,
 
4221
  if NATURAL join, then NULL
 
4222
  found_using_fields [out] number of fields from the USING clause that were
 
4223
  found among the common fields
4224
4224
 
4225
4225
  DESCRIPTION
4226
 
    The procedure finds the common columns of two relations (either
4227
 
    tables or intermediate join results), and adds an equi-join condition
4228
 
    to the ON clause of 'table_ref_2' for each pair of matching columns.
4229
 
    If some of table_ref_XXX represents a base table or view, then we
4230
 
    create new 'Natural_join_column' instances for each column
4231
 
    reference and store them in the 'join_columns' of the table
4232
 
    reference.
 
4226
  The procedure finds the common columns of two relations (either
 
4227
  tables or intermediate join results), and adds an equi-join condition
 
4228
  to the ON clause of 'table_ref_2' for each pair of matching columns.
 
4229
  If some of table_ref_XXX represents a base table or view, then we
 
4230
  create new 'Natural_join_column' instances for each column
 
4231
  reference and store them in the 'join_columns' of the table
 
4232
  reference.
4233
4233
 
4234
4234
  IMPLEMENTATION
4235
 
    The procedure assumes that store_natural_using_join_columns() was
4236
 
    called for the previous level of NATURAL/USING joins.
 
4235
  The procedure assumes that store_natural_using_join_columns() was
 
4236
  called for the previous level of NATURAL/USING joins.
4237
4237
 
4238
4238
  RETURN
4239
 
    true   error when some common column is non-unique, or out of memory
4240
 
    false  OK
 
4239
  true   error when some common column is non-unique, or out of memory
 
4240
  false  OK
4241
4241
*/
4242
4242
 
4243
4243
static bool
4253
4253
    if the leaves are != NULL.
4254
4254
  */
4255
4255
  TableList *leaf_1= (table_ref_1->nested_join &&
4256
 
                       !table_ref_1->is_natural_join) ?
4257
 
                      NULL : table_ref_1;
 
4256
                      !table_ref_1->is_natural_join) ?
 
4257
    NULL : table_ref_1;
4258
4258
  TableList *leaf_2= (table_ref_2->nested_join &&
4259
 
                       !table_ref_2->is_natural_join) ?
4260
 
                      NULL : table_ref_2;
 
4259
                      !table_ref_2->is_natural_join) ?
 
4260
    NULL : table_ref_2;
4261
4261
 
4262
4262
  *found_using_fields= 0;
4263
4263
 
4346
4346
        of sub-classes of Item_ident.
4347
4347
      */
4348
4348
      assert(item_1->type() == Item::FIELD_ITEM ||
4349
 
                  item_1->type() == Item::REF_ITEM);
 
4349
             item_1->type() == Item::REF_ITEM);
4350
4350
      assert(item_2->type() == Item::FIELD_ITEM ||
4351
 
                  item_2->type() == Item::REF_ITEM);
 
4351
             item_2->type() == Item::REF_ITEM);
4352
4352
 
4353
4353
      /*
4354
4354
        We need to cast item_1,2 to Item_ident, because we need to hook name
4373
4373
        Add the new equi-join condition to the ON clause. Notice that
4374
4374
        fix_fields() is applied to all ON conditions in setup_conds()
4375
4375
        so we don't do it here.
4376
 
       */
 
4376
      */
4377
4377
      add_join_on((table_ref_1->outer_join & JOIN_TYPE_RIGHT ?
4378
4378
                   table_ref_1 : table_ref_2),
4379
4379
                  eq_cond);
4423
4423
  Materialize and store the row type of NATURAL/USING join.
4424
4424
 
4425
4425
  SYNOPSIS
4426
 
    store_natural_using_join_columns()
4427
 
    session                current thread
4428
 
    natural_using_join the table reference of the NATURAL/USING join
4429
 
    table_ref_1        the first (left) operand (of a NATURAL/USING join).
4430
 
    table_ref_2        the second (right) operand (of a NATURAL/USING join).
4431
 
    using_fields       if the join is JOIN...USING - the join columns,
4432
 
                       if NATURAL join, then NULL
4433
 
    found_using_fields number of fields from the USING clause that were
4434
 
                       found among the common fields
 
4426
  store_natural_using_join_columns()
 
4427
  session                current thread
 
4428
  natural_using_join the table reference of the NATURAL/USING join
 
4429
  table_ref_1        the first (left) operand (of a NATURAL/USING join).
 
4430
  table_ref_2        the second (right) operand (of a NATURAL/USING join).
 
4431
  using_fields       if the join is JOIN...USING - the join columns,
 
4432
  if NATURAL join, then NULL
 
4433
  found_using_fields number of fields from the USING clause that were
 
4434
  found among the common fields
4435
4435
 
4436
4436
  DESCRIPTION
4437
 
    Iterate over the columns of both join operands and sort and store
4438
 
    all columns into the 'join_columns' list of natural_using_join
4439
 
    where the list is formed by three parts:
4440
 
      part1: The coalesced columns of table_ref_1 and table_ref_2,
4441
 
             sorted according to the column order of the first table.
4442
 
      part2: The other columns of the first table, in the order in
4443
 
             which they were defined in CREATE TABLE.
4444
 
      part3: The other columns of the second table, in the order in
4445
 
             which they were defined in CREATE TABLE.
4446
 
    Time complexity - O(N1+N2), where Ni = length(table_ref_i).
4447
 
 
4448
 
  IMPLEMENTATION
4449
 
    The procedure assumes that mark_common_columns() has been called
4450
 
    for the join that is being processed.
4451
 
 
4452
 
  RETURN
4453
 
    true    error: Some common column is ambiguous
4454
 
    false   OK
 
4437
  Iterate over the columns of both join operands and sort and store
 
4438
  all columns into the 'join_columns' list of natural_using_join
 
4439
  where the list is formed by three parts:
 
4440
part1: The coalesced columns of table_ref_1 and table_ref_2,
 
4441
sorted according to the column order of the first table.
 
4442
part2: The other columns of the first table, in the order in
 
4443
which they were defined in CREATE TABLE.
 
4444
part3: The other columns of the second table, in the order in
 
4445
which they were defined in CREATE TABLE.
 
4446
Time complexity - O(N1+N2), where Ni = length(table_ref_i).
 
4447
 
 
4448
IMPLEMENTATION
 
4449
The procedure assumes that mark_common_columns() has been called
 
4450
for the join that is being processed.
 
4451
 
 
4452
RETURN
 
4453
true    error: Some common column is ambiguous
 
4454
false   OK
4455
4455
*/
4456
4456
 
4457
4457
static bool
4547
4547
  Precompute and store the row types of the top-most NATURAL/USING joins.
4548
4548
 
4549
4549
  SYNOPSIS
4550
 
    store_top_level_join_columns()
4551
 
    session            current thread
4552
 
    table_ref      nested join or table in a FROM clause
4553
 
    left_neighbor  neighbor table reference to the left of table_ref at the
4554
 
                   same level in the join tree
4555
 
    right_neighbor neighbor table reference to the right of table_ref at the
4556
 
                   same level in the join tree
 
4550
  store_top_level_join_columns()
 
4551
  session            current thread
 
4552
  table_ref      nested join or table in a FROM clause
 
4553
  left_neighbor  neighbor table reference to the left of table_ref at the
 
4554
  same level in the join tree
 
4555
  right_neighbor neighbor table reference to the right of table_ref at the
 
4556
  same level in the join tree
4557
4557
 
4558
4558
  DESCRIPTION
4559
 
    The procedure performs a post-order traversal of a nested join tree
4560
 
    and materializes the row types of NATURAL/USING joins in a
4561
 
    bottom-up manner until it reaches the TableList elements that
4562
 
    represent the top-most NATURAL/USING joins. The procedure should be
4563
 
    applied to each element of Select_Lex::top_join_list (i.e. to each
4564
 
    top-level element of the FROM clause).
 
4559
  The procedure performs a post-order traversal of a nested join tree
 
4560
  and materializes the row types of NATURAL/USING joins in a
 
4561
  bottom-up manner until it reaches the TableList elements that
 
4562
  represent the top-most NATURAL/USING joins. The procedure should be
 
4563
  applied to each element of Select_Lex::top_join_list (i.e. to each
 
4564
  top-level element of the FROM clause).
4565
4565
 
4566
4566
  IMPLEMENTATION
4567
 
    Notice that the table references in the list nested_join->join_list
4568
 
    are in reverse order, thus when we iterate over it, we are moving
4569
 
    from the right to the left in the FROM clause.
 
4567
  Notice that the table references in the list nested_join->join_list
 
4568
  are in reverse order, thus when we iterate over it, we are moving
 
4569
  from the right to the left in the FROM clause.
4570
4570
 
4571
4571
  RETURN
4572
 
    true   Error
4573
 
    false  OK
 
4572
  true   Error
 
4573
  false  OK
4574
4574
*/
4575
4575
 
4576
4576
static bool
4617
4617
        neighbors at the same level.
4618
4618
      */
4619
4619
      real_left_neighbor=  (same_level_left_neighbor) ?
4620
 
                           same_level_left_neighbor : left_neighbor;
 
4620
        same_level_left_neighbor : left_neighbor;
4621
4621
      real_right_neighbor= (same_level_right_neighbor) ?
4622
 
                           same_level_right_neighbor : right_neighbor;
 
4622
        same_level_right_neighbor : right_neighbor;
4623
4623
 
4624
4624
      if (cur_table_ref->nested_join &&
4625
4625
          store_top_level_join_columns(session, cur_table_ref,
4636
4636
  if (table_ref->is_natural_join)
4637
4637
  {
4638
4638
    assert(table_ref->nested_join &&
4639
 
                table_ref->nested_join->join_list.elements == 2);
 
4639
           table_ref->nested_join->join_list.elements == 2);
4640
4640
    List_iterator_fast<TableList> operand_it(table_ref->nested_join->join_list);
4641
4641
    /*
4642
4642
      Notice that the order of join operands depends on whether table_ref
4643
4643
      represents a LEFT or a RIGHT join. In a RIGHT join, the operands are
4644
4644
      in inverted order.
4645
 
     */
 
4645
    */
4646
4646
    TableList *table_ref_2= operand_it++; /* Second NATURAL join operand.*/
4647
4647
    TableList *table_ref_1= operand_it++; /* First NATURAL join operand. */
4648
4648
    List<String> *using_fields= table_ref->join_using_fields;
4711
4711
  in a FROM clause.
4712
4712
 
4713
4713
  SYNOPSIS
4714
 
    setup_natural_join_row_types()
4715
 
    session          current thread
4716
 
    from_clause  list of top-level table references in a FROM clause
 
4714
  setup_natural_join_row_types()
 
4715
  session          current thread
 
4716
  from_clause  list of top-level table references in a FROM clause
4717
4717
 
4718
4718
  DESCRIPTION
4719
 
    Apply the procedure 'store_top_level_join_columns' to each of the
4720
 
    top-level table referencs of the FROM clause. Adjust the list of tables
4721
 
    for name resolution - context->first_name_resolution_table to the
4722
 
    top-most, lef-most NATURAL/USING join.
 
4719
  Apply the procedure 'store_top_level_join_columns' to each of the
 
4720
  top-level table referencs of the FROM clause. Adjust the list of tables
 
4721
  for name resolution - context->first_name_resolution_table to the
 
4722
  top-most, lef-most NATURAL/USING join.
4723
4723
 
4724
4724
  IMPLEMENTATION
4725
 
    Notice that the table references in 'from_clause' are in reverse
4726
 
    order, thus when we iterate over it, we are moving from the right
4727
 
    to the left in the FROM clause.
 
4725
  Notice that the table references in 'from_clause' are in reverse
 
4726
  order, thus when we iterate over it, we are moving from the right
 
4727
  to the left in the FROM clause.
4728
4728
 
4729
4729
  RETURN
4730
 
    true   Error
4731
 
    false  OK
 
4730
  true   Error
 
4731
  false  OK
4732
4732
*/
4733
4733
static bool setup_natural_join_row_types(Session *session,
4734
4734
                                         List<TableList> *from_clause,
4777
4777
 
4778
4778
 
4779
4779
/****************************************************************************
4780
 
** Expand all '*' in given fields
4781
 
****************************************************************************/
 
4780
 ** Expand all '*' in given fields
 
4781
 ****************************************************************************/
4782
4782
 
4783
4783
int setup_wild(Session *session, List<Item> &fields,
4784
4784
               List<Item> *sum_func_list,
4795
4795
  {
4796
4796
    if (item->type() == Item::FIELD_ITEM &&
4797
4797
        ((Item_field*) item)->field_name &&
4798
 
        ((Item_field*) item)->field_name[0] == '*' &&
4799
 
        !((Item_field*) item)->field)
 
4798
        ((Item_field*) item)->field_name[0] == '*' &&
 
4799
        !((Item_field*) item)->field)
4800
4800
    {
4801
4801
      uint32_t elem= fields.elements;
4802
4802
      bool any_privileges= ((Item_field *) item)->any_privileges;
4817
4817
                             ((Item_field*) item)->table_name, &it,
4818
4818
                             any_privileges))
4819
4819
      {
4820
 
        return(-1);
 
4820
        return(-1);
4821
4821
      }
4822
4822
      if (sum_func_list)
4823
4823
      {
4824
 
        /*
4825
 
          sum_func_list is a list that has the fields list as a tail.
4826
 
          Because of this we have to update the element count also for this
4827
 
          list after expanding the '*' entry.
4828
 
        */
4829
 
        sum_func_list->elements+= fields.elements - elem;
 
4824
        /*
 
4825
          sum_func_list is a list that has the fields list as a tail.
 
4826
          Because of this we have to update the element count also for this
 
4827
          list after expanding the '*' entry.
 
4828
        */
 
4829
        sum_func_list->elements+= fields.elements - elem;
4830
4830
      }
4831
4831
      wild_num--;
4832
4832
    }
4838
4838
}
4839
4839
 
4840
4840
/****************************************************************************
4841
 
** Check that all given fields exists and fill struct with current data
4842
 
****************************************************************************/
 
4841
 ** Check that all given fields exists and fill struct with current data
 
4842
 ****************************************************************************/
4843
4843
 
4844
4844
bool setup_fields(Session *session, Item **ref_pointer_array,
4845
4845
                  List<Item> &fields, enum_mark_columns mark_used_columns,
4866
4866
    There is other way to solve problem: fill array with pointers to list,
4867
4867
    but it will be slower.
4868
4868
 
4869
 
    TODO: remove it when (if) we made one list for allfields and
4870
 
    ref_pointer_array
 
4869
TODO: remove it when (if) we made one list for allfields and
 
4870
ref_pointer_array
4871
4871
  */
4872
4872
  if (ref_pointer_array)
4873
4873
    memset(ref_pointer_array, 0, sizeof(Item *) * fields.elements);
4886
4886
    if (ref)
4887
4887
      *(ref++)= item;
4888
4888
    if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM &&
4889
 
        sum_func_list)
 
4889
        sum_func_list)
4890
4890
      item->split_sum_func(session, ref_pointer_array, *sum_func_list);
4891
4891
    session->used_tables|= item->used_tables();
4892
4892
    session->lex->current_select->cur_pos_in_select_list++;
4904
4904
  make list of leaves of join table tree
4905
4905
 
4906
4906
  SYNOPSIS
4907
 
    make_leaves_list()
4908
 
    list    pointer to pointer on list first element
4909
 
    tables  table list
 
4907
  make_leaves_list()
 
4908
  list    pointer to pointer on list first element
 
4909
  tables  table list
4910
4910
 
4911
4911
  RETURN pointer on pointer to next_leaf of last element
4912
4912
*/
4927
4927
  prepare tables
4928
4928
 
4929
4929
  SYNOPSIS
4930
 
    setup_tables()
4931
 
    session               Thread handler
4932
 
    context       name resolution contest to setup table list there
4933
 
    from_clause   Top-level list of table references in the FROM clause
4934
 
    tables        Table list (select_lex->table_list)
4935
 
    leaves        List of join table leaves list (select_lex->leaf_tables)
4936
 
    refresh       It is onle refresh for subquery
4937
 
    select_insert It is SELECT ... INSERT command
 
4930
  setup_tables()
 
4931
  session                 Thread handler
 
4932
  context       name resolution contest to setup table list there
 
4933
  from_clause   Top-level list of table references in the FROM clause
 
4934
  tables          Table list (select_lex->table_list)
 
4935
  leaves        List of join table leaves list (select_lex->leaf_tables)
 
4936
  refresh       It is onle refresh for subquery
 
4937
  select_insert It is SELECT ... INSERT command
4938
4938
 
4939
4939
  NOTE
4940
 
    Check also that the 'used keys' and 'ignored keys' exists and set up the
4941
 
    table structure accordingly.
4942
 
    Create a list of leaf tables. For queries with NATURAL/USING JOINs,
4943
 
    compute the row types of the top most natural/using join table references
4944
 
    and link these into a list of table references for name resolution.
 
4940
  Check also that the 'used keys' and 'ignored keys' exists and set up the
 
4941
  table structure accordingly.
 
4942
  Create a list of leaf tables. For queries with NATURAL/USING JOINs,
 
4943
  compute the row types of the top most natural/using join table references
 
4944
  and link these into a list of table references for name resolution.
4945
4945
 
4946
 
    This has to be called for all tables that are used by items, as otherwise
4947
 
    table->map is not set and all Item_field will be regarded as const items.
 
4946
  This has to be called for all tables that are used by items, as otherwise
 
4947
  table->map is not set and all Item_field will be regarded as const items.
4948
4948
 
4949
4949
  RETURN
4950
 
    false ok;  In this case *map will includes the chosen index
4951
 
    true  error
 
4950
  false ok;  In this case *map will includes the chosen index
 
4951
  true  error
4952
4952
*/
4953
4953
 
4954
4954
bool setup_tables(Session *session, Name_resolution_context *context,
4958
4958
  uint32_t tablenr= 0;
4959
4959
 
4960
4960
  assert ((select_insert && !tables->next_name_resolution_table) || !tables ||
4961
 
               (context->table_list && context->first_name_resolution_table));
 
4961
          (context->table_list && context->first_name_resolution_table));
4962
4962
  /*
4963
4963
    this is used for INSERT ... SELECT.
4964
4964
    For select we setup tables except first (and its underlying tables)
5004
5004
  prepare tables and check access for the view tables
5005
5005
 
5006
5006
  SYNOPSIS
5007
 
    setup_tables_and_check_view_access()
5008
 
    session               Thread handler
5009
 
    context       name resolution contest to setup table list there
5010
 
    from_clause   Top-level list of table references in the FROM clause
5011
 
    tables        Table list (select_lex->table_list)
5012
 
    conds         Condition of current SELECT (can be changed by VIEW)
5013
 
    leaves        List of join table leaves list (select_lex->leaf_tables)
5014
 
    refresh       It is onle refresh for subquery
5015
 
    select_insert It is SELECT ... INSERT command
5016
 
    want_access   what access is needed
 
5007
  setup_tables_and_check_view_access()
 
5008
  session                 Thread handler
 
5009
  context       name resolution contest to setup table list there
 
5010
  from_clause   Top-level list of table references in the FROM clause
 
5011
  tables          Table list (select_lex->table_list)
 
5012
  conds   Condition of current SELECT (can be changed by VIEW)
 
5013
  leaves        List of join table leaves list (select_lex->leaf_tables)
 
5014
  refresh       It is onle refresh for subquery
 
5015
  select_insert It is SELECT ... INSERT command
 
5016
  want_access   what access is needed
5017
5017
 
5018
5018
  NOTE
5019
 
    a wrapper for check_tables that will also check the resulting
5020
 
    table leaves list for access to all the tables that belong to a view
 
5019
  a wrapper for check_tables that will also check the resulting
 
5020
  table leaves list for access to all the tables that belong to a view
5021
5021
 
5022
5022
  RETURN
5023
 
    false ok;  In this case *map will include the chosen index
5024
 
    true  error
 
5023
  false ok;  In this case *map will include the chosen index
 
5024
  true  error
5025
5025
*/
5026
5026
bool setup_tables_and_check_access(Session *session,
5027
5027
                                   Name_resolution_context *context,
5049
5049
 
5050
5050
 
5051
5051
/*
5052
 
   Create a key_map from a list of index names
5053
 
 
5054
 
   SYNOPSIS
5055
 
     get_key_map_from_key_list()
5056
 
     map                key_map to fill in
5057
 
     table              Table
5058
 
     index_list         List of index names
5059
 
 
5060
 
   RETURN
5061
 
     0  ok;  In this case *map will includes the choosed index
5062
 
     1  error
 
5052
  Create a key_map from a list of index names
 
5053
 
 
5054
  SYNOPSIS
 
5055
  get_key_map_from_key_list()
 
5056
  map           key_map to fill in
 
5057
  table         Table
 
5058
  index_list            List of index names
 
5059
 
 
5060
  RETURN
 
5061
  0     ok;  In this case *map will includes the choosed index
 
5062
  1     error
5063
5063
*/
5064
5064
 
5065
5065
bool get_key_map_from_key_list(key_map *map, Table *table,
5078
5078
        0)
5079
5079
    {
5080
5080
      my_error(ER_KEY_DOES_NOT_EXITS, MYF(0), name->c_ptr(),
5081
 
               table->pos_in_table_list->alias);
 
5081
               table->pos_in_table_list->alias);
5082
5082
      map->set();
5083
5083
      return 1;
5084
5084
    }
5092
5092
  Drops in all fields instead of current '*' field
5093
5093
 
5094
5094
  SYNOPSIS
5095
 
    insert_fields()
5096
 
    session                     Thread handler
5097
 
    context             Context for name resolution
5098
 
    db_name             Database name in case of 'database_name.table_name.*'
5099
 
    table_name          Table name in case of 'table_name.*'
5100
 
    it                  Pointer to '*'
5101
 
    any_privileges      0 If we should ensure that we have SELECT privileges
5102
 
                          for all columns
5103
 
                        1 If any privilege is ok
 
5095
  insert_fields()
 
5096
  session                       Thread handler
 
5097
  context             Context for name resolution
 
5098
  db_name               Database name in case of 'database_name.table_name.*'
 
5099
  table_name            Table name in case of 'table_name.*'
 
5100
  it                    Pointer to '*'
 
5101
  any_privileges        0 If we should ensure that we have SELECT privileges
 
5102
  for all columns
 
5103
  1 If any privilege is ok
5104
5104
  RETURN
5105
 
    0   ok     'it' is updated to point at last inserted
5106
 
    1   error.  Error message is generated but not sent to client
 
5105
  0     ok     'it' is updated to point at last inserted
 
5106
  1     error.  Error message is generated but not sent to client
5107
5107
*/
5108
5108
 
5109
5109
bool
5135
5135
    tables.
5136
5136
  */
5137
5137
  for (TableList *tables= (table_name ? context->table_list :
5138
 
                            context->first_name_resolution_table);
 
5138
                           context->first_name_resolution_table);
5139
5139
       tables;
5140
5140
       tables= (table_name ? tables->next_local :
5141
5141
                tables->next_name_resolution_table)
5142
 
       )
 
5142
      )
5143
5143
  {
5144
5144
    Field *field;
5145
5145
    Table *table= tables->table;
5227
5227
    return(false);
5228
5228
 
5229
5229
  /*
5230
 
    TODO: in the case when we skipped all columns because there was a
5231
 
    qualified '*', and all columns were coalesced, we have to give a more
5232
 
    meaningful message than ER_BAD_TABLE_ERROR.
 
5230
TODO: in the case when we skipped all columns because there was a
 
5231
qualified '*', and all columns were coalesced, we have to give a more
 
5232
meaningful message than ER_BAD_TABLE_ERROR.
5233
5233
  */
5234
5234
  if (!table_name)
5235
5235
    my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
5244
5244
  Fix all conditions and outer join expressions.
5245
5245
 
5246
5246
  SYNOPSIS
5247
 
    setup_conds()
5248
 
    session     thread handler
5249
 
    tables  list of tables for name resolving (select_lex->table_list)
5250
 
    leaves  list of leaves of join table tree (select_lex->leaf_tables)
5251
 
    conds   WHERE clause
 
5247
  setup_conds()
 
5248
  session     thread handler
 
5249
  tables  list of tables for name resolving (select_lex->table_list)
 
5250
  leaves  list of leaves of join table tree (select_lex->leaf_tables)
 
5251
  conds   WHERE clause
5252
5252
 
5253
5253
  DESCRIPTION
5254
 
    TODO
 
5254
  TODO
5255
5255
 
5256
5256
  RETURN
5257
 
    true  if some error occured (e.g. out of memory)
5258
 
    false if all is OK
 
5257
  true  if some error occured (e.g. out of memory)
 
5258
  false if all is OK
5259
5259
*/
5260
5260
 
5261
5261
int setup_conds(Session *session, TableList *leaves, COND **conds)
5267
5267
    it_is_update set to true when tables of primary Select_Lex (Select_Lex
5268
5268
    which belong to LEX, i.e. most up SELECT) will be updated by
5269
5269
    INSERT/UPDATE/LOAD
5270
 
    NOTE: using this condition helps to prevent call of prepare_check_option()
5271
 
    from subquery of VIEW, because tables of subquery belongs to VIEW
5272
 
    (see condition before prepare_check_option() call)
 
5270
NOTE: using this condition helps to prevent call of prepare_check_option()
 
5271
from subquery of VIEW, because tables of subquery belongs to VIEW
 
5272
(see condition before prepare_check_option() call)
5273
5273
  */
5274
5274
  bool save_is_item_list_lookup= select_lex->is_item_list_lookup;
5275
5275
  select_lex->is_item_list_lookup= 0;
5284
5284
  {
5285
5285
    session->where="where clause";
5286
5286
    if ((!(*conds)->fixed && (*conds)->fix_fields(session, conds)) ||
5287
 
        (*conds)->check_cols(1))
 
5287
        (*conds)->check_cols(1))
5288
5288
      goto err_no_arena;
5289
5289
  }
5290
5290
  session->session_marker= save_session_marker;
5306
5306
        session->session_marker= (void*)embedded;
5307
5307
        session->where="on clause";
5308
5308
        if ((!embedded->on_expr->fixed && embedded->on_expr->fix_fields(session, &embedded->on_expr)) ||
5309
 
            embedded->on_expr->check_cols(1))
5310
 
          goto err_no_arena;
 
5309
            embedded->on_expr->check_cols(1))
 
5310
          goto err_no_arena;
5311
5311
        select_lex->cond_count++;
5312
5312
      }
5313
5313
      embedding= embedded->embedding;
5328
5328
 
5329
5329
 
5330
5330
/******************************************************************************
5331
 
** Fill a record with data (for INSERT or UPDATE)
5332
 
** Returns : 1 if some field has wrong type
5333
 
******************************************************************************/
 
5331
 ** Fill a record with data (for INSERT or UPDATE)
 
5332
 ** Returns : 1 if some field has wrong type
 
5333
 ******************************************************************************/
5334
5334
 
5335
5335
 
5336
5336
/*
5337
5337
  Fill fields with given items.
5338
5338
 
5339
5339
  SYNOPSIS
5340
 
    fill_record()
5341
 
    session           thread handler
5342
 
    fields        Item_fields list to be filled
5343
 
    values        values to fill with
5344
 
    ignore_errors true if we should ignore errors
 
5340
  fill_record()
 
5341
  session           thread handler
 
5342
  fields        Item_fields list to be filled
 
5343
  values        values to fill with
 
5344
  ignore_errors true if we should ignore errors
5345
5345
 
5346
5346
  NOTE
5347
 
    fill_record() may set table->auto_increment_field_not_null and a
5348
 
    caller should make sure that it is reset after their last call to this
5349
 
    function.
 
5347
  fill_record() may set table->auto_increment_field_not_null and a
 
5348
  caller should make sure that it is reset after their last call to this
 
5349
  function.
5350
5350
 
5351
5351
  RETURN
5352
 
    false   OK
5353
 
    true    error occured
 
5352
  false   OK
 
5353
  true    error occured
5354
5354
*/
5355
5355
 
5356
5356
bool
5433
5433
  Fill field buffer with values from Field list
5434
5434
 
5435
5435
  SYNOPSIS
5436
 
    fill_record()
5437
 
    session           thread handler
5438
 
    ptr           pointer on pointer to record
5439
 
    values        list of fields
5440
 
    ignore_errors true if we should ignore errors
 
5436
  fill_record()
 
5437
  session           thread handler
 
5438
  ptr           pointer on pointer to record
 
5439
  values        list of fields
 
5440
  ignore_errors true if we should ignore errors
5441
5441
 
5442
5442
  NOTE
5443
 
    fill_record() may set table->auto_increment_field_not_null and a
5444
 
    caller should make sure that it is reset after their last call to this
5445
 
    function.
 
5443
  fill_record() may set table->auto_increment_field_not_null and a
 
5444
  caller should make sure that it is reset after their last call to this
 
5445
  function.
5446
5446
 
5447
5447
  RETURN
5448
 
    false   OK
5449
 
    true    error occured
 
5448
  false   OK
 
5449
  true    error occured
5450
5450
*/
5451
5451
 
5452
5452
bool
5585
5585
 
5586
5586
 
5587
5587
/*****************************************************************************
5588
 
        unireg support functions
5589
 
*****************************************************************************/
 
5588
  unireg support functions
 
5589
 *****************************************************************************/
5590
5590
 
5591
5591
/*
5592
5592
  Invalidate any cache entries that are for some DB
5593
5593
 
5594
5594
  SYNOPSIS
5595
 
    remove_db_from_cache()
5596
 
    db          Database name. This will be in lower case if
5597
 
                lower_case_table_name is set
 
5595
  remove_db_from_cache()
 
5596
  db            Database name. This will be in lower case if
 
5597
  lower_case_table_name is set
5598
5598
 
5599
 
  NOTE:
5600
 
  We can't use hash_delete when looping hash_elements. We mark them first
5601
 
  and afterwards delete those marked unused.
 
5599
NOTE:
 
5600
We can't use hash_delete when looping hash_elements. We mark them first
 
5601
and afterwards delete those marked unused.
5602
5602
*/
5603
5603
 
5604
5604
void remove_db_from_cache(const char *db)
5610
5610
    {
5611
5611
      table->s->version= 0L;                    /* Free when thread is ready */
5612
5612
      if (!table->in_use)
5613
 
        relink_unused(table);
 
5613
        relink_unused(table);
5614
5614
    }
5615
5615
  }
5616
5616
  while (unused_tables && !unused_tables->s->version)
5622
5622
  free all unused tables
5623
5623
 
5624
5624
  NOTE
5625
 
    This is called by 'handle_manager' when one wants to periodicly flush
5626
 
    all not used tables.
 
5625
  This is called by 'handle_manager' when one wants to periodicly flush
 
5626
  all not used tables.
5627
5627
*/
5628
5628
 
5629
5629
void flush_tables()
5642
5642
  close_thread_tables() is called.
5643
5643
 
5644
5644
  PREREQUISITES
5645
 
    Lock on LOCK_open()
 
5645
  Lock on LOCK_open()
5646
5646
 
5647
5647
  RETURN
5648
 
    0  This thread now have exclusive access to this table and no other thread
5649
 
       can access the table until close_thread_tables() is called.
5650
 
    1  Table is in use by another thread
 
5648
  0  This thread now have exclusive access to this table and no other thread
 
5649
  can access the table until close_thread_tables() is called.
 
5650
  1  Table is in use by another thread
5651
5651
*/
5652
5652
 
5653
5653
bool remove_table_from_cache(Session *session, const char *db, const char *table_name,
5692
5692
        in_use->some_tables_deleted= 1;
5693
5693
        if (table->is_name_opened())
5694
5694
        {
5695
 
          result=1;
 
5695
          result=1;
5696
5696
        }
5697
5697
        /*
5698
 
          Now we must abort all tables locks used by this thread
5699
 
          as the thread may be waiting to get a lock for another table.
 
5698
          Now we must abort all tables locks used by this thread
 
5699
          as the thread may be waiting to get a lock for another table.
5700
5700
          Note that we need to hold LOCK_open while going through the
5701
5701
          list. So that the other thread cannot change it. The other
5702
5702
          thread must also hold LOCK_open whenever changing the
5704
5704
          closed and before the parent is closed would be fatal.
5705
5705
        */
5706
5706
        for (Table *session_table= in_use->open_tables;
5707
 
             session_table ;
5708
 
             session_table= session_table->next)
 
5707
             session_table ;
 
5708
             session_table= session_table->next)
5709
5709
        {
5710
5710
          /* Do not handle locks of MERGE children. */
5711
 
          if (session_table->db_stat)   // If table is open
5712
 
            signalled|= mysql_lock_abort_for_thread(session, session_table);
 
5711
          if (session_table->db_stat)   // If table is open
 
5712
            signalled|= mysql_lock_abort_for_thread(session, session_table);
5713
5713
        }
5714
5714
      }
5715
5715
      else
5720
5720
 
5721
5721
    /* Remove table from table definition cache if it's not in use */
5722
5722
    if ((share= (TableShare*) hash_search(&table_def_cache,(unsigned char*) key,
5723
 
                                           key_length)))
 
5723
                                          key_length)))
5724
5724
    {
5725
5725
      share->version= 0;                          // Mark for delete
5726
5726
      if (share->ref_count == 0)