1 /*
2   Copyright (c) 2015, 2021, Oracle and/or its affiliates.
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 "table.h"
26 #include "pattern_matcher.h"
27 #include <boost/algorithm/string.hpp>
28 
29 using namespace Mysql::Tools::Dump;
30 
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)31 Table::Table(uint64 id, const std::string& name, const std::string& schema,
32   const std::string& sql_formatted_definition, std::vector<Field>& fields,
33   std::string type, uint64 row_count, uint64 row_bound, uint64 data_lenght)
34   : Abstract_plain_sql_object(id, name, schema, sql_formatted_definition),
35   m_fields(fields),
36   m_type(type),
37   m_row_count(row_count),
38   m_row_bound(row_bound),
39   m_data_lenght(data_lenght)
40 {
41   using Detail::Pattern_matcher;
42   bool engine_line_read= false;
43   std::vector<std::string> definition_lines;
44   boost::split(definition_lines, sql_formatted_definition,
45     boost::is_any_of("\n"), boost::token_compress_on);
46   for (std::vector<std::string>::iterator it= definition_lines.begin();
47     it != definition_lines.end(); ++it)
48   {
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(*it);
55     if (!engine_line_read)
56       boost::trim_if(*it, boost::is_any_of(","));
57     if (boost::starts_with(*it, "KEY ")
58       || boost::starts_with(*it, "INDEX ")
59       || boost::starts_with(*it, "UNIQUE KEY ")
60       || boost::starts_with(*it, "UNIQUE INDEX ")
61       || boost::starts_with(*it, "FULLTEXT KEY ")
62       || boost::starts_with(*it, "FULLTEXT INDEX ")
63       || boost::starts_with(*it, "SPATIAL KEY ")
64       || boost::starts_with(*it, "SPATIAL INDEX ")
65       || boost::starts_with(*it, "CONSTRAINT "))
66     {
67       m_indexes_sql_definition.push_back(*it);
68     }
69     else
70     {
71       /*
72         Make sure we detect the table options clauses,
73         even with different syntaxes (with or without TABLESPACE)
74       */
75       if (boost::starts_with(*it, ")") &&
76           boost::contains(*it, "ENGINE="))
77       {
78         engine_line_read= true;
79         std::string &sql_def = m_sql_definition_without_indexes;
80         sql_def = boost::algorithm::replace_last_copy(sql_def, ",", "");
81       }
82       else if (it != definition_lines.begin() && !engine_line_read)
83         *it+= ",";
84       m_sql_definition_without_indexes+= *it + '\n';
85     }
86   }
87 }
88 
get_sql_definition_without_indexes() const89 const std::string& Table::get_sql_definition_without_indexes() const
90 {
91   return m_sql_definition_without_indexes;
92 }
93 
get_indexes_sql_definition() const94 const std::vector<std::string>& Table::get_indexes_sql_definition() const
95 {
96   return m_indexes_sql_definition;
97 }
98 
get_fields() const99 const std::vector<Field>& Table::get_fields() const
100 {
101   return m_fields;
102 }
103 
get_row_data_lenght() const104 uint64 Table::get_row_data_lenght() const
105 {
106   return m_data_lenght;
107 }
108 
get_row_count_bound() const109 uint64 Table::get_row_count_bound() const
110 {
111   return m_row_bound;
112 }
113 
get_row_count() const114 uint64 Table::get_row_count() const
115 {
116   return m_row_count;
117 }
118 
get_type() const119 std::string Table::get_type() const
120 {
121   return m_type;
122 }
123