~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.cc

  • Committer: Andrew Hutchings
  • Date: 2010-11-09 13:38:01 UTC
  • mto: (1919.1.2 trunk)
  • mto: This revision was merged to the branch mainline in revision 1920.
  • Revision ID: andrew@linuxjedi.co.uk-20101109133801-byjzsao76346395x
Add FLUSH GLOBAL STATUS; command
Clears the global status variables for the server

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 string &from, string &to);
 
50
 
52
51
/*
53
52
  Translate a cursor name to a table name (WL #1324).
54
53
 
208
207
{
209
208
  bool conversion_error= false;
210
209
 
211
 
  conversion_error= util::tablename_to_filename(in_db, in_path);
 
210
  conversion_error= tablename_to_filename(in_db, in_path);
212
211
  if (conversion_error)
213
212
  {
214
213
    errmsg_printf(ERRMSG_LVL_ERROR,
225
224
  }
226
225
  else
227
226
  {
228
 
    conversion_error= util::tablename_to_filename(in_table_name, in_path);
 
227
    conversion_error= tablename_to_filename(in_table_name, in_path);
229
228
    if (conversion_error)
230
229
    {
231
230
      errmsg_printf(ERRMSG_LVL_ERROR,
238
237
  return in_path.length();
239
238
}
240
239
 
 
240
 
 
241
/*
 
242
  Translate a table name to a cursor name (WL #1324).
 
243
 
 
244
  SYNOPSIS
 
245
    tablename_to_filename()
 
246
      from                      The table name
 
247
      to                OUT     The cursor name
 
248
      to_length                 The size of the cursor name buffer.
 
249
 
 
250
  RETURN
 
251
    true if errors happen. false on success.
 
252
*/
 
253
static bool tablename_to_filename(const string &from, string &to)
 
254
{
 
255
  
 
256
  string::const_iterator iter= from.begin();
 
257
  for (; iter != from.end(); ++iter)
 
258
  {
 
259
    if (isascii(*iter))
 
260
    {
 
261
      if ((isdigit(*iter)) ||
 
262
          (islower(*iter)) ||
 
263
          (*iter == '_') ||
 
264
          (*iter == ' ') ||
 
265
          (*iter == '-'))
 
266
      {
 
267
        to.push_back(*iter);
 
268
        continue;
 
269
      }
 
270
 
 
271
      if (isupper(*iter))
 
272
      {
 
273
        to.push_back(tolower(*iter));
 
274
        continue;
 
275
      }
 
276
    }
 
277
   
 
278
    /* We need to escape this char in a way that can be reversed */
 
279
    to.push_back('@');
 
280
    to.push_back(hexchars[(*iter >> 4) & 15]);
 
281
    to.push_back(hexchars[(*iter) & 15]);
 
282
  }
 
283
 
 
284
  if (internal::check_if_legal_tablename(to.c_str()))
 
285
  {
 
286
    to.append("@@@");
 
287
  }
 
288
  return false;
 
289
}
 
290
 
241
291
TableIdentifier::TableIdentifier(const drizzled::Table &table) :
242
292
  SchemaIdentifier(table.getShare()->getSchemaName()),
243
293
  type(table.getShare()->getTableType()),
272
322
  util::insensitive_hash hasher;
273
323
  hash_value= hasher(path);
274
324
 
275
 
  std::string tb_name(getTableName());
276
 
  std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
277
 
 
278
 
  key.set(getKeySize(), getSchemaName(), tb_name);
 
325
  key.set(getKeySize(), getSchemaName(), getTableName());
279
326
}
280
327
 
281
328
 
284
331
  return path;
285
332
}
286
333
 
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;
 
334
const std::string &TableIdentifier::getSQLPath()  // @todo this is just used for errors, we should find a way to optimize it
 
335
{
 
336
  if (sql_path.empty())
 
337
  {
 
338
    switch (type) {
 
339
    case message::Table::FUNCTION:
 
340
    case message::Table::STANDARD:
 
341
      sql_path.append(getSchemaName());
 
342
      sql_path.append(".");
 
343
      sql_path.append(table_name);
 
344
      break;
 
345
    case message::Table::INTERNAL:
 
346
      sql_path.append("temporary.");
 
347
      sql_path.append(table_name);
 
348
      break;
 
349
    case message::Table::TEMPORARY:
 
350
      sql_path.append(getSchemaName());
 
351
      sql_path.append(".#");
 
352
      sql_path.append(table_name);
 
353
      break;
 
354
    }
 
355
  }
 
356
 
 
357
  return sql_path;
365
358
}
366
359
 
367
360