~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.cc

  • Committer: Monty Taylor
  • Date: 2010-08-20 20:54:13 UTC
  • mto: (1728.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 1729.
  • Revision ID: mordred@inaugust.com-20100820205413-2zebseruww4uq3dg
Removed a HASH in xa_resource_manager. It's not actually used, but I left it
in case someone did actually want to implement RECOVER some day.

Show diffs side-by-side

added added

removed removed

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