~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/schema.cc

  • Committer: Brian Aker
  • Date: 2011-02-22 06:12:02 UTC
  • mfrom: (2190.1.6 drizzle-build)
  • Revision ID: brian@tangent.org-20110222061202-k03czxykqy4x9hjs
List update, header fixes, multiple symbols, and David deletes some code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
18
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
19
 */
20
20
 
21
 
#include "config.h"
 
21
#include <config.h>
22
22
 
23
23
#include <assert.h>
24
24
 
25
 
#include "drizzled/identifier.h"
26
 
#include "drizzled/session.h"
27
 
#include "drizzled/current_session.h"
28
 
#include "drizzled/internal/my_sys.h"
 
25
#include <drizzled/identifier.h>
 
26
#include <drizzled/session.h>
 
27
#include <drizzled/internal/my_sys.h>
29
28
 
30
 
#include "drizzled/util/backtrace.h"
 
29
#include <drizzled/util/tablename_to_filename.h>
 
30
#include <drizzled/util/backtrace.h>
31
31
 
32
32
#include <algorithm>
33
33
#include <sstream>
40
40
namespace drizzled
41
41
{
42
42
 
 
43
namespace identifier
 
44
{
 
45
 
43
46
extern string drizzle_tmpdir;
44
 
extern pid_t current_pid;
45
 
 
46
 
static const char hexchars[]= "0123456789abcdef";
47
 
 
48
 
static bool tablename_to_filename(const string &from, string &to);
49
47
 
50
48
static size_t build_schema_filename(string &path, const string &db)
51
49
{
52
50
  path.append("");
53
51
  bool conversion_error= false;
54
52
 
55
 
  conversion_error= tablename_to_filename(db, path);
 
53
  conversion_error= util::tablename_to_filename(db, path);
56
54
  if (conversion_error)
57
55
  {
58
 
    errmsg_printf(ERRMSG_LVL_ERROR,
 
56
    errmsg_printf(error::ERROR,
59
57
                  _("Schema name cannot be encoded and fit within filesystem "
60
58
                    "name length restrictions."));
61
59
    return 0;
64
62
  return path.length();
65
63
}
66
64
 
67
 
 
68
 
/*
69
 
  Translate a table name to a cursor name (WL #1324).
70
 
 
71
 
  SYNOPSIS
72
 
    tablename_to_filename()
73
 
      from                      The table name
74
 
      to                OUT     The cursor name
75
 
 
76
 
  RETURN
77
 
    true if errors happen. false on success.
78
 
*/
79
 
static bool tablename_to_filename(const string &from, string &to)
80
 
{
81
 
  
82
 
  string::const_iterator iter= from.begin();
83
 
  for (; iter != from.end(); ++iter)
84
 
  {
85
 
    if (isascii(*iter))
86
 
    {
87
 
      if ((isdigit(*iter)) ||
88
 
          (islower(*iter)) ||
89
 
          (*iter == '_') ||
90
 
          (*iter == ' ') ||
91
 
          (*iter == '-'))
92
 
      {
93
 
        to.push_back(*iter);
94
 
        continue;
95
 
      }
96
 
 
97
 
      if (isupper(*iter))
98
 
      {
99
 
        to.push_back(tolower(*iter));
100
 
        continue;
101
 
      }
102
 
    }
103
 
   
104
 
    /* We need to escape this char in a way that can be reversed */
105
 
    to.push_back('@');
106
 
    to.push_back(hexchars[(*iter >> 4) & 15]);
107
 
    to.push_back(hexchars[(*iter) & 15]);
108
 
  }
109
 
 
110
 
  if (internal::check_if_legal_tablename(to.c_str()))
111
 
  {
112
 
    to.append("@@@");
113
 
  }
114
 
  return false;
115
 
}
116
 
 
117
 
SchemaIdentifier::SchemaIdentifier(const std::string &db_arg) :
 
65
Schema::Schema(const std::string &db_arg) :
118
66
  db(db_arg),
119
 
  db_path(""),
120
 
  catalog("LOCAL")
 
67
  db_path("")
121
68
122
69
#if 0
123
70
  string::size_type lastPos= db.find_first_of('/', 0);
131
78
 
132
79
  if (not db_arg.empty())
133
80
  {
134
 
    drizzled::build_schema_filename(db_path, db);
 
81
    build_schema_filename(db_path, db);
135
82
    assert(db_path.length()); // TODO throw exception, this is a possibility
136
83
  }
137
84
}
138
85
 
139
 
const std::string &SchemaIdentifier::getSQLPath()
 
86
void Schema::getSQLPath(std::string &arg) const
140
87
{
141
 
  return getSchemaName();
 
88
  arg= db;
142
89
}
143
90
 
144
 
const std::string &SchemaIdentifier::getPath() const
 
91
const std::string &Schema::getPath() const
145
92
{
146
93
  return db_path;
147
94
}
148
95
 
149
 
bool SchemaIdentifier::compare(const std::string &arg) const
 
96
bool Schema::compare(const std::string &arg) const
150
97
{
151
98
  return boost::iequals(arg, db);
152
99
}
153
100
 
154
 
bool SchemaIdentifier::isValid() const
155
 
{
156
 
  if (db.empty())
157
 
    return false;
158
 
 
159
 
  if (db.size() > NAME_LEN)
160
 
    return false;
161
 
 
162
 
  if (db.at(db.length() -1) == ' ')
163
 
    return false;
164
 
 
165
 
  const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
166
 
 
167
 
  int well_formed_error;
168
 
  uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
169
 
                                          NAME_CHAR_LEN, &well_formed_error);
170
 
 
171
 
  if (well_formed_error)
172
 
  {
173
 
    my_error(ER_INVALID_CHARACTER_STRING, MYF(0), "identifier", db.c_str());
 
101
bool Schema::compare(Schema::const_reference arg) const
 
102
{
 
103
  return boost::iequals(arg.getSchemaName(), db);
 
104
}
 
105
 
 
106
bool Schema::isValid() const
 
107
{
 
108
  bool error= false;
 
109
 
 
110
  do
 
111
  {
 
112
    if (db.empty())
 
113
    {
 
114
      error= true;
 
115
      break;
 
116
    }
 
117
 
 
118
    if (db.size() > NAME_LEN)
 
119
    {
 
120
      error= true;
 
121
      break;
 
122
    }
 
123
 
 
124
    if (db.at(db.length() -1) == ' ')
 
125
    {
 
126
      error= true;
 
127
      break;
 
128
    }
 
129
 
 
130
    if (db.at(0) == '.')
 
131
    {
 
132
      error= true;
 
133
      break;
 
134
    }
 
135
 
 
136
    {
 
137
      const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
 
138
 
 
139
      int well_formed_error;
 
140
      uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
 
141
                                              NAME_CHAR_LEN, &well_formed_error);
 
142
      if (well_formed_error or db.length() != res)
 
143
      {
 
144
        error= true;
 
145
        break;
 
146
      }
 
147
    }
 
148
  } while (0);
 
149
 
 
150
  if (error)
 
151
  {
 
152
    my_error(ER_WRONG_DB_NAME, *this);
 
153
 
174
154
    return false;
175
155
  }
176
156
 
177
 
  if (db.length() != res)
178
 
    return false;
179
 
 
180
157
  return true;
181
158
}
182
159
 
 
160
const std::string &Schema::getCatalogName() const
 
161
{
 
162
  return drizzled::catalog::local_identifier().name();
 
163
}
 
164
 
 
165
std::ostream& operator<<(std::ostream& output, const Schema&identifier)
 
166
{
 
167
  output << "identifier::Schema:(";
 
168
  output <<  catalog::local_identifier();
 
169
  output << ", ";
 
170
  output <<  identifier.getSchemaName().c_str();
 
171
  output << ", ";
 
172
  output << identifier.getPath().c_str();
 
173
  output << ")";
 
174
 
 
175
  return output;  // for multiple << operators.
 
176
}
 
177
 
 
178
} /* namespace identifier */
183
179
} /* namespace drizzled */