Drizzled Public API Documentation

show_schema_proto.cc
1 /* vim: expandtab:shiftwidth=2:tabstop=2:smarttab:
2  Copyright (C) 2009 Sun Microsystems, Inc.
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; version 2 of the License.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program; if not, write to the Free Software
15  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16 
17 #include <config.h>
18 
19 #include <drizzled/charset.h>
20 #include <drizzled/error.h>
21 #include <drizzled/function/str/strfunc.h>
22 #include <drizzled/internal/my_sys.h>
23 #include <drizzled/item/func.h>
24 #include <drizzled/message/schema.h>
25 #include <drizzled/plugin/function.h>
26 #include <drizzled/plugin/storage_engine.h>
27 
28 #include <iostream>
29 #include <stdio.h>
30 #include <string>
31 
32 #include <google/protobuf/io/zero_copy_stream.h>
33 #include <google/protobuf/io/zero_copy_stream_impl.h>
34 #include <google/protobuf/text_format.h>
35 
36 using namespace std;
37 using namespace drizzled;
38 using namespace google;
39 
41 {
42 public:
43  String *val_str(String*);
44 
45  void fix_length_and_dec()
46  {
47  max_length= 16384; /* Guesswork? */
48  args[0]->collation.set(get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT), DERIVATION_COERCIBLE);
49  }
50 
51  const char *func_name() const
52  {
53  return "show_schema_proto";
54  }
55 
56  bool check_argument_count(int n)
57  {
58  return (n == 1);
59  }
60 };
61 
62 
64 {
65  assert(fixed == true);
66 
67  String *db_sptr= args[0]->val_str(str);
68 
69  if (not db_sptr)
70  {
71  null_value= true;
72  return NULL;
73  }
74 
75  null_value= false;
76 
77  identifier::Schema schema_identifier(*db_sptr);
78  message::schema::shared_ptr proto= plugin::StorageEngine::getSchemaDefinition(schema_identifier);
79  if (not proto)
80  {
81  my_error(ER_BAD_DB_ERROR, schema_identifier);
82  return NULL;
83  }
84 
85  string proto_as_text;
86  protobuf::TextFormat::PrintToString(*proto, &proto_as_text);
87 
88  str->alloc(proto_as_text.length());
89  str->length(proto_as_text.length());
90 
91  strncpy(str->ptr(),proto_as_text.c_str(), proto_as_text.length());
92 
93  return str;
94 }
95 
96 plugin::Create_function<ShowSchemaProtoFunction> *show_schema_proto_func= NULL;
97 
98 static int initialize(module::Context &context)
99 {
100  show_schema_proto_func= new plugin::Create_function<ShowSchemaProtoFunction>("show_schema_proto");
101  context.add(show_schema_proto_func);
102  return 0;
103 }
104 
105 DRIZZLE_DECLARE_PLUGIN
106 {
107  DRIZZLE_VERSION_ID,
108  "show_schema_proto",
109  "1.0",
110  "Stewart Smith",
111  N_("Shows text representation of schema definition proto"),
112  PLUGIN_LICENSE_GPL,
113  initialize,
114  NULL,
115  NULL
116 }
117 DRIZZLE_DECLARE_PLUGIN_END;