~drizzle-trunk/drizzle/development

1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
22
23
#include <assert.h>
24
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
25
#include <drizzled/identifier.h>
26
#include <drizzled/session.h>
27
#include <drizzled/internal/my_sys.h>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
28
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
29
#include <drizzled/util/tablename_to_filename.h>
30
#include <drizzled/util/backtrace.h>
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
31
32
#include <algorithm>
33
#include <sstream>
34
#include <cstdio>
35
36
#include <boost/algorithm/string/compare.hpp>
37
38
using namespace std;
39
40
namespace drizzled
41
{
42
43
namespace identifier
44
{
45
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
46
static void build_schema_filename(string &path, const string &name_arg)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
47
{
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
48
  path.append("../");
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
49
  bool conversion_error= false;
50
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
51
  conversion_error= util::tablename_to_filename(name_arg, path);
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
52
  if (conversion_error)
53
  {
2126.3.3 by Brian Aker
Merge in error message rework. Many error messages are fixed in this patch.
54
    errmsg_printf(error::ERROR,
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
55
                  _("Catalog name cannot be encoded and fit within filesystem "
56
                    "name length restrictions."));
57
  }
58
}
59
60
Catalog::Catalog(const std::string &name_arg) :
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
61
  _name(name_arg)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
62
{ 
2073.1.2 by Brian Aker
Basic DDL for catalog.
63
  init();
64
}
65
66
Catalog::Catalog(const drizzled::LEX_STRING &name_arg) :
67
  _name(name_arg.str, name_arg.length)
68
{
69
  init();
70
}
71
72
void  Catalog::init()
73
{ 
74
  assert(not _name.empty());
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
75
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
76
  build_schema_filename(path, _name);
1960.1.8 by Brian Aker
Big hunk of burning create/drop work.
77
  assert(path.length()); // TODO throw exception, this is a possibility
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
78
79
  util::insensitive_hash hasher;
80
  hash_value= hasher(path);
81
}
82
83
const std::string &Catalog::getPath() const
84
{
85
  return path;
86
}
87
88
bool Catalog::compare(const std::string &arg) const
89
{
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
90
  return boost::iequals(arg, _name);
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
91
}
92
93
bool Catalog::isValid() const
94
{
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
95
  if (_name.empty())
96
    return false;
97
98
  if (_name.size() > NAME_LEN)
99
    return false;
100
101
  if (_name.at(_name.length() -1) == ' ')
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
102
    return false;
103
104
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
105
106
  int well_formed_error;
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
107
  uint32_t res= cs->cset->well_formed_len(cs, _name.c_str(), _name.c_str() + _name.length(),
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
108
                                          NAME_CHAR_LEN, &well_formed_error);
109
110
  if (well_formed_error)
111
  {
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
112
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", _name.c_str());
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
113
    return false;
114
  }
115
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
116
  if (_name.length() != res)
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
117
    return false;
118
119
  return true;
120
}
121
122
std::size_t hash_value(Catalog const& b)
123
{
124
  return b.getHashValue();
125
}
126
2126.2.1 by Brian Aker
Merge in changes for catalogs usage of constants for identifier.
127
void Catalog::getSQLPath(std::string &sql_path) const
128
{
129
  sql_path= _name;
130
}
131
132
1960.1.5 by Brian Aker
Merging in the first pass through the catalog work.
133
134
} /* namespace identifier */
135
} /* namespace drizzled */