18
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
23
#include <assert.h>
24
24
#include <boost/lexical_cast.hpp>
25
#include "drizzled/identifier.h"
26
#include "drizzled/session.h"
27
#include "drizzled/internal/my_sys.h"
29
#include "drizzled/table.h"
31
#include "drizzled/util/string.h"
32
#include "drizzled/util/tablename_to_filename.h"
25
#include <drizzled/identifier.h>
26
#include <drizzled/internal/my_sys.h>
28
#include <drizzled/error.h>
29
#include <drizzled/errmsg_print.h>
30
#include <drizzled/gettext.h>
32
#include <drizzled/table.h>
34
#include <drizzled/util/string.h>
35
#include <drizzled/util/tablename_to_filename.h>
34
37
#include <algorithm>
204
206
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)
209
size_t Table::build_table_filename(std::string &in_path, const std::string &in_db, const std::string &in_table_name, bool is_tmp)
209
bool conversion_error= false;
211
conversion_error= util::tablename_to_filename(in_db, in_path);
212
if (conversion_error)
211
if (util::tablename_to_filename(in_db, in_path))
214
errmsg_printf(ERRMSG_LVL_ERROR,
215
_("Schema name cannot be encoded and fit within filesystem "
216
"name length restrictions."));
213
errmsg_printf(error::ERROR, _("Schema name cannot be encoded and fit within filesystem name length restrictions."));
224
221
in_path.append(in_table_name);
223
else if (util::tablename_to_filename(in_table_name, in_path))
228
conversion_error= util::tablename_to_filename(in_table_name, in_path);
229
if (conversion_error)
231
errmsg_printf(ERRMSG_LVL_ERROR,
232
_("Table name cannot be encoded and fit within filesystem "
233
"name length restrictions."));
225
errmsg_printf(error::ERROR, _("Table name cannot be encoded and fit within filesystem name length restrictions."));
238
229
return in_path.length();
241
TableIdentifier::TableIdentifier(const drizzled::Table &table) :
242
SchemaIdentifier(table.getShare()->getSchemaName()),
232
Table::Table(const drizzled::Table &table) :
233
identifier::Schema(table.getShare()->getSchemaName()),
243
234
type(table.getShare()->getTableType()),
244
235
table_name(table.getShare()->getTableName())
272
util::insensitive_hash hasher;
273
hash_value= hasher(path);
264
case message::Table::FUNCTION:
265
case message::Table::STANDARD:
266
case message::Table::INTERNAL:
268
case message::Table::TEMPORARY:
272
pos= path.find("tmp/#sql");
273
if (pos != std::string::npos)
275
key_path= path.substr(pos);
281
hash_value= util::insensitive_hash()(path);
275
283
std::string tb_name(getTableName());
276
284
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
282
const std::string &TableIdentifier::getPath() const
290
const std::string &Table::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
295
const std::string &Table::getKeyPath() const
297
return key_path.empty() ? path : key_path;
300
std::string Table::getSQLPath() const // @todo this is just used for errors, we should find a way to optimize it
290
304
case message::Table::FUNCTION:
291
305
case message::Table::STANDARD:
292
sql_path.append(getSchemaName());
293
sql_path.append(".");
294
sql_path.append(table_name);
306
return getSchemaName() + "." + table_name;
296
307
case message::Table::INTERNAL:
297
sql_path.append("temporary.");
298
sql_path.append(table_name);
308
return "temporary." + table_name;
300
309
case message::Table::TEMPORARY:
301
sql_path.append(getSchemaName());
302
sql_path.append(".#");
303
sql_path.append(table_name);
310
return getSchemaName() + ".#" + table_name;
308
bool TableIdentifier::isValid() const
316
bool Table::isValid() const
310
if (not SchemaIdentifier::isValid())
318
if (not identifier::Schema::isValid())
313
321
bool error= false;
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());
322
if (table_name.empty()
323
|| table_name.size() > NAME_LEN
324
|| table_name[table_name.length() - 1] == ' '
325
|| table_name[0] == '.')
331
int well_formed_error;
332
uint32_t res= my_charset_utf8mb4_general_ci.cset->well_formed_len(&my_charset_utf8mb4_general_ci,
333
table_name.c_str(), table_name.c_str() + table_name.length(), NAME_CHAR_LEN, &well_formed_error);
334
if (well_formed_error or table_name.length() != res)
339
my_error(ER_WRONG_TABLE_NAME, MYF(0), getSQLPath().c_str());
368
void TableIdentifier::copyToTableMessage(message::Table &message) const
343
void Table::copyToTableMessage(message::Table &message) const
370
345
message.set_name(table_name);
371
346
message.set_schema(getSchemaName());
374
void TableIdentifier::Key::set(size_t resize_arg, const std::string &a, const std::string &b)
349
void Table::Key::set(size_t resize_arg, const std::string &a, const std::string &b)
376
351
key_buffer.resize(resize_arg);
382
357
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();
360
std::size_t hash_value(Table const& b)
362
return b.getHashValue();
365
std::size_t hash_value(Table::Key const& b)
367
return b.getHashValue();
370
std::ostream& operator<<(std::ostream& output, const Table& identifier)
372
return output << "Table:(" << identifier.getSchemaName() << ", " << identifier.getTableName() << ", " << message::type(identifier.getType()) << ", " << identifier.getPath() << ", " << identifier.getHashValue() << ")";
375
} /* namespace identifier */
395
376
} /* namespace drizzled */