Drizzled Public API Documentation

schema.cc
00001 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2009 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <cassert>
00023 #include <drizzled/errmsg_print.h>
00024 #include <drizzled/gettext.h>
00025 #include <drizzled/identifier.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/internal/my_sys.h>
00028 #include <drizzled/catalog/local.h>
00029 #include <drizzled/util/tablename_to_filename.h>
00030 #include <drizzled/util/backtrace.h>
00031 #include <drizzled/charset.h>
00032 
00033 #include <algorithm>
00034 #include <sstream>
00035 #include <cstdio>
00036 
00037 #include <boost/algorithm/string/compare.hpp>
00038 
00039 using namespace std;
00040 
00041 namespace drizzled {
00042 namespace identifier {
00043 
00044 Schema::Schema(str_ref db_arg) :
00045   db(db_arg.data(), db_arg.size())
00046 { 
00047 #if 0
00048   string::size_type lastPos= db.find_first_of('/', 0);
00049 
00050   if (lastPos != std::string::npos) 
00051   {
00052     catalog= db.substr(0, lastPos);
00053     db.erase(0, lastPos + 1);
00054   }
00055 #endif
00056 
00057   if (not db_arg.empty())
00058   {
00059     db_path += util::tablename_to_filename(db);
00060     assert(db_path.length()); // TODO throw exception, this is a possibility
00061   }
00062 }
00063 
00064 const std::string &Schema::getPath() const
00065 {
00066   return db_path;
00067 }
00068 
00069 bool Schema::compare(const std::string &arg) const
00070 {
00071   return boost::iequals(arg, db);
00072 }
00073 
00074 bool Schema::compare(const Schema& arg) const
00075 {
00076   return boost::iequals(arg.getSchemaName(), db);
00077 }
00078 
00079 bool Schema::isValid() const
00080 {
00081   const charset_info_st& cs= my_charset_utf8mb4_general_ci;
00082   int well_formed_error;
00083   if (not db.empty()
00084     && db.size() <= NAME_LEN
00085     && db.at(0) != '.'
00086     && db.at(db.size() - 1) != ' '
00087     && db.size() == cs.cset->well_formed_len(cs, db, NAME_CHAR_LEN, &well_formed_error))
00088   {
00089     if (not well_formed_error)
00090       return true;
00091   }
00092   my_error(ER_WRONG_DB_NAME, *this);
00093   return false;
00094 }
00095 
00096 const std::string &Schema::getCatalogName() const
00097 {
00098   return drizzled::catalog::local_identifier().name();
00099 }
00100 
00101 std::ostream& operator<<(std::ostream& output, const Schema&identifier)
00102 {
00103   return output << "identifier::Schema:(" <<  drizzled::catalog::local_identifier() << ", " <<  identifier.getSchemaName() << ", " << identifier.getPath() << ")";
00104 }
00105 
00106 } /* namespace identifier */
00107 } /* namespace drizzled */