1 /* c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil
2  * vi: set shiftwidth=4 tabstop=4 expandtab:
3  *  :indentSize=4:tabSize=4:noTabs=true:
4  *
5  * Copyright (C) 2016 MariaDB Corporation
6  *
7  * This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; version 2 of
10  * the License.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  */
22 
23 #include "idb_mysql.h"
24 #include <vector>
25 
26 #include <boost/shared_ptr.hpp>
27 #include "calpontsystemcatalog.h"
28 #include "dataconvert.h"
29 #include "is_columnstore.h"
30 
31 // Required declaration as it isn't in a MairaDB include
32 bool schema_table_store_record(THD* thd, TABLE* table);
33 
34 ST_FIELD_INFO is_columnstore_tables_fields[] =
35 {
36     Show::Column("TABLE_SCHEMA", Show::Varchar(64), NOT_NULL),
37     Show::Column("TABLE_NAME", Show::Varchar(64), NOT_NULL),
38     Show::Column("OBJECT_ID", Show::SLong(0), NOT_NULL),
39     Show::Column("CREATION_DATE", Show::Datetime(0), NOT_NULL), // TODO: Make a date if possible
40     Show::Column("COLUMN_COUNT", Show::SLong(0), NOT_NULL),
41     Show::Column("AUTOINCREMENT", Show::SLong(0), NULLABLE),
42     Show::CEnd()
43 };
44 
get_cond_item(Item_func * item,String ** table,String ** db)45 static void get_cond_item(Item_func* item, String** table, String** db)
46 {
47     char tmp_char[MAX_FIELD_WIDTH];
48     Item_field* item_field = (Item_field*) item->arguments()[0]->real_item();
49 
50     if (strcasecmp(item_field->field_name.str, "table_name") == 0)
51     {
52         String str_buf(tmp_char, sizeof(tmp_char), system_charset_info);
53         *table = item->arguments()[1]->val_str(&str_buf);
54         return;
55     }
56     else if (strcasecmp(item_field->field_name.str, "table_schema") == 0)
57     {
58         String str_buf(tmp_char, sizeof(tmp_char), system_charset_info);
59         *db = item->arguments()[1]->val_str(&str_buf);
60         return;
61     }
62 }
63 
get_cond_items(COND * cond,String ** table,String ** db)64 static void get_cond_items(COND* cond, String** table, String** db)
65 {
66     if (cond->type() == Item::FUNC_ITEM)
67     {
68         Item_func* fitem = (Item_func*) cond;
69 
70         if (fitem->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
71                 fitem->arguments()[1]->const_item())
72         {
73             get_cond_item(fitem, table, db);
74         }
75     }
76     else if ((cond->type() == Item::COND_ITEM) && (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC))
77     {
78         List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
79         Item* item;
80 
81         while ((item = li++))
82         {
83             if (item->type() == Item::FUNC_ITEM)
84             {
85                 get_cond_item((Item_func*)item, table, db);
86             }
87             else
88             {
89                 get_cond_items(item, table, db);
90             }
91         }
92     }
93 }
94 
is_columnstore_tables_fill(THD * thd,TABLE_LIST * tables,COND * cond)95 static int is_columnstore_tables_fill(THD* thd, TABLE_LIST* tables, COND* cond)
96 {
97     CHARSET_INFO* cs = system_charset_info;
98     TABLE* table = tables->table;
99     String* table_name = NULL;
100     String* db_name = NULL;
101 
102     boost::shared_ptr<execplan::CalpontSystemCatalog> systemCatalogPtr =
103         execplan::CalpontSystemCatalog::makeCalpontSystemCatalog(execplan::CalpontSystemCatalog::idb_tid2sid(thd->thread_id));
104 
105     systemCatalogPtr->identity(execplan::CalpontSystemCatalog::FE);
106 
107     if (cond)
108     {
109         get_cond_items(cond, &table_name, &db_name);
110     }
111 
112     const std::vector< std::pair<execplan::CalpontSystemCatalog::OID, execplan::CalpontSystemCatalog::TableName> > catalog_tables
113         = systemCatalogPtr->getTables();
114 
115     for (std::vector<std::pair<execplan::CalpontSystemCatalog::OID, execplan::CalpontSystemCatalog::TableName> >::const_iterator it = catalog_tables.begin();
116             it != catalog_tables.end(); ++it)
117     {
118         if (db_name)
119         {
120             if ((*it).second.schema.compare(db_name->ptr()) != 0)
121             {
122                 continue;
123             }
124         }
125 
126         if (table_name)
127         {
128             if ((*it).second.table.compare(table_name->ptr()) != 0)
129             {
130                 continue;
131             }
132         }
133 
134         try
135         {
136             execplan::CalpontSystemCatalog::TableInfo tb_info = systemCatalogPtr->tableInfo((*it).second);
137             std::string create_date = dataconvert::DataConvert::dateToString((*it).second.create_date);
138             table->field[0]->store((*it).second.schema.c_str(), (*it).second.schema.length(), cs);
139             table->field[1]->store((*it).second.table.c_str(), (*it).second.table.length(), cs);
140             table->field[2]->store((*it).first);
141             table->field[3]->store(create_date.c_str(), create_date.length(), cs);
142             table->field[4]->store(tb_info.numOfCols);
143 
144             if (tb_info.tablewithautoincr)
145             {
146                 table->field[5]->set_notnull();
147                 table->field[5]->store(systemCatalogPtr->nextAutoIncrValue((*it).second));
148             }
149             else
150             {
151                 table->field[5]->set_null();
152             }
153 
154             table->field[5]->store(tb_info.tablewithautoincr);
155 
156             if (schema_table_store_record(thd, table))
157                 return 1;
158         }
159         catch (std::runtime_error& e)
160         {
161             std::cerr << e.what() << std::endl;
162         }
163     }
164 
165     return 0;
166 }
167 
is_columnstore_tables_plugin_init(void * p)168 int is_columnstore_tables_plugin_init(void* p)
169 {
170     ST_SCHEMA_TABLE* schema = (ST_SCHEMA_TABLE*) p;
171     schema->fields_info = is_columnstore_tables_fields;
172     schema->fill_table = is_columnstore_tables_fill;
173     return 0;
174 }
175 
176