~drizzle-trunk/drizzle/development

584.4.1 by Monty Taylor
Split out DTCollation.
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
4
 *  Copyright (C) 2008 Sun Microsystems
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; version 2 of the License.
9
 *
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.
14
 *
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
18
 */
19
20
#include <drizzled/server_includes.h>
584.4.7 by Monty Taylor
Removed a big bank of includes from item.h.
21
#include <drizzled/dtcollation.h>
22
584.4.1 by Monty Taylor
Split out DTCollation.
23
#include <drizzled/definitions.h>
24
#include <mysys/my_sys.h>
25
#include <mystrings/m_ctype.h>
26
#include <drizzled/error.h>
670.1.20 by Monty Taylor
Renamed functions to function... everything else is singular.
27
#include <drizzled/function/str/conv_charset.h>
584.1.15 by Monty Taylor
The mega-patch from hell. Renamed sql_class to session (since that's what it is) and removed it and field and table from common_includes.
28
#include <drizzled/session.h>
584.4.1 by Monty Taylor
Split out DTCollation.
29
30
31
DTCollation::DTCollation()
32
{
33
  collation= &my_charset_bin;
34
  derivation= DERIVATION_NONE;
35
}
36
37
38
DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
39
                         Derivation derivation_arg)
40
{
41
  collation= collation_arg;
42
  derivation= derivation_arg;
43
}
44
45
46
void DTCollation::set(DTCollation &dt)
47
{
48
  collation= dt.collation;
49
  derivation= dt.derivation;
50
}
51
52
53
void DTCollation::set(const CHARSET_INFO * const collation_arg,
54
                      Derivation derivation_arg)
55
{
56
  collation= collation_arg;
57
  derivation= derivation_arg;
58
}
59
60
61
void DTCollation::set(const CHARSET_INFO * const collation_arg)
62
{
63
  collation= collation_arg;
64
}
65
66
67
void DTCollation::set(Derivation derivation_arg)
68
{
69
  derivation= derivation_arg;
70
}
71
72
73
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
74
{
75
  if (!my_charset_same(collation, dt.collation))
76
  {
77
    /*
78
      We do allow to use binary strings (like BLOBS)
79
      together with character strings.
80
      Binaries have more precedence than a character
81
      string of the same derivation.
82
    */
83
    if (collation == &my_charset_bin)
84
    {
85
      if (derivation <= dt.derivation)
86
        ; // Do nothing
87
      else
88
      {
89
        set(dt);
90
      }
91
    }
92
    else if (dt.collation == &my_charset_bin)
93
    {
94
      if (dt.derivation <= derivation)
95
      {
96
        set(dt);
97
      }
98
      else
99
      {
100
        // Do nothing
101
      }
102
    }
103
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
104
             collation->state & MY_CS_UNICODE &&
105
             (derivation < dt.derivation ||
106
              (derivation == dt.derivation &&
107
               !(dt.collation->state & MY_CS_UNICODE))))
584.4.1 by Monty Taylor
Split out DTCollation.
108
    {
109
      // Do nothing
110
    }
111
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
112
             dt.collation->state & MY_CS_UNICODE &&
113
             (dt.derivation < derivation ||
114
              (dt.derivation == derivation &&
115
               !(collation->state & MY_CS_UNICODE))))
584.4.1 by Monty Taylor
Split out DTCollation.
116
    {
117
      set(dt);
118
    }
119
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
120
             derivation < dt.derivation &&
121
             dt.derivation >= DERIVATION_SYSCONST)
122
    {
123
      // Do nothing;
124
    }
125
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
126
             dt.derivation < derivation &&
127
             derivation >= DERIVATION_SYSCONST)
128
    {
129
      set(dt);
130
    }
131
    else
132
    {
133
      // Cannot apply conversion
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
134
      set(0, DERIVATION_NONE);
135
      return true;
584.4.1 by Monty Taylor
Split out DTCollation.
136
    }
137
  }
138
  else if (derivation < dt.derivation)
139
  {
140
    // Do nothing
141
  }
142
  else if (dt.derivation < derivation)
143
  {
144
    set(dt);
145
  }
146
  else
147
  {
148
    if (collation == dt.collation)
149
    {
150
      // Do nothing
151
    }
152
    else
153
    {
154
      if (derivation == DERIVATION_EXPLICIT)
155
      {
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
156
        set(0, DERIVATION_NONE);
157
        return true;
584.4.1 by Monty Taylor
Split out DTCollation.
158
      }
159
      if (collation->state & MY_CS_BINSORT)
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
160
        return false;
584.4.1 by Monty Taylor
Split out DTCollation.
161
      if (dt.collation->state & MY_CS_BINSORT)
162
      {
163
        set(dt);
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
164
        return false;
584.4.1 by Monty Taylor
Split out DTCollation.
165
      }
862 by Brian Aker
Remove charset directory code.
166
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
584.4.1 by Monty Taylor
Split out DTCollation.
167
      set(bin, DERIVATION_NONE);
168
    }
169
  }
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
170
171
  return false;
584.4.1 by Monty Taylor
Split out DTCollation.
172
}
173
174
175
bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
176
{ set(dt1); return aggregate(dt2, flags); }
177
178
179
const char *DTCollation::derivation_name() const
180
{
181
  switch(derivation)
182
  {
183
  case DERIVATION_IGNORABLE: return "IGNORABLE";
184
  case DERIVATION_COERCIBLE: return "COERCIBLE";
185
  case DERIVATION_IMPLICIT:  return "IMPLICIT";
186
  case DERIVATION_SYSCONST:  return "SYSCONST";
187
  case DERIVATION_EXPLICIT:  return "EXPLICIT";
188
  case DERIVATION_NONE:      return "NONE";
189
  default: return "UNKNOWN";
190
  }
191
}
192
193
194
bool agg_item_collations(DTCollation &c, const char *fname,
195
                         Item **av, uint32_t count,
196
                         uint32_t flags, int item_sep)
197
{
198
  uint32_t i;
199
  Item **arg;
200
  c.set(av[0]->collation);
201
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
202
  {
203
    if (c.aggregate((*arg)->collation, flags))
204
    {
205
      my_coll_agg_error(av, count, fname, item_sep);
206
      return true;
207
    }
208
  }
209
  if ((flags & MY_COLL_DISALLOW_NONE) &&
210
      c.derivation == DERIVATION_NONE)
211
  {
212
    my_coll_agg_error(av, count, fname, item_sep);
213
    return true;
214
  }
215
  return false;
216
}
217
218
219
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
220
                                        Item **av, uint32_t count,
221
                                        uint32_t flags)
222
{
223
  return (agg_item_collations(c, fname, av, count,
224
                              flags | MY_COLL_DISALLOW_NONE, 1));
225
}
226
227
228
bool agg_item_charsets(DTCollation &coll, const char *fname,
229
                       Item **args, uint32_t nargs, uint32_t flags,
230
                       int item_sep)
231
{
232
  Item **arg, *safe_args[2];
233
234
  memset(safe_args, 0, sizeof(safe_args));
235
236
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
237
    return true;
238
239
  /*
240
    For better error reporting: save the first and the second argument.
241
    We need this only if the the number of args is 3 or 2:
242
    - for a longer argument list, "Illegal mix of collations"
243
      doesn't display each argument's characteristics.
244
    - if nargs is 1, then this error cannot happen.
245
  */
246
  if (nargs >=2 && nargs <= 3)
247
  {
248
    safe_args[0]= args[0];
249
    safe_args[1]= args[item_sep];
250
  }
251
252
  Session *session= current_session;
253
  bool res= false;
254
  uint32_t i;
255
256
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
257
  {
258
    Item* conv;
259
    uint32_t dummy_offset;
260
    if (!String::needs_conversion(0, (*arg)->collation.collation,
261
                                  coll.collation,
262
                                  &dummy_offset))
263
      continue;
264
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
265
    if (!(conv= (*arg)->safe_charset_converter(coll.collation)))
584.4.1 by Monty Taylor
Split out DTCollation.
266
    {
267
      if (nargs >=2 && nargs <= 3)
268
      {
269
        /* restore the original arguments for better error message */
270
        args[0]= safe_args[0];
271
        args[item_sep]= safe_args[1];
272
      }
273
      my_coll_agg_error(args, nargs, fname, item_sep);
274
      res= true;
275
      break; // we cannot return here, we need to restore "arena".
276
    }
277
    if ((*arg)->type() == Item::FIELD_ITEM)
278
      ((Item_field *)(*arg))->no_const_subst= 1;
279
    /*
280
      If in statement prepare, then we create a converter for two
281
      constant items, do it once and then reuse it.
282
      If we're in execution of a prepared statement, arena is NULL,
283
      and the conv was created in runtime memory. This can be
284
      the case only if the argument is a parameter marker ('?'),
285
      because for all true constants the charset converter has already
286
      been created in prepare. In this case register the change for
287
      rollback.
288
    */
289
    session->change_item_tree(arg, conv);
290
    /*
291
      We do not check conv->fixed, because Item_func_conv_charset which can
292
      be return by safe_charset_converter can't be fixed at creation
293
    */
294
    conv->fix_fields(session, arg);
295
  }
296
297
  return res;
298
}
299
300
301
void my_coll_agg_error(DTCollation &c1,
302
                       DTCollation &c2, const char *fname)
303
{
304
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
305
           c1.collation->name,c1.derivation_name(),
306
           c2.collation->name,c2.derivation_name(),
307
           fname);
308
}
309
310
311
void my_coll_agg_error(DTCollation &c1,
312
                       DTCollation &c2,
313
                       DTCollation &c3,
314
                       const char *fname)
315
{
316
  my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
317
           c1.collation->name,c1.derivation_name(),
318
           c2.collation->name,c2.derivation_name(),
319
           c3.collation->name,c3.derivation_name(),
320
           fname);
321
}
322
323
324
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
325
                       int item_sep)
326
{
327
  if (count == 2)
328
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
329
  else if (count == 3)
330
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
331
                      args[2*item_sep]->collation, fname);
332
  else
333
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
334
}
335