137
133
static uint32_t get_counter()
139
135
boost::mutex::scoped_lock lock(counter_mutex);
148
size_t Table::build_tmptable_filename(std::string &buffer)
150
size_t tmpdir_length;
151
ostringstream post_tmpdir_str;
153
buffer.append(drizzle_tmpdir);
154
tmpdir_length= buffer.length();
156
post_tmpdir_str << "/" << TMP_FILE_PREFIX << current_pid;
157
post_tmpdir_str << pthread_self() << "-" << get_counter();
159
buffer.append(post_tmpdir_str.str());
161
transform(buffer.begin() + tmpdir_length, buffer.end(), buffer.begin() + tmpdir_length, ::tolower);
163
return buffer.length();
166
size_t Table::build_tmptable_filename(std::vector<char> &buffer)
168
ostringstream post_tmpdir_str;
170
post_tmpdir_str << drizzle_tmpdir << "/" << TMP_FILE_PREFIX << current_pid;
171
post_tmpdir_str << pthread_self() << "-" << get_counter();
173
buffer.resize(post_tmpdir_str.str().length() + 1);
174
memcpy(&buffer[0], post_tmpdir_str.str().c_str(), post_tmpdir_str.str().size());
175
buffer[post_tmpdir_str.str().size()]= 0;
177
return buffer.size();
141
std::string Table::build_tmptable_filename()
144
os << "/" << TMP_FILE_PREFIX << current_pid << pthread_self() << "-" << get_counter();
145
return drizzle_tmpdir + boost::to_lower_copy(os.str());
182
149
Creates path to a cursor: drizzle_data_dir/db/table.ext
210
177
path length on success, 0 on failure
213
size_t Table::build_table_filename(std::string &in_path, const std::string &in_db, const std::string &in_table_name, bool is_tmp)
180
std::string Table::build_table_filename(const std::string &in_db, const std::string &in_table_name, bool is_tmp)
215
bool conversion_error= false;
217
conversion_error= util::tablename_to_filename(in_db, in_path);
218
if (conversion_error)
220
errmsg_printf(error::ERROR,
221
_("Schema name cannot be encoded and fit within filesystem "
222
"name length restrictions."));
226
in_path.append(FN_ROOTDIR);
228
if (is_tmp) // It a conversion tmp
230
in_path.append(in_table_name);
234
conversion_error= util::tablename_to_filename(in_table_name, in_path);
235
if (conversion_error)
237
errmsg_printf(error::ERROR,
238
_("Table name cannot be encoded and fit within filesystem "
239
"name length restrictions."));
244
return in_path.length();
182
string in_path= util::tablename_to_filename(in_db) + FN_LIBCHAR;
183
return in_path + (is_tmp ? in_table_name : util::tablename_to_filename(in_table_name));
247
186
Table::Table(const drizzled::Table &table) :
248
identifier::Schema(table.getShare()->getSchemaName()),
187
identifier::Schema(str_ref(table.getShare()->getSchemaName())),
249
188
type(table.getShare()->getTableType()),
250
189
table_name(table.getShare()->getTableName())
258
197
void Table::init()
261
201
case message::Table::FUNCTION:
262
202
case message::Table::STANDARD:
263
assert(path.size() == 0);
264
build_table_filename(path, getSchemaName(), table_name, false);
203
assert(path.empty());
204
path= build_table_filename(getSchemaName(), table_name, false);
266
206
case message::Table::INTERNAL:
267
assert(path.size() == 0);
268
build_table_filename(path, getSchemaName(), table_name, true);
207
assert(path.empty());
208
path= build_table_filename(getSchemaName(), table_name, true);
270
210
case message::Table::TEMPORARY:
271
211
if (path.empty())
273
build_tmptable_filename(path);
279
case message::Table::FUNCTION:
280
case message::Table::STANDARD:
281
case message::Table::INTERNAL:
283
case message::Table::TEMPORARY:
287
pos= path.find("tmp/#sql");
288
if (pos != std::string::npos)
290
key_path= path.substr(pos);
296
util::insensitive_hash hasher;
297
hash_value= hasher(path);
299
std::string tb_name(getTableName());
300
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
302
key.set(getKeySize(), getSchemaName(), tb_name);
212
path= build_tmptable_filename();
216
if (type == message::Table::TEMPORARY)
218
size_t pos= path.find("tmp/#sql");
219
if (pos != std::string::npos)
220
key_path= path.substr(pos);
223
hash_value= util::insensitive_hash()(path);
224
key.set(getKeySize(), getCatalogName(), getSchemaName(), boost::to_lower_copy(std::string(getTableName())));
311
233
const std::string &Table::getKeyPath() const
313
if (key_path.empty())
235
return key_path.empty() ? path : key_path;
319
void Table::getSQLPath(std::string &sql_path) const // @todo this is just used for errors, we should find a way to optimize it
238
std::string Table::getSQLPath() const // @todo this is just used for errors, we should find a way to optimize it
322
242
case message::Table::FUNCTION:
323
243
case message::Table::STANDARD:
324
sql_path.append(getSchemaName());
325
sql_path.append(".");
326
sql_path.append(table_name);
244
return getSchemaName() + "." + table_name;
328
245
case message::Table::INTERNAL:
329
sql_path.append("temporary.");
330
sql_path.append(table_name);
246
return "temporary." + table_name;
332
247
case message::Table::TEMPORARY:
333
sql_path.append(getSchemaName());
334
sql_path.append(".#");
335
sql_path.append(table_name);
248
return getSchemaName() + ".#" + table_name;
340
254
bool Table::isValid() const
345
259
bool error= false;
348
if (table_name.empty())
354
if (table_name.size() > NAME_LEN)
360
if (table_name.at(table_name.length() -1) == ' ')
366
if (table_name.at(0) == '.')
373
const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
375
int well_formed_error;
376
uint32_t res= cs->cset->well_formed_len(cs, table_name.c_str(), table_name.c_str() + table_name.length(),
377
NAME_CHAR_LEN, &well_formed_error);
378
if (well_formed_error or table_name.length() != res)
391
my_error(ER_WRONG_TABLE_NAME, MYF(0), name.c_str());
260
if (table_name.empty()
261
|| table_name.size() > NAME_LEN
262
|| table_name[table_name.length() - 1] == ' '
263
|| table_name[0] == '.')
269
const charset_info_st& cs= my_charset_utf8mb4_general_ci;
270
int well_formed_error;
271
uint32_t res= cs.cset->well_formed_len(cs, table_name, NAME_CHAR_LEN, &well_formed_error);
272
if (well_formed_error or table_name.length() != res)
277
my_error(ER_WRONG_TABLE_NAME, MYF(0), getSQLPath().c_str());
400
281
void Table::copyToTableMessage(message::Table &message) const
402
283
message.set_name(table_name);
403
284
message.set_schema(getSchemaName());
406
void Table::Key::set(size_t resize_arg, const std::string &a, const std::string &b)
287
void Table::Key::set(size_t resize_arg, const std::string &a, const std::string &b, const std::string &c)
408
289
key_buffer.resize(resize_arg);
410
291
std::copy(a.begin(), a.end(), key_buffer.begin());
411
292
std::copy(b.begin(), b.end(), key_buffer.begin() + a.length() + 1);
293
std::copy(c.begin(), c.end(),
294
key_buffer.begin() + a.length() + 1 + b.length() + 1);
413
296
util::sensitive_hash hasher;
414
297
hash_value= hasher(key_buffer);
424
307
return b.getHashValue();
428
std::ostream& operator<<(std::ostream& output, Table::const_reference identifier)
310
std::ostream& operator<<(std::ostream& output, const Table& identifier)
431
output << identifier.getSchemaName();
433
output << identifier.getTableName();
435
output << message::type(identifier.getType());
437
output << identifier.getPath();
439
output << identifier.getHashValue();
442
return output; // for multiple << operators.
312
return output << "Table:(" << identifier.getSchemaName() << ", " << identifier.getTableName() << ", " << message::type(identifier.getType()) << ", " << identifier.getPath() << ", " << identifier.getHashValue() << ")";
445
315
} /* namespace identifier */