~drizzle-trunk/drizzle/development

1812.5.9 by Brian Aker
First pass on moving show create schema out.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2010 Brian Aker
5
 *
6
 *  This program is free software; you can redistribute it and/or modify
7
 *  it under the terms of the GNU General Public License as published by
8
 *  the Free Software Foundation; either version 2 of the License, or
9
 *  (at your option) any later version.
10
 *
11
 *  This program is distributed in the hope that it will be useful,
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU General Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU General Public License
17
 *  along with this program; if not, write to the Free Software
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 */
20
2173.2.1 by Monty Taylor
Fixes incorrect usage of include
21
#include <config.h>
22
#include <plugin/show_dictionary/dictionary.h>
23
#include <drizzled/identifier.h>
24
#include <drizzled/message.h>
25
#include <drizzled/message/statement_transform.h>
1812.5.9 by Brian Aker
First pass on moving show create schema out.
26
#include <string>
27
28
using namespace std;
29
using namespace drizzled;
30
31
ShowCreateSchema::ShowCreateSchema() :
1874.2.4 by Brian Aker
Have show functions be invisible.
32
  show_dictionary::Show("SCHEMA_SQL_DEFINITION")
1812.5.9 by Brian Aker
First pass on moving show create schema out.
33
{
34
  add_field("SCHEMA_NAME", plugin::TableFunction::STRING, MAXIMUM_IDENTIFIER_LENGTH, false);
35
  add_field("SCHEMA_SQL_DEFINITION", plugin::TableFunction::STRING, TABLE_FUNCTION_BLOB_SIZE, false);
36
}
37
38
ShowCreateSchema::Generator::Generator(Field **arg) :
1874.2.4 by Brian Aker
Have show functions be invisible.
39
  show_dictionary::Show::Generator(arg),
1812.5.9 by Brian Aker
First pass on moving show create schema out.
40
  if_not_exists(false)
41
{
2059.1.1 by Andrew Hutchings
Refix show_dictionary plugin crash when not using 'SHOW' to access.
42
  if (not isShowQuery())
43
   return;
44
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
45
  statement::Show& select= static_cast<statement::Show&>(statement());
1812.5.9 by Brian Aker
First pass on moving show create schema out.
46
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
47
  if (not select.getShowSchema().empty())
1812.5.9 by Brian Aker
First pass on moving show create schema out.
48
  {
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
49
    schema_name.append(select.getShowTable());
50
    identifier::Schema identifier(select.getShowSchema());
1812.5.9 by Brian Aker
First pass on moving show create schema out.
51
2179.6.6 by Stewart Smith
add authorization check to SHOW CREATE SCHEMA
52
    if (not plugin::Authorization::isAuthorized(*getSession().user(),
53
                                                identifier, false))
54
    {
55
      drizzled::error::access(*getSession().user(), identifier);
56
      return;
57
    }
58
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
59
    schema_message= plugin::StorageEngine::getSchemaDefinition(identifier);
1812.5.9 by Brian Aker
First pass on moving show create schema out.
60
2227.4.7 by Olaf van der Spek
plugin::TableFunction::Generator::statement()
61
    if_not_exists= select.getShowExists();
1812.5.9 by Brian Aker
First pass on moving show create schema out.
62
  }
63
}
64
65
bool ShowCreateSchema::Generator::populate()
66
{
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
67
  if (not schema_message)
1812.5.9 by Brian Aker
First pass on moving show create schema out.
68
    return false;
69
70
  std::string buffer;
71
72
  /* This needs to be moved out to its own function */
73
  {
74
    buffer.append("CREATE DATABASE ");
75
76
    if (if_not_exists)
77
      buffer.append("IF NOT EXISTS ");
78
79
    buffer.append("`");
1834 by Brian Aker
Merge in shared_ptr usage.
80
    buffer.append(schema_message->name());
1812.5.9 by Brian Aker
First pass on moving show create schema out.
81
    buffer.append("`");
82
1834 by Brian Aker
Merge in shared_ptr usage.
83
    if (schema_message->has_collation())
1812.5.9 by Brian Aker
First pass on moving show create schema out.
84
    {
85
      buffer.append(" COLLATE = ");
1834 by Brian Aker
Merge in shared_ptr usage.
86
      buffer.append(schema_message->collation());
1812.5.9 by Brian Aker
First pass on moving show create schema out.
87
    }
2168.3.1 by Brian Aker
Basic DDL working for replication.
88
2187.7.6 by Brian Aker
This fixes the message such that the table inherits the no replication
89
    if (not message::is_replicated(*schema_message))
2168.3.1 by Brian Aker
Basic DDL working for replication.
90
    {
2187.7.6 by Brian Aker
This fixes the message such that the table inherits the no replication
91
      buffer.append(" REPLICATE = FALSE");
2168.3.1 by Brian Aker
Basic DDL working for replication.
92
    }
1812.5.9 by Brian Aker
First pass on moving show create schema out.
93
  }
94
1834 by Brian Aker
Merge in shared_ptr usage.
95
  push(schema_message->name());
1812.5.9 by Brian Aker
First pass on moving show create schema out.
96
  push(buffer);
2159.2.1 by Brian Aker
Remove the pass by reference, it really wasn't a good choice.
97
98
  schema_message.reset();
1812.5.9 by Brian Aker
First pass on moving show create schema out.
99
100
  return true;
101
}