~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.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:
802
802
 
803
803
 
804
804
/**
805
 
  Mark all temporary tables which were used by the current statement or
806
 
  substatement as free for reuse, but only if the query_id can be cleared.
807
 
 
808
 
  @param session thread context
809
 
 
810
 
  @remark For temp tables associated with a open SQL HANDLER the query_id
811
 
          is not reset until the HANDLER is closed.
812
 
*/
813
 
 
814
 
static void mark_temp_tables_as_free_for_reuse(Session *session)
815
 
{
816
 
  for (Table *table= session->temporary_tables ; table ; table= table->next)
817
 
  {
818
 
    if (table->query_id == session->query_id)
819
 
    {
820
 
      table->query_id= 0;
821
 
      table->file->ha_reset();
822
 
    }
823
 
  }
824
 
}
825
 
 
826
 
 
827
 
/*
828
 
  Mark all tables in the list which were used by current substatement
829
 
  as free for reuse.
830
 
 
831
 
  SYNOPSIS
832
 
    mark_used_tables_as_free_for_reuse()
833
 
      session   - thread context
834
 
      table - head of the list of tables
835
 
 
836
 
  DESCRIPTION
837
 
    Marks all tables in the list which were used by current substatement
838
 
    (they are marked by its query_id) as free for reuse.
839
 
 
840
 
  NOTE
841
 
    The reason we reset query_id is that it's not enough to just test
842
 
    if table->query_id != session->query_id to know if a table is in use.
843
 
 
844
 
    For example
845
 
    SELECT f1_that_uses_t1() FROM t1;
846
 
    In f1_that_uses_t1() we will see one instance of t1 where query_id is
847
 
    set to query_id of original query.
848
 
*/
849
 
 
850
 
static void mark_used_tables_as_free_for_reuse(Session *session, Table *table)
851
 
{
852
 
  for (; table ; table= table->next)
853
 
  {
854
 
    if (table->query_id == session->query_id)
855
 
    {
856
 
      table->query_id= 0;
857
 
      table->file->ha_reset();
858
 
    }
859
 
  }
860
 
}
861
 
 
862
 
 
863
 
/**
864
805
  Auxiliary function to close all tables in the open_tables list.
865
806
 
866
807
  @param session Thread context.
868
809
  @remark It should not ordinarily be called directly.
869
810
*/
870
811
 
871
 
static void close_open_tables(Session *session)
 
812
void Session::close_open_tables()
872
813
{
873
814
  bool found_old_table= 0;
874
815
 
876
817
 
877
818
  pthread_mutex_lock(&LOCK_open);
878
819
 
879
 
  while (session->open_tables)
880
 
    found_old_table|= close_thread_table(session, &session->open_tables);
881
 
  session->some_tables_deleted= 0;
 
820
  while (open_tables)
 
821
    found_old_table|= close_thread_table(this, &open_tables);
 
822
  some_tables_deleted= 0;
882
823
 
883
824
  /* Free tables to hold down open files */
884
825
  while (open_cache.records > table_cache_size && unused_tables)
893
834
}
894
835
 
895
836
 
896
 
/*
897
 
  Close all tables used by the current substatement, or all tables
898
 
  used by this thread if we are on the upper level.
899
 
 
900
 
  SYNOPSIS
901
 
    close_thread_tables()
902
 
    session                     Thread handler
903
 
 
904
 
  IMPLEMENTATION
905
 
    Unlocks tables and frees derived tables.
906
 
    Put all normal tables used by thread in free list.
907
 
 
908
 
    It will only close/mark as free for reuse tables opened by this
909
 
    substatement, it will also check if we are closing tables after
910
 
    execution of complete query (i.e. we are on upper level) and will
911
 
    leave prelocked mode if needed.
912
 
*/
913
 
 
914
 
void close_thread_tables(Session *session)
915
 
{
916
 
  Table *table;
917
 
 
918
 
  /*
919
 
    We are assuming here that session->derived_tables contains ONLY derived
920
 
    tables for this substatement. i.e. instead of approach which uses
921
 
    query_id matching for determining which of the derived tables belong
922
 
    to this substatement we rely on the ability of substatements to
923
 
    save/restore session->derived_tables during their execution.
924
 
 
925
 
    TODO: Probably even better approach is to simply associate list of
926
 
          derived tables with (sub-)statement instead of thread and destroy
927
 
          them at the end of its execution.
928
 
  */
929
 
  if (session->derived_tables)
930
 
  {
931
 
    Table *next;
932
 
    /*
933
 
      Close all derived tables generated in queries like
934
 
      SELECT * FROM (SELECT * FROM t1)
935
 
    */
936
 
    for (table= session->derived_tables ; table ; table= next)
937
 
    {
938
 
      next= table->next;
939
 
      table->free_tmp_table(session);
940
 
    }
941
 
    session->derived_tables= 0;
942
 
  }
943
 
 
944
 
  /*
945
 
    Mark all temporary tables used by this statement as free for reuse.
946
 
  */
947
 
  mark_temp_tables_as_free_for_reuse(session);
948
 
  /*
949
 
    Let us commit transaction for statement. Since in 5.0 we only have
950
 
    one statement transaction and don't allow several nested statement
951
 
    transactions this call will do nothing if we are inside of stored
952
 
    function or trigger (i.e. statement transaction is already active and
953
 
    does not belong to statement for which we do close_thread_tables()).
954
 
    TODO: This should be fixed in later releases.
955
 
   */
956
 
  if (!(session->state_flags & Open_tables_state::BACKUPS_AVAIL))
957
 
  {
958
 
    session->main_da.can_overwrite_status= true;
959
 
    ha_autocommit_or_rollback(session, session->is_error());
960
 
    session->main_da.can_overwrite_status= false;
961
 
    session->transaction.stmt.reset();
962
 
  }
963
 
 
964
 
  if (session->locked_tables)
965
 
  {
966
 
 
967
 
    /* Ensure we are calling ha_reset() for all used tables */
968
 
    mark_used_tables_as_free_for_reuse(session, session->open_tables);
969
 
 
970
 
    /*
971
 
      We are under simple LOCK TABLES so should not do anything else.
972
 
    */
973
 
    return;
974
 
  }
975
 
 
976
 
  if (session->lock)
977
 
  {
978
 
    /*
979
 
      For RBR we flush the pending event just before we unlock all the
980
 
      tables.  This means that we are at the end of a topmost
981
 
      statement, so we ensure that the STMT_END_F flag is set on the
982
 
      pending event.  For statements that are *inside* stored
983
 
      functions, the pending event will not be flushed: that will be
984
 
      handled either before writing a query log event (inside
985
 
      binlog_query()) or when preparing a pending event.
986
 
     */
987
 
    mysql_unlock_tables(session, session->lock);
988
 
    session->lock=0;
989
 
  }
990
 
  /*
991
 
    Note that we need to hold LOCK_open while changing the
992
 
    open_tables list. Another thread may work on it.
993
 
    (See: remove_table_from_cache(), mysql_wait_completed_table())
994
 
    Closing a MERGE child before the parent would be fatal if the
995
 
    other thread tries to abort the MERGE lock in between.
996
 
  */
997
 
  if (session->open_tables)
998
 
    close_open_tables(session);
999
 
 
1000
 
  return;
1001
 
}
1002
 
 
1003
 
 
1004
837
/* move one table to free list */
1005
838
 
1006
839
bool close_thread_table(Session *session, Table **table_ptr)
3333
3166
  session->lex->chop_off_not_own_tables();
3334
3167
  for (TableList *tmp= *tables; tmp; tmp= tmp->next_global)
3335
3168
    tmp->table= 0;
3336
 
  close_thread_tables(session);
 
3169
  session->close_thread_tables();
3337
3170
}
3338
3171
 
3339
3172