~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/dtcollation.cc

  • Committer: Monty Taylor
  • Date: 2009-04-14 19:16:51 UTC
  • mto: (997.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 994.
  • Revision ID: mordred@inaugust.com-20090414191651-ltbww6hpqks8k7qk
Clarified instructions in README.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
3
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
4
 *  Copyright (C) 2008 Sun Microsystems
5
5
 *
6
6
 *  This program is free software; you can redistribute it and/or modify
7
7
 *  it under the terms of the GNU General Public License as published by
17
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
18
 */
19
19
 
20
 
#include "config.h"
 
20
#include <drizzled/server_includes.h>
21
21
#include <drizzled/dtcollation.h>
22
22
 
23
23
#include <drizzled/definitions.h>
24
 
#include "drizzled/internal/my_sys.h"
25
 
#include "drizzled/charset_info.h"
 
24
#include <mysys/my_sys.h>
 
25
#include <mystrings/m_ctype.h>
26
26
#include <drizzled/error.h>
27
27
#include <drizzled/function/str/conv_charset.h>
28
28
#include <drizzled/session.h>
29
 
#include "drizzled/charset.h"
30
 
 
31
 
namespace drizzled
 
29
 
 
30
 
 
31
static bool
 
32
left_is_superset(const DTCollation &left, const DTCollation &right)
32
33
{
 
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))))
 
39
    return true;
 
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))))
 
45
    return true;
 
46
  /* Disallow conversion otherwise */
 
47
  return false;
 
48
}
 
49
 
33
50
 
34
51
DTCollation::DTCollation()
35
52
{
36
53
  collation= &my_charset_bin;
37
54
  derivation= DERIVATION_NONE;
 
55
  repertoire= MY_REPERTOIRE_UNICODE30;
38
56
}
39
57
 
40
58
 
43
61
{
44
62
  collation= collation_arg;
45
63
  derivation= derivation_arg;
 
64
  set_repertoire_from_charset(collation_arg);
 
65
}
 
66
void DTCollation::set_repertoire_from_charset(const CHARSET_INFO * const cs)
 
67
{
 
68
  repertoire= cs->state & MY_CS_PUREASCII ?
 
69
    MY_REPERTOIRE_ASCII : MY_REPERTOIRE_UNICODE30;
46
70
}
47
71
 
48
72
 
50
74
{
51
75
  collation= dt.collation;
52
76
  derivation= dt.derivation;
 
77
  repertoire= dt.repertoire;
53
78
}
54
79
 
55
80
 
58
83
{
59
84
  collation= collation_arg;
60
85
  derivation= derivation_arg;
 
86
  set_repertoire_from_charset(collation_arg);
 
87
}
 
88
 
 
89
 
 
90
void DTCollation::set(const CHARSET_INFO * const collation_arg,
 
91
                      Derivation derivation_arg,
 
92
                      uint32_t repertoire_arg)
 
93
{
 
94
  collation= collation_arg;
 
95
  derivation= derivation_arg;
 
96
  repertoire= repertoire_arg;
61
97
}
62
98
 
63
99
 
64
100
void DTCollation::set(const CHARSET_INFO * const collation_arg)
65
101
{
66
102
  collation= collation_arg;
 
103
  set_repertoire_from_charset(collation_arg);
67
104
}
68
105
 
69
106
 
104
141
      }
105
142
    }
106
143
    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))))
 
144
             left_is_superset(*this, dt))
111
145
    {
112
146
      // Do nothing
113
147
    }
114
148
    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))))
 
149
             left_is_superset(dt, *this))
119
150
    {
120
151
      set(dt);
121
152
    }
134
165
    else
135
166
    {
136
167
      // Cannot apply conversion
137
 
      set(0, DERIVATION_NONE);
138
 
      return true;
 
168
      set(0, DERIVATION_NONE, 0);
 
169
      return 1;
139
170
    }
140
171
  }
141
172
  else if (derivation < dt.derivation)
156
187
    {
157
188
      if (derivation == DERIVATION_EXPLICIT)
158
189
      {
159
 
        set(0, DERIVATION_NONE);
160
 
        return true;
 
190
        set(0, DERIVATION_NONE, 0);
 
191
        return 1;
161
192
      }
162
193
      if (collation->state & MY_CS_BINSORT)
163
 
        return false;
 
194
        return 0;
164
195
      if (dt.collation->state & MY_CS_BINSORT)
165
196
      {
166
197
        set(dt);
167
 
        return false;
 
198
        return 0;
168
199
      }
169
200
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
170
201
      set(bin, DERIVATION_NONE);
171
202
    }
172
203
  }
173
 
 
174
 
  return false;
 
204
  repertoire|= dt.repertoire;
 
205
  return 0;
175
206
}
176
207
 
177
208
 
232
263
                       Item **args, uint32_t nargs, uint32_t flags,
233
264
                       int item_sep)
234
265
{
 
266
  Item **arg, *safe_args[2];
 
267
 
 
268
  memset(safe_args, 0, sizeof(safe_args));
 
269
 
235
270
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
236
271
    return true;
237
272
 
238
 
  return false;
 
273
  /*
 
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.
 
279
  */
 
280
  if (nargs >=2 && nargs <= 3)
 
281
  {
 
282
    safe_args[0]= args[0];
 
283
    safe_args[1]= args[item_sep];
 
284
  }
 
285
 
 
286
  Session *session= current_session;
 
287
  bool res= false;
 
288
  uint32_t i;
 
289
 
 
290
  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
 
291
  {
 
292
    Item* conv;
 
293
    uint32_t dummy_offset;
 
294
    if (!String::needs_conversion(0, (*arg)->collation.collation,
 
295
                                  coll.collation,
 
296
                                  &dummy_offset))
 
297
      continue;
 
298
 
 
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);
 
302
 
 
303
    if (!conv)
 
304
    {
 
305
      if (nargs >=2 && nargs <= 3)
 
306
      {
 
307
        /* restore the original arguments for better error message */
 
308
        args[0]= safe_args[0];
 
309
        args[item_sep]= safe_args[1];
 
310
      }
 
311
      my_coll_agg_error(args, nargs, fname, item_sep);
 
312
      res= true;
 
313
      break; // we cannot return here, we need to restore "arena".
 
314
    }
 
315
    if ((*arg)->type() == Item::FIELD_ITEM)
 
316
      ((Item_field *)(*arg))->no_const_subst= 1;
 
317
    /*
 
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
 
325
      rollback.
 
326
    */
 
327
    session->change_item_tree(arg, conv);
 
328
    /*
 
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
 
331
    */
 
332
    conv->fix_fields(session, arg);
 
333
  }
 
334
 
 
335
  return res;
239
336
}
240
337
 
241
338
 
274
371
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
275
372
}
276
373
 
277
 
} /* namespace drizzled */