1 /*
2   Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
6   as published by the Free Software Foundation.
7 
8   This program is also distributed with certain software (including
9   but not limited to OpenSSL) that is licensed under separate terms,
10   as designated in a particular file or component or in included license
11   documentation.  The authors of MySQL hereby grant you an additional
12   permission to link the program and your derivative works with the
13   separately licensed software that they have included with MySQL.
14 
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License, version 2.0, for more details.
19 
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #include "client/dump/table.h"
26 
27 #include <boost/algorithm/string.hpp>
28 #include <sstream>
29 
30 #include "client/dump/pattern_matcher.h"
31 
32 using namespace Mysql::Tools::Dump;
33 
Table(uint64 id,const std::string & name,const std::string & schema,const std::string & sql_formatted_definition,std::vector<Field> & fields,std::string type,uint64 row_count,uint64 row_bound,uint64 data_lenght)34 Table::Table(uint64 id, const std::string &name, const std::string &schema,
35              const std::string &sql_formatted_definition,
36              std::vector<Field> &fields, std::string type, uint64 row_count,
37              uint64 row_bound, uint64 data_lenght)
38     : Abstract_plain_sql_object(id, name, schema, sql_formatted_definition),
39       m_fields(fields),
40       m_type(type),
41       m_row_count(row_count),
42       m_row_bound(row_bound),
43       m_data_lenght(data_lenght) {
44   using Detail::Pattern_matcher;
45   bool engine_line_read = false;
46   bool first_line = true;
47   std::stringstream definition_stream(sql_formatted_definition);
48   for (std::string line; std::getline(definition_stream, line);) {
49     /*
50       MAINTAINER: This code parses the output of SHOW CREATE TABLE.
51       @TODO: Instead, look up INFORMATION_SCHEMA and get the table details.
52     */
53 
54     boost::trim_left(line);
55     if (!engine_line_read) boost::trim_if(line, boost::is_any_of(","));
56     if (boost::starts_with(line, "KEY ") ||
57         boost::starts_with(line, "INDEX ") ||
58         boost::starts_with(line, "UNIQUE KEY ") ||
59         boost::starts_with(line, "UNIQUE INDEX ") ||
60         boost::starts_with(line, "FULLTEXT KEY ") ||
61         boost::starts_with(line, "FULLTEXT INDEX ") ||
62         boost::starts_with(line, "SPATIAL KEY ") ||
63         boost::starts_with(line, "SPATIAL INDEX ") ||
64         boost::starts_with(line, "CONSTRAINT ")) {
65       m_indexes_sql_definition.push_back(line);
66     } else {
67       /*
68         Make sure we detect the table options clauses,
69         even with different syntaxes (with or without TABLESPACE)
70       */
71       if (boost::starts_with(line, ")") && boost::contains(line, "ENGINE=")) {
72         engine_line_read = true;
73         std::string &sql_def = m_sql_definition_without_indexes;
74         sql_def = boost::algorithm::replace_last_copy(sql_def, ",", "");
75       } else if (!first_line && !engine_line_read)
76         line += ",";
77       m_sql_definition_without_indexes += line + '\n';
78     }
79     first_line = false;
80   }
81 }
82 
get_sql_definition_without_indexes() const83 const std::string &Table::get_sql_definition_without_indexes() const {
84   return m_sql_definition_without_indexes;
85 }
86 
get_indexes_sql_definition() const87 const std::vector<std::string> &Table::get_indexes_sql_definition() const {
88   return m_indexes_sql_definition;
89 }
90 
get_fields() const91 const std::vector<Field> &Table::get_fields() const { return m_fields; }
92 
get_row_data_lenght() const93 uint64 Table::get_row_data_lenght() const { return m_data_lenght; }
94 
get_row_count_bound() const95 uint64 Table::get_row_count_bound() const { return m_row_bound; }
96 
get_row_count() const97 uint64 Table::get_row_count() const { return m_row_count; }
98 
get_type() const99 std::string Table::get_type() const { return m_type; }
100