00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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;
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,
00126 NULL,
00127 NULL
00128 }
00129 DRIZZLE_DECLARE_PLUGIN_END;