~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/dtcollation.cc

  • Committer: Brian Aker
  • Date: 2010-05-27 01:25:56 UTC
  • mfrom: (1567.1.4 new-staging)
  • Revision ID: brian@gaz-20100527012556-5zgkirkl7swbigd6
Merge of Brian, Paul. PBXT compile issue, and test framework cleanup. 

Show diffs side-by-side

added added

removed removed

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