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 
00023 #include <assert.h>
00024 
00025 #include <drizzled/identifier.h>
00026 #include <drizzled/session.h>
00027 #include <drizzled/internal/my_sys.h>
00028 
00029 #include <drizzled/util/tablename_to_filename.h>
00030 #include <drizzled/util/backtrace.h>
00031 
00032 #include <algorithm>
00033 #include <sstream>
00034 #include <cstdio>
00035 
00036 #include <boost/algorithm/string/compare.hpp>
00037 
00038 using namespace std;
00039 
00040 namespace drizzled
00041 {
00042 
00043 namespace identifier
00044 {
00045 
00046 extern string drizzle_tmpdir;
00047 
00048 static size_t build_schema_filename(string &path, const string &db)
00049 {
00050   path.append("");
00051   bool conversion_error= false;
00052 
00053   conversion_error= util::tablename_to_filename(db, path);
00054   if (conversion_error)
00055   {
00056     errmsg_printf(error::ERROR,
00057                   _("Schema name cannot be encoded and fit within filesystem "
00058                     "name length restrictions."));
00059     return 0;
00060   }
00061 
00062   return path.length();
00063 }
00064 
00065 Schema::Schema(const std::string &db_arg) :
00066   db(db_arg),
00067   db_path("")
00068 { 
00069 #if 0
00070   string::size_type lastPos= db.find_first_of('/', 0);
00071 
00072   if (lastPos != std::string::npos) 
00073   {
00074     catalog= db.substr(0, lastPos);
00075     db.erase(0, lastPos + 1);
00076   }
00077 #endif
00078 
00079   if (not db_arg.empty())
00080   {
00081     build_schema_filename(db_path, db);
00082     assert(db_path.length()); // TODO throw exception, this is a possibility
00083   }
00084 }
00085 
00086 void Schema::getSQLPath(std::string &arg) const
00087 {
00088   arg= db;
00089 }
00090 
00091 const std::string &Schema::getPath() const
00092 {
00093   return db_path;
00094 }
00095 
00096 bool Schema::compare(const std::string &arg) const
00097 {
00098   return boost::iequals(arg, db);
00099 }
00100 
00101 bool Schema::compare(Schema::const_reference arg) const
00102 {
00103   return boost::iequals(arg.getSchemaName(), db);
00104 }
00105 
00106 bool Schema::isValid() const
00107 {
00108   bool error= false;
00109 
00110   do
00111   {
00112     if (db.empty())
00113     {
00114       error= true;
00115       break;
00116     }
00117 
00118     if (db.size() > NAME_LEN)
00119     {
00120       error= true;
00121       break;
00122     }
00123 
00124     if (db.at(db.length() -1) == ' ')
00125     {
00126       error= true;
00127       break;
00128     }
00129 
00130     if (db.at(0) == '.')
00131     {
00132       error= true;
00133       break;
00134     }
00135 
00136     {
00137       const CHARSET_INFO * const cs= &my_charset_utf8mb4_general_ci;
00138 
00139       int well_formed_error;
00140       uint32_t res= cs->cset->well_formed_len(cs, db.c_str(), db.c_str() + db.length(),
00141                                               NAME_CHAR_LEN, &well_formed_error);
00142       if (well_formed_error or db.length() != res)
00143       {
00144         error= true;
00145         break;
00146       }
00147     }
00148   } while (0);
00149 
00150   if (error)
00151   {
00152     my_error(ER_WRONG_DB_NAME, *this);
00153 
00154     return false;
00155   }
00156 
00157   return true;
00158 }
00159 
00160 const std::string &Schema::getCatalogName() const
00161 {
00162   return drizzled::catalog::local_identifier().name();
00163 }
00164 
00165 std::ostream& operator<<(std::ostream& output, const Schema&identifier)
00166 {
00167   output << "identifier::Schema:(";
00168   output <<  catalog::local_identifier();
00169   output << ", ";
00170   output <<  identifier.getSchemaName().c_str();
00171   output << ", ";
00172   output << identifier.getPath().c_str();
00173   output << ")";
00174 
00175   return output;  // for multiple << operators.
00176 }
00177 
00178 } /* namespace identifier */
00179 } /* namespace drizzled */