35
#include <boost/algorithm/string/compare.hpp>
35
37
using namespace std;
40
extern char *drizzle_tmpdir;
42
extern string drizzle_tmpdir;
41
43
extern pid_t current_pid;
43
45
static const char hexchars[]= "0123456789abcdef";
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);
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)
49
char dbbuff[FN_REFLEN];
50
52
bool conversion_error= false;
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)
56
57
errmsg_printf(ERRMSG_LVL_ERROR,
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.
92
92
true if errors happen. false on success.
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)
98
for (; *from && length < to_length; length++, from++)
97
string::const_iterator iter= from.begin();
98
for (; iter != from.end(); ++iter)
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) ||
117
if ((*iter >= 'A' && *iter <= 'Z'))
119
to.push_back(tolower(*iter));
118
if (length + 3 >= to_length)
121
123
/* We need to escape this char in a way that can be reversed */
123
to[length++]= hexchars[(*from >> 4) & 15];
124
to[length]= hexchars[(*from) & 15];
125
to.push_back(hexchars[(*iter >> 4) & 15]);
126
to.push_back(hexchars[(*iter) & 15]);
127
if (internal::check_if_legal_tablename(to) &&
128
length + 4 < to_length)
129
if (internal::check_if_legal_tablename(to.c_str()))
130
memcpy(to + length, "@@@", 4);
136
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
140
if (not db_arg.empty())
142
drizzled::build_schema_filename(db_path, db);
143
assert(db_path.length()); // TODO throw exception, this is a possibility
136
147
const std::string &SchemaIdentifier::getSQLPath()
138
149
return getSchemaName();
141
const std::string &SchemaIdentifier::getPath()
152
const std::string &SchemaIdentifier::getPath() const
145
drizzled::build_schema_filename(db_path, lower_db.c_str());
146
assert(db_path.length()); // TODO throw exception, this is a possibility
152
bool SchemaIdentifier::compare(std::string arg)
157
bool SchemaIdentifier::compare(const std::string &arg) const
154
std::transform(arg.begin(), arg.end(),
155
arg.begin(), ::tolower);
157
return arg == lower_db;
159
return boost::iequals(arg, db);
160
bool SchemaIdentifier::isValid()
162
bool SchemaIdentifier::isValid() const
162
if (lower_db.empty())
165
if (lower_db.size() > NAME_LEN)
168
if (lower_db.at(lower_db.length() -1) == ' ')
167
if (db.size() > NAME_LEN)
170
if (db.at(db.length() -1) == ' ')
171
173
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
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);
177
179
if (well_formed_error)
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());
183
if (lower_db.length() != res)
185
if (db.length() != res)