~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/schema.cc

Merge in Hartmut's work

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