~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
21
#include "config.h"
22
23
#include <assert.h>
24
1660.1.1 by Brian Aker
Merge in move identifier work.
25
#include "drizzled/identifier.h"
1415 by Brian Aker
Mass overhaul to use schema_identifier.
26
#include "drizzled/session.h"
27
#include "drizzled/current_session.h"
28
#include "drizzled/internal/my_sys.h"
29
1904.1.1 by Brian Aker
Merge in change to have just a single function for both
30
#include "drizzled/util/tablename_to_filename.h"
1861.5.1 by Brian Aker
This just fixes our current catalog to be displayed properly.
31
#include "drizzled/util/backtrace.h"
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
41
namespace drizzled
42
{
43
2087.4.1 by Brian Aker
Merge in schema identifier.
44
namespace identifier
45
{
46
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
47
extern string drizzle_tmpdir;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
48
extern pid_t current_pid;
49
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
50
static size_t build_schema_filename(string &path, const string &db)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
51
{
1864.3.10 by Brian Aker
Restore so that local is not added to schema for path.
52
  path.append("");
1415 by Brian Aker
Mass overhaul to use schema_identifier.
53
  bool conversion_error= false;
54
1904.1.1 by Brian Aker
Merge in change to have just a single function for both
55
  conversion_error= util::tablename_to_filename(db, path);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
56
  if (conversion_error)
57
  {
58
    errmsg_printf(ERRMSG_LVL_ERROR,
59
                  _("Schema name cannot be encoded and fit within filesystem "
60
                    "name length restrictions."));
61
    return 0;
62
  }
63
64
  return path.length();
65
}
66
2087.4.1 by Brian Aker
Merge in schema identifier.
67
Schema::Schema(const std::string &db_arg) :
1613 by Brian Aker
Fix solaris warning.
68
  db(db_arg),
2039.6.6 by Brian Aker
Update so that CATALOG is correctly being displayed.
69
  db_path("")
1613 by Brian Aker
Fix solaris warning.
70
{ 
1861.5.1 by Brian Aker
This just fixes our current catalog to be displayed properly.
71
#if 0
72
  string::size_type lastPos= db.find_first_of('/', 0);
73
74
  if (lastPos != std::string::npos) 
75
  {
76
    catalog= db.substr(0, lastPos);
77
    db.erase(0, lastPos + 1);
78
  }
79
#endif
80
1685.2.12 by Brian Aker
This fixes the lower casing of names from Schema even when we should not.
81
  if (not db_arg.empty())
1613 by Brian Aker
Fix solaris warning.
82
  {
2087.4.1 by Brian Aker
Merge in schema identifier.
83
    build_schema_filename(db_path, db);
1613 by Brian Aker
Fix solaris warning.
84
    assert(db_path.length()); // TODO throw exception, this is a possibility
85
  }
86
}
87
2087.4.1 by Brian Aker
Merge in schema identifier.
88
void Schema::getSQLPath(std::string &arg) const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
89
{
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
90
  arg= db;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
91
}
92
2087.4.1 by Brian Aker
Merge in schema identifier.
93
const std::string &Schema::getPath() const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
94
{
95
  return db_path;
96
}
97
2087.4.1 by Brian Aker
Merge in schema identifier.
98
bool Schema::compare(const std::string &arg) const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
99
{
1685.2.11 by Brian Aker
First pass around removing need for lower case version of DB in the
100
  return boost::iequals(arg, db);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
101
}
102
2087.4.1 by Brian Aker
Merge in schema identifier.
103
bool Schema::compare(Schema::const_reference arg) const
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
104
{
105
  return boost::iequals(arg.getSchemaName(), db);
106
}
107
2087.4.1 by Brian Aker
Merge in schema identifier.
108
bool Schema::isValid() const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
109
{
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
110
  bool error= false;
111
112
  do
113
  {
114
    if (db.empty())
115
    {
116
      error= true;
117
      break;
118
    }
119
120
    if (db.size() > NAME_LEN)
121
    {
122
      error= true;
123
      break;
124
    }
125
126
    if (db.at(db.length() -1) == ' ')
127
    {
128
      error= true;
129
      break;
130
    }
131
132
    if (db.at(0) == '.')
133
    {
134
      error= true;
135
      break;
136
    }
137
138
    {
139
      const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
140
141
      int well_formed_error;
142
      uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
143
                                              NAME_CHAR_LEN, &well_formed_error);
144
      if (well_formed_error or db.length() != res)
145
      {
146
        error= true;
147
        break;
148
      }
149
    }
150
  } while (0);
151
152
  if (error)
153
  {
2041.3.15 by Brian Aker
Cleanup error usage around identifier usage.
154
    my_error(ER_WRONG_DB_NAME, *this);
1992.4.1 by Brian Aker
Update code for testing identifiers/table/schema name correctness.
155
1415 by Brian Aker
Mass overhaul to use schema_identifier.
156
    return false;
157
  }
158
159
  return true;
160
}
161
2087.4.1 by Brian Aker
Merge in schema identifier.
162
const std::string &Schema::getCatalogName() const
2060.4.1 by Brian Aker
Merge in trunk to catalogs.
163
{
164
  return drizzled::catalog::local_identifier().name();
165
}
166
2087.4.1 by Brian Aker
Merge in schema identifier.
167
std::ostream& operator<<(std::ostream& output, const Schema&identifier)
2060.4.1 by Brian Aker
Merge in trunk to catalogs.
168
{
2087.4.1 by Brian Aker
Merge in schema identifier.
169
  output << "identifier::Schema:(";
2060.4.1 by Brian Aker
Merge in trunk to catalogs.
170
  output <<  catalog::local_identifier();
171
  output << ", ";
172
  output <<  identifier.getSchemaName().c_str();
173
  output << ", ";
174
  output << identifier.getPath().c_str();
175
  output << ")";
176
177
  return output;  // for multiple << operators.
178
}
179
2087.4.1 by Brian Aker
Merge in schema identifier.
180
} /* namespace identifier */
1415 by Brian Aker
Mass overhaul to use schema_identifier.
181
} /* namespace drizzled */