1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21
#include <drizzled/dtcollation.h>
23
#include <drizzled/definitions.h>
24
#include "drizzled/internal/my_sys.h"
25
#include "drizzled/charset_info.h"
26
#include <drizzled/error.h>
27
#include <drizzled/function/str/conv_charset.h>
28
#include <drizzled/session.h>
29
#include "drizzled/charset.h"
34
DTCollation::DTCollation()
36
collation= &my_charset_bin;
37
derivation= DERIVATION_NONE;
41
DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
42
Derivation derivation_arg)
44
collation= collation_arg;
45
derivation= derivation_arg;
49
void DTCollation::set(DTCollation &dt)
51
collation= dt.collation;
52
derivation= dt.derivation;
56
void DTCollation::set(const CHARSET_INFO * const collation_arg,
57
Derivation derivation_arg)
59
collation= collation_arg;
60
derivation= derivation_arg;
64
void DTCollation::set(const CHARSET_INFO * const collation_arg)
66
collation= collation_arg;
70
void DTCollation::set(Derivation derivation_arg)
72
derivation= derivation_arg;
76
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
78
if (!my_charset_same(collation, dt.collation))
81
We do allow to use binary strings (like BLOBS)
82
together with character strings.
83
Binaries have more precedence than a character
84
string of the same derivation.
86
if (collation == &my_charset_bin)
88
if (derivation <= dt.derivation)
95
else if (dt.collation == &my_charset_bin)
97
if (dt.derivation <= derivation)
106
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
107
collation->state & MY_CS_UNICODE &&
108
(derivation < dt.derivation ||
109
(derivation == dt.derivation &&
110
!(dt.collation->state & MY_CS_UNICODE))))
114
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
115
dt.collation->state & MY_CS_UNICODE &&
116
(dt.derivation < derivation ||
117
(dt.derivation == derivation &&
118
!(collation->state & MY_CS_UNICODE))))
122
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
123
derivation < dt.derivation &&
124
dt.derivation >= DERIVATION_SYSCONST)
128
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
129
dt.derivation < derivation &&
130
derivation >= DERIVATION_SYSCONST)
136
// Cannot apply conversion
137
set(0, DERIVATION_NONE);
141
else if (derivation < dt.derivation)
145
else if (dt.derivation < derivation)
151
if (collation == dt.collation)
157
if (derivation == DERIVATION_EXPLICIT)
159
set(0, DERIVATION_NONE);
162
if (collation->state & MY_CS_BINSORT)
164
if (dt.collation->state & MY_CS_BINSORT)
169
const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
170
set(bin, DERIVATION_NONE);
178
bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
179
{ set(dt1); return aggregate(dt2, flags); }
182
const char *DTCollation::derivation_name() const
186
case DERIVATION_IGNORABLE: return "IGNORABLE";
187
case DERIVATION_COERCIBLE: return "COERCIBLE";
188
case DERIVATION_IMPLICIT: return "IMPLICIT";
189
case DERIVATION_SYSCONST: return "SYSCONST";
190
case DERIVATION_EXPLICIT: return "EXPLICIT";
191
case DERIVATION_NONE: return "NONE";
192
default: return "UNKNOWN";
197
bool agg_item_collations(DTCollation &c, const char *fname,
198
Item **av, uint32_t count,
199
uint32_t flags, int item_sep)
203
c.set(av[0]->collation);
204
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
206
if (c.aggregate((*arg)->collation, flags))
208
my_coll_agg_error(av, count, fname, item_sep);
212
if ((flags & MY_COLL_DISALLOW_NONE) &&
213
c.derivation == DERIVATION_NONE)
215
my_coll_agg_error(av, count, fname, item_sep);
222
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
223
Item **av, uint32_t count,
226
return (agg_item_collations(c, fname, av, count,
227
flags | MY_COLL_DISALLOW_NONE, 1));
231
bool agg_item_charsets(DTCollation &coll, const char *fname,
232
Item **args, uint32_t nargs, uint32_t flags,
235
Item **arg, *safe_args[2];
237
memset(safe_args, 0, sizeof(safe_args));
239
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
243
For better error reporting: save the first and the second argument.
244
We need this only if the the number of args is 3 or 2:
245
- for a longer argument list, "Illegal mix of collations"
246
doesn't display each argument's characteristics.
247
- if nargs is 1, then this error cannot happen.
249
if (nargs >=2 && nargs <= 3)
251
safe_args[0]= args[0];
252
safe_args[1]= args[item_sep];
255
Session *session= current_session;
259
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
262
uint32_t dummy_offset;
263
if (!String::needs_conversion(0, (*arg)->collation.collation,
268
if (!(conv= (*arg)->safe_charset_converter(coll.collation)))
270
if (nargs >=2 && nargs <= 3)
272
/* restore the original arguments for better error message */
273
args[0]= safe_args[0];
274
args[item_sep]= safe_args[1];
276
my_coll_agg_error(args, nargs, fname, item_sep);
278
break; // we cannot return here, we need to restore "arena".
280
if ((*arg)->type() == Item::FIELD_ITEM)
281
((Item_field *)(*arg))->no_const_subst= 1;
283
If in statement prepare, then we create a converter for two
284
constant items, do it once and then reuse it.
285
If we're in execution of a prepared statement, arena is NULL,
286
and the conv was created in runtime memory. This can be
287
the case only if the argument is a parameter marker ('?'),
288
because for all true constants the charset converter has already
289
been created in prepare. In this case register the change for
292
session->change_item_tree(arg, conv);
294
We do not check conv->fixed, because Item_func_conv_charset which can
295
be return by safe_charset_converter can't be fixed at creation
297
conv->fix_fields(session, arg);
304
void my_coll_agg_error(DTCollation &c1,
305
DTCollation &c2, const char *fname)
307
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
308
c1.collation->name,c1.derivation_name(),
309
c2.collation->name,c2.derivation_name(),
314
void my_coll_agg_error(DTCollation &c1,
319
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
320
c1.collation->name,c1.derivation_name(),
321
c2.collation->name,c2.derivation_name(),
322
c3.collation->name,c3.derivation_name(),
327
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
331
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
333
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
334
args[2*item_sep]->collation, fname);
336
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
339
} /* namespace drizzled */