~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
 *
4
 *  Copyright (C) 2009 Sun Microsystems
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
25
#include "drizzled/schema_identifier.h"
26
#include "drizzled/session.h"
27
#include "drizzled/current_session.h"
28
#include "drizzled/internal/my_sys.h"
29
#include "drizzled/data_home.h"
30
31
#include <algorithm>
32
#include <sstream>
33
#include <cstdio>
34
35
using namespace std;
36
37
namespace drizzled
38
{
39
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
40
extern string drizzle_tmpdir;
1415 by Brian Aker
Mass overhaul to use schema_identifier.
41
extern pid_t current_pid;
42
43
static const char hexchars[]= "0123456789abcdef";
44
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
45
static bool tablename_to_filename(const string &from, string &to);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
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
static size_t build_schema_filename(string &path, const string &db)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
48
{
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
49
  string dbbuff("");
1415 by Brian Aker
Mass overhaul to use schema_identifier.
50
  bool conversion_error= false;
51
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
52
  conversion_error= tablename_to_filename(db, dbbuff);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
53
  if (conversion_error)
54
  {
55
    errmsg_printf(ERRMSG_LVL_ERROR,
56
                  _("Schema name cannot be encoded and fit within filesystem "
57
                    "name length restrictions."));
58
    return 0;
59
  }
60
   
61
62
  int rootdir_len= strlen(FN_ROOTDIR);
1300.5.25 by Monty Taylor
Fixed two merge issues.
63
  path.append(data_home);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
64
  ssize_t without_rootdir= path.length() - rootdir_len;
65
66
  /* Don't add FN_ROOTDIR if dirzzle_data_home already includes it */
67
  if (without_rootdir >= 0)
68
  {
69
    const char *tmp= path.c_str() + without_rootdir;
70
71
    if (memcmp(tmp, FN_ROOTDIR, rootdir_len) != 0)
72
      path.append(FN_ROOTDIR);
73
  }
74
75
  path.append(dbbuff);
76
77
  return path.length();
78
}
79
80
81
/*
82
  Translate a table name to a cursor name (WL #1324).
83
84
  SYNOPSIS
85
    tablename_to_filename()
86
      from                      The table name
87
      to                OUT     The cursor name
88
89
  RETURN
90
    true if errors happen. false on success.
91
*/
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
92
static bool tablename_to_filename(const string &from, string &to)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
93
{
94
  
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
95
  string::const_iterator iter= from.begin();
96
  for (; iter != from.end(); ++iter)
1415 by Brian Aker
Mass overhaul to use schema_identifier.
97
  {
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
98
    if ((*iter >= '0' && *iter <= '9') ||
99
        (*iter >= 'A' && *iter <= 'Z') ||
100
        (*iter >= 'a' && *iter <= 'z') ||
1415 by Brian Aker
Mass overhaul to use schema_identifier.
101
/* OSX defines an extra set of high-bit and multi-byte characters
102
   that cannot be used on the filesystem. Instead of trying to sort
103
   those out, we'll just escape encode all high-bit-set chars on OSX.
104
   It won't really hurt anything - it'll just make some filenames ugly. */
105
#if !defined(TARGET_OS_OSX)
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
106
        ((unsigned char)*iter >= 128) ||
1415 by Brian Aker
Mass overhaul to use schema_identifier.
107
#endif
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
108
        (*iter == '_') ||
109
        (*iter == ' ') ||
110
        (*iter == '-'))
1415 by Brian Aker
Mass overhaul to use schema_identifier.
111
    {
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
112
      to.push_back(*iter);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
113
      continue;
114
    }
115
   
116
    /* We need to escape this char in a way that can be reversed */
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
117
    to.push_back('@');
118
    to.push_back(hexchars[(*iter >> 4) & 15]);
119
    to.push_back(hexchars[(*iter) & 15]);
1415 by Brian Aker
Mass overhaul to use schema_identifier.
120
  }
121
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
122
  if (internal::check_if_legal_tablename(to.c_str()))
1415 by Brian Aker
Mass overhaul to use schema_identifier.
123
  {
1627.2.4 by Monty Taylor
Fixed the valgrind error that crept in in schema_identifier. Score one for std:string over char*.
124
    to.append("@@@");
1415 by Brian Aker
Mass overhaul to use schema_identifier.
125
  }
126
  return false;
127
}
128
1613 by Brian Aker
Fix solaris warning.
129
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
130
  db(db_arg),
1627 by Monty Taylor
Killed a valgrind warning that had snuck in.
131
  db_path(""),
1613 by Brian Aker
Fix solaris warning.
132
  lower_db(db_arg)
133
{ 
134
  std::transform(lower_db.begin(), lower_db.end(),
135
                 lower_db.begin(), ::tolower);
136
137
  if (not lower_db.empty())
138
  {
1627 by Monty Taylor
Killed a valgrind warning that had snuck in.
139
    drizzled::build_schema_filename(db_path, lower_db);
1613 by Brian Aker
Fix solaris warning.
140
    assert(db_path.length()); // TODO throw exception, this is a possibility
141
  }
142
}
143
1415 by Brian Aker
Mass overhaul to use schema_identifier.
144
const std::string &SchemaIdentifier::getSQLPath()
145
{
146
  return getSchemaName();
147
}
148
1613 by Brian Aker
Fix solaris warning.
149
const std::string &SchemaIdentifier::getPath() const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
150
{
151
  return db_path;
152
}
153
1418.1.2 by Monty Taylor
Fixed Solaris issue with the operator< by adding some constness. While I was
154
bool SchemaIdentifier::compare(std::string arg) const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
155
{
156
  std::transform(arg.begin(), arg.end(),
157
                 arg.begin(), ::tolower);
158
159
  return arg == lower_db;
160
}
161
1613 by Brian Aker
Fix solaris warning.
162
bool SchemaIdentifier::isValid() const
1415 by Brian Aker
Mass overhaul to use schema_identifier.
163
{
164
  if (lower_db.empty())
165
    return false;
166
167
  if (lower_db.size() > NAME_LEN)
168
    return false;
169
170
  if (lower_db.at(lower_db.length() -1) == ' ')
171
    return false;
172
173
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
174
175
  int well_formed_error;
176
  uint32_t res= cs->cset->well_formed_len(cs, lower_db.c_str(), lower_db.c_str() + lower_db.length(),
177
                                          NAME_CHAR_LEN, &well_formed_error);
178
179
  if (well_formed_error)
180
  {
181
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", lower_db.c_str());
182
    return false;
183
  }
184
185
  if (lower_db.length() != res)
186
    return false;
187
188
  return true;
189
}
190
191
} /* namespace drizzled */