~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/schema.cc

  • Committer: Brian Aker
  • Date: 2010-09-20 00:00:20 UTC
  • Revision ID: brian@tangent.org-20100920000020-s6x30brpajr83pkr
Update session/memory to use boost specific.

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <assert.h>
24
24
 
25
 
#include "drizzled/schema_identifier.h"
 
25
#include "drizzled/identifier.h"
26
26
#include "drizzled/session.h"
27
27
#include "drizzled/current_session.h"
28
28
#include "drizzled/internal/my_sys.h"
32
32
#include <sstream>
33
33
#include <cstdio>
34
34
 
 
35
#include <boost/algorithm/string/compare.hpp>
 
36
 
35
37
using namespace std;
36
38
 
37
39
namespace drizzled
38
40
{
39
41
 
40
 
extern char *drizzle_tmpdir;
 
42
extern string drizzle_tmpdir;
41
43
extern pid_t current_pid;
42
44
 
43
45
static const char hexchars[]= "0123456789abcdef";
44
46
 
45
 
static bool tablename_to_filename(const char *from, char *to, size_t to_length);
 
47
static bool tablename_to_filename(const string &from, string &to);
46
48
 
47
 
static size_t build_schema_filename(std::string &path, const char *db)
 
49
static size_t build_schema_filename(string &path, const string &db)
48
50
{
49
 
  char dbbuff[FN_REFLEN];
 
51
  string dbbuff("");
50
52
  bool conversion_error= false;
51
53
 
52
 
  memset(dbbuff, 0, sizeof(dbbuff));
53
 
  conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
 
54
  conversion_error= tablename_to_filename(db, dbbuff);
54
55
  if (conversion_error)
55
56
  {
56
57
    errmsg_printf(ERRMSG_LVL_ERROR,
61
62
   
62
63
 
63
64
  int rootdir_len= strlen(FN_ROOTDIR);
64
 
  path.append(drizzle_data_home);
 
65
  path.append(data_home);
65
66
  ssize_t without_rootdir= path.length() - rootdir_len;
66
67
 
67
68
  /* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
86
87
    tablename_to_filename()
87
88
      from                      The table name
88
89
      to                OUT     The cursor name
89
 
      to_length                 The size of the cursor name buffer.
90
90
 
91
91
  RETURN
92
92
    true if errors happen. false on success.
93
93
*/
94
 
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
 
94
static bool tablename_to_filename(const string &from, string &to)
95
95
{
96
96
  
97
 
  size_t length= 0;
98
 
  for (; *from  && length < to_length; length++, from++)
 
97
  string::const_iterator iter= from.begin();
 
98
  for (; iter != from.end(); ++iter)
99
99
  {
100
 
    if ((*from >= '0' && *from <= '9') ||
101
 
        (*from >= 'A' && *from <= 'Z') ||
102
 
        (*from >= 'a' && *from <= 'z') ||
103
 
/* OSX defines an extra set of high-bit and multi-byte characters
104
 
   that cannot be used on the filesystem. Instead of trying to sort
105
 
   those out, we'll just escape encode all high-bit-set chars on OSX.
106
 
   It won't really hurt anything - it'll just make some filenames ugly. */
 
100
    if ((*iter >= '0' && *iter <= '9') ||
 
101
        (*iter >= 'a' && *iter <= 'z') ||
 
102
        /* OSX defines an extra set of high-bit and multi-byte characters
 
103
          that cannot be used on the filesystem. Instead of trying to sort
 
104
          those out, we'll just escape encode all high-bit-set chars on OSX.
 
105
          It won't really hurt anything - it'll just make some filenames ugly. */
107
106
#if !defined(TARGET_OS_OSX)
108
 
        ((unsigned char)*from >= 128) ||
 
107
        ((unsigned char)*iter >= 128) ||
109
108
#endif
110
 
        (*from == '_') ||
111
 
        (*from == ' ') ||
112
 
        (*from == '-'))
113
 
    {
114
 
      to[length]= *from;
 
109
        (*iter == '_') ||
 
110
        (*iter == ' ') ||
 
111
        (*iter == '-'))
 
112
    {
 
113
      to.push_back(*iter);
 
114
      continue;
 
115
    }
 
116
 
 
117
    if ((*iter >= 'A' && *iter <= 'Z'))
 
118
    {
 
119
      to.push_back(tolower(*iter));
115
120
      continue;
116
121
    }
117
122
   
118
 
    if (length + 3 >= to_length)
119
 
      return true;
120
 
 
121
123
    /* We need to escape this char in a way that can be reversed */
122
 
    to[length++]= '@';
123
 
    to[length++]= hexchars[(*from >> 4) & 15];
124
 
    to[length]= hexchars[(*from) & 15];
 
124
    to.push_back('@');
 
125
    to.push_back(hexchars[(*iter >> 4) & 15]);
 
126
    to.push_back(hexchars[(*iter) & 15]);
125
127
  }
126
128
 
127
 
  if (internal::check_if_legal_tablename(to) &&
128
 
      length + 4 < to_length)
 
129
  if (internal::check_if_legal_tablename(to.c_str()))
129
130
  {
130
 
    memcpy(to + length, "@@@", 4);
131
 
    length+= 3;
 
131
    to.append("@@@");
132
132
  }
133
133
  return false;
134
134
}
135
135
 
 
136
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
 
137
  db(db_arg),
 
138
  db_path("")
 
139
 
140
  if (not db_arg.empty())
 
141
  {
 
142
    drizzled::build_schema_filename(db_path, db);
 
143
    assert(db_path.length()); // TODO throw exception, this is a possibility
 
144
  }
 
145
}
 
146
 
136
147
const std::string &SchemaIdentifier::getSQLPath()
137
148
{
138
149
  return getSchemaName();
139
150
}
140
151
 
141
 
const std::string &SchemaIdentifier::getPath()
 
152
const std::string &SchemaIdentifier::getPath() const
142
153
{
143
 
  if (db_path.empty())
144
 
  {
145
 
    drizzled::build_schema_filename(db_path, lower_db.c_str());
146
 
    assert(db_path.length()); // TODO throw exception, this is a possibility
147
 
  }
148
 
 
149
154
  return db_path;
150
155
}
151
156
 
152
 
bool SchemaIdentifier::compare(std::string arg)
 
157
bool SchemaIdentifier::compare(const std::string &arg) const
153
158
{
154
 
  std::transform(arg.begin(), arg.end(),
155
 
                 arg.begin(), ::tolower);
156
 
 
157
 
  return arg == lower_db;
 
159
  return boost::iequals(arg, db);
158
160
}
159
161
 
160
 
bool SchemaIdentifier::isValid()
 
162
bool SchemaIdentifier::isValid() const
161
163
{
162
 
  if (lower_db.empty())
163
 
    return false;
164
 
 
165
 
  if (lower_db.size() > NAME_LEN)
166
 
    return false;
167
 
 
168
 
  if (lower_db.at(lower_db.length() -1) == ' ')
 
164
  if (db.empty())
 
165
    return false;
 
166
 
 
167
  if (db.size() > NAME_LEN)
 
168
    return false;
 
169
 
 
170
  if (db.at(db.length() -1) == ' ')
169
171
    return false;
170
172
 
171
173
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
172
174
 
173
175
  int well_formed_error;
174
 
  uint32_t res= cs->cset->well_formed_len(cs, lower_db.c_str(), lower_db.c_str() + lower_db.length(),
 
176
  uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
175
177
                                          NAME_CHAR_LEN, &well_formed_error);
176
178
 
177
179
  if (well_formed_error)
178
180
  {
179
 
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", lower_db.c_str());
 
181
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", db.c_str());
180
182
    return false;
181
183
  }
182
184
 
183
 
  if (lower_db.length() != res)
 
185
  if (db.length() != res)
184
186
    return false;
185
187
 
186
188
  return true;