23
23
#include <assert.h>
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"
30
#include "drizzled/util/tablename_to_filename.h"
31
#include "drizzled/util/backtrace.h"
29
#include "drizzled/data_home.h"
33
31
#include <algorithm>
37
#include <boost/algorithm/string/compare.hpp>
39
35
using namespace std;
44
extern string drizzle_tmpdir;
40
extern char *drizzle_tmpdir;
45
41
extern pid_t current_pid;
47
static size_t build_schema_filename(string &path, const string &db)
43
static const char hexchars[]= "0123456789abcdef";
45
static bool tablename_to_filename(const char *from, char *to, size_t to_length);
47
static size_t build_schema_filename(std::string &path, const char *db)
49
char dbbuff[FN_REFLEN];
50
50
bool conversion_error= false;
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)
55
56
errmsg_printf(ERRMSG_LVL_ERROR,
57
58
"name length restrictions."));
63
int rootdir_len= strlen(FN_ROOTDIR);
64
path.append(data_home);
65
ssize_t without_rootdir= path.length() - rootdir_len;
67
/* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
68
if (without_rootdir >= 0)
70
const char *tmp= path.c_str() + without_rootdir;
72
if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
73
path.append(FN_ROOTDIR);
61
78
return path.length();
64
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
70
string::size_type lastPos= db.find_first_of('/', 0);
72
if (lastPos != std::string::npos)
83
Translate a table name to a cursor name (WL #1324).
86
tablename_to_filename()
88
to OUT The cursor name
89
to_length The size of the cursor name buffer.
92
true if errors happen. false on success.
94
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
98
for (; *from && length < to_length; length++, from++)
74
catalog= db.substr(0, lastPos);
75
db.erase(0, lastPos + 1);
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) ||
79
if (not db_arg.empty())
81
drizzled::build_schema_filename(db_path, db);
118
if (length + 3 >= to_length)
121
/* 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];
127
if (internal::check_if_legal_tablename(to) &&
128
length + 4 < to_length)
130
memcpy(to + length, "@@@", 4);
136
const std::string &SchemaIdentifier::getSQLPath()
138
return getSchemaName();
141
const std::string &SchemaIdentifier::getPath()
143
if (db_path.empty() && not lower_db.empty())
145
drizzled::build_schema_filename(db_path, lower_db.c_str());
82
146
assert(db_path.length()); // TODO throw exception, this is a possibility
86
void SchemaIdentifier::getSQLPath(std::string &arg) const
88
arg.append(getSchemaName());
91
const std::string &SchemaIdentifier::getPath() const
96
bool SchemaIdentifier::compare(const std::string &arg) const
98
return boost::iequals(arg, db);
101
bool SchemaIdentifier::compare(SchemaIdentifier::const_reference arg) const
103
return boost::iequals(arg.getSchemaName(), db);
106
bool SchemaIdentifier::isValid() const
118
if (db.size() > NAME_LEN)
124
if (db.at(db.length() -1) == ' ')
137
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
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)
155
my_error(ER_WRONG_DB_NAME, MYF(0), name.c_str());
152
bool SchemaIdentifier::compare(std::string arg) const
154
std::transform(arg.begin(), arg.end(),
155
arg.begin(), ::tolower);
157
return arg == lower_db;
160
bool SchemaIdentifier::isValid()
162
if (lower_db.empty())
165
if (lower_db.size() > NAME_LEN)
168
if (lower_db.at(lower_db.length() -1) == ' ')
171
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
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);
177
if (well_formed_error)
179
my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", lower_db.c_str());
183
if (lower_db.length() != res)