204
165
path length on success, 0 on failure
207
size_t TableIdentifier::build_table_filename(std::string &in_path, const std::string &in_db, const std::string &in_table_name, bool is_tmp)
168
size_t build_table_filename(std::string &path, const char *db, const char *table_name, bool is_tmp)
170
char dbbuff[FN_REFLEN];
171
char tbbuff[FN_REFLEN];
209
172
bool conversion_error= false;
211
conversion_error= util::tablename_to_filename(in_db, in_path);
174
memset(tbbuff, 0, sizeof(tbbuff));
175
if (is_tmp) // FN_FROM_IS_TMP | FN_TO_IS_TMP
177
strncpy(tbbuff, table_name, sizeof(tbbuff));
181
conversion_error= tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
182
if (conversion_error)
184
errmsg_printf(ERRMSG_LVL_ERROR,
185
_("Table name cannot be encoded and fit within filesystem "
186
"name length restrictions."));
190
memset(dbbuff, 0, sizeof(dbbuff));
191
conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
212
192
if (conversion_error)
214
194
errmsg_printf(ERRMSG_LVL_ERROR,
216
196
"name length restrictions."));
220
in_path.append(FN_ROOTDIR);
222
if (is_tmp) // It a conversion tmp
201
int rootdir_len= strlen(FN_ROOTDIR);
202
path.append(drizzle_data_home);
203
ssize_t without_rootdir= path.length() - rootdir_len;
205
/* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
206
if (without_rootdir >= 0)
224
in_path.append(in_table_name);
208
const char *tmp= path.c_str() + without_rootdir;
210
if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
211
path.append(FN_ROOTDIR);
215
path.append(FN_ROOTDIR);
218
return path.length();
223
Translate a table name to a cursor name (WL #1324).
226
tablename_to_filename()
228
to OUT The cursor name
229
to_length The size of the cursor name buffer.
232
true if errors happen. false on success.
234
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
238
for (; *from && length < to_length; length++, from++)
228
conversion_error= util::tablename_to_filename(in_table_name, in_path);
229
if (conversion_error)
240
if ((*from >= '0' && *from <= '9') ||
241
(*from >= 'A' && *from <= 'Z') ||
242
(*from >= 'a' && *from <= 'z') ||
243
/* OSX defines an extra set of high-bit and multi-byte characters
244
that cannot be used on the filesystem. Instead of trying to sort
245
those out, we'll just escape encode all high-bit-set chars on OSX.
246
It won't really hurt anything - it'll just make some filenames ugly. */
247
#if !defined(TARGET_OS_OSX)
248
((unsigned char)*from >= 128) ||
231
errmsg_printf(ERRMSG_LVL_ERROR,
232
_("Table name cannot be encoded and fit within filesystem "
233
"name length restrictions."));
238
return in_path.length();
241
TableIdentifier::TableIdentifier(const drizzled::Table &table) :
242
SchemaIdentifier(table.getShare()->getSchemaName()),
243
type(table.getShare()->getTableType()),
244
table_name(table.getShare()->getTableName())
246
if (type == message::Table::TEMPORARY)
247
path= table.getShare()->getPath();
252
void TableIdentifier::init()
255
case message::Table::FUNCTION:
256
case message::Table::STANDARD:
257
assert(path.size() == 0);
258
build_table_filename(path, getSchemaName(), table_name, false);
260
case message::Table::INTERNAL:
261
assert(path.size() == 0);
262
build_table_filename(path, getSchemaName(), table_name, true);
264
case message::Table::TEMPORARY:
258
if (length + 3 >= to_length)
261
/* We need to escape this char in a way that can be reversed */
263
to[length++]= hexchars[(*from >> 4) & 15];
264
to[length]= hexchars[(*from) & 15];
267
if (internal::check_if_legal_tablename(to) &&
268
length + 4 < to_length)
270
memcpy(to + length, "@@@", 4);
278
const char *TableIdentifier::getPath()
284
build_table_filename(path, lower_db.c_str(), lower_table_name.c_str(), false);
286
case INTERNAL_TMP_TABLE:
287
build_table_filename(path, lower_db.c_str(), lower_table_name.c_str(), true);
267
290
build_tmptable_filename(path);
272
util::insensitive_hash hasher;
273
hash_value= hasher(path);
275
std::string tb_name(getTableName());
276
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
278
key.set(getKeySize(), getSchemaName(), tb_name);
282
const std::string &TableIdentifier::getPath() const
287
void TableIdentifier::getSQLPath(std::string &sql_path) const // @todo this is just used for errors, we should find a way to optimize it
290
case message::Table::FUNCTION:
291
case message::Table::STANDARD:
292
sql_path.append(getSchemaName());
293
sql_path.append(".");
294
sql_path.append(table_name);
296
case message::Table::INTERNAL:
297
sql_path.append("temporary.");
298
sql_path.append(table_name);
300
case message::Table::TEMPORARY:
301
sql_path.append(getSchemaName());
302
sql_path.append(".#");
303
sql_path.append(table_name);
308
bool TableIdentifier::isValid() const
310
if (not SchemaIdentifier::isValid())
316
if (table_name.empty())
322
if (table_name.size() > NAME_LEN)
328
if (table_name.at(table_name.length() -1) == ' ')
334
if (table_name.at(0) == '.')
341
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
343
int well_formed_error;
344
uint32_t res= cs->cset->well_formed_len(cs, table_name.c_str(), table_name.c_str() + table_name.length(),
345
NAME_CHAR_LEN, &well_formed_error);
346
if (well_formed_error or table_name.length() != res)
359
my_error(ER_WRONG_TABLE_NAME, MYF(0), name.c_str());
368
void TableIdentifier::copyToTableMessage(message::Table &message) const
370
message.set_name(table_name);
371
message.set_schema(getSchemaName());
374
void TableIdentifier::Key::set(size_t resize_arg, const std::string &a, const std::string &b)
376
key_buffer.resize(resize_arg);
378
std::copy(a.begin(), a.end(), key_buffer.begin());
379
std::copy(b.begin(), b.end(), key_buffer.begin() + a.length() + 1);
381
util::sensitive_hash hasher;
382
hash_value= hasher(key_buffer);
385
std::size_t hash_value(TableIdentifier const& b)
387
return b.getHashValue();
390
std::size_t hash_value(TableIdentifier::Key const& b)
392
return b.getHashValue();
292
case SYSTEM_TMP_TABLE:
296
assert(path.length()); // TODO throw exception, this is a possibility
395
302
} /* namespace drizzled */