1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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"
40
extern char *drizzle_tmpdir;
41
extern pid_t current_pid;
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
bool conversion_error= false;
52
memset(dbbuff, 0, sizeof(dbbuff));
53
conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
56
errmsg_printf(ERRMSG_LVL_ERROR,
57
_("Schema name cannot be encoded and fit within filesystem "
58
"name length restrictions."));
63
int rootdir_len= strlen(FN_ROOTDIR);
64
path.append(drizzle_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);
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++)
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) ||
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()
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)
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)
189
} /* namespace drizzled */