Drizzled Public API Documentation

schema.cc
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, Inc.
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 #include <cassert>
23 #include <drizzled/errmsg_print.h>
24 #include <drizzled/gettext.h>
25 #include <drizzled/identifier.h>
26 #include <drizzled/session.h>
27 #include <drizzled/internal/my_sys.h>
28 #include <drizzled/catalog/local.h>
29 #include <drizzled/util/tablename_to_filename.h>
30 #include <drizzled/util/backtrace.h>
31 #include <drizzled/charset.h>
32 
33 #include <algorithm>
34 #include <sstream>
35 #include <cstdio>
36 
37 #include <boost/algorithm/string/compare.hpp>
38 
39 using namespace std;
40 
41 namespace drizzled {
42 namespace identifier {
43 
44 Schema::Schema(const drizzled::identifier::Catalog &catalog_arg,
45  str_ref db_arg) :
46  db(db_arg.data(), db_arg.size()),
47  _catalog(catalog_arg)
48 {
49 #if 0
50  string::size_type lastPos= db.find_first_of('/', 0);
51 
52  if (lastPos != std::string::npos)
53  {
54  catalog= db.substr(0, lastPos);
55  db.erase(0, lastPos + 1);
56  }
57 #endif
58 
59  if (db_arg.empty() == false)
60  {
61  db_path += _catalog.getPath();
62  db_path += FN_LIBCHAR;
63  db_path += util::tablename_to_filename(db);
64  assert(db_path.length()); // TODO throw exception, this is a possibility
65  }
66 
67  _corrected_db= boost::to_lower_copy(db);
68 }
69 
70 const std::string &Schema::getPath() const
71 {
72  return db_path;
73 }
74 
75 bool Schema::compare(const std::string &arg) const
76 {
77  return boost::iequals(arg, db);
78 }
79 
80 bool Schema::compare(const Schema& arg) const
81 {
82  return boost::iequals(arg.getSchemaName(), db);
83 }
84 
85 bool Schema::isValid() const
86 {
87  const charset_info_st& cs= my_charset_utf8mb4_general_ci;
88  int well_formed_error;
89  if (not db.empty()
90  && db.size() <= NAME_LEN
91  && db.at(0) != '.'
92  && db.at(db.size() - 1) != ' '
93  && db.size() == cs.cset->well_formed_len(cs, db, NAME_CHAR_LEN, &well_formed_error))
94  {
95  if (not well_formed_error)
96  return true;
97  }
98  my_error(ER_WRONG_DB_NAME, *this);
99  return false;
100 }
101 
102 const std::string &Schema::getCatalogName() const
103 {
104  return _catalog.name();
105 }
106 
107 std::ostream& operator<<(std::ostream& output, const Schema&identifier)
108 {
109  return output << "identifier::Schema:(" << identifier.getCatalogName() << ", " << identifier.getSchemaName() << ", " << identifier.getPath() << ")";
110 }
111 
112 } /* namespace identifier */
113 } /* namespace drizzled */