~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/schema.cc

  • Committer: Brian Aker
  • Date: 2008-07-08 16:17:31 UTC
  • Revision ID: brian@tangent.org-20080708161731-io36j7igglok79py
DATE cleanup.

Show diffs side-by-side

added added

removed removed

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