~jaypipes/drizzle/proto-definitions

« back to all changes in this revision

Viewing changes to drizzled/serialize/index_definition.cc

  • Committer: Jay Pipes
  • Date: 2008-09-25 18:13:26 UTC
  • mfrom: (352.5.30 codestyle)
  • Revision ID: jay@mysql.com-20080925181326-2z7pr377hcvf28qt
Merged in trunk and added additional definition wrappers for proto buffer messages

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2008 Sun Microsystems
 
2
 
 
3
   Author: Jay Pipes <jay.pipes@sun.com>
 
4
 
 
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.
 
8
 
 
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.
 
13
 
 
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 */
 
17
 
 
18
#include "index_definition.h"
 
19
 
 
20
#include <stdio.h>
 
21
 
 
22
using namespace Drizzled::Serialize;
 
23
using namespace std;
 
24
 
 
25
IndexDefinition::IndexDefinition():
 
26
  initialized(false)
 
27
{
 
28
  /** @TODO What to initialize here, if anything? */
 
29
}
 
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();
 
34
 
 
35
 
 
36
}
 
37
 
 
38
void IndexDefinition::add_index_part_after(const IndexPartDefinition& definition, uint32_t after_index) {
 
39
  uint32_t corrected_index= after_index;
 
40
 
 
41
  if (after_index >= message.index_part_size())
 
42
    corrected_index= (message.index_part_size() - 1);
 
43
 
 
44
  /* We add AFTER the requested index... */
 
45
  corrected_index++;
 
46
 
 
47
  Index::IndexPart* new_index_part= message.mutable_index_part(corrected_index);
 
48
}
 
49
 
 
50
/**
 
51
 * We output a non-newline-ended string containing the SQL definition
 
52
 * of the field.  
 
53
 *
 
54
 * @note
 
55
 *
 
56
 * We do not check syntax here, as we assume field definition
 
57
 * has been already validated.
 
58
 */
 
59
const string& IndexDefinition::get_sql() const {
 
60
  uint32_t x;
 
61
 
 
62
  Index::IndexType index_type= message.type();
 
63
  string buffer= "";
 
64
 
 
65
  if (message.is_primary())
 
66
    buffer.append("PRIMARY ");
 
67
  else if (message.is_unique())
 
68
    buffer.append("UNIQUE ");
 
69
 
 
70
  buffer.append("INDEX `");
 
71
 
 
72
  buffer.append(message.name());
 
73
  buffer.append("` (");
 
74
 
 
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());
 
80
 
 
81
    /* Take care of any comparison length for character field parts */
 
82
    if (current.has_compare_length()) {
 
83
      buffer.append("(");
 
84
      {
 
85
        char* length;
 
86
        (void) sprintf(length, "%d", current.compare_length());
 
87
        buffer.append(length);
 
88
      }
 
89
      buffer.append(")");
 
90
    }
 
91
 
 
92
    if (x != (num_index_parts - 1))
 
93
      buffer.append(", ");
 
94
  }
 
95
  buffer.append(")");
 
96
 
 
97
  const string& result(buffer);
 
98
  return result;
 
99
}
 
100