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"
32
DTCollation::DTCollation()
34
collation= &my_charset_bin;
35
derivation= DERIVATION_NONE;
39
DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
40
Derivation derivation_arg)
42
collation= collation_arg;
43
derivation= derivation_arg;
47
void DTCollation::set(DTCollation &dt)
49
collation= dt.collation;
50
derivation= dt.derivation;
54
void DTCollation::set(const CHARSET_INFO * const collation_arg,
55
Derivation derivation_arg)
57
collation= collation_arg;
58
derivation= derivation_arg;
62
void DTCollation::set(const CHARSET_INFO * const collation_arg)
64
collation= collation_arg;
68
void DTCollation::set(Derivation derivation_arg)
70
derivation= derivation_arg;
74
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
76
if (!my_charset_same(collation, dt.collation))
79
We do allow to use binary strings (like BLOBS)
80
together with character strings.
81
Binaries have more precedence than a character
82
string of the same derivation.
84
if (collation == &my_charset_bin)
86
if (derivation <= dt.derivation)
93
else if (dt.collation == &my_charset_bin)
95
if (dt.derivation <= derivation)
104
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
105
collation->state & MY_CS_UNICODE &&
106
(derivation < dt.derivation ||
107
(derivation == dt.derivation &&
108
!(dt.collation->state & MY_CS_UNICODE))))
112
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
113
dt.collation->state & MY_CS_UNICODE &&
114
(dt.derivation < derivation ||
115
(dt.derivation == derivation &&
116
!(collation->state & MY_CS_UNICODE))))
120
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
121
derivation < dt.derivation &&
122
dt.derivation >= DERIVATION_SYSCONST)
126
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
127
dt.derivation < derivation &&
128
derivation >= DERIVATION_SYSCONST)
134
// Cannot apply conversion
135
set(0, DERIVATION_NONE);
139
else if (derivation < dt.derivation)
143
else if (dt.derivation < derivation)
149
if (collation == dt.collation)
155
if (derivation == DERIVATION_EXPLICIT)
157
set(0, DERIVATION_NONE);
160
if (collation->state & MY_CS_BINSORT)
162
if (dt.collation->state & MY_CS_BINSORT)
167
const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
168
set(bin, DERIVATION_NONE);
176
bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
177
{ set(dt1); return aggregate(dt2, flags); }
180
const char *DTCollation::derivation_name() const
184
case DERIVATION_IGNORABLE: return "IGNORABLE";
185
case DERIVATION_COERCIBLE: return "COERCIBLE";
186
case DERIVATION_IMPLICIT: return "IMPLICIT";
187
case DERIVATION_SYSCONST: return "SYSCONST";
188
case DERIVATION_EXPLICIT: return "EXPLICIT";
189
case DERIVATION_NONE: return "NONE";
190
default: return "UNKNOWN";
195
bool agg_item_collations(DTCollation &c, const char *fname,
196
Item **av, uint32_t count,
197
uint32_t flags, int item_sep)
201
c.set(av[0]->collation);
202
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
204
if (c.aggregate((*arg)->collation, flags))
206
my_coll_agg_error(av, count, fname, item_sep);
210
if ((flags & MY_COLL_DISALLOW_NONE) &&
211
c.derivation == DERIVATION_NONE)
213
my_coll_agg_error(av, count, fname, item_sep);
220
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
221
Item **av, uint32_t count,
224
return (agg_item_collations(c, fname, av, count,
225
flags | MY_COLL_DISALLOW_NONE, 1));
229
bool agg_item_charsets(DTCollation &coll, const char *fname,
230
Item **args, uint32_t nargs, uint32_t flags,
233
Item **arg, *safe_args[2];
235
memset(safe_args, 0, sizeof(safe_args));
237
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
241
For better error reporting: save the first and the second argument.
242
We need this only if the the number of args is 3 or 2:
243
- for a longer argument list, "Illegal mix of collations"
244
doesn't display each argument's characteristics.
245
- if nargs is 1, then this error cannot happen.
247
if (nargs >=2 && nargs <= 3)
249
safe_args[0]= args[0];
250
safe_args[1]= args[item_sep];
253
Session *session= current_session;
257
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
260
uint32_t dummy_offset;
261
if (!String::needs_conversion(0, (*arg)->collation.collation,
266
if (!(conv= (*arg)->safe_charset_converter(coll.collation)))
268
if (nargs >=2 && nargs <= 3)
270
/* restore the original arguments for better error message */
271
args[0]= safe_args[0];
272
args[item_sep]= safe_args[1];
274
my_coll_agg_error(args, nargs, fname, item_sep);
276
break; // we cannot return here, we need to restore "arena".
278
if ((*arg)->type() == Item::FIELD_ITEM)
279
((Item_field *)(*arg))->no_const_subst= 1;
281
If in statement prepare, then we create a converter for two
282
constant items, do it once and then reuse it.
283
If we're in execution of a prepared statement, arena is NULL,
284
and the conv was created in runtime memory. This can be
285
the case only if the argument is a parameter marker ('?'),
286
because for all true constants the charset converter has already
287
been created in prepare. In this case register the change for
290
session->change_item_tree(arg, conv);
292
We do not check conv->fixed, because Item_func_conv_charset which can
293
be return by safe_charset_converter can't be fixed at creation
295
conv->fix_fields(session, arg);
302
void my_coll_agg_error(DTCollation &c1,
303
DTCollation &c2, const char *fname)
305
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
306
c1.collation->name,c1.derivation_name(),
307
c2.collation->name,c2.derivation_name(),
312
void my_coll_agg_error(DTCollation &c1,
317
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
318
c1.collation->name,c1.derivation_name(),
319
c2.collation->name,c2.derivation_name(),
320
c3.collation->name,c3.derivation_name(),
325
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
329
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
331
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
332
args[2*item_sep]->collation, fname);
334
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);