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   ShowSchemaProtoFunction() : Item_str_func() {}
00044   String *val_str(String*);
00045 
00046   void fix_length_and_dec()
00047   {
00048     max_length= 16384; /* Guesswork? */
00049     args[0]->collation.set(
00050       get_charset_by_csname(args[0]->collation.collation->csname,
00051                             MY_CS_BINSORT), DERIVATION_COERCIBLE);
00052   }
00053 
00054   const char *func_name() const
00055   {
00056     return "show_schema_proto";
00057   }
00058 
00059   bool check_argument_count(int n)
00060   {
00061     return (n == 1);
00062   }
00063 };
00064 
00065 
00066 String *ShowSchemaProtoFunction::val_str(String *str)
00067 {
00068   assert(fixed == true);
00069 
00070   String *db_sptr= args[0]->val_str(str);
00071 
00072   if (db_sptr == NULL)
00073   {
00074     null_value= true;
00075     return NULL;
00076   }
00077 
00078   null_value= false;
00079 
00080   const char* db= db_sptr->c_ptr_safe();
00081 
00082   string proto_as_text("");
00083   message::schema::shared_ptr proto;
00084 
00085 
00086   identifier::Schema schema_identifier(db);
00087   if (not (proto= plugin::StorageEngine::getSchemaDefinition(schema_identifier)))
00088   {
00089     my_error(ER_BAD_DB_ERROR, schema_identifier);
00090     return NULL;
00091   }
00092 
00093   protobuf::TextFormat::PrintToString(*proto, &proto_as_text);
00094 
00095   if (str->alloc(proto_as_text.length()))
00096   {
00097     null_value= true;
00098     return NULL;
00099   }
00100 
00101   str->length(proto_as_text.length());
00102 
00103   strncpy(str->ptr(),proto_as_text.c_str(), proto_as_text.length());
00104 
00105   return str;
00106 }
00107 
00108 plugin::Create_function<ShowSchemaProtoFunction> *show_schema_proto_func= NULL;
00109 
00110 static int initialize(module::Context &context)
00111 {
00112   show_schema_proto_func= new plugin::Create_function<ShowSchemaProtoFunction>("show_schema_proto");
00113   context.add(show_schema_proto_func);
00114   return 0;
00115 }
00116 
00117 DRIZZLE_DECLARE_PLUGIN
00118 {
00119   DRIZZLE_VERSION_ID,
00120   "show_schema_proto",
00121   "1.0",
00122   "Stewart Smith",
00123   "Shows text representation of schema definition proto",
00124   PLUGIN_LICENSE_GPL,
00125   initialize, /* Plugin Init */
00126   NULL,   /* depends */
00127   NULL    /* config options */
00128 }
00129 DRIZZLE_DECLARE_PLUGIN_END;