drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
uuid.cc
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
35namespace drizzled
36{
37namespace field
38{
39
40Uuid::Uuid(unsigned char *ptr_arg,
41 uint32_t len_arg,
42 unsigned char *null_ptr_arg,
43 unsigned char null_bit_arg,
44 const char *field_name_arg) :
45 Field(ptr_arg, len_arg,
46 null_ptr_arg,
47 null_bit_arg,
48 Field::NONE,
49 field_name_arg),
50 is_set(false)
51{
52}
53
54int Uuid::cmp(const unsigned char *a, const unsigned char *b)
55{
56 return memcmp(a, b, sizeof(uuid_t));
57}
58
59int Uuid::store(const char *from, uint32_t length, const charset_info_st * const )
60{
61 ASSERT_COLUMN_MARKED_FOR_WRITE;
62 type::Uuid uu;
63
64 if (is_set)
65 {
66 is_set= false;
67 return 0;
68 }
69
70 if (length != type::Uuid::DISPLAY_LENGTH)
71 {
72 my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
73 return 1;
74 }
75
76 if (uu.parse(from))
77 {
78 my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
79 return 1;
80 }
81
82 uu.pack(ptr);
83
84 return 0;
85}
86
87int Uuid::store(int64_t , bool )
88{
89 ASSERT_COLUMN_MARKED_FOR_WRITE;
90 my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
91 return 1;
92}
93
94int Uuid::store_decimal(const drizzled::type::Decimal*)
95{
96 ASSERT_COLUMN_MARKED_FOR_WRITE;
97 my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
98 return 1;
99}
100
101double Uuid::val_real() const
102{
103 ASSERT_COLUMN_MARKED_FOR_READ;
104 my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
105 return 0;
106}
107
108int64_t Uuid::val_int() const
109{
110 ASSERT_COLUMN_MARKED_FOR_READ;
111 my_error(ER_INVALID_UUID_VALUE, MYF(ME_FATALERROR));
112 return 0;
113}
114
115#ifdef NOT_YET
116void Uuid::generate()
117{
118 uuid_t uu;
119 uuid_generate_time(uu);
120 memcpy(ptr, uu, sizeof(uuid_t));
121 is_set= true;
122}
123
124void Uuid::set(const unsigned char *arg)
125{
126 memcpy(ptr, arg, sizeof(uuid_t));
127 is_set= true;
128}
129#endif
130
131String *Uuid::val_str(String *val_buffer, String *) const
132{
133 const charset_info_st * const cs= &my_charset_bin;
134 uint32_t mlength= (type::Uuid::DISPLAY_BUFFER_LENGTH) * cs->mbmaxlen;
135 type::Uuid uu;
136
137 val_buffer->alloc(mlength);
138 char *buffer=(char*) val_buffer->ptr();
139
140 ASSERT_COLUMN_MARKED_FOR_READ;
141
142 uu.unpack(ptr);
143 uu.unparse(buffer);
144
145 val_buffer->length(type::Uuid::DISPLAY_LENGTH);
146
147 return val_buffer;
148}
149
150void Uuid::sort_string(unsigned char *to, uint32_t length_arg)
151{
152 assert(length_arg == type::Uuid::LENGTH);
153 memcpy(to, ptr, length_arg);
154}
155
156bool Uuid::get_date(type::Time &ltime, uint32_t ) const
157{
158 type::Uuid uu;
159
160 uu.unpack(ptr);
161
162 if (uu.isTimeType())
163 {
164 struct timeval ret_tv;
165
166 memset(&ret_tv, 0, sizeof(struct timeval));
167
168 uu.time(ret_tv);
169
170 ltime.store(ret_tv);
171
172 return false;
173 }
174 ltime.reset();
175
176 return true;
177}
178
179bool Uuid::get_time(type::Time &ltime) const
180{
181 return get_date(ltime, 0);
182}
183
184} /* namespace field */
185} /* namespace drizzled */
TODO: Rename this file - func.h is stupid.