173.1.4
by Toru Maesaka
ripped out NULL and YEAR and moved to field/ |
1 |
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
|
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 |
#ifdef USE_PRAGMA_IMPLEMENTATION
|
|
22 |
#pragma implementation // gcc: Class implementation |
|
23 |
#endif
|
|
24 |
||
202.1.7
by Monty Taylor
Moved include/field to include/drizzle/server/field |
25 |
#include "drizzle/server/field/year.h" |
173.1.4
by Toru Maesaka
ripped out NULL and YEAR and moved to field/ |
26 |
|
27 |
/****************************************************************************
|
|
28 |
** year type
|
|
29 |
** Save in a byte the year 0, 1901->2155
|
|
30 |
** Can handle 2 byte or 4 byte years!
|
|
31 |
****************************************************************************/
|
|
32 |
||
33 |
int Field_year::store(const char *from, uint len,CHARSET_INFO *cs) |
|
34 |
{
|
|
35 |
char *end; |
|
36 |
int error; |
|
37 |
int64_t nr= cs->cset->strntoull10rnd(cs, from, len, 0, &end, &error); |
|
38 |
||
39 |
if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155 || |
|
40 |
error == MY_ERRNO_ERANGE) |
|
41 |
{
|
|
42 |
*ptr=0; |
|
43 |
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1); |
|
44 |
return 1; |
|
45 |
}
|
|
46 |
if (table->in_use->count_cuted_fields && |
|
47 |
(error= check_int(cs, from, len, end, error))) |
|
48 |
{
|
|
49 |
if (error == 1) /* empty or incorrect string */ |
|
50 |
{
|
|
51 |
*ptr= 0; |
|
52 |
return 1; |
|
53 |
}
|
|
54 |
error= 1; |
|
55 |
}
|
|
56 |
||
57 |
if (nr != 0 || len != 4) |
|
58 |
{
|
|
59 |
if (nr < YY_PART_YEAR) |
|
60 |
nr+=100; // 2000 - 2069 |
|
61 |
else if (nr > 1900) |
|
62 |
nr-= 1900; |
|
63 |
}
|
|
64 |
*ptr= (char) (uchar) nr; |
|
65 |
return error; |
|
66 |
}
|
|
67 |
||
68 |
||
69 |
int Field_year::store(double nr) |
|
70 |
{
|
|
71 |
if (nr < 0.0 || nr >= 2155.0) |
|
72 |
{
|
|
73 |
(void) Field_year::store((int64_t) -1, false); |
|
74 |
return 1; |
|
75 |
}
|
|
76 |
return Field_year::store((int64_t) nr, false); |
|
77 |
}
|
|
78 |
||
79 |
||
80 |
int Field_year::store(int64_t nr, |
|
81 |
bool unsigned_val __attribute__((__unused__))) |
|
82 |
{
|
|
83 |
if (nr < 0 || (nr >= 100 && nr <= 1900) || nr > 2155) |
|
84 |
{
|
|
85 |
*ptr= 0; |
|
86 |
set_warning(MYSQL_ERROR::WARN_LEVEL_WARN, ER_WARN_DATA_OUT_OF_RANGE, 1); |
|
87 |
return 1; |
|
88 |
}
|
|
89 |
if (nr != 0 || field_length != 4) // 0000 -> 0; 00 -> 2000 |
|
90 |
{
|
|
91 |
if (nr < YY_PART_YEAR) |
|
92 |
nr+=100; // 2000 - 2069 |
|
93 |
else if (nr > 1900) |
|
94 |
nr-= 1900; |
|
95 |
}
|
|
96 |
*ptr= (char) (uchar) nr; |
|
97 |
return 0; |
|
98 |
}
|
|
99 |
||
100 |
||
101 |
bool Field_year::send_binary(Protocol *protocol) |
|
102 |
{
|
|
103 |
uint64_t tmp= Field_year::val_int(); |
|
104 |
return protocol->store_short(tmp); |
|
105 |
}
|
|
106 |
||
107 |
||
108 |
double Field_year::val_real(void) |
|
109 |
{
|
|
110 |
return (double) Field_year::val_int(); |
|
111 |
}
|
|
112 |
||
113 |
||
114 |
int64_t Field_year::val_int(void) |
|
115 |
{
|
|
116 |
int tmp= (int) ptr[0]; |
|
117 |
if (field_length != 4) |
|
118 |
tmp%=100; // Return last 2 char |
|
119 |
else if (tmp) |
|
120 |
tmp+=1900; |
|
121 |
return (int64_t) tmp; |
|
122 |
}
|
|
123 |
||
124 |
||
125 |
String *Field_year::val_str(String *val_buffer, |
|
126 |
String *val_ptr __attribute__((unused))) |
|
127 |
{
|
|
128 |
val_buffer->alloc(5); |
|
129 |
val_buffer->length(field_length); |
|
130 |
char *to=(char*) val_buffer->ptr(); |
|
131 |
sprintf(to,field_length == 2 ? "%02d" : "%04d",(int) Field_year::val_int()); |
|
132 |
return val_buffer; |
|
133 |
}
|
|
134 |
||
135 |
||
136 |
void Field_year::sql_type(String &res) const |
|
137 |
{
|
|
138 |
CHARSET_INFO *cs=res.charset(); |
|
139 |
res.length(cs->cset->snprintf(cs,(char*)res.ptr(),res.alloced_length(), |
|
140 |
"year(%d)",(int) field_length)); |
|
141 |
}
|
|
142 |