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/dtcollation.h>
23
#include <drizzled/definitions.h>
24
#include <mysys/my_sys.h>
25
#include <mystrings/m_ctype.h>
26
#include <drizzled/error.h>
27
#include <drizzled/functions/str/conv_charset.h>
28
#include <drizzled/session.h>
32
left_is_superset(const DTCollation &left, const DTCollation &right)
34
/* Allow convert to Unicode */
35
if (left.collation->state & MY_CS_UNICODE &&
36
(left.derivation < right.derivation ||
37
(left.derivation == right.derivation &&
38
!(right.collation->state & MY_CS_UNICODE))))
40
/* Allow convert from ASCII */
41
if (right.repertoire == MY_REPERTOIRE_ASCII &&
42
(left.derivation < right.derivation ||
43
(left.derivation == right.derivation &&
44
!(left.repertoire == MY_REPERTOIRE_ASCII))))
46
/* Disallow conversion otherwise */
51
DTCollation::DTCollation()
53
collation= &my_charset_bin;
54
derivation= DERIVATION_NONE;
55
repertoire= MY_REPERTOIRE_UNICODE30;
59
DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
60
Derivation derivation_arg)
62
collation= collation_arg;
63
derivation= derivation_arg;
64
set_repertoire_from_charset(collation_arg);
66
void DTCollation::set_repertoire_from_charset(const CHARSET_INFO * const cs)
68
repertoire= cs->state & MY_CS_PUREASCII ?
69
MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
73
void DTCollation::set(DTCollation &dt)
75
collation= dt.collation;
76
derivation= dt.derivation;
77
repertoire= dt.repertoire;
81
void DTCollation::set(const CHARSET_INFO * const collation_arg,
82
Derivation derivation_arg)
84
collation= collation_arg;
85
derivation= derivation_arg;
86
set_repertoire_from_charset(collation_arg);
90
void DTCollation::set(const CHARSET_INFO * const collation_arg,
91
Derivation derivation_arg,
92
uint32_t repertoire_arg)
94
collation= collation_arg;
95
derivation= derivation_arg;
96
repertoire= repertoire_arg;
100
void DTCollation::set(const CHARSET_INFO * const collation_arg)
102
collation= collation_arg;
103
set_repertoire_from_charset(collation_arg);
107
void DTCollation::set(Derivation derivation_arg)
109
derivation= derivation_arg;
113
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
115
if (!my_charset_same(collation, dt.collation))
118
We do allow to use binary strings (like BLOBS)
119
together with character strings.
120
Binaries have more precedence than a character
121
string of the same derivation.
123
if (collation == &my_charset_bin)
125
if (derivation <= dt.derivation)
132
else if (dt.collation == &my_charset_bin)
134
if (dt.derivation <= derivation)
143
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
144
left_is_superset(*this, dt))
148
else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
149
left_is_superset(dt, *this))
153
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
154
derivation < dt.derivation &&
155
dt.derivation >= DERIVATION_SYSCONST)
159
else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
160
dt.derivation < derivation &&
161
derivation >= DERIVATION_SYSCONST)
167
// Cannot apply conversion
168
set(0, DERIVATION_NONE, 0);
172
else if (derivation < dt.derivation)
176
else if (dt.derivation < derivation)
182
if (collation == dt.collation)
188
if (derivation == DERIVATION_EXPLICIT)
190
set(0, DERIVATION_NONE, 0);
193
if (collation->state & MY_CS_BINSORT)
195
if (dt.collation->state & MY_CS_BINSORT)
200
const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname,
203
set(bin, DERIVATION_NONE);
206
repertoire|= dt.repertoire;
211
bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
212
{ set(dt1); return aggregate(dt2, flags); }
215
const char *DTCollation::derivation_name() const
219
case DERIVATION_IGNORABLE: return "IGNORABLE";
220
case DERIVATION_COERCIBLE: return "COERCIBLE";
221
case DERIVATION_IMPLICIT: return "IMPLICIT";
222
case DERIVATION_SYSCONST: return "SYSCONST";
223
case DERIVATION_EXPLICIT: return "EXPLICIT";
224
case DERIVATION_NONE: return "NONE";
225
default: return "UNKNOWN";
230
bool agg_item_collations(DTCollation &c, const char *fname,
231
Item **av, uint32_t count,
232
uint32_t flags, int item_sep)
236
c.set(av[0]->collation);
237
for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
239
if (c.aggregate((*arg)->collation, flags))
241
my_coll_agg_error(av, count, fname, item_sep);
245
if ((flags & MY_COLL_DISALLOW_NONE) &&
246
c.derivation == DERIVATION_NONE)
248
my_coll_agg_error(av, count, fname, item_sep);
255
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
256
Item **av, uint32_t count,
259
return (agg_item_collations(c, fname, av, count,
260
flags | MY_COLL_DISALLOW_NONE, 1));
264
bool agg_item_charsets(DTCollation &coll, const char *fname,
265
Item **args, uint32_t nargs, uint32_t flags,
268
Item **arg, *safe_args[2];
270
memset(safe_args, 0, sizeof(safe_args));
272
if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
276
For better error reporting: save the first and the second argument.
277
We need this only if the the number of args is 3 or 2:
278
- for a longer argument list, "Illegal mix of collations"
279
doesn't display each argument's characteristics.
280
- if nargs is 1, then this error cannot happen.
282
if (nargs >=2 && nargs <= 3)
284
safe_args[0]= args[0];
285
safe_args[1]= args[item_sep];
288
Session *session= current_session;
292
for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
295
uint32_t dummy_offset;
296
if (!String::needs_conversion(0, (*arg)->collation.collation,
301
if (!(conv= (*arg)->safe_charset_converter(coll.collation)) &&
302
((*arg)->collation.repertoire == MY_REPERTOIRE_ASCII))
303
conv= new Item_func_conv_charset(*arg, coll.collation, 1);
307
if (nargs >=2 && nargs <= 3)
309
/* restore the original arguments for better error message */
310
args[0]= safe_args[0];
311
args[item_sep]= safe_args[1];
313
my_coll_agg_error(args, nargs, fname, item_sep);
315
break; // we cannot return here, we need to restore "arena".
317
if ((*arg)->type() == Item::FIELD_ITEM)
318
((Item_field *)(*arg))->no_const_subst= 1;
320
If in statement prepare, then we create a converter for two
321
constant items, do it once and then reuse it.
322
If we're in execution of a prepared statement, arena is NULL,
323
and the conv was created in runtime memory. This can be
324
the case only if the argument is a parameter marker ('?'),
325
because for all true constants the charset converter has already
326
been created in prepare. In this case register the change for
329
session->change_item_tree(arg, conv);
331
We do not check conv->fixed, because Item_func_conv_charset which can
332
be return by safe_charset_converter can't be fixed at creation
334
conv->fix_fields(session, arg);
341
void my_coll_agg_error(DTCollation &c1,
342
DTCollation &c2, const char *fname)
344
my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
345
c1.collation->name,c1.derivation_name(),
346
c2.collation->name,c2.derivation_name(),
351
void my_coll_agg_error(DTCollation &c1,
356
my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
357
c1.collation->name,c1.derivation_name(),
358
c2.collation->name,c2.derivation_name(),
359
c3.collation->name,c3.derivation_name(),
364
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
368
my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
370
my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
371
args[2*item_sep]->collation, fname);
373
my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);