Drizzled Public API Documentation

show_schema_proto.cc
00001 /* vim: expandtab:shiftwidth=2:tabstop=2:smarttab:
00002    Copyright (C) 2009 Sun Microsystems, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; version 2 of the License.
00007 
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012 
00013    You should have received a copy of the GNU General Public License
00014    along with this program; if not, write to the Free Software
00015    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00016 
00017 #include <config.h>
00018 
00019 #include <drizzled/charset.h>
00020 #include <drizzled/error.h>
00021 #include <drizzled/function/str/strfunc.h>
00022 #include <drizzled/internal/my_sys.h>
00023 #include <drizzled/item/func.h>
00024 #include <drizzled/message/schema.h>
00025 #include <drizzled/plugin/function.h>
00026 #include <drizzled/plugin/storage_engine.h>
00027 
00028 #include <iostream>
00029 #include <stdio.h>
00030 #include <string>
00031 
00032 #include <google/protobuf/io/zero_copy_stream.h>
00033 #include <google/protobuf/io/zero_copy_stream_impl.h>
00034 #include <google/protobuf/text_format.h>
00035 
00036 using namespace std;
00037 using namespace drizzled;
00038 using namespace google;
00039 
00040 class ShowSchemaProtoFunction : public Item_str_func
00041 {
00042 public:
00043   String *val_str(String*);
00044 
00045   void fix_length_and_dec()
00046   {
00047     max_length= 16384; /* Guesswork? */
00048     args[0]->collation.set(get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT), DERIVATION_COERCIBLE);
00049   }
00050 
00051   const char *func_name() const
00052   {
00053     return "show_schema_proto";
00054   }
00055 
00056   bool check_argument_count(int n)
00057   {
00058     return (n == 1);
00059   }
00060 };
00061 
00062 
00063 String *ShowSchemaProtoFunction::val_str(String *str)
00064 {
00065   assert(fixed == true);
00066 
00067   String *db_sptr= args[0]->val_str(str);
00068 
00069   if (not db_sptr)
00070   {
00071     null_value= true;
00072     return NULL;
00073   }
00074 
00075   null_value= false;
00076 
00077   identifier::Schema schema_identifier(*db_sptr);
00078   message::schema::shared_ptr proto= plugin::StorageEngine::getSchemaDefinition(schema_identifier);
00079   if (not proto)
00080   {
00081     my_error(ER_BAD_DB_ERROR, schema_identifier);
00082     return NULL;
00083   }
00084 
00085   string proto_as_text;
00086   protobuf::TextFormat::PrintToString(*proto, &proto_as_text);
00087 
00088   str->alloc(proto_as_text.length());
00089   str->length(proto_as_text.length());
00090 
00091   strncpy(str->ptr(),proto_as_text.c_str(), proto_as_text.length());
00092 
00093   return str;
00094 }
00095 
00096 plugin::Create_function<ShowSchemaProtoFunction> *show_schema_proto_func= NULL;
00097 
00098 static int initialize(module::Context &context)
00099 {
00100   show_schema_proto_func= new plugin::Create_function<ShowSchemaProtoFunction>("show_schema_proto");
00101   context.add(show_schema_proto_func);
00102   return 0;
00103 }
00104 
00105 DRIZZLE_DECLARE_PLUGIN
00106 {
00107   DRIZZLE_VERSION_ID,
00108   "show_schema_proto",
00109   "1.0",
00110   "Stewart Smith",
00111   "Shows text representation of schema definition proto",
00112   PLUGIN_LICENSE_GPL,
00113   initialize, /* Plugin Init */
00114   NULL,   /* depends */
00115   NULL    /* config options */
00116 }
00117 DRIZZLE_DECLARE_PLUGIN_END;