~posulliv/drizzle/optimizer-style-cleanup

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Padraig O'Sullivan
  • Date: 2010-03-15 14:05:26 UTC
  • mfrom: (1237.9.99 staging)
  • Revision ID: osullivan.padraig@gmail.com-20100315140526-opbgwdwn6tfecdkq
MergeĀ fromĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
51
51
#include "drizzled/global_charset_info.h"
52
52
#include "drizzled/pthread_globals.h"
53
53
#include "drizzled/internal/iocache.h"
 
54
#include "drizzled/plugin/authorization.h"
54
55
 
55
56
using namespace std;
56
57
 
85
86
  return (unsigned char*) entry->s->table_cache_key.str;
86
87
}
87
88
 
 
89
HASH *get_open_cache()
 
90
{
 
91
  return &open_cache;
 
92
}
 
93
 
88
94
 
89
95
bool table_cache_init(void)
90
96
{
99
105
  refresh_version++;                            // Force close of open tables
100
106
 
101
107
  while (unused_tables)
102
 
    hash_delete(&open_cache,(unsigned char*) unused_tables);
 
108
    hash_delete(&open_cache, (unsigned char*) unused_tables);
103
109
 
104
 
  if (!open_cache.records)                      // Safety first
 
110
  if (not open_cache.records)                   // Safety first
105
111
    hash_free(&open_cache);
106
112
}
107
113
 
161
167
}
162
168
 
163
169
 
164
 
 
165
 
/*
166
 
  Create a list for all open tables matching SQL expression
167
 
 
168
 
  SYNOPSIS
169
 
  list_open_tables()
170
 
  wild          SQL like expression
171
 
 
172
 
  NOTES
173
 
  One gets only a list of tables for which one has any kind of privilege.
174
 
  db and table names are allocated in result struct, so one doesn't need
175
 
  a lock on LOCK_open when traversing the return list.
176
 
 
177
 
  RETURN VALUES
178
 
  true  Error 
179
 
*/
180
 
 
181
 
bool list_open_tables(const char *db, 
182
 
                      const char *wild, 
183
 
                      bool(*func)(Table *table, 
184
 
                                  open_table_list_st& open_list,
185
 
                                  plugin::InfoSchemaTable *schema_table), 
186
 
                      Table *display,
187
 
                      plugin::InfoSchemaTable *schema_table)
188
 
{
189
 
  vector<open_table_list_st> open_list;
190
 
  vector<open_table_list_st>::iterator it;
191
 
  open_table_list_st table;
192
 
 
193
 
  /* What we really need is an optimization for knowing unique tables */
194
 
  if (db && wild)
195
 
    open_list.reserve(sizeof(open_table_list_st) * (open_cache.records % 2));
196
 
  else
197
 
    open_list.reserve(sizeof(open_table_list_st) * open_cache.records);
198
 
 
199
 
  pthread_mutex_lock(&LOCK_open); /* List all open tables */
200
 
 
201
 
  for (uint32_t idx= 0; idx < open_cache.records; idx++)
202
 
  {
203
 
    bool found= false;
204
 
    Table *entry=(Table*) hash_element(&open_cache,idx);
205
 
 
206
 
    if (db && my_strcasecmp(system_charset_info, db, entry->s->db.str))
207
 
      continue;
208
 
    if (wild && internal::wild_compare(entry->s->table_name.str, wild, 0))
209
 
      continue;
210
 
 
211
 
    for (it= open_list.begin(); it < open_list.end(); it++)
212
 
    {
213
 
      if (!(*it).table.compare(entry->s->table_name.str) &&
214
 
          !(*it).db.compare(entry->s->db.str))
215
 
      {
216
 
        if (entry->in_use)
217
 
          (*it).in_use++;
218
 
        if (entry->locked_by_name)
219
 
          (*it).locked++;
220
 
 
221
 
        found= true;
222
 
 
223
 
        break;
224
 
      }
225
 
    }
226
 
 
227
 
    if (found)
228
 
      continue;
229
 
 
230
 
    table.db= entry->s->db.str;
231
 
    table.table= entry->s->table_name.str;
232
 
    open_list.push_back(table);
233
 
  }
234
 
  pthread_mutex_unlock(&LOCK_open);
235
 
 
236
 
  for (it= open_list.begin(); it < open_list.end(); it++)
237
 
  {
238
 
    if (func(display, *it, schema_table))
239
 
      return true;
240
 
  }
241
 
 
242
 
  return false;
243
 
}
244
 
 
245
170
/*****************************************************************************
246
171
 *       Functions to free open table cache
247
172
 ****************************************************************************/
269
194
{
270
195
  Table *table= static_cast<Table *>(entry);
271
196
  table->intern_close_table();
272
 
  if (!table->in_use)
 
197
  if (not table->in_use)
273
198
  {
274
199
    table->next->prev=table->prev;              /* remove from used chain */
275
200
    table->prev->next=table->next;
566
491
{
567
492
  for (; table; table= table->*link )
568
493
  {
569
 
    if ((table->table == 0 || table->table->s->tmp_table == NO_TMP_TABLE) &&
 
494
    if ((table->table == 0 || table->table->s->tmp_table == STANDARD_TABLE) &&
570
495
        strcmp(table->db, db_name) == 0 &&
571
496
        strcmp(table->table_name, table_name) == 0)
572
497
      break;
630
555
  if (table->table)
631
556
  {
632
557
    /* temporary table is always unique */
633
 
    if (table->table && table->table->s->tmp_table != NO_TMP_TABLE)
 
558
    if (table->table && table->table->s->tmp_table != STANDARD_TABLE)
634
559
      return 0;
635
560
    table= table->find_underlying_table(table->table);
636
561
    /*
661
586
}
662
587
 
663
588
 
 
589
void Session::doGetTableNames(CachedDirectory &,
 
590
                              const std::string& db_name,
 
591
                              std::set<std::string>& set_of_names)
 
592
{
 
593
  for (Table *table= temporary_tables ; table ; table= table->next)
 
594
  {
 
595
    if (not db_name.compare(table->s->db.str))
 
596
    {
 
597
      set_of_names.insert(table->s->table_name.str);
 
598
    }
 
599
  }
 
600
}
 
601
 
 
602
int Session::doGetTableDefinition(const char *,
 
603
                                  const char *db_arg,
 
604
                                  const char *table_name_arg,
 
605
                                  const bool ,
 
606
                                  message::Table *table_proto)
 
607
{
 
608
  for (Table *table= temporary_tables ; table ; table= table->next)
 
609
  {
 
610
    if (table->s->tmp_table == TEMP_TABLE)
 
611
    {
 
612
      if (not strcmp(db_arg, table->s->db.str))
 
613
      {
 
614
        if (not strcmp(table_name_arg, table->s->table_name.str))
 
615
        {
 
616
          if (table_proto)
 
617
            table_proto->CopyFrom(*(table->s->getTableProto()));
 
618
 
 
619
          return EEXIST;
 
620
        }
 
621
      }
 
622
    }
 
623
  }
 
624
 
 
625
  return ENOENT;
 
626
}
 
627
 
664
628
Table *Session::find_temporary_table(const char *new_db, const char *table_name)
665
629
{
666
630
  char  key[MAX_DBKEY_LENGTH];
829
793
      that something has happened.
830
794
    */
831
795
    unlink_open_table(table);
832
 
    TableIdentifier identifier(db_name, table_name, NO_TMP_TABLE);
 
796
    TableIdentifier identifier(db_name, table_name, STANDARD_TABLE);
833
797
    quick_rm_table(*this, identifier);
834
798
    pthread_mutex_unlock(&LOCK_open);
835
799
  }
1325
1289
 
1326
1290
    if (table_list->create)
1327
1291
    {
1328
 
      TableIdentifier  lock_table_identifier(table_list->db, table_list->table_name, NO_TMP_TABLE);
 
1292
      TableIdentifier  lock_table_identifier(table_list->db, table_list->table_name, STANDARD_TABLE);
1329
1293
 
1330
 
      if (plugin::StorageEngine::getTableDefinition(*this, lock_table_identifier) != EEXIST)
 
1294
      if (not plugin::StorageEngine::doesTableExist(*this, lock_table_identifier))
1331
1295
      {
1332
1296
        /*
1333
1297
          Table to be created, so we need to create placeholder in table-cache.
1379
1343
  table->reginfo.lock_type= TL_READ; /* Assume read */
1380
1344
 
1381
1345
reset:
1382
 
  assert(table->s->ref_count > 0 || table->s->tmp_table != NO_TMP_TABLE);
 
1346
  assert(table->s->ref_count > 0 || table->s->tmp_table != STANDARD_TABLE);
1383
1347
 
1384
1348
  if (lex->need_correct_ident())
1385
1349
    table->alias_name_used= my_strcasecmp(table_alias_charset,
2072
2036
    (*counter)++;
2073
2037
 
2074
2038
    /*
 
2039
     * Is the user authorized to see this table? Do this before we check
 
2040
     * to see if it exists so that an unauthorized user cannot phish for
 
2041
     * table/schema information via error messages
 
2042
     */
 
2043
    if (not plugin::Authorization::isAuthorized(getSecurityContext(),
 
2044
                                                string(tables->db),
 
2045
                                                string(tables->table_name)))
 
2046
    {
 
2047
      result= -1;                               // Fatal error
 
2048
      break;
 
2049
    }
 
2050
 
 
2051
 
 
2052
    /*
2075
2053
      Not a placeholder: must be a base table or a view, and the table is
2076
2054
      not opened yet. Try to open the table.
2077
2055
    */
2110
2088
    {
2111
2089
      if (tables->lock_type == TL_WRITE_DEFAULT)
2112
2090
        tables->table->reginfo.lock_type= update_lock_default;
2113
 
      else if (tables->table->s->tmp_table == NO_TMP_TABLE)
 
2091
      else if (tables->table->s->tmp_table == STANDARD_TABLE)
2114
2092
        tables->table->reginfo.lock_type= tables->lock_type;
2115
2093
    }
2116
2094
  }
4145
4123
    return false;
4146
4124
 
4147
4125
  /*
4148
 
TODO: in the case when we skipped all columns because there was a
4149
 
qualified '*', and all columns were coalesced, we have to give a more
4150
 
meaningful message than ER_BAD_TABLE_ERROR.
 
4126
    @TODO in the case when we skipped all columns because there was a
 
4127
    qualified '*', and all columns were coalesced, we have to give a more
 
4128
    meaningful message than ER_BAD_TABLE_ERROR.
4151
4129
  */
4152
4130
  if (!table_name)
4153
4131
    my_message(ER_NO_TABLES_USED, ER(ER_NO_TABLES_USED), MYF(0));
4418
4396
and afterwards delete those marked unused.
4419
4397
*/
4420
4398
 
4421
 
void remove_db_from_cache(const char *db)
 
4399
void remove_db_from_cache(const std::string schema_name)
4422
4400
{
4423
4401
  safe_mutex_assert_owner(&LOCK_open);
4424
4402
 
4425
4403
  for (uint32_t idx=0 ; idx < open_cache.records ; idx++)
4426
4404
  {
4427
4405
    Table *table=(Table*) hash_element(&open_cache,idx);
4428
 
    if (!strcmp(table->s->db.str, db))
 
4406
    if (not strcmp(table->s->db.str, schema_name.c_str()))
4429
4407
    {
4430
4408
      table->s->version= 0L;                    /* Free when thread is ready */
4431
 
      if (!table->in_use)
 
4409
      if (not table->in_use)
4432
4410
        relink_unused(table);
4433
4411
    }
4434
4412
  }