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
20
#include <drizzled/server_includes.h>
21
#include <drizzled/definitions.h>
22
#include <mysys/my_sys.h>
23
#include <mystrings/m_ctype.h>
24
#include <drizzled/error.h>
26
#include <drizzled/dtcollation.h>
30
left_is_superset(const DTCollation &left, const DTCollation &right)
32
/* Allow convert to Unicode */
33
if (left.collation->state & MY_CS_UNICODE &&
34
(left.derivation < right.derivation ||
35
(left.derivation == right.derivation &&
36
!(right.collation->state & MY_CS_UNICODE))))
38
/* Allow convert from ASCII */
39
if (right.repertoire == MY_REPERTOIRE_ASCII &&
40
(left.derivation < right.derivation ||
41
(left.derivation == right.derivation &&
42
!(left.repertoire == MY_REPERTOIRE_ASCII))))
44
/* Disallow conversion otherwise */
49
DTCollation::DTCollation()
51
collation= &my_charset_bin;
52
derivation= DERIVATION_NONE;
53
repertoire= MY_REPERTOIRE_UNICODE30;
57
DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
58
Derivation derivation_arg)
60
collation= collation_arg;
61
derivation= derivation_arg;
62
set_repertoire_from_charset(collation_arg);
64
void DTCollation::set_repertoire_from_charset(const CHARSET_INFO * const cs)
66
repertoire= cs->state & MY_CS_PUREASCII ?
67
MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
71
void DTCollation::set(DTCollation &dt)
73
collation= dt.collation;
74
derivation= dt.derivation;
75
repertoire= dt.repertoire;
79
void DTCollation::set(const CHARSET_INFO * const collation_arg,
80
Derivation derivation_arg)
82
collation= collation_arg;
83
derivation= derivation_arg;
84
set_repertoire_from_charset(collation_arg);
88
void DTCollation::set(const CHARSET_INFO * const collation_arg,
89
Derivation derivation_arg,
90
uint32_t repertoire_arg)
92
collation= collation_arg;
93
derivation= derivation_arg;
94
repertoire= repertoire_arg;
98
void DTCollation::set(const CHARSET_INFO * const collation_arg)
100
collation= collation_arg;
101
set_repertoire_from_charset(collation_arg);
105
void DTCollation::set(Derivation derivation_arg)
107
derivation= derivation_arg;
111
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
113
if (!my_charset_same(collation, dt.collation))
116
We do allow to use binary strings (like BLOBS)
117
together with character strings.
118
Binaries have more precedence than a character
119
string of the same derivation.
121
if (collation == &my_charset_bin)
123
if (derivation <= dt.derivation)
130
else if (dt.collation == &my_charset_bin)
132
if (dt.derivation <= derivation)
141
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
142
left_is_superset(*this, dt))
146
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
147
left_is_superset(dt, *this))
151
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
152
derivation < dt.derivation &&
153
dt.derivation >= DERIVATION_SYSCONST)
157
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
158
dt.derivation < derivation &&
159
derivation >= DERIVATION_SYSCONST)
165
// Cannot apply conversion
166
set(0, DERIVATION_NONE, 0);
170
else if (derivation < dt.derivation)
174
else if (dt.derivation < derivation)
180
if (collation == dt.collation)
186
if (derivation == DERIVATION_EXPLICIT)
188
set(0, DERIVATION_NONE, 0);
191
if (collation->state & MY_CS_BINSORT)
193
if (dt.collation->state & MY_CS_BINSORT)
198
const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname,
201
set(bin, DERIVATION_NONE);
204
repertoire|= dt.repertoire;
209
bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
210
{ set(dt1); return aggregate(dt2, flags); }
213
const char *DTCollation::derivation_name() const
217
case DERIVATION_IGNORABLE: return "IGNORABLE";
218
case DERIVATION_COERCIBLE: return "COERCIBLE";
219
case DERIVATION_IMPLICIT: return "IMPLICIT";
220
case DERIVATION_SYSCONST: return "SYSCONST";
221
case DERIVATION_EXPLICIT: return "EXPLICIT";
222
case DERIVATION_NONE: return "NONE";
223
default: return "UNKNOWN";
228
bool agg_item_collations(DTCollation &c, const char *fname,
229
Item **av, uint32_t count,
230
uint32_t flags, int item_sep)
234
c.set(av[0]->collation);
235
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
237
if (c.aggregate((*arg)->collation, flags))
239
my_coll_agg_error(av, count, fname, item_sep);
243
if ((flags & MY_COLL_DISALLOW_NONE) &&
244
c.derivation == DERIVATION_NONE)
246
my_coll_agg_error(av, count, fname, item_sep);
253
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
254
Item **av, uint32_t count,
257
return (agg_item_collations(c, fname, av, count,
258
flags | MY_COLL_DISALLOW_NONE, 1));
262
bool agg_item_charsets(DTCollation &coll, const char *fname,
263
Item **args, uint32_t nargs, uint32_t flags,
266
Item **arg, *safe_args[2];
268
memset(safe_args, 0, sizeof(safe_args));
270
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
274
For better error reporting: save the first and the second argument.
275
We need this only if the the number of args is 3 or 2:
276
- for a longer argument list, "Illegal mix of collations"
277
doesn't display each argument's characteristics.
278
- if nargs is 1, then this error cannot happen.
280
if (nargs >=2 && nargs <= 3)
282
safe_args[0]= args[0];
283
safe_args[1]= args[item_sep];
286
Session *session= current_session;
290
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
293
uint32_t dummy_offset;
294
if (!String::needs_conversion(0, (*arg)->collation.collation,
299
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
300
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
301
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
305
if (nargs >=2 && nargs <= 3)
307
/* restore the original arguments for better error message */
308
args[0]= safe_args[0];
309
args[item_sep]= safe_args[1];
311
my_coll_agg_error(args, nargs, fname, item_sep);
313
break; // we cannot return here, we need to restore "arena".
315
if ((*arg)->type() == Item::FIELD_ITEM)
316
((Item_field *)(*arg))->no_const_subst= 1;
318
If in statement prepare, then we create a converter for two
319
constant items, do it once and then reuse it.
320
If we're in execution of a prepared statement, arena is NULL,
321
and the conv was created in runtime memory. This can be
322
the case only if the argument is a parameter marker ('?'),
323
because for all true constants the charset converter has already
324
been created in prepare. In this case register the change for
327
session->change_item_tree(arg, conv);
329
We do not check conv->fixed, because Item_func_conv_charset which can
330
be return by safe_charset_converter can't be fixed at creation
332
conv->fix_fields(session, arg);
339
void my_coll_agg_error(DTCollation &c1,
340
DTCollation &c2, const char *fname)
342
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
343
c1.collation->name,c1.derivation_name(),
344
c2.collation->name,c2.derivation_name(),
349
void my_coll_agg_error(DTCollation &c1,
354
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
355
c1.collation->name,c1.derivation_name(),
356
c2.collation->name,c2.derivation_name(),
357
c3.collation->name,c3.derivation_name(),
362
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
366
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
368
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
369
args[2*item_sep]->collation, fname);
371
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);