~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/session.cc

  • Committer: Brian Aker
  • Date: 2009-05-30 22:30:05 UTC
  • mto: This revision was merged to the branch mainline in revision 1045.
  • Revision ID: brian@gaz-20090530223005-hmylm6iywddfentm
A lot of little cleanups (most based off lcov)

Show diffs side-by-side

added added

removed removed

Lines of Context:
399
399
  if (locked_tables)
400
400
  {
401
401
    lock=locked_tables; locked_tables=0;
402
 
    close_thread_tables(this);
 
402
    close_thread_tables();
403
403
  }
404
404
  hash_free(&user_vars);
405
405
  close_temporary_tables();
821
821
  {
822
822
    lock= locked_tables;
823
823
    locked_tables= 0;                   // Will be automatically closed
824
 
    close_thread_tables(this);                  // Free tables
 
824
    close_thread_tables();                      // Free tables
825
825
  }
826
826
  if (! endActiveTransaction())
827
827
    result= false;
2116
2116
 
2117
2117
  return entry;
2118
2118
}
 
2119
 
 
2120
/**
 
2121
  Mark all temporary tables which were used by the current statement or
 
2122
  substatement as free for reuse, but only if the query_id can be cleared.
 
2123
 
 
2124
  @param session thread context
 
2125
 
 
2126
  @remark For temp tables associated with a open SQL HANDLER the query_id
 
2127
          is not reset until the HANDLER is closed.
 
2128
*/
 
2129
 
 
2130
void Session::mark_temp_tables_as_free_for_reuse()
 
2131
{
 
2132
  for (Table *table= temporary_tables ; table ; table= table->next)
 
2133
  {
 
2134
    if (table->query_id == query_id)
 
2135
    {
 
2136
      table->query_id= 0;
 
2137
      table->file->ha_reset();
 
2138
    }
 
2139
  }
 
2140
}
 
2141
 
 
2142
 
 
2143
/*
 
2144
  Mark all tables in the list which were used by current substatement
 
2145
  as free for reuse.
 
2146
 
 
2147
  SYNOPSIS
 
2148
    mark_used_tables_as_free_for_reuse()
 
2149
      session   - thread context
 
2150
      table - head of the list of tables
 
2151
 
 
2152
  DESCRIPTION
 
2153
    Marks all tables in the list which were used by current substatement
 
2154
    (they are marked by its query_id) as free for reuse.
 
2155
 
 
2156
  NOTE
 
2157
    The reason we reset query_id is that it's not enough to just test
 
2158
    if table->query_id != session->query_id to know if a table is in use.
 
2159
 
 
2160
    For example
 
2161
    SELECT f1_that_uses_t1() FROM t1;
 
2162
    In f1_that_uses_t1() we will see one instance of t1 where query_id is
 
2163
    set to query_id of original query.
 
2164
*/
 
2165
 
 
2166
void Session::mark_used_tables_as_free_for_reuse(Table *table)
 
2167
{
 
2168
  for (; table ; table= table->next)
 
2169
  {
 
2170
    if (table->query_id == query_id)
 
2171
    {
 
2172
      table->query_id= 0;
 
2173
      table->file->ha_reset();
 
2174
    }
 
2175
  }
 
2176
}
 
2177
 
 
2178
 
 
2179
/*
 
2180
  Close all tables used by the current substatement, or all tables
 
2181
  used by this thread if we are on the upper level.
 
2182
 
 
2183
  SYNOPSIS
 
2184
    close_thread_tables()
 
2185
    session                     Thread handler
 
2186
 
 
2187
  IMPLEMENTATION
 
2188
    Unlocks tables and frees derived tables.
 
2189
    Put all normal tables used by thread in free list.
 
2190
 
 
2191
    It will only close/mark as free for reuse tables opened by this
 
2192
    substatement, it will also check if we are closing tables after
 
2193
    execution of complete query (i.e. we are on upper level) and will
 
2194
    leave prelocked mode if needed.
 
2195
*/
 
2196
 
 
2197
void Session::close_thread_tables()
 
2198
{
 
2199
  Table *table;
 
2200
 
 
2201
  /*
 
2202
    We are assuming here that session->derived_tables contains ONLY derived
 
2203
    tables for this substatement. i.e. instead of approach which uses
 
2204
    query_id matching for determining which of the derived tables belong
 
2205
    to this substatement we rely on the ability of substatements to
 
2206
    save/restore session->derived_tables during their execution.
 
2207
 
 
2208
    TODO: Probably even better approach is to simply associate list of
 
2209
          derived tables with (sub-)statement instead of thread and destroy
 
2210
          them at the end of its execution.
 
2211
  */
 
2212
  if (derived_tables)
 
2213
  {
 
2214
    Table *next;
 
2215
    /*
 
2216
      Close all derived tables generated in queries like
 
2217
      SELECT * FROM (SELECT * FROM t1)
 
2218
    */
 
2219
    for (table= derived_tables ; table ; table= next)
 
2220
    {
 
2221
      next= table->next;
 
2222
      table->free_tmp_table(this);
 
2223
    }
 
2224
    derived_tables= 0;
 
2225
  }
 
2226
 
 
2227
  /*
 
2228
    Mark all temporary tables used by this statement as free for reuse.
 
2229
  */
 
2230
  mark_temp_tables_as_free_for_reuse();
 
2231
  /*
 
2232
    Let us commit transaction for statement. Since in 5.0 we only have
 
2233
    one statement transaction and don't allow several nested statement
 
2234
    transactions this call will do nothing if we are inside of stored
 
2235
    function or trigger (i.e. statement transaction is already active and
 
2236
    does not belong to statement for which we do close_thread_tables()).
 
2237
    TODO: This should be fixed in later releases.
 
2238
   */
 
2239
  if (!(state_flags & Open_tables_state::BACKUPS_AVAIL))
 
2240
  {
 
2241
    main_da.can_overwrite_status= true;
 
2242
    ha_autocommit_or_rollback(this, is_error());
 
2243
    main_da.can_overwrite_status= false;
 
2244
    transaction.stmt.reset();
 
2245
  }
 
2246
 
 
2247
  if (locked_tables)
 
2248
  {
 
2249
 
 
2250
    /* Ensure we are calling ha_reset() for all used tables */
 
2251
    mark_used_tables_as_free_for_reuse(open_tables);
 
2252
 
 
2253
    /*
 
2254
      We are under simple LOCK TABLES so should not do anything else.
 
2255
    */
 
2256
    return;
 
2257
  }
 
2258
 
 
2259
  if (lock)
 
2260
  {
 
2261
    /*
 
2262
      For RBR we flush the pending event just before we unlock all the
 
2263
      tables.  This means that we are at the end of a topmost
 
2264
      statement, so we ensure that the STMT_END_F flag is set on the
 
2265
      pending event.  For statements that are *inside* stored
 
2266
      functions, the pending event will not be flushed: that will be
 
2267
      handled either before writing a query log event (inside
 
2268
      binlog_query()) or when preparing a pending event.
 
2269
     */
 
2270
    mysql_unlock_tables(this, lock);
 
2271
    lock= 0;
 
2272
  }
 
2273
  /*
 
2274
    Note that we need to hold LOCK_open while changing the
 
2275
    open_tables list. Another thread may work on it.
 
2276
    (See: remove_table_from_cache(), mysql_wait_completed_table())
 
2277
    Closing a MERGE child before the parent would be fatal if the
 
2278
    other thread tries to abort the MERGE lock in between.
 
2279
  */
 
2280
  if (open_tables)
 
2281
    close_open_tables();
 
2282
}