~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.cc

  • Committer: Brian Aker
  • Date: 2010-10-21 08:55:44 UTC
  • mto: (1866.1.1 merge)
  • mto: This revision was merged to the branch mainline in revision 1867.
  • Revision ID: brian@tangent.org-20101021085544-chpce06zm4tdaqdi
This creates a function for seeing which catalog you are in. It also
refactors some of the functions to be in the utility_function collect.

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
#include "drizzled/table.h"
30
30
 
31
31
#include "drizzled/util/string.h"
32
 
#include "drizzled/util/tablename_to_filename.h"
33
32
 
34
33
#include <algorithm>
35
34
#include <sstream>
42
41
namespace drizzled
43
42
{
44
43
 
45
 
class SchemaIdentifier;
46
 
 
47
44
extern std::string drizzle_tmpdir;
48
45
extern pid_t current_pid;
49
46
 
50
47
static const char hexchars[]= "0123456789abcdef";
51
48
 
 
49
static bool tablename_to_filename(const char *from, char *to, size_t to_length);
 
50
 
52
51
/*
53
52
  Translate a cursor name to a table name (WL #1324).
54
53
 
204
203
    path length on success, 0 on failure
205
204
*/
206
205
 
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)
 
206
size_t TableIdentifier::build_table_filename(std::string &path, const char *db, const char *table_name, bool is_tmp)
208
207
{
 
208
  char dbbuff[FN_REFLEN];
 
209
  char tbbuff[FN_REFLEN];
209
210
  bool conversion_error= false;
210
211
 
211
 
  conversion_error= util::tablename_to_filename(in_db, in_path);
212
 
  if (conversion_error)
213
 
  {
214
 
    errmsg_printf(ERRMSG_LVL_ERROR,
215
 
                  _("Schema name cannot be encoded and fit within filesystem "
216
 
                    "name length restrictions."));
217
 
    return 0;
218
 
  }
219
 
 
220
 
  in_path.append(FN_ROOTDIR);
221
 
 
 
212
  memset(tbbuff, 0, sizeof(tbbuff));
222
213
  if (is_tmp) // It a conversion tmp
223
214
  {
224
 
    in_path.append(in_table_name);
 
215
    strncpy(tbbuff, table_name, sizeof(tbbuff));
225
216
  }
226
217
  else
227
218
  {
228
 
    conversion_error= util::tablename_to_filename(in_table_name, in_path);
 
219
    conversion_error= tablename_to_filename(table_name, tbbuff, sizeof(tbbuff));
229
220
    if (conversion_error)
230
221
    {
231
222
      errmsg_printf(ERRMSG_LVL_ERROR,
234
225
      return 0;
235
226
    }
236
227
  }
237
 
   
238
 
  return in_path.length();
 
228
  memset(dbbuff, 0, sizeof(dbbuff));
 
229
  conversion_error= tablename_to_filename(db, dbbuff, sizeof(dbbuff));
 
230
  if (conversion_error)
 
231
  {
 
232
    errmsg_printf(ERRMSG_LVL_ERROR,
 
233
                  _("Schema name cannot be encoded and fit within filesystem "
 
234
                    "name length restrictions."));
 
235
    return 0;
 
236
  }
 
237
   
 
238
  path.append(dbbuff);
 
239
  path.append(FN_ROOTDIR);
 
240
  path.append(tbbuff);
 
241
 
 
242
  return path.length();
 
243
}
 
244
 
 
245
 
 
246
/*
 
247
  Translate a table name to a cursor name (WL #1324).
 
248
 
 
249
  SYNOPSIS
 
250
    tablename_to_filename()
 
251
      from                      The table name
 
252
      to                OUT     The cursor name
 
253
      to_length                 The size of the cursor name buffer.
 
254
 
 
255
  RETURN
 
256
    true if errors happen. false on success.
 
257
*/
 
258
static bool tablename_to_filename(const char *from, char *to, size_t to_length)
 
259
{
 
260
  
 
261
  size_t length= 0;
 
262
  for (; *from  && length < to_length; length++, from++)
 
263
  {
 
264
    if ((*from >= '0' && *from <= '9') ||
 
265
        (*from >= 'a' && *from <= 'z') ||
 
266
/* OSX defines an extra set of high-bit and multi-byte characters
 
267
   that cannot be used on the filesystem. Instead of trying to sort
 
268
   those out, we'll just escape encode all high-bit-set chars on OSX.
 
269
   It won't really hurt anything - it'll just make some filenames ugly. */
 
270
#if !defined(TARGET_OS_OSX)
 
271
        ((unsigned char)*from >= 128) ||
 
272
#endif
 
273
        (*from == '_') ||
 
274
        (*from == ' ') ||
 
275
        (*from == '-'))
 
276
    {
 
277
      to[length]= tolower(*from);
 
278
      continue;
 
279
    }
 
280
 
 
281
    if ((*from >= 'A' && *from <= 'Z'))
 
282
    {
 
283
      to[length]= tolower(*from);
 
284
      continue;
 
285
    }
 
286
   
 
287
    if (length + 3 >= to_length)
 
288
      return true;
 
289
 
 
290
    /* We need to escape this char in a way that can be reversed */
 
291
    to[length++]= '@';
 
292
    to[length++]= hexchars[(*from >> 4) & 15];
 
293
    to[length]= hexchars[(*from) & 15];
 
294
  }
 
295
 
 
296
  if (internal::check_if_legal_tablename(to) &&
 
297
      length + 4 < to_length)
 
298
  {
 
299
    memcpy(to + length, "@@@", 4);
 
300
    length+= 3;
 
301
  }
 
302
  return false;
239
303
}
240
304
 
241
305
TableIdentifier::TableIdentifier(const drizzled::Table &table) :
255
319
  case message::Table::FUNCTION:
256
320
  case message::Table::STANDARD:
257
321
    assert(path.size() == 0);
258
 
    build_table_filename(path, getSchemaName(), table_name, false);
 
322
    build_table_filename(path, getSchemaName().c_str(), table_name.c_str(), false);
259
323
    break;
260
324
  case message::Table::INTERNAL:
261
325
    assert(path.size() == 0);
262
 
    build_table_filename(path, getSchemaName(), table_name, true);
 
326
    build_table_filename(path, getSchemaName().c_str(), table_name.c_str(), true);
263
327
    break;
264
328
  case message::Table::TEMPORARY:
265
329
    if (path.empty())
272
336
  util::insensitive_hash hasher;
273
337
  hash_value= hasher(path);
274
338
 
275
 
  std::string tb_name(getTableName());
276
 
  std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
 
339
  key.resize(getKeySize());
 
340
  size_t key_length= TableIdentifier::createKey(&key[0], *this);
277
341
 
278
 
  key.set(getKeySize(), getSchemaName(), tb_name);
 
342
  assert(key_length == getKeySize()); // If this is off, then we have a memory issue.
279
343
}
280
344
 
281
345
 
284
348
  return path;
285
349
}
286
350
 
287
 
void TableIdentifier::getSQLPath(std::string &sql_path) const  // @todo this is just used for errors, we should find a way to optimize it
288
 
{
289
 
  switch (type) {
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);
295
 
    break;
296
 
  case message::Table::INTERNAL:
297
 
    sql_path.append("temporary.");
298
 
    sql_path.append(table_name);
299
 
    break;
300
 
  case message::Table::TEMPORARY:
301
 
    sql_path.append(getSchemaName());
302
 
    sql_path.append(".#");
303
 
    sql_path.append(table_name);
304
 
    break;
305
 
  }
306
 
}
307
 
 
308
 
bool TableIdentifier::isValid() const
309
 
{
310
 
  if (not SchemaIdentifier::isValid())
311
 
    return false;
312
 
 
313
 
  bool error= false;
314
 
  do
315
 
  {
316
 
    if (table_name.empty())
317
 
    {
318
 
      error= true;
319
 
      break;
320
 
    }
321
 
 
322
 
    if (table_name.size() > NAME_LEN)
323
 
    {
324
 
      error= true;
325
 
      break;
326
 
    }
327
 
 
328
 
    if (table_name.at(table_name.length() -1) == ' ')
329
 
    {
330
 
      error= true;
331
 
      break;
332
 
    }
333
 
 
334
 
    if (table_name.at(0) == '.')
335
 
    {
336
 
      error= true;
337
 
      break;
338
 
    }
339
 
 
340
 
    {
341
 
      const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
342
 
 
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)
347
 
      {
348
 
        error= true;
349
 
        break;
350
 
      }
351
 
    }
352
 
  } while (0);
353
 
 
354
 
  if (error)
355
 
  {
356
 
    std::string name;
357
 
 
358
 
    getSQLPath(name);
359
 
    my_error(ER_WRONG_TABLE_NAME, MYF(0), name.c_str());
360
 
 
361
 
    return false;
362
 
  }
363
 
 
364
 
  return true;
 
351
const std::string &TableIdentifier::getSQLPath()  // @todo this is just used for errors, we should find a way to optimize it
 
352
{
 
353
  if (sql_path.empty())
 
354
  {
 
355
    switch (type) {
 
356
    case message::Table::FUNCTION:
 
357
    case message::Table::STANDARD:
 
358
      sql_path.append(getSchemaName());
 
359
      sql_path.append(".");
 
360
      sql_path.append(table_name);
 
361
      break;
 
362
    case message::Table::INTERNAL:
 
363
      sql_path.append("temporary.");
 
364
      sql_path.append(table_name);
 
365
      break;
 
366
    case message::Table::TEMPORARY:
 
367
      sql_path.append(getSchemaName());
 
368
      sql_path.append(".#");
 
369
      sql_path.append(table_name);
 
370
      break;
 
371
    }
 
372
  }
 
373
 
 
374
  return sql_path;
365
375
}
366
376
 
367
377
 
371
381
  message.set_schema(getSchemaName());
372
382
}
373
383
 
374
 
void TableIdentifier::Key::set(size_t resize_arg, const std::string &a, const std::string &b)
375
 
{
376
 
  key_buffer.resize(resize_arg);
377
 
 
378
 
  std::copy(a.begin(), a.end(), key_buffer.begin());
379
 
  std::copy(b.begin(), b.end(), key_buffer.begin() + a.length() + 1);
380
 
 
381
 
  util::sensitive_hash hasher;
382
 
  hash_value= hasher(key_buffer);
383
 
}
384
 
 
385
384
std::size_t hash_value(TableIdentifier const& b)
386
385
{
387
386
  return b.getHashValue();
388
387
}
389
388
 
390
 
std::size_t hash_value(TableIdentifier::Key const& b)
391
 
{
392
 
  return b.getHashValue();
393
 
}
394
 
 
395
389
} /* namespace drizzled */