~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/schema_identifier.cc

  • Committer: Paul McCullagh
  • Date: 2010-04-26 11:13:26 UTC
  • mto: (1511.1.2 new-staging)
  • mto: This revision was merged to the branch mainline in revision 1512.
  • Revision ID: paul.mccullagh@primebase.org-20100426111326-wz8uhj3uu7qo07oa
Fixed bug #567402 pbxt doesn't build on debian ppc 32bit

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 
23
23
#include <assert.h>
24
24
 
25
 
#include "drizzled/identifier.h"
 
25
#include "drizzled/schema_identifier.h"
26
26
#include "drizzled/session.h"
27
27
#include "drizzled/current_session.h"
28
28
#include "drizzled/internal/my_sys.h"
29
 
 
30
 
#include "drizzled/util/tablename_to_filename.h"
31
 
#include "drizzled/util/backtrace.h"
 
29
#include "drizzled/data_home.h"
32
30
 
33
31
#include <algorithm>
34
32
#include <sstream>
35
33
#include <cstdio>
36
34
 
37
 
#include <boost/algorithm/string/compare.hpp>
38
 
 
39
35
using namespace std;
40
36
 
41
37
namespace drizzled
42
38
{
43
39
 
44
 
extern string drizzle_tmpdir;
 
40
extern char *drizzle_tmpdir;
45
41
extern pid_t current_pid;
46
42
 
47
 
static size_t build_schema_filename(string &path, const string &db)
 
43
static const char hexchars[]= "0123456789abcdef";
 
44
 
 
45
static bool tablename_to_filename(const char *from, char *to, size_t to_length);
 
46
 
 
47
static size_t build_schema_filename(std::string &path, const char *db)
48
48
{
49
 
  path.append("");
 
49
  char dbbuff[FN_REFLEN];
50
50
  bool conversion_error= false;
51
51
 
52
 
  conversion_error= util::tablename_to_filename(db, path);
 
52
  memset(dbbuff, 0, sizeof(dbbuff));
 
53
  conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
53
54
  if (conversion_error)
54
55
  {
55
56
    errmsg_printf(ERRMSG_LVL_ERROR,
57
58
                    "name length restrictions."));
58
59
    return 0;
59
60
  }
 
61
   
 
62
 
 
63
  int rootdir_len= strlen(FN_ROOTDIR);
 
64
  path.append(data_home);
 
65
  ssize_t without_rootdir= path.length() - rootdir_len;
 
66
 
 
67
  /* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
 
68
  if (without_rootdir >= 0)
 
69
  {
 
70
    const char *tmp= path.c_str() + without_rootdir;
 
71
 
 
72
    if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
 
73
      path.append(FN_ROOTDIR);
 
74
  }
 
75
 
 
76
  path.append(dbbuff);
60
77
 
61
78
  return path.length();
62
79
}
63
80
 
64
 
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
65
 
  db(db_arg),
66
 
  db_path(""),
67
 
  catalog("LOCAL")
68
 
69
 
#if 0
70
 
  string::size_type lastPos= db.find_first_of('/', 0);
71
 
 
72
 
  if (lastPos != std::string::npos) 
 
81
 
 
82
/*
 
83
  Translate a table name to a cursor name (WL #1324).
 
84
 
 
85
  SYNOPSIS
 
86
    tablename_to_filename()
 
87
      from                      The table name
 
88
      to                OUT     The cursor name
 
89
      to_length                 The size of the cursor name buffer.
 
90
 
 
91
  RETURN
 
92
    true if errors happen. false on success.
 
93
*/
 
94
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
 
95
{
 
96
  
 
97
  size_t length= 0;
 
98
  for (; *from  && length < to_length; length++, from++)
73
99
  {
74
 
    catalog= db.substr(0, lastPos);
75
 
    db.erase(0, lastPos + 1);
76
 
  }
 
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. */
 
107
#if !defined(TARGET_OS_OSX)
 
108
        ((unsigned char)*from >= 128) ||
77
109
#endif
78
 
 
79
 
  if (not db_arg.empty())
80
 
  {
81
 
    drizzled::build_schema_filename(db_path, db);
 
110
        (*from == '_') ||
 
111
        (*from == ' ') ||
 
112
        (*from == '-'))
 
113
    {
 
114
      to[length]= *from;
 
115
      continue;
 
116
    }
 
117
   
 
118
    if (length + 3 >= to_length)
 
119
      return true;
 
120
 
 
121
    /* 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];
 
125
  }
 
126
 
 
127
  if (internal::check_if_legal_tablename(to) &&
 
128
      length + 4 < to_length)
 
129
  {
 
130
    memcpy(to + length, "@@@", 4);
 
131
    length+= 3;
 
132
  }
 
133
  return false;
 
134
}
 
135
 
 
136
const std::string &SchemaIdentifier::getSQLPath()
 
137
{
 
138
  return getSchemaName();
 
139
}
 
140
 
 
141
const std::string &SchemaIdentifier::getPath()
 
142
{
 
143
  if (db_path.empty() && not lower_db.empty())
 
144
  {
 
145
    drizzled::build_schema_filename(db_path, lower_db.c_str());
82
146
    assert(db_path.length()); // TODO throw exception, this is a possibility
83
147
  }
84
 
}
85
 
 
86
 
void SchemaIdentifier::getSQLPath(std::string &arg) const
87
 
{
88
 
  arg.append(getSchemaName());
89
 
}
90
 
 
91
 
const std::string &SchemaIdentifier::getPath() const
92
 
{
 
148
 
93
149
  return db_path;
94
150
}
95
151
 
96
 
bool SchemaIdentifier::compare(const std::string &arg) const
97
 
{
98
 
  return boost::iequals(arg, db);
99
 
}
100
 
 
101
 
bool SchemaIdentifier::compare(SchemaIdentifier::const_reference arg) const
102
 
{
103
 
  return boost::iequals(arg.getSchemaName(), db);
104
 
}
105
 
 
106
 
bool SchemaIdentifier::isValid() const
107
 
{
108
 
  bool error= false;
109
 
 
110
 
  do
111
 
  {
112
 
    if (db.empty())
113
 
    {
114
 
      error= true;
115
 
      break;
116
 
    }
117
 
 
118
 
    if (db.size() > NAME_LEN)
119
 
    {
120
 
      error= true;
121
 
      break;
122
 
    }
123
 
 
124
 
    if (db.at(db.length() -1) == ' ')
125
 
    {
126
 
      error= true;
127
 
      break;
128
 
    }
129
 
 
130
 
    if (db.at(0) == '.')
131
 
    {
132
 
      error= true;
133
 
      break;
134
 
    }
135
 
 
136
 
    {
137
 
      const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
138
 
 
139
 
      int well_formed_error;
140
 
      uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
141
 
                                              NAME_CHAR_LEN, &well_formed_error);
142
 
      if (well_formed_error or db.length() != res)
143
 
      {
144
 
        error= true;
145
 
        break;
146
 
      }
147
 
    }
148
 
  } while (0);
149
 
 
150
 
  if (error)
151
 
  {
152
 
    std::string name;
153
 
 
154
 
    getSQLPath(name);
155
 
    my_error(ER_WRONG_DB_NAME, MYF(0), name.c_str());
156
 
 
 
152
bool SchemaIdentifier::compare(std::string arg) const
 
153
{
 
154
  std::transform(arg.begin(), arg.end(),
 
155
                 arg.begin(), ::tolower);
 
156
 
 
157
  return arg == lower_db;
 
158
}
 
159
 
 
160
bool SchemaIdentifier::isValid()
 
161
{
 
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) == ' ')
 
169
    return false;
 
170
 
 
171
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
172
 
 
173
  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(),
 
175
                                          NAME_CHAR_LEN, &well_formed_error);
 
176
 
 
177
  if (well_formed_error)
 
178
  {
 
179
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", lower_db.c_str());
157
180
    return false;
158
181
  }
159
182
 
 
183
  if (lower_db.length() != res)
 
184
    return false;
 
185
 
160
186
  return true;
161
187
}
162
188