~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/identifier/table.h

  • Committer: Andrew Hutchings
  • Date: 2011-01-21 11:23:19 UTC
  • mto: (2100.1.1 build)
  • mto: This revision was merged to the branch mainline in revision 2101.
  • Revision ID: andrew@linuxjedi.co.uk-20110121112319-nj1cvg0yt3nnf2rr
Add errors page to drizzle client docs
Add link to specific error in migration docs
Minor changes to migration docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2009 Sun Microsystems
 
4
 *  Copyright (C) 2010 Brian Aker
 
5
 *  Copyright (C) 2009 Sun Microsystems, Inc.
5
6
 *
6
7
 *  This program is free software; you can redistribute it and/or modify
7
8
 *  it under the terms of the GNU General Public License as published by
29
30
  This will replace Table_ident.
30
31
  */
31
32
 
32
 
#ifndef DRIZZLED_TABLE_IDENTIFIER_H
33
 
#define DRIZZLED_TABLE_IDENTIFIER_H
 
33
#ifndef DRIZZLED_IDENTIFIER_TABLE_H
 
34
#define DRIZZLED_IDENTIFIER_TABLE_H
34
35
 
35
36
#include <drizzled/enum.h>
36
37
#include "drizzled/definitions.h"
37
38
#include "drizzled/message/table.pb.h"
38
39
 
39
 
#include "drizzled/schema_identifier.h"
 
40
#include "drizzled/identifier.h"
40
41
 
41
42
#include <string.h>
42
43
 
47
48
#include <algorithm>
48
49
#include <functional>
49
50
 
 
51
#include <boost/functional/hash.hpp>
 
52
 
50
53
namespace drizzled {
51
 
 
52
54
class Table;
53
55
 
54
 
uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
55
 
size_t build_table_filename(std::string &buff, const char *db, const char *table_name, bool is_tmp);
 
56
namespace identifier {
56
57
 
57
 
class TableIdentifier : public SchemaIdentifier
 
58
class Table : public identifier::Schema
58
59
{
59
60
public:
60
61
  typedef message::Table::TableType Type;
 
62
  typedef std::vector <Table> vector;
 
63
  typedef const Table& const_reference;
 
64
  typedef Table& reference;
 
65
 
 
66
  class Key
 
67
  {
 
68
    std::vector<char> key_buffer;
 
69
    size_t hash_value;
 
70
 
 
71
  public:
 
72
 
 
73
    Key() :
 
74
      hash_value(0)
 
75
    {
 
76
    }
 
77
 
 
78
    const char *vector() const
 
79
    {
 
80
      return &key_buffer[0];
 
81
    }
 
82
 
 
83
    std::vector<char> &vectorPtr()
 
84
    {
 
85
      return key_buffer;
 
86
    }
 
87
 
 
88
    void set(size_t resize_arg, const std::string &a, const std::string &b);
 
89
 
 
90
    friend bool operator==(const Key &left, const Key &right)
 
91
    {
 
92
      if (left.hash_value == right.hash_value and left.key_buffer.size() == right.key_buffer.size())
 
93
      {
 
94
        if (memcmp(&left.key_buffer[0], &right.key_buffer[0], left.key_buffer.size()) == 0)
 
95
          return true;
 
96
      }
 
97
 
 
98
      return false;
 
99
    }
 
100
 
 
101
    friend bool operator<(const Key &left, const Key &right)
 
102
    {
 
103
      return left.key_buffer < right.key_buffer;
 
104
    }
 
105
 
 
106
    size_t size() const
 
107
    {
 
108
      return key_buffer.size();
 
109
    }
 
110
 
 
111
    size_t getHashValue() const
 
112
    {
 
113
      return hash_value;
 
114
    }
 
115
  };
 
116
 
61
117
private:
62
118
 
63
119
  Type type;
64
120
  std::string path;
65
121
  std::string table_name;
66
 
  std::string lower_table_name;
67
 
  std::string sql_path;
 
122
  Key key;
 
123
  size_t hash_value;
68
124
 
69
125
  void init();
70
126
 
 
127
  size_t getKeySize() const
 
128
  {
 
129
    return getSchemaName().size() + getTableName().size() + 2;
 
130
  }
 
131
 
71
132
public:
72
 
  TableIdentifier(const Table &table);
 
133
 
 
134
  Table(const drizzled::Table &table);
73
135
                   
74
 
  TableIdentifier( const SchemaIdentifier &schema,
75
 
                   const std::string &table_name_arg,
76
 
                   Type tmp_arg= message::Table::STANDARD) :
77
 
    SchemaIdentifier(schema),
78
 
    type(tmp_arg),
79
 
    table_name(table_name_arg)
80
 
  { 
81
 
    init();
82
 
  }
83
 
 
84
 
  TableIdentifier( const std::string &db_arg,
85
 
                   const std::string &table_name_arg,
86
 
                   Type tmp_arg= message::Table::STANDARD) :
87
 
    SchemaIdentifier(db_arg),
88
 
    type(tmp_arg),
89
 
    table_name(table_name_arg)
90
 
  { 
91
 
    init();
92
 
  }
93
 
 
94
 
  TableIdentifier( const std::string &schema_name_arg,
 
136
  Table(const identifier::Schema &schema,
 
137
        const std::string &table_name_arg,
 
138
        Type tmp_arg= message::Table::STANDARD) :
 
139
    Schema(schema),
 
140
    type(tmp_arg),
 
141
    table_name(table_name_arg)
 
142
  { 
 
143
    init();
 
144
  }
 
145
 
 
146
  Table( const std::string &db_arg,
 
147
                   const std::string &table_name_arg,
 
148
                   Type tmp_arg= message::Table::STANDARD) :
 
149
    Schema(db_arg),
 
150
    type(tmp_arg),
 
151
    table_name(table_name_arg)
 
152
  { 
 
153
    init();
 
154
  }
 
155
 
 
156
  Table( const std::string &schema_name_arg,
95
157
                   const std::string &table_name_arg,
96
158
                   const std::string &path_arg ) :
97
 
    SchemaIdentifier(schema_name_arg),
 
159
    Schema(schema_name_arg),
98
160
    type(message::Table::TEMPORARY),
99
161
    path(path_arg),
100
162
    table_name(table_name_arg)
102
164
    init();
103
165
  }
104
166
 
105
 
  using SchemaIdentifier::compare;
106
 
  bool compare(std::string schema_arg, std::string table_arg);
 
167
  using Schema::compare;
107
168
 
108
169
  bool isTmp() const
109
170
  {
112
173
    return false;
113
174
  }
114
175
 
 
176
  static bool isView(message::Table::TableType arg) // Not a SQL view, but a view for I_S
 
177
  {
 
178
    switch (arg)
 
179
    {
 
180
    default:
 
181
    case message::Table::STANDARD:
 
182
    case message::Table::TEMPORARY:
 
183
    case message::Table::INTERNAL:
 
184
      break;
 
185
    case message::Table::FUNCTION:
 
186
      return true;
 
187
    }
 
188
 
 
189
    return false;
 
190
  }
 
191
 
 
192
  bool isView() const // Not a SQL view, but a view for I_S
 
193
  {
 
194
    return isView(type);
 
195
  }
 
196
 
115
197
  Type getType() const
116
198
  {
117
199
    return type;
118
200
  }
119
201
 
120
 
  const std::string &getSQLPath();
 
202
  virtual void getSQLPath(std::string &sql_path) const;
121
203
 
122
 
  const std::string &getPath();
 
204
  virtual const std::string &getPath() const;
123
205
 
124
206
  void setPath(const std::string &new_path)
125
207
  {
131
213
    return table_name;
132
214
  }
133
215
 
134
 
  void copyToTableMessage(message::Table &message);
 
216
  void copyToTableMessage(message::Table &message) const;
135
217
 
136
 
  bool operator<(TableIdentifier &right)
 
218
  friend bool operator<(Table::const_reference left, Table::const_reference right)
137
219
  {
138
 
    int first= getLower().compare(right.getLower());
139
 
 
140
 
    if (first < 0)
 
220
    if (left.getKey() < right.getKey())
141
221
    {
142
222
      return true;
143
223
    }
144
 
    else if (first > 0)
145
 
    {
146
 
      return false;
147
 
    }
148
 
    else
149
 
    {
150
 
      int val= lower_table_name.compare(right.lower_table_name);
151
 
 
152
 
      if (val < 0)
153
 
      {
154
 
        return true;
155
 
      }
156
 
      else if (val > 0)
157
 
      {
158
 
        return false;
159
 
      }
160
 
      else
161
 
      {
162
 
        if (type < right.type)
163
 
        {
164
 
          return true;
165
 
        }
166
 
      }
167
 
    }
168
224
 
169
225
    return false;
170
226
  }
171
227
 
172
 
  friend std::ostream& operator<<(std::ostream& output, const TableIdentifier &identifier)
 
228
  friend std::ostream& operator<<(std::ostream& output, Table::const_reference identifier)
173
229
  {
174
230
    const char *type_str;
175
231
 
176
 
    output << "TableIdentifier:(";
 
232
    output << "Table:(";
177
233
    output <<  identifier.getSchemaName();
178
234
    output << ", ";
179
235
    output << identifier.getTableName();
197
253
    output << type_str;
198
254
    output << ", ";
199
255
    output << identifier.path;
 
256
    output << ", ";
 
257
    output << identifier.getHashValue();
200
258
    output << ")";
201
259
 
202
260
    return output;  // for multiple << operators.
203
261
  }
204
262
 
205
 
  friend bool operator==(TableIdentifier &left, TableIdentifier &right)
 
263
  friend bool operator==(Table::const_reference left, Table::const_reference right)
206
264
  {
207
 
    if (left.type == right.type)
 
265
    if (left.getHashValue() == right.getHashValue())
208
266
    {
209
 
      if (static_cast<SchemaIdentifier &>(left) == static_cast<SchemaIdentifier &>(right))
210
 
      {
211
 
        if (left.lower_table_name == right.lower_table_name)
212
 
        {
213
 
          return true;
214
 
        }
215
 
      }
 
267
      if (left.getKey() == right.getKey())
 
268
        return true;
216
269
    }
217
270
 
218
271
    return false;
219
272
  }
220
273
 
 
274
  static uint32_t filename_to_tablename(const char *from, char *to, uint32_t to_length);
 
275
  static size_t build_table_filename(std::string &path, const std::string &db, const std::string &table_name, bool is_tmp);
 
276
  static size_t build_tmptable_filename(std::string &buffer);
 
277
  static size_t build_tmptable_filename(std::vector<char> &buffer);
 
278
 
 
279
public:
 
280
  bool isValid() const;
 
281
 
 
282
  size_t getHashValue() const
 
283
  {
 
284
    return hash_value;
 
285
  }
 
286
 
 
287
  const Key &getKey() const
 
288
  {
 
289
    return key;
 
290
  }
221
291
};
222
292
 
223
 
typedef std::vector <TableIdentifier> TableIdentifierList;
224
 
typedef std::list <TableIdentifier> TableIdentifiers;
 
293
std::size_t hash_value(Table const& b);
 
294
std::size_t hash_value(Table::Key const& b);
225
295
 
 
296
} /* namespace identifier */
226
297
} /* namespace drizzled */
227
298
 
228
 
#endif /* DRIZZLED_TABLE_IDENTIFIER_H */
 
299
#endif /* DRIZZLED_IDENTIFIER_TABLE_H */