~drizzle-trunk/drizzle/development

466 by Monty Taylor
Fixed modelines... these files are c++.
1
/* - mode: c++ c-basic-offset: 2; indent-tabs-mode: nil; -*-
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 MySQL
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
243.1.17 by Jay Pipes
FINAL PHASE removal of mysql_priv.h (Bye, bye my friend.)
22
#include <drizzled/server_includes.h>
214 by Brian Aker
Rename of fields (fix issue with string and decimal .h clashing).
23
#include <drizzled/field/timetype.h>
549 by Monty Taylor
Took gettext.h out of header files.
24
#include <drizzled/error.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
25
#include <drizzled/table.h>
26
#include <drizzled/session.h>
572.1.4 by Monty Taylor
Removed a bunch of unusued tests and defines from autoconf.
27
#include CMATH_H
28
29
#if defined(CMATH_NAMESPACE)
30
using namespace CMATH_NAMESPACE;
31
#endif
32
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
33
34
/****************************************************************************
35
** time type
36
** In string context: HH:MM:SS
37
** In number context: HHMMSS
38
** Stored as a 3 byte unsigned int
39
****************************************************************************/
40
41
int Field_time::store(const char *from,
482 by Brian Aker
Remove uint.
42
                      uint32_t len,
264.2.6 by Andrey Hristov
Constify the usage of CHARSET_INFO almost to the last place in the code.
43
                      const CHARSET_INFO * const cs __attribute__((unused)))
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
44
{
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
45
  DRIZZLE_TIME ltime;
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
46
  long tmp;
47
  int error= 0;
48
  int warning;
49
50
  if (str_to_time(from, len, &ltime, &warning))
51
  {
52
    tmp=0L;
53
    error= 2;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
54
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_TRUNCATED,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
55
                         from, len, DRIZZLE_TIMESTAMP_TIME, 1);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
56
  }
57
  else
58
  {
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
59
    if (warning & DRIZZLE_TIME_WARN_TRUNCATED)
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
60
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
61
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
212.5.42 by Monty Taylor
Ding dong include is dead.
62
                           ER_WARN_DATA_TRUNCATED,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
63
                           from, len, DRIZZLE_TIMESTAMP_TIME, 1);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
64
      error= 1;
65
    }
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
66
    if (warning & DRIZZLE_TIME_WARN_OUT_OF_RANGE)
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
67
    {
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
68
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, 
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
69
                           ER_WARN_DATA_OUT_OF_RANGE,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
70
                           from, len, DRIZZLE_TIMESTAMP_TIME, !error);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
71
      error= 1;
72
    }
73
    if (ltime.month)
74
      ltime.day=0;
75
    tmp=(ltime.day*24L+ltime.hour)*10000L+(ltime.minute*100+ltime.second);
76
  }
77
  
78
  if (ltime.neg)
79
    tmp= -tmp;
80
  int3store(ptr,tmp);
81
  return error;
82
}
83
84
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
85
int Field_time::store_time(DRIZZLE_TIME *ltime,
398.1.1 by Monty Taylor
Remove typedef enum enum_drizzle_timestamp_type timestamp_type;
86
                           enum enum_drizzle_timestamp_type time_type __attribute__((unused)))
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
87
{
88
  long tmp= ((ltime->month ? 0 : ltime->day * 24L) + ltime->hour) * 10000L +
89
            (ltime->minute * 100 + ltime->second);
90
  if (ltime->neg)
91
    tmp= -tmp;
92
  return Field_time::store((int64_t) tmp, false);
93
}
94
95
96
int Field_time::store(double nr)
97
{
98
  long tmp;
99
  int error= 0;
100
  if (nr > (double)TIME_MAX_VALUE)
101
  {
102
    tmp= TIME_MAX_VALUE;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
103
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
104
                         ER_WARN_DATA_OUT_OF_RANGE, nr, DRIZZLE_TIMESTAMP_TIME);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
105
    error= 1;
106
  }
107
  else if (nr < (double)-TIME_MAX_VALUE)
108
  {
109
    tmp= -TIME_MAX_VALUE;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
110
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, 
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
111
                         ER_WARN_DATA_OUT_OF_RANGE, nr, DRIZZLE_TIMESTAMP_TIME);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
112
    error= 1;
113
  }
114
  else
115
  {
116
    tmp=(long) floor(fabs(nr));			// Remove fractions
117
    if (nr < 0)
118
      tmp= -tmp;
119
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
120
    {
121
      tmp=0;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
122
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, 
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
123
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
124
                           DRIZZLE_TIMESTAMP_TIME);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
125
      error= 1;
126
    }
127
  }
128
  int3store(ptr,tmp);
129
  return error;
130
}
131
132
133
int Field_time::store(int64_t nr, bool unsigned_val)
134
{
135
  long tmp;
136
  int error= 0;
137
  if (nr < (int64_t) -TIME_MAX_VALUE && !unsigned_val)
138
  {
139
    tmp= -TIME_MAX_VALUE;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
140
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, 
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
141
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
142
                         DRIZZLE_TIMESTAMP_TIME, 1);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
143
    error= 1;
144
  }
145
  else if (nr > (int64_t) TIME_MAX_VALUE || (nr < 0 && unsigned_val))
146
  {
147
    tmp= TIME_MAX_VALUE;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
148
    set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, 
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
149
                         ER_WARN_DATA_OUT_OF_RANGE, nr,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
150
                         DRIZZLE_TIMESTAMP_TIME, 1);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
151
    error= 1;
152
  }
153
  else
154
  {
155
    tmp=(long) nr;
156
    if (tmp % 100 > 59 || tmp/100 % 100 > 59)
157
    {
158
      tmp=0;
261.4.1 by Felipe
- Renamed MYSQL_ERROR to DRIZZLE_ERROR.
159
      set_datetime_warning(DRIZZLE_ERROR::WARN_LEVEL_WARN, 
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
160
                           ER_WARN_DATA_OUT_OF_RANGE, nr,
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
161
                           DRIZZLE_TIMESTAMP_TIME, 1);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
162
      error= 1;
163
    }
164
  }
165
  int3store(ptr,tmp);
166
  return error;
167
}
168
169
170
double Field_time::val_real(void)
171
{
205 by Brian Aker
uint32 -> uin32_t
172
  uint32_t j= (uint32_t) uint3korr(ptr);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
173
  return (double) j;
174
}
175
176
int64_t Field_time::val_int(void)
177
{
178
  return (int64_t) sint3korr(ptr);
179
}
180
181
182
/**
183
  @note
184
  This function is multi-byte safe as the result string is always of type
185
  my_charset_bin
186
*/
187
188
String *Field_time::val_str(String *val_buffer,
189
			    String *val_ptr __attribute__((unused)))
190
{
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
191
  DRIZZLE_TIME ltime;
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
192
  val_buffer->alloc(MAX_DATE_STRING_REP_LENGTH);
193
  long tmp=(long) sint3korr(ptr);
194
  ltime.neg= 0;
195
  if (tmp < 0)
196
  {
197
    tmp= -tmp;
198
    ltime.neg= 1;
199
  }
200
  ltime.day= (uint) 0;
201
  ltime.hour= (uint) (tmp/10000);
202
  ltime.minute= (uint) (tmp/100 % 100);
203
  ltime.second= (uint) (tmp % 100);
204
  make_time((DATE_TIME_FORMAT*) 0, &ltime, val_buffer);
205
  return val_buffer;
206
}
207
208
209
/**
210
  @note
211
  Normally we would not consider 'time' as a valid date, but we allow
212
  get_date() here to be able to do things like
213
  DATE_FORMAT(time, "%l.%i %p")
214
*/
215
 
482 by Brian Aker
Remove uint.
216
bool Field_time::get_date(DRIZZLE_TIME *ltime, uint32_t fuzzydate)
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
217
{
218
  long tmp;
520.1.22 by Brian Aker
Second pass of thd cleanup
219
  Session *session= table ? table->in_use : current_session;
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
220
  if (!(fuzzydate & TIME_FUZZY_DATE))
221
  {
520.1.22 by Brian Aker
Second pass of thd cleanup
222
    push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN,
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
223
                        ER_WARN_DATA_OUT_OF_RANGE,
224
                        ER(ER_WARN_DATA_OUT_OF_RANGE), field_name,
520.1.22 by Brian Aker
Second pass of thd cleanup
225
                        session->row_count);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
226
    return 1;
227
  }
228
  tmp=(long) sint3korr(ptr);
229
  ltime->neg=0;
230
  if (tmp < 0)
231
  {
232
    ltime->neg= 1;
233
    tmp=-tmp;
234
  }
235
  ltime->hour=tmp/10000;
236
  tmp-=ltime->hour*10000;
237
  ltime->minute=   tmp/100;
238
  ltime->second= tmp % 100;
239
  ltime->year= ltime->month= ltime->day= ltime->second_part= 0;
240
  return 0;
241
}
242
243
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
244
bool Field_time::get_time(DRIZZLE_TIME *ltime)
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
245
{
246
  long tmp=(long) sint3korr(ptr);
247
  ltime->neg=0;
248
  if (tmp < 0)
249
  {
250
    ltime->neg= 1;
251
    tmp=-tmp;
252
  }
253
  ltime->day= 0;
254
  ltime->hour=   (int) (tmp/10000);
255
  tmp-=ltime->hour*10000;
256
  ltime->minute= (int) tmp/100;
257
  ltime->second= (int) tmp % 100;
258
  ltime->second_part=0;
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
259
  ltime->time_type= DRIZZLE_TIMESTAMP_TIME;
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
260
  return 0;
261
}
262
263
264
bool Field_time::send_binary(Protocol *protocol)
265
{
236.1.24 by Monty Taylor
Renamed MYSQL_TIME to DRIZZLE_TIME.
266
  DRIZZLE_TIME tm;
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
267
  Field_time::get_time(&tm);
268
  tm.day= tm.hour/24;				// Move hours to days
269
  tm.hour-= tm.day*24;
270
  return protocol->store_time(&tm);
271
}
272
273
481 by Brian Aker
Remove all of uchar.
274
int Field_time::cmp(const unsigned char *a_ptr, const unsigned char *b_ptr)
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
275
{
205 by Brian Aker
uint32 -> uin32_t
276
  int32_t a,b;
277
  a=(int32_t) sint3korr(a_ptr);
278
  b=(int32_t) sint3korr(b_ptr);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
279
  return (a < b) ? -1 : (a > b) ? 1 : 0;
280
}
281
482 by Brian Aker
Remove uint.
282
void Field_time::sort_string(unsigned char *to,uint32_t length __attribute__((unused)))
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
283
{
481 by Brian Aker
Remove all of uchar.
284
  to[0] = (unsigned char) (ptr[2] ^ 128);
173.1.7 by Toru Maesaka
ripped out TIME and VARSTRING, moved to field/
285
  to[1] = ptr[1];
286
  to[2] = ptr[0];
287
}
288
289
void Field_time::sql_type(String &res) const
290
{
291
  res.set_ascii(STRING_WITH_LEN("time"));
292
}
293
294
295
296
297