~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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
20
#include "config.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>
1241.9.64 by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal.
24
#include "drizzled/internal/my_sys.h"
1241.9.61 by Monty Taylor
No more mystrings in drizzled/
25
#include "drizzled/charset_info.h"
584.4.1 by Monty Taylor
Split out DTCollation.
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>
1241.9.57 by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined.
29
#include "drizzled/charset.h"
584.4.1 by Monty Taylor
Split out DTCollation.
30
31
32
DTCollation::DTCollation()
33
{
34
  collation= &my_charset_bin;
35
  derivation= DERIVATION_NONE;
36
}
37
38
39
DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
40
                         Derivation derivation_arg)
41
{
42
  collation= collation_arg;
43
  derivation= derivation_arg;
44
}
45
46
47
void DTCollation::set(DTCollation &dt)
48
{
49
  collation= dt.collation;
50
  derivation= dt.derivation;
51
}
52
53
54
void DTCollation::set(const CHARSET_INFO * const collation_arg,
55
                      Derivation derivation_arg)
56
{
57
  collation= collation_arg;
58
  derivation= derivation_arg;
59
}
60
61
62
void DTCollation::set(const CHARSET_INFO * const collation_arg)
63
{
64
  collation= collation_arg;
65
}
66
67
68
void DTCollation::set(Derivation derivation_arg)
69
{
70
  derivation= derivation_arg;
71
}
72
73
74
bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
75
{
76
  if (!my_charset_same(collation, dt.collation))
77
  {
78
    /*
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.
83
    */
84
    if (collation == &my_charset_bin)
85
    {
86
      if (derivation <= dt.derivation)
87
        ; // Do nothing
88
      else
89
      {
90
        set(dt);
91
      }
92
    }
93
    else if (dt.collation == &my_charset_bin)
94
    {
95
      if (dt.derivation <= derivation)
96
      {
97
        set(dt);
98
      }
99
      else
100
      {
101
        // Do nothing
102
      }
103
    }
104
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
105
             collation->state & MY_CS_UNICODE &&
106
             (derivation < dt.derivation ||
107
              (derivation == dt.derivation &&
108
               !(dt.collation->state & MY_CS_UNICODE))))
584.4.1 by Monty Taylor
Split out DTCollation.
109
    {
110
      // Do nothing
111
    }
112
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
113
             dt.collation->state & MY_CS_UNICODE &&
114
             (dt.derivation < derivation ||
115
              (dt.derivation == derivation &&
116
               !(collation->state & MY_CS_UNICODE))))
584.4.1 by Monty Taylor
Split out DTCollation.
117
    {
118
      set(dt);
119
    }
120
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
121
             derivation < dt.derivation &&
122
             dt.derivation >= DERIVATION_SYSCONST)
123
    {
124
      // Do nothing;
125
    }
126
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
127
             dt.derivation < derivation &&
128
             derivation >= DERIVATION_SYSCONST)
129
    {
130
      set(dt);
131
    }
132
    else
133
    {
134
      // Cannot apply conversion
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
135
      set(0, DERIVATION_NONE);
136
      return true;
584.4.1 by Monty Taylor
Split out DTCollation.
137
    }
138
  }
139
  else if (derivation < dt.derivation)
140
  {
141
    // Do nothing
142
  }
143
  else if (dt.derivation < derivation)
144
  {
145
    set(dt);
146
  }
147
  else
148
  {
149
    if (collation == dt.collation)
150
    {
151
      // Do nothing
152
    }
153
    else
154
    {
155
      if (derivation == DERIVATION_EXPLICIT)
156
      {
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
157
        set(0, DERIVATION_NONE);
158
        return true;
584.4.1 by Monty Taylor
Split out DTCollation.
159
      }
160
      if (collation->state & MY_CS_BINSORT)
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
161
        return false;
584.4.1 by Monty Taylor
Split out DTCollation.
162
      if (dt.collation->state & MY_CS_BINSORT)
163
      {
164
        set(dt);
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
165
        return false;
584.4.1 by Monty Taylor
Split out DTCollation.
166
      }
862 by Brian Aker
Remove charset directory code.
167
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
584.4.1 by Monty Taylor
Split out DTCollation.
168
      set(bin, DERIVATION_NONE);
169
    }
170
  }
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
171
172
  return false;
584.4.1 by Monty Taylor
Split out DTCollation.
173
}
174
175
176
bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
177
{ set(dt1); return aggregate(dt2, flags); }
178
179
180
const char *DTCollation::derivation_name() const
181
{
182
  switch(derivation)
183
  {
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";
191
  }
192
}
193
194
195
bool agg_item_collations(DTCollation &c, const char *fname,
196
                         Item **av, uint32_t count,
197
                         uint32_t flags, int item_sep)
198
{
199
  uint32_t i;
200
  Item **arg;
201
  c.set(av[0]->collation);
202
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
203
  {
204
    if (c.aggregate((*arg)->collation, flags))
205
    {
206
      my_coll_agg_error(av, count, fname, item_sep);
207
      return true;
208
    }
209
  }
210
  if ((flags & MY_COLL_DISALLOW_NONE) &&
211
      c.derivation == DERIVATION_NONE)
212
  {
213
    my_coll_agg_error(av, count, fname, item_sep);
214
    return true;
215
  }
216
  return false;
217
}
218
219
220
bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
221
                                        Item **av, uint32_t count,
222
                                        uint32_t flags)
223
{
224
  return (agg_item_collations(c, fname, av, count,
225
                              flags | MY_COLL_DISALLOW_NONE, 1));
226
}
227
228
229
bool agg_item_charsets(DTCollation &coll, const char *fname,
230
                       Item **args, uint32_t nargs, uint32_t flags,
231
                       int item_sep)
232
{
233
  Item **arg, *safe_args[2];
234
235
  memset(safe_args, 0, sizeof(safe_args));
236
237
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
238
    return true;
239
240
  /*
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.
246
  */
247
  if (nargs >=2 && nargs <= 3)
248
  {
249
    safe_args[0]= args[0];
250
    safe_args[1]= args[item_sep];
251
  }
252
253
  Session *session= current_session;
254
  bool res= false;
255
  uint32_t i;
256
257
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
258
  {
259
    Item* conv;
260
    uint32_t dummy_offset;
261
    if (!String::needs_conversion(0, (*arg)->collation.collation,
262
                                  coll.collation,
263
                                  &dummy_offset))
264
      continue;
265
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
266
    if (!(conv= (*arg)->safe_charset_converter(coll.collation)))
584.4.1 by Monty Taylor
Split out DTCollation.
267
    {
268
      if (nargs >=2 && nargs <= 3)
269
      {
270
        /* restore the original arguments for better error message */
271
        args[0]= safe_args[0];
272
        args[item_sep]= safe_args[1];
273
      }
274
      my_coll_agg_error(args, nargs, fname, item_sep);
275
      res= true;
276
      break; // we cannot return here, we need to restore "arena".
277
    }
278
    if ((*arg)->type() == Item::FIELD_ITEM)
279
      ((Item_field *)(*arg))->no_const_subst= 1;
280
    /*
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
288
      rollback.
289
    */
290
    session->change_item_tree(arg, conv);
291
    /*
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
294
    */
295
    conv->fix_fields(session, arg);
296
  }
297
298
  return res;
299
}
300
301
302
void my_coll_agg_error(DTCollation &c1,
303
                       DTCollation &c2, const char *fname)
304
{
305
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
306
           c1.collation->name,c1.derivation_name(),
307
           c2.collation->name,c2.derivation_name(),
308
           fname);
309
}
310
311
312
void my_coll_agg_error(DTCollation &c1,
313
                       DTCollation &c2,
314
                       DTCollation &c3,
315
                       const char *fname)
316
{
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(),
321
           fname);
322
}
323
324
325
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
326
                       int item_sep)
327
{
328
  if (count == 2)
329
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
330
  else if (count == 3)
331
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
332
                      args[2*item_sep]->collation, fname);
333
  else
334
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
335
}
336