~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.cc

  • Committer: Olaf van der Spek
  • Date: 2011-06-23 11:44:30 UTC
  • mto: This revision was merged to the branch mainline in revision 2348.
  • Revision ID: olafvdspek@gmail.com-20110623114430-no355yypk4y3icqb
Refactor

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
 
21
#include <config.h>
22
22
 
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"
28
 
 
29
 
#include "drizzled/table.h"
30
 
 
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>
 
27
 
 
28
#include <drizzled/error.h>
 
29
#include <drizzled/errmsg_print.h>
 
30
#include <drizzled/gettext.h>
 
31
 
 
32
#include <drizzled/table.h>
 
33
 
 
34
#include <drizzled/util/string.h>
 
35
#include <drizzled/util/tablename_to_filename.h>
33
36
 
34
37
#include <algorithm>
35
38
#include <sstream>
39
42
 
40
43
using namespace std;
41
44
 
42
 
namespace drizzled
43
 
{
44
 
 
45
 
class SchemaIdentifier;
 
45
namespace drizzled {
46
46
 
47
47
extern std::string drizzle_tmpdir;
48
48
extern pid_t current_pid;
49
49
 
 
50
namespace identifier {
 
51
 
50
52
static const char hexchars[]= "0123456789abcdef";
51
53
 
52
54
/*
61
63
  RETURN
62
64
    Table name length.
63
65
*/
64
 
uint32_t TableIdentifier::filename_to_tablename(const char *from, char *to, uint32_t to_length)
 
66
uint32_t Table::filename_to_tablename(const char *from, char *to, uint32_t to_length)
65
67
{
66
68
  uint32_t length= 0;
67
69
 
139
141
 
140
142
#endif
141
143
 
142
 
size_t TableIdentifier::build_tmptable_filename(std::string &buffer)
 
144
size_t Table::build_tmptable_filename(std::string &buffer)
143
145
{
144
146
  size_t tmpdir_length;
145
147
  ostringstream post_tmpdir_str;
157
159
  return buffer.length();
158
160
}
159
161
 
160
 
size_t TableIdentifier::build_tmptable_filename(std::vector<char> &buffer)
 
162
size_t Table::build_tmptable_filename(std::vector<char> &buffer)
161
163
{
162
164
  ostringstream post_tmpdir_str;
163
165
 
204
206
    path length on success, 0 on failure
205
207
*/
206
208
 
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)
208
210
{
209
 
  bool conversion_error= false;
210
 
 
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))
213
212
  {
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."));
217
214
    return 0;
218
215
  }
219
216
 
223
220
  {
224
221
    in_path.append(in_table_name);
225
222
  }
226
 
  else
 
223
  else if (util::tablename_to_filename(in_table_name, in_path))
227
224
  {
228
 
    conversion_error= util::tablename_to_filename(in_table_name, in_path);
229
 
    if (conversion_error)
230
 
    {
231
 
      errmsg_printf(ERRMSG_LVL_ERROR,
232
 
                    _("Table name cannot be encoded and fit within filesystem "
233
 
                      "name length restrictions."));
234
 
      return 0;
235
 
    }
 
225
    errmsg_printf(error::ERROR, _("Table name cannot be encoded and fit within filesystem name length restrictions."));
 
226
    return 0;
236
227
  }
237
228
   
238
229
  return in_path.length();
239
230
}
240
231
 
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())
245
236
{
249
240
  init();
250
241
}
251
242
 
252
 
void TableIdentifier::init()
 
243
void Table::init()
253
244
{
254
245
  switch (type) {
255
246
  case message::Table::FUNCTION:
269
260
    break;
270
261
  }
271
262
 
272
 
  util::insensitive_hash hasher;
273
 
  hash_value= hasher(path);
 
263
  switch (type) {
 
264
  case message::Table::FUNCTION:
 
265
  case message::Table::STANDARD:
 
266
  case message::Table::INTERNAL:
 
267
    break;
 
268
  case message::Table::TEMPORARY:
 
269
    {
 
270
      size_t pos;
 
271
 
 
272
      pos= path.find("tmp/#sql");
 
273
      if (pos != std::string::npos) 
 
274
      {
 
275
        key_path= path.substr(pos);
 
276
      }
 
277
    }
 
278
    break;
 
279
  }
 
280
 
 
281
  hash_value= util::insensitive_hash()(path);
274
282
 
275
283
  std::string tb_name(getTableName());
276
284
  std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
279
287
}
280
288
 
281
289
 
282
 
const std::string &TableIdentifier::getPath() const
 
290
const std::string &Table::getPath() const
283
291
{
284
292
  return path;
285
293
}
286
294
 
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) {
 
295
const std::string &Table::getKeyPath() const
 
296
{
 
297
  return key_path.empty() ? path : key_path;
 
298
}
 
299
 
 
300
std::string Table::getSQLPath() const  // @todo this is just used for errors, we should find a way to optimize it
 
301
{
 
302
  switch (type) 
 
303
        {
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);
295
 
    break;
 
306
                return getSchemaName() + "." + table_name;
296
307
  case message::Table::INTERNAL:
297
 
    sql_path.append("temporary.");
298
 
    sql_path.append(table_name);
299
 
    break;
 
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);
304
 
    break;
 
310
    return getSchemaName() + ".#" + table_name;
305
311
  }
 
312
        assert(false);
 
313
        return "<no table>";
306
314
}
307
315
 
308
 
bool TableIdentifier::isValid() const
 
316
bool Table::isValid() const
309
317
{
310
 
  if (not SchemaIdentifier::isValid())
 
318
  if (not identifier::Schema::isValid())
311
319
    return false;
312
320
 
313
321
  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;
 
322
  if (table_name.empty()
 
323
                || table_name.size() > NAME_LEN
 
324
                || table_name[table_name.length() - 1] == ' '
 
325
                || table_name[0] == '.')
 
326
  {
 
327
    error= true;
 
328
  }
 
329
        else
 
330
  {
 
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)
 
335
      error= true;
 
336
  }
 
337
  if (not error)
 
338
                return true;
 
339
  my_error(ER_WRONG_TABLE_NAME, MYF(0), getSQLPath().c_str());
 
340
  return false;
365
341
}
366
342
 
367
 
 
368
 
void TableIdentifier::copyToTableMessage(message::Table &message) const
 
343
void Table::copyToTableMessage(message::Table &message) const
369
344
{
370
345
  message.set_name(table_name);
371
346
  message.set_schema(getSchemaName());
372
347
}
373
348
 
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)
375
350
{
376
351
  key_buffer.resize(resize_arg);
377
352
 
382
357
  hash_value= hasher(key_buffer);
383
358
}
384
359
 
385
 
std::size_t hash_value(TableIdentifier const& b)
386
 
{
387
 
  return b.getHashValue();
388
 
}
389
 
 
390
 
std::size_t hash_value(TableIdentifier::Key const& b)
391
 
{
392
 
  return b.getHashValue();
393
 
}
394
 
 
 
360
std::size_t hash_value(Table const& b)
 
361
{
 
362
  return b.getHashValue();
 
363
}
 
364
 
 
365
std::size_t hash_value(Table::Key const& b)
 
366
{
 
367
  return b.getHashValue();
 
368
}
 
369
 
 
370
std::ostream& operator<<(std::ostream& output, const Table& identifier)
 
371
{
 
372
  return output << "Table:(" <<  identifier.getSchemaName() << ", " << identifier.getTableName() << ", " << message::type(identifier.getType()) << ", " << identifier.getPath() << ", " << identifier.getHashValue() << ")";
 
373
}
 
374
 
 
375
} /* namespace identifier */
395
376
} /* namespace drizzled */