18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
23
#include <assert.h>
25
#include <drizzled/identifier.h>
26
#include <drizzled/session.h>
27
#include <drizzled/internal/my_sys.h>
29
#include <drizzled/util/tablename_to_filename.h>
30
#include <drizzled/util/backtrace.h>
25
#include "drizzled/schema_identifier.h"
26
#include "drizzled/session.h"
27
#include "drizzled/current_session.h"
28
#include "drizzled/internal/my_sys.h"
29
#include "drizzled/data_home.h"
32
31
#include <algorithm>
36
#include <boost/algorithm/string/compare.hpp>
38
35
using namespace std;
46
40
extern string drizzle_tmpdir;
41
extern pid_t current_pid;
43
static const char hexchars[]= "0123456789abcdef";
45
static bool tablename_to_filename(const string &from, string &to);
48
47
static size_t build_schema_filename(string &path, const string &db)
51
50
bool conversion_error= false;
53
conversion_error= util::tablename_to_filename(db, path);
52
conversion_error= tablename_to_filename(db, dbbuff);
54
53
if (conversion_error)
56
errmsg_printf(error::ERROR,
55
errmsg_printf(ERRMSG_LVL_ERROR,
57
56
_("Schema name cannot be encoded and fit within filesystem "
58
57
"name length restrictions."));
62
int rootdir_len= strlen(FN_ROOTDIR);
63
path.append(data_home);
64
ssize_t without_rootdir= path.length() - rootdir_len;
66
/* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
67
if (without_rootdir >= 0)
69
const char *tmp= path.c_str() + without_rootdir;
71
if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
72
path.append(FN_ROOTDIR);
62
77
return path.length();
65
Schema::Schema(const std::string &db_arg) :
82
Translate a table name to a cursor name (WL #1324).
85
tablename_to_filename()
87
to OUT The cursor name
90
true if errors happen. false on success.
92
static bool tablename_to_filename(const string &from, string &to)
95
string::const_iterator iter= from.begin();
96
for (; iter != from.end(); ++iter)
98
if ((*iter >= '0' && *iter <= '9') ||
99
(*iter >= 'A' && *iter <= 'Z') ||
100
(*iter >= 'a' && *iter <= 'z') ||
101
/* OSX defines an extra set of high-bit and multi-byte characters
102
that cannot be used on the filesystem. Instead of trying to sort
103
those out, we'll just escape encode all high-bit-set chars on OSX.
104
It won't really hurt anything - it'll just make some filenames ugly. */
105
#if !defined(TARGET_OS_OSX)
106
((unsigned char)*iter >= 128) ||
116
/* We need to escape this char in a way that can be reversed */
118
to.push_back(hexchars[(*iter >> 4) & 15]);
119
to.push_back(hexchars[(*iter) & 15]);
122
if (internal::check_if_legal_tablename(to.c_str()))
129
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
70
string::size_type lastPos= db.find_first_of('/', 0);
72
if (lastPos != std::string::npos)
74
catalog= db.substr(0, lastPos);
75
db.erase(0, lastPos + 1);
79
if (not db_arg.empty())
81
build_schema_filename(db_path, db);
134
std::transform(lower_db.begin(), lower_db.end(),
135
lower_db.begin(), ::tolower);
137
if (not lower_db.empty())
139
drizzled::build_schema_filename(db_path, lower_db);
82
140
assert(db_path.length()); // TODO throw exception, this is a possibility
86
void Schema::getSQLPath(std::string &arg) const
144
const std::string &SchemaIdentifier::getSQLPath()
146
return getSchemaName();
91
const std::string &Schema::getPath() const
149
const std::string &SchemaIdentifier::getPath() const
96
bool Schema::compare(const std::string &arg) const
98
return boost::iequals(arg, db);
101
bool Schema::compare(Schema::const_reference arg) const
103
return boost::iequals(arg.getSchemaName(), db);
106
bool Schema::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)
152
my_error(ER_WRONG_DB_NAME, *this);
154
bool SchemaIdentifier::compare(std::string arg) const
156
std::transform(arg.begin(), arg.end(),
157
arg.begin(), ::tolower);
159
return arg == lower_db;
162
bool SchemaIdentifier::isValid() const
164
if (lower_db.empty())
167
if (lower_db.size() > NAME_LEN)
170
if (lower_db.at(lower_db.length() -1) == ' ')
173
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
175
int well_formed_error;
176
uint32_t res= cs->cset->well_formed_len(cs, lower_db.c_str(), lower_db.c_str() + lower_db.length(),
177
NAME_CHAR_LEN, &well_formed_error);
179
if (well_formed_error)
181
my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", lower_db.c_str());
185
if (lower_db.length() != res)
160
const std::string &Schema::getCatalogName() const
162
return drizzled::catalog::local_identifier().name();
165
std::ostream& operator<<(std::ostream& output, const Schema&identifier)
167
output << "identifier::Schema:(";
168
output << catalog::local_identifier();
170
output << identifier.getSchemaName().c_str();
172
output << identifier.getPath().c_str();
175
return output; // for multiple << operators.
178
} /* namespace identifier */
179
191
} /* namespace drizzled */