Drizzled Public API Documentation

indexes.cc

00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  *  Copyright (C) 2010 Sun Microsystems, Inc.
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00019  */
00020 
00021 #include <config.h>
00022 #include <plugin/schema_dictionary/dictionary.h>
00023 
00024 using namespace std;
00025 using namespace drizzled;
00026 
00027 IndexesTool::IndexesTool() :
00028   TablesTool("INDEXES")
00029 {
00030   add_field("TABLE_SCHEMA");
00031   add_field("TABLE_NAME");
00032   add_field("INDEX_NAME");
00033   add_field("IS_USED_IN_PRIMARY", plugin::TableFunction::BOOLEAN, 0, false);
00034   add_field("IS_UNIQUE", plugin::TableFunction::BOOLEAN, 0, false);
00035   add_field("IS_NULLABLE", plugin::TableFunction::BOOLEAN, 0, false);
00036   add_field("KEY_LENGTH", plugin::TableFunction::NUMBER, 0, false);
00037   add_field("INDEX_TYPE");
00038   add_field("INDEX_COMMENT", plugin::TableFunction::STRING, 1024, true);
00039 }
00040 
00041 IndexesTool::Generator::Generator(Field **arg) :
00042   TablesTool::Generator(arg),
00043   index_iterator(0),
00044   is_index_primed(false)
00045 {
00046 }
00047 
00048 bool IndexesTool::Generator::nextIndexCore()
00049 {
00050   if (isIndexesPrimed())
00051   {
00052     index_iterator++;
00053   }
00054   else
00055   {
00056     if (not isTablesPrimed())
00057       return false;
00058 
00059     index_iterator= 0;
00060     is_index_primed= true;
00061   }
00062 
00063   if (index_iterator >= getTableProto().indexes_size())
00064     return false;
00065 
00066   index= getTableProto().indexes(index_iterator);
00067 
00068   return true;
00069 }
00070 
00071 bool IndexesTool::Generator::nextIndex()
00072 {
00073   while (not nextIndexCore())
00074   {
00075     if (not nextTable())
00076       return false;
00077     is_index_primed= false;
00078   }
00079 
00080   return true;
00081 }
00082 
00083 bool IndexesTool::Generator::populate()
00084 {
00085   if (not nextIndex())
00086     return false;
00087 
00088   fill();
00089 
00090   return true;
00091 }
00092 
00093 void IndexesTool::Generator::fill()
00094 {
00095   /* TABLE_SCHEMA */
00096   push(getTableProto().schema());
00097 
00098   /* TABLE_NAME */
00099   push(getTableProto().name());
00100 
00101   /* INDEX_NAME */
00102   push(index.name());
00103 
00104   /* IS_USED_IN_PRIMARY */
00105   push(index.is_primary());
00106 
00107   /* IS_UNIQUE */
00108   push(index.is_unique());
00109 
00110   /* IS_NULLABLE */
00111   push(index.options().null_part_key());
00112 
00113   /* KEY_LENGTH */
00114   push(static_cast<uint64_t>(index.key_length()));
00115 
00116   /* INDEX_TYPE */
00117   {
00118     const char *str;
00119     uint32_t length;
00120 
00121     switch (index.type())
00122     {
00123     default:
00124     case message::Table::Index::UNKNOWN_INDEX:
00125       str= "UNKNOWN";
00126       length= sizeof("UNKNOWN");
00127       break;
00128     case message::Table::Index::BTREE:
00129       str= "BTREE";
00130       length= sizeof("BTREE");
00131       break;
00132     case message::Table::Index::RTREE:
00133       str= "RTREE";
00134       length= sizeof("RTREE");
00135       break;
00136     case message::Table::Index::HASH:
00137       str= "HASH";
00138       length= sizeof("HASH");
00139       break;
00140     case message::Table::Index::FULLTEXT:
00141       str= "FULLTEXT";
00142       length= sizeof("FULLTEXT");
00143       break;
00144     }
00145     /* Subtract 1 here, because sizeof gives us the wrong amount */
00146     push(str, length - 1);
00147   }
00148 
00149  /* "INDEX_COMMENT" */
00150   if (index.has_comment())
00151   {
00152     push(index.comment());
00153   }
00154   else
00155   {
00156     push();
00157   }
00158 }