~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/db.cc

  • Committer: lbieber
  • Date: 2009-06-09 03:08:16 UTC
  • mfrom: (1055 staging)
  • mto: (1061.1.3 merge-all)
  • mto: This revision was merged to the branch mainline in revision 1062.
  • Revision ID: lbieber@lbieber-laptop-20090609030816-r0exbzj7gbirrab1
merge with latest from trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
53
53
            
54
54
 
55
55
 
56
 
/* Database lock hash */
57
 
HASH lock_db_cache;
58
 
pthread_mutex_t LOCK_lock_db;
59
 
bool dbcache_init= false;
60
 
int creating_database= 0;  // how many database locks are made
61
 
 
62
 
 
63
56
/* Structure for database lock */
64
57
typedef struct my_dblock_st
65
58
{
68
61
} my_dblock_t;
69
62
 
70
63
 
71
 
/*
72
 
  lock_db key.
73
 
*/
74
 
 
75
 
extern "C" unsigned char* lock_db_get_key(my_dblock_t *, size_t *, bool not_used);
76
 
 
77
 
unsigned char* lock_db_get_key(my_dblock_t *ptr, size_t *length,
78
 
                       bool )
79
 
{
80
 
  *length= ptr->name_length;
81
 
  return (unsigned char*) ptr->name;
82
 
}
83
 
 
84
 
 
85
 
/*
86
 
  Free lock_db hash element.
87
 
*/
88
 
 
89
 
extern "C" void lock_db_free_element(void *ptr);
90
 
 
91
 
void lock_db_free_element(void *ptr)
92
 
{
93
 
  free(ptr);
94
 
}
95
 
 
96
 
 
97
 
/*
98
 
  Delete a database lock entry from hash.
99
 
*/
100
 
 
101
 
void lock_db_delete(const char *name, uint32_t length)
102
 
{
103
 
  my_dblock_t *opt;
104
 
  safe_mutex_assert_owner(&LOCK_lock_db);
105
 
  if ((opt= (my_dblock_t *)hash_search(&lock_db_cache,
106
 
                                       (const unsigned char*) name, length)))
107
 
    hash_delete(&lock_db_cache, (unsigned char*) opt);
108
 
}
109
 
 
110
 
/*
111
 
  Initialize database option hash and locked database hash.
112
 
 
113
 
  SYNOPSIS
114
 
    my_database_names()
115
 
 
116
 
  NOTES
117
 
    Must be called before any other database function is called.
118
 
 
119
 
  RETURN
120
 
    0   ok
121
 
    1   Fatal error
122
 
*/
123
 
 
124
 
bool my_database_names_init(void)
125
 
{
126
 
  bool error= false;
127
 
  if (!dbcache_init)
128
 
  {
129
 
    dbcache_init= true;
130
 
    error= hash_init(&lock_db_cache, &my_charset_bin,
131
 
                     32, 0, 0, (hash_get_key) lock_db_get_key,
132
 
                     lock_db_free_element,0);
133
 
 
134
 
  }
135
 
  return error;
136
 
}
137
 
 
138
64
/**
139
65
  Return default database collation.
140
66
 
773
699
  }
774
700
}
775
701
 
776
 
 
777
 
 
778
 
/**
779
 
  Backup the current database name before switch.
780
 
 
781
 
  @param[in]      session             thread handle
782
 
  @param[in, out] saved_db_name   IN: "str" points to a buffer where to store
783
 
                                  the old database name, "length" contains the
784
 
                                  buffer size
785
 
                                  OUT: if the current (default) database is
786
 
                                  not NULL, its name is copied to the
787
 
                                  buffer pointed at by "str"
788
 
                                  and "length" is updated accordingly.
789
 
                                  Otherwise "str" is set to NULL and
790
 
                                  "length" is set to 0.
791
 
*/
792
 
 
793
 
static void backup_current_db_name(Session *session,
794
 
                                   LEX_STRING *saved_db_name)
795
 
{
796
 
  if (!session->db)
797
 
  {
798
 
    /* No current (default) database selected. */
799
 
 
800
 
    saved_db_name->str= NULL;
801
 
    saved_db_name->length= 0;
802
 
  }
803
 
  else
804
 
  {
805
 
    strncpy(saved_db_name->str, session->db, saved_db_name->length - 1);
806
 
    saved_db_name->length= session->db_length;
807
 
  }
808
 
}
809
 
 
810
 
 
811
702
/**
812
703
  Return true if db1_name is equal to db2_name, false otherwise.
813
704
 
899
790
  LEX_STRING new_db_file_name;
900
791
  const CHARSET_INFO *db_default_cl;
901
792
 
902
 
  if (new_db_name == NULL ||
903
 
      new_db_name->length == 0)
904
 
  {
905
 
    if (force_switch)
906
 
    {
907
 
      /*
908
 
        This can happen only if we're switching the current database back
909
 
        after loading stored program. The thing is that loading of stored
910
 
        program can happen when there is no current database.
911
 
 
912
 
        TODO: actually, new_db_name and new_db_name->str seem to be always
913
 
        non-NULL. In case of stored program, new_db_name->str == "" and
914
 
        new_db_name->length == 0.
915
 
      */
916
 
 
917
 
      mysql_change_db_impl(session, NULL);
918
 
 
919
 
      return false;
920
 
    }
921
 
    else
922
 
    {
923
 
      my_message(ER_NO_DB_ERROR, ER(ER_NO_DB_ERROR), MYF(0));
924
 
 
925
 
      return true;
926
 
    }
927
 
  }
 
793
  assert(new_db_name);
 
794
  assert(new_db_name->length);
928
795
 
929
796
  if (my_strcasecmp(system_charset_info, new_db_name->str,
930
797
                    INFORMATION_SCHEMA_NAME.c_str()) == 0)
1018
885
  return false;
1019
886
}
1020
887
 
1021
 
 
1022
 
/**
1023
 
  Change the current database and its attributes if needed.
1024
 
 
1025
 
  @param          session             thread handle
1026
 
  @param          new_db_name     database name
1027
 
  @param[in, out] saved_db_name   IN: "str" points to a buffer where to store
1028
 
                                  the old database name, "length" contains the
1029
 
                                  buffer size
1030
 
                                  OUT: if the current (default) database is
1031
 
                                  not NULL, its name is copied to the
1032
 
                                  buffer pointed at by "str"
1033
 
                                  and "length" is updated accordingly.
1034
 
                                  Otherwise "str" is set to NULL and
1035
 
                                  "length" is set to 0.
1036
 
  @param          force_switch    @see mysql_change_db()
1037
 
  @param[out]     cur_db_changed  out-flag to indicate whether the current
1038
 
                                  database has been changed (valid only if
1039
 
                                  the function suceeded)
1040
 
*/
1041
 
 
1042
 
bool mysql_opt_change_db(Session *session,
1043
 
                         const LEX_STRING *new_db_name,
1044
 
                         LEX_STRING *saved_db_name,
1045
 
                         bool force_switch,
1046
 
                         bool *cur_db_changed)
1047
 
{
1048
 
  *cur_db_changed= !cmp_db_names(session->db, new_db_name->str);
1049
 
 
1050
 
  if (!*cur_db_changed)
1051
 
    return false;
1052
 
 
1053
 
  backup_current_db_name(session, saved_db_name);
1054
 
 
1055
 
  return mysql_change_db(session, new_db_name, force_switch);
1056
 
}
1057
 
 
1058
 
 
1059
888
/*
1060
889
  Check if there is directory for the database name.
1061
890