~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/field/uuid.cc

  • Committer: Brian Aker
  • Date: 2010-12-18 02:06:13 UTC
  • Revision ID: brian@tangent.org-20101218020613-8lxhcvsy812bu960
Formatting + remove default from switch/case.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2010 Brian Aker
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
 
 
22
#include "config.h"
 
23
 
 
24
#include <algorithm>
 
25
 
 
26
#include <uuid/uuid.h>
 
27
 
 
28
#include "drizzled/field/uuid.h"
 
29
 
 
30
#include "drizzled/error.h"
 
31
#include "drizzled/internal/my_sys.h"
 
32
#include "drizzled/session.h"
 
33
#include "drizzled/table.h"
 
34
#include "drizzled/temporal.h"
 
35
 
 
36
namespace drizzled
 
37
{
 
38
namespace field
 
39
{
 
40
 
 
41
Uuid::Uuid(unsigned char *ptr_arg,
 
42
           uint32_t len_arg,
 
43
           unsigned char *null_ptr_arg,
 
44
           unsigned char null_bit_arg,
 
45
           const char *field_name_arg) :
 
46
  Field(ptr_arg, len_arg,
 
47
        null_ptr_arg,
 
48
        null_bit_arg,
 
49
        Field::NONE,
 
50
        field_name_arg),
 
51
  is_set(false)
 
52
{
 
53
}
 
54
 
 
55
int Uuid::cmp(const unsigned char *a, const unsigned char *b)
 
56
 
57
  return memcmp(a, b, sizeof(uuid_t));
 
58
}
 
59
 
 
60
int Uuid::store(const char *from, uint32_t length, const CHARSET_INFO * const )
 
61
{
 
62
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
63
  uuid_st uu;
 
64
 
 
65
  if (is_set)
 
66
  {
 
67
    is_set= false;
 
68
    return 0;
 
69
  }
 
70
 
 
71
  if (length != uuid_st::DISPLAY_LENGTH)
 
72
  {
 
73
    my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
 
74
    return 1;
 
75
  }
 
76
 
 
77
  if (uu.parse(from))
 
78
  {
 
79
    my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
 
80
    return 1;
 
81
  }
 
82
 
 
83
  uu.pack(ptr);
 
84
 
 
85
  return 0;
 
86
}
 
87
 
 
88
int Uuid::store(int64_t , bool )
 
89
{
 
90
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
91
  my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
 
92
  return 1;
 
93
}
 
94
 
 
95
int Uuid::store_decimal(const drizzled::my_decimal*)
 
96
{
 
97
  ASSERT_COLUMN_MARKED_FOR_WRITE;
 
98
  my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
 
99
  return 1;
 
100
}
 
101
 
 
102
void Uuid::sql_type(String &res) const
 
103
{
 
104
  res.set_ascii(STRING_WITH_LEN("uuid"));
 
105
}
 
106
 
 
107
double Uuid::val_real()
 
108
{
 
109
  ASSERT_COLUMN_MARKED_FOR_READ;
 
110
  my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
 
111
  return 0;
 
112
}
 
113
 
 
114
int64_t Uuid::val_int()
 
115
{
 
116
  ASSERT_COLUMN_MARKED_FOR_READ;
 
117
  my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
 
118
  return 0;
 
119
}
 
120
 
 
121
#ifdef NOT_YET
 
122
void Uuid::generate()
 
123
{
 
124
  uuid_t uu;
 
125
  uuid_generate_time(uu);
 
126
  memcpy(ptr, uu, sizeof(uuid_t));
 
127
  is_set= true;
 
128
}
 
129
 
 
130
void Uuid::set(const unsigned char *arg)
 
131
{
 
132
  memcpy(ptr, arg, sizeof(uuid_t));
 
133
  is_set= true;
 
134
}
 
135
#endif
 
136
 
 
137
String *Uuid::val_str(String *val_buffer, String *)
 
138
{
 
139
  const CHARSET_INFO * const cs= &my_charset_bin;
 
140
  uint32_t mlength= (uuid_st::DISPLAY_BUFFER_LENGTH) * cs->mbmaxlen;
 
141
  uuid_st uu;
 
142
 
 
143
  val_buffer->alloc(mlength);
 
144
  char *buffer=(char*) val_buffer->ptr();
 
145
 
 
146
  ASSERT_COLUMN_MARKED_FOR_READ;
 
147
 
 
148
  uu.unpack(ptr);
 
149
  uu.unparse(buffer);
 
150
 
 
151
  val_buffer->length(uuid_st::DISPLAY_LENGTH);
 
152
 
 
153
  return val_buffer;
 
154
}
 
155
 
 
156
void Uuid::sort_string(unsigned char *to, uint32_t length_arg)
 
157
{
 
158
  assert(length_arg == uuid_st::LENGTH);
 
159
  memcpy(to, ptr, length_arg);
 
160
}
 
161
 
 
162
bool Uuid::get_date(DRIZZLE_TIME *ltime, uint32_t )
 
163
{
 
164
  uuid_st uu;
 
165
 
 
166
  uu.unpack(ptr);
 
167
 
 
168
  if (uu.isTimeType())
 
169
  {
 
170
    Timestamp temporal;
 
171
    struct timeval ret_tv;
 
172
 
 
173
    ret_tv.tv_sec= ret_tv.tv_usec= 0;
 
174
 
 
175
    uu.time(ret_tv);
 
176
 
 
177
    temporal.from_time_t(ret_tv.tv_sec);
 
178
 
 
179
    ltime->time_type= DRIZZLE_TIMESTAMP_DATETIME;
 
180
    ltime->year= temporal.years();
 
181
    ltime->month= temporal.months();
 
182
    ltime->day= temporal.days();
 
183
    ltime->hour= temporal.hours();
 
184
    ltime->minute= temporal.minutes();
 
185
    ltime->second= temporal.seconds();
 
186
    ltime->second_part= temporal.nseconds();
 
187
 
 
188
    return false;
 
189
  }
 
190
  memset(ltime, 0, sizeof(DRIZZLE_TIME));
 
191
 
 
192
  return true;
 
193
}
 
194
 
 
195
bool Uuid::get_time(DRIZZLE_TIME *ltime)
 
196
{
 
197
  return get_date(ltime, 0);
 
198
}
 
199
 
 
200
} /* namespace field */
 
201
} /* namespace drizzled */