1
/* Copyright (C) 2008 Sun Microsystems
3
Author: Jay Pipes <jay.pipes@sun.com>
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; version 2 of the License.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public License
15
along with this program; if not, write to the Free Software
16
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
#include "index_definition.h"
22
using namespace Drizzled::Serialize;
25
IndexDefinition::IndexDefinition():
28
/** @TODO What to initialize here, if anything? */
30
void IndexDefinition::add_index_part(const IndexPartDefinition& definition) {
31
Index::IndexPart* new_index_part= message.add_index_part();
32
const FieldDefinition& field_definition= definition.get_field_definition();
33
Field* new_field= new_index_part->mutable_field();
38
void IndexDefinition::add_index_part_after(const IndexPartDefinition& definition, uint32_t after_index) {
39
uint32_t corrected_index= after_index;
41
if (after_index >= message.index_part_size())
42
corrected_index= (message.index_part_size() - 1);
44
/* We add AFTER the requested index... */
47
Index::IndexPart* new_index_part= message.mutable_index_part(corrected_index);
51
* We output a non-newline-ended string containing the SQL definition
56
* We do not check syntax here, as we assume field definition
57
* has been already validated.
59
const string& IndexDefinition::get_sql() const {
62
Index::IndexType index_type= message.type();
65
if (message.is_primary())
66
buffer.append("PRIMARY ");
67
else if (message.is_unique())
68
buffer.append("UNIQUE ");
70
buffer.append("INDEX `");
72
buffer.append(message.name());
75
/* Add the index's field list */
76
uint32_t num_index_parts= message.index_part_size();
77
for (x= 0; x < num_index_parts; ++x) {
78
const Index::IndexPart& current= message.index_part(x);
79
buffer.append(current.field().name());
81
/* Take care of any comparison length for character field parts */
82
if (current.has_compare_length()) {
86
(void) sprintf(length, "%d", current.compare_length());
87
buffer.append(length);
92
if (x != (num_index_parts - 1))
97
const string& result(buffer);