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 <drizzled/identifier.h>
29 #include <drizzled/session.h>
30 #include <drizzled/catalog/instance.h>
31 
32 #include <iostream>
33 #include <stdio.h>
34 #include <string>
35 
36 #include <google/protobuf/io/zero_copy_stream.h>
37 #include <google/protobuf/io/zero_copy_stream_impl.h>
38 #include <google/protobuf/text_format.h>
39 
40 using namespace std;
41 using namespace drizzled;
42 using namespace google;
43 
45 {
46 public:
47  String *val_str(String*);
48 
49  void fix_length_and_dec()
50  {
51  max_length= 16384; /* Guesswork? */
52  args[0]->collation.set(get_charset_by_csname(args[0]->collation.collation->csname, MY_CS_BINSORT), DERIVATION_COERCIBLE);
53  }
54 
55  const char *func_name() const
56  {
57  return "show_schema_proto";
58  }
59 
61  {
62  return (n == 1);
63  }
64 };
65 
66 
68 {
69  assert(fixed == true);
70 
71  String *db_sptr= args[0]->val_str(str);
72 
73  if (not db_sptr)
74  {
75  null_value= true;
76  return NULL;
77  }
78 
79  null_value= false;
80 
81  identifier::Schema schema_identifier(getSession().catalog().identifier(),
82  *db_sptr);
83  message::schema::shared_ptr proto= plugin::StorageEngine::getSchemaDefinition(schema_identifier);
84  if (not proto)
85  {
86  my_error(ER_BAD_DB_ERROR, schema_identifier);
87  return NULL;
88  }
89 
90  string proto_as_text;
91  protobuf::TextFormat::PrintToString(*proto, &proto_as_text);
92 
93  str->alloc(proto_as_text.length());
94  str->length(proto_as_text.length());
95 
96  strncpy(str->ptr(),proto_as_text.c_str(), proto_as_text.length());
97 
98  return str;
99 }
100 
101 plugin::Create_function<ShowSchemaProtoFunction> *show_schema_proto_func= NULL;
102 
103 static int initialize(module::Context &context)
104 {
105  show_schema_proto_func= new plugin::Create_function<ShowSchemaProtoFunction>("show_schema_proto");
106  context.add(show_schema_proto_func);
107  return 0;
108 }
109 
110 DRIZZLE_DECLARE_PLUGIN
111 {
112  DRIZZLE_VERSION_ID,
113  "show_schema_proto",
114  "1.0",
115  "Stewart Smith",
116  N_("Shows text representation of schema definition proto"),
117  PLUGIN_LICENSE_GPL,
118  initialize,
119  NULL,
120  NULL
121 }
122 DRIZZLE_DECLARE_PLUGIN_END;
String * val_str(String *)