~drizzle-trunk/drizzle/development

1415 by Brian Aker
Mass overhaul to use schema_identifier.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
1415 by Brian Aker
Mass overhaul to use schema_identifier.
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>
2239.1.6 by Olaf van der Spek
Refactor includes
22
#include <cassert>
23
#include <drizzled/errmsg_print.h>
24
#include <drizzled/gettext.h>
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>
2241.3.14 by Olaf van der Spek
Refactor
28
#include <drizzled/catalog/local.h>
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>
2241.4.16 by Stewart Smith
fix charset_info includes
31
#include <drizzled/charset_info.h>
1861.5.1 by Brian Aker
This just fixes our current catalog to be displayed properly.
32
1415 by Brian Aker
Mass overhaul to use schema_identifier.
33
#include <algorithm>
34
#include <sstream>
35
#include <cstdio>
36
1685.2.11 by Brian Aker
First pass around removing need for lower case version of DB in the
37
#include <boost/algorithm/string/compare.hpp>
38
1415 by Brian Aker
Mass overhaul to use schema_identifier.
39
using namespace std;
40
2241.3.14 by Olaf van der Spek
Refactor
41
namespace drizzled {
42
namespace identifier {
2087.4.1 by Brian Aker
Merge in schema identifier.
43
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
44
extern string drizzle_tmpdir;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
45
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
46
static size_t build_schema_filename(string &path, const string &db)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
47
{
1864.3.10 by Brian Aker
Restore so that local is not added to schema for path.
48
  path.append("");
1415 by Brian Aker
Mass overhaul to use schema_identifier.
49
  bool conversion_error= false;
50
1904.1.1 by Brian Aker
Merge in change to have just a single function for both
51
  conversion_error= util::tablename_to_filename(db, path);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
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,
1415 by Brian Aker
Mass overhaul to use schema_identifier.
55
                  _("Schema name cannot be encoded and fit within filesystem "
56
                    "name length restrictions."));
57
    return 0;
58
  }
59
60
  return path.length();
61
}
62
2087.4.1 by Brian Aker
Merge in schema identifier.
63
Schema::Schema(const std::string &db_arg) :
2241.3.14 by Olaf van der Spek
Refactor
64
  db(db_arg)
1613 by Brian Aker
Fix solaris warning.
65
{ 
1861.5.1 by Brian Aker
This just fixes our current catalog to be displayed properly.
66
#if 0
67
  string::size_type lastPos= db.find_first_of('/', 0);
68
69
  if (lastPos != std::string::npos) 
70
  {
71
    catalog= db.substr(0, lastPos);
72
    db.erase(0, lastPos + 1);
73
  }
74
#endif
75
1685.2.12 by Brian Aker
This fixes the lower casing of names from Schema even when we should not.
76
  if (not db_arg.empty())
1613 by Brian Aker
Fix solaris warning.
77
  {
2087.4.1 by Brian Aker
Merge in schema identifier.
78
    build_schema_filename(db_path, db);
1613 by Brian Aker
Fix solaris warning.
79
    assert(db_path.length()); // TODO throw exception, this is a possibility
80
  }
81
}
82
2087.4.1 by Brian Aker
Merge in schema identifier.
83
const std::string &Schema::getPath() const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
84
{
85
  return db_path;
86
}
87
2087.4.1 by Brian Aker
Merge in schema identifier.
88
bool Schema::compare(const std::string &arg) const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
89
{
1685.2.11 by Brian Aker
First pass around removing need for lower case version of DB in the
90
  return boost::iequals(arg, db);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
91
}
92
2246.4.9 by Olaf van der Spek
Remove const_reference and reference from identifier::Schema
93
bool Schema::compare(const Schema& arg) const
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
94
{
95
  return boost::iequals(arg.getSchemaName(), db);
96
}
97
2087.4.1 by Brian Aker
Merge in schema identifier.
98
bool Schema::isValid() const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
99
{
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
100
  bool error= false;
101
102
  do
103
  {
104
    if (db.empty())
105
    {
106
      error= true;
107
      break;
108
    }
109
110
    if (db.size() > NAME_LEN)
111
    {
112
      error= true;
113
      break;
114
    }
115
116
    if (db.at(db.length() -1) == ' ')
117
    {
118
      error= true;
119
      break;
120
    }
121
122
    if (db.at(0) == '.')
123
    {
124
      error= true;
125
      break;
126
    }
127
128
    {
2254 by Brian Aker
Shift CHARSET_INFO to charset_info_st
129
      const charset_info_st * const cs= &my_charset_utf8mb4_general_ci;
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
130
131
      int well_formed_error;
132
      uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
133
                                              NAME_CHAR_LEN, &well_formed_error);
134
      if (well_formed_error or db.length() != res)
135
      {
136
        error= true;
137
        break;
138
      }
139
    }
140
  } while (0);
141
142
  if (error)
143
  {
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
144
    my_error(ER_WRONG_DB_NAME, *this);
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
145
1415 by Brian Aker
Mass overhaul to use schema_identifier.
146
    return false;
147
  }
148
149
  return true;
150
}
151
2087.4.1 by Brian Aker
Merge in schema identifier.
152
const std::string &Schema::getCatalogName() const
2060.4.1 by Brian Aker
Merge in trunk to catalogs.
153
{
154
  return drizzled::catalog::local_identifier().name();
155
}
156
2087.4.1 by Brian Aker
Merge in schema identifier.
157
std::ostream& operator<<(std::ostream& output, const Schema&identifier)
2060.4.1 by Brian Aker
Merge in trunk to catalogs.
158
{
2241.3.14 by Olaf van der Spek
Refactor
159
  return output << "identifier::Schema:(" <<  catalog::local_identifier() << ", " <<  identifier.getSchemaName() << ", " << identifier.getPath() << ")";
2060.4.1 by Brian Aker
Merge in trunk to catalogs.
160
}
161
2087.4.1 by Brian Aker
Merge in schema identifier.
162
} /* namespace identifier */
1415 by Brian Aker
Mass overhaul to use schema_identifier.
163
} /* namespace drizzled */