~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/db.cc

Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#include <drizzled/lock.h>
35
35
#include <drizzled/errmsg_print.h>
36
36
#include <drizzled/transaction_services.h>
 
37
#include <drizzled/message/schema.pb.h>
37
38
 
38
39
extern drizzled::TransactionServices transaction_services;
39
40
 
51
52
static void mysql_change_db_impl(Session *session, LEX_STRING *new_db_name);
52
53
            
53
54
 
54
 
 
55
 
/* Structure for database lock */
56
 
typedef struct my_dblock_st
57
 
{
58
 
  char *name;        /* Database name        */
59
 
  uint32_t name_length;  /* Database length name */
60
 
} my_dblock_t;
61
 
 
62
 
 
63
55
/**
64
56
  Return default database collation.
65
57
 
72
64
 
73
65
const CHARSET_INFO *get_default_db_collation(const char *db_name)
74
66
{
75
 
  HA_CREATE_INFO db_info;
76
 
 
77
 
  /*
78
 
    db_info.default_table_charset contains valid character set
79
 
    (collation_server).
80
 
  */
81
 
 
82
 
  load_db_opt_by_name(db_name, &db_info);
83
 
 
84
 
  return db_info.default_table_charset;
 
67
  drizzled::message::Schema db;
 
68
 
 
69
  get_database_metadata(db_name, &db);
 
70
 
 
71
  /* If for some reason the db.opt file lacks a collation,
 
72
     we just return the default */
 
73
 
 
74
  if (db.has_collation())
 
75
  {
 
76
    const string buffer= db.collation();
 
77
    const CHARSET_INFO* cs= get_charset_by_name(buffer.c_str());
 
78
 
 
79
    if (!cs)
 
80
    {
 
81
      errmsg_printf(ERRMSG_LVL_ERROR,
 
82
                    _("Error while loading database options: '%s':"),db_name);
 
83
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
 
84
 
 
85
      return default_charset_info;
 
86
    }
 
87
 
 
88
    return cs;
 
89
  }
 
90
 
 
91
  return default_charset_info;
85
92
}
86
93
 
87
94
/* path is path to database, not schema file */
130
137
  return 0;
131
138
}
132
139
 
133
 
static int load_db_opt(const char *path, HA_CREATE_INFO *create)
134
 
{
135
 
  drizzled::message::Schema db;
136
 
 
137
 
  memset(create, 0, sizeof(*create));
138
 
  create->default_table_charset= default_charset_info;
139
 
 
140
 
  int fd= open(path, O_RDONLY);
141
 
 
142
 
  if (fd == -1)
143
 
    return errno;
144
 
 
145
 
  if (!db.ParseFromFileDescriptor(fd))
146
 
  {
147
 
    close(fd);
148
 
    return -1;
149
 
  }
150
 
  close(fd);
151
 
 
152
 
  /* If for some reason the db.opt file lacks a collation, we just return the default */
153
 
  if (db.has_collation())
154
 
  {
155
 
    string buffer;
156
 
    buffer= db.collation();
157
 
    if (!(create->default_table_charset= get_charset_by_name(buffer.c_str())))
158
 
    {
159
 
      errmsg_printf(ERRMSG_LVL_ERROR,
160
 
                    _("Error while loading database options: '%s':"),path);
161
 
      errmsg_printf(ERRMSG_LVL_ERROR, ER(ER_UNKNOWN_COLLATION), buffer.c_str());
162
 
      create->default_table_charset= default_charset_info;
163
 
      return -1;
164
 
    }
165
 
  }
166
 
 
167
 
  return 0;
168
 
}
169
 
 
170
 
int load_db_opt_by_name(const char *db_name, HA_CREATE_INFO *db_create_info)
 
140
int get_database_metadata(const char *dbname, drizzled::message::Schema *db)
171
141
{
172
142
  char db_opt_path[FN_REFLEN];
173
143
  size_t length;
177
147
    to avoid table name to file name encoding.
178
148
  */
179
149
  length= build_table_filename(db_opt_path, sizeof(db_opt_path),
180
 
                              db_name, "", false);
 
150
                              dbname, "", false);
181
151
  strcpy(db_opt_path + length, MY_DB_OPT_FILE);
182
152
 
183
 
  return load_db_opt(db_opt_path, db_create_info);
 
153
  int fd= open(db_opt_path, O_RDONLY);
 
154
 
 
155
  if (fd == -1)
 
156
    return errno;
 
157
 
 
158
  if (!db->ParseFromFileDescriptor(fd))
 
159
  {
 
160
    close(fd);
 
161
    return -1;
 
162
  }
 
163
  close(fd);
 
164
 
 
165
  return 0;
184
166
}
185
167
 
186
 
 
187
168
/*
188
169
  Create a database
189
170