Drizzled Public API Documentation

message.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Brian Aker
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 <drizzled/show.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/schema.h>
00026 #include <drizzled/plugin/event_observer.h>
00027 #include <drizzled/message.h>
00028 
00029 #include <drizzled/message/table.pb.h>
00030 #include <drizzled/message/schema.pb.h>
00031 
00032 #include <string>
00033 
00034 namespace drizzled {
00035 namespace message {
00036 
00037 static const std::string PROGRAM_ERROR("PROGRAM_ERROR");
00038 
00039 // These are used to generate strings for types
00040 static const std::string VARCHAR("VARCHAR");
00041 static const std::string VARBINARY("VARBINARY");
00042 static const std::string DOUBLE("DOUBLE");
00043 static const std::string TEXT("TEXT");
00044 static const std::string BLOB("BLOB");
00045 static const std::string ENUM("ENUM");
00046 static const std::string INTEGER("INTEGER");
00047 static const std::string BIGINT("BIGINT");
00048 static const std::string DECIMAL("DECIMAL");
00049 static const std::string DATE("DATE");
00050 static const std::string EPOCH("EPOCH");
00051 static const std::string TIMESTAMP("TIMESTAMP");
00052 static const std::string MICROTIME("MICROTIME");
00053 static const std::string DATETIME("DATETIME");
00054 static const std::string TIME("TIME");
00055 static const std::string UUID("UUID");
00056 static const std::string BOOLEAN("BOOLEAN");
00057 
00058 static const std::string UNDEFINED("UNDEFINED");
00059 static const std::string RESTRICT("RESTRICT");
00060 static const std::string CASCADE("CASCADE");
00061 static const std::string SET_NULL("SET NULL");
00062 static const std::string NO_ACTION("NO ACTION");
00063 static const std::string SET_DEFAULT("SET DEFAULT");
00064 
00065 static const std::string YES("YES");
00066 static const std::string NO("NO");
00067 
00068 static const std::string UNKNOWN_INDEX("UNKNOWN_INDEX");
00069 static const std::string BTREE("BTREE");
00070 static const std::string RTREE("RTREE");
00071 static const std::string HASH("HASH");
00072 static const std::string FULLTEXT("FULLTEXT");
00073 
00074 static const std::string MATCH_FULL("FULL");
00075 static const std::string MATCH_PARTIAL("PARTIAL");
00076 static const std::string MATCH_SIMPLE("SIMPLE");
00077 
00078 const static std::string STANDARD_STRING("STANDARD");
00079 const static std::string TEMPORARY_STRING("TEMPORARY");
00080 const static std::string INTERNAL_STRING("INTERNAL");
00081 const static std::string FUNCTION_STRING("FUNCTION");
00082 
00083 void update(drizzled::message::Schema &arg)
00084 {
00085   arg.set_version(arg.version() + 1);
00086   arg.set_update_timestamp(time(NULL));
00087 }
00088 
00089 void update(drizzled::message::Table &arg)
00090 {
00091   arg.set_version(arg.version() + 1);
00092   arg.set_update_timestamp(time(NULL));
00093 }
00094 
00095 bool is_numeric(const message::Table::Field &field)
00096 {
00097   message::Table::Field::FieldType type= field.type();
00098 
00099   switch (type)
00100   {
00101   case message::Table::Field::DOUBLE:
00102   case message::Table::Field::INTEGER:
00103   case message::Table::Field::BIGINT:
00104   case message::Table::Field::DECIMAL:
00105     return true;
00106   case message::Table::Field::BLOB:
00107   case message::Table::Field::VARCHAR:
00108   case message::Table::Field::ENUM:
00109   case message::Table::Field::DATE:
00110   case message::Table::Field::EPOCH:
00111   case message::Table::Field::DATETIME:
00112   case message::Table::Field::TIME:
00113   case message::Table::Field::UUID:
00114   case message::Table::Field::BOOLEAN:
00115     break;
00116   }
00117 
00118   return false;
00119 }
00120 
00121 const std::string &type(const message::Table::Field &field)
00122 {
00123   message::Table::Field::FieldType type= field.type();
00124 
00125   switch (type)
00126   {
00127   case message::Table::Field::VARCHAR:
00128     return field.string_options().collation().compare("binary") ? VARCHAR : VARBINARY;
00129   case message::Table::Field::DOUBLE:
00130     return DOUBLE;
00131   case message::Table::Field::BLOB:
00132     return field.string_options().collation().compare("binary") ? TEXT : BLOB;
00133   case message::Table::Field::ENUM:
00134     return ENUM;
00135   case message::Table::Field::INTEGER:
00136     return INTEGER;
00137   case message::Table::Field::BIGINT:
00138     return BIGINT;
00139   case message::Table::Field::DECIMAL:
00140     return DECIMAL;
00141   case message::Table::Field::DATE:
00142     return DATE;
00143   case message::Table::Field::EPOCH:
00144     return TIMESTAMP;
00145   case message::Table::Field::DATETIME:
00146     return DATETIME;
00147   case message::Table::Field::TIME:
00148     return TIME;
00149   case message::Table::Field::UUID:
00150     return UUID;
00151   case message::Table::Field::BOOLEAN:
00152     return BOOLEAN;
00153   }
00154 
00155   abort();
00156 }
00157 
00158 const std::string &type(drizzled::message::Table::Field::FieldType type)
00159 {
00160   switch (type)
00161   {
00162   case message::Table::Field::VARCHAR:
00163     return VARCHAR;
00164   case message::Table::Field::DOUBLE:
00165     return DOUBLE;
00166   case message::Table::Field::BLOB:
00167     return BLOB;
00168   case message::Table::Field::ENUM:
00169     return ENUM;
00170   case message::Table::Field::INTEGER:
00171     return INTEGER;
00172   case message::Table::Field::BIGINT:
00173     return BIGINT;
00174   case message::Table::Field::DECIMAL:
00175     return DECIMAL;
00176   case message::Table::Field::DATE:
00177     return DATE;
00178   case message::Table::Field::EPOCH:
00179     return EPOCH;
00180   case message::Table::Field::DATETIME:
00181     return DATETIME;
00182   case message::Table::Field::TIME:
00183     return TIME;
00184   case message::Table::Field::UUID:
00185     return UUID;
00186   case message::Table::Field::BOOLEAN:
00187     return BOOLEAN;
00188   }
00189 
00190   abort();
00191 }
00192 
00193 const std::string &type(drizzled::message::Table::ForeignKeyConstraint::ForeignKeyOption type)
00194 {
00195   switch (type)
00196   {
00197   case message::Table::ForeignKeyConstraint::OPTION_RESTRICT:
00198     return RESTRICT;
00199   case message::Table::ForeignKeyConstraint::OPTION_CASCADE:
00200     return CASCADE;
00201   case message::Table::ForeignKeyConstraint::OPTION_SET_NULL:
00202     return SET_NULL;
00203   case message::Table::ForeignKeyConstraint::OPTION_UNDEF:
00204   case message::Table::ForeignKeyConstraint::OPTION_NO_ACTION:
00205     return NO_ACTION;
00206   case message::Table::ForeignKeyConstraint::OPTION_SET_DEFAULT:
00207     return SET_DEFAULT;
00208   }
00209 
00210   return NO_ACTION;
00211 }
00212 
00213 // This matches SQL standard of using YES/NO not the normal TRUE/FALSE
00214 const std::string &type(bool type)
00215 {
00216   return type ? YES : NO;
00217 }
00218 
00219 const std::string &type(drizzled::message::Table::Index::IndexType type)
00220 {
00221   switch (type)
00222   {
00223   case message::Table::Index::UNKNOWN_INDEX:
00224     return UNKNOWN_INDEX;
00225   case message::Table::Index::BTREE:
00226     return BTREE;
00227   case message::Table::Index::RTREE:
00228     return RTREE;
00229   case message::Table::Index::HASH:
00230     return HASH;
00231   case message::Table::Index::FULLTEXT:
00232     return FULLTEXT;
00233   }
00234 
00235   assert(0);
00236   return PROGRAM_ERROR;
00237 }
00238 
00239 const std::string &type(drizzled::message::Table::ForeignKeyConstraint::ForeignKeyMatchOption type)
00240 {
00241   switch (type)
00242   {
00243   case message::Table::ForeignKeyConstraint::MATCH_FULL:
00244     return MATCH_FULL;
00245   case message::Table::ForeignKeyConstraint::MATCH_PARTIAL:
00246     return MATCH_PARTIAL;
00247   case message::Table::ForeignKeyConstraint::MATCH_UNDEFINED:
00248   case message::Table::ForeignKeyConstraint::MATCH_SIMPLE:
00249     return MATCH_SIMPLE;
00250   }
00251 
00252   return MATCH_SIMPLE;
00253 }
00254 
00255 const std::string &type(drizzled::message::Table::TableType type)
00256 {
00257   switch (type)
00258   {
00259   case message::Table::STANDARD:
00260     return STANDARD_STRING;
00261   case message::Table::TEMPORARY:
00262     return TEMPORARY_STRING;
00263   case message::Table::INTERNAL:
00264     return INTERNAL_STRING;
00265   case message::Table::FUNCTION:
00266     return FUNCTION_STRING;
00267   }
00268 
00269   assert(0);
00270   return PROGRAM_ERROR;
00271 }
00272 
00273 #if 0
00274 std::ostream& operator<<(std::ostream& output, const message::Transaction &message)
00275 { 
00276     std::string buffer;
00277 
00278     google::protobuf::TextFormat::PrintToString(message, &buffer);
00279     output << buffer;
00280 
00281     return output;
00282 }
00283 
00284 std::ostream& operator<<(std::ostream& output, const message::Table &message)
00285 { 
00286   std::string buffer;
00287 
00288   google::protobuf::TextFormat::PrintToString(message, &buffer);
00289   output << buffer;
00290 
00291   return output;
00292 }
00293 #endif
00294 
00295 
00296 } /* namespace message */
00297 } /* namespace drizzled */