~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "config.h"
#include <drizzled/dtcollation.h>

#include <drizzled/definitions.h>
#include "drizzled/internal/my_sys.h"
#include "drizzled/charset_info.h"
#include <drizzled/error.h>
#include <drizzled/function/str/conv_charset.h>
#include <drizzled/session.h>
#include "drizzled/charset.h"

namespace drizzled
{

DTCollation::DTCollation()
{
  collation= &my_charset_bin;
  derivation= DERIVATION_NONE;
}


DTCollation::DTCollation(const CHARSET_INFO * const collation_arg,
                         Derivation derivation_arg)
{
  collation= collation_arg;
  derivation= derivation_arg;
}


void DTCollation::set(DTCollation &dt)
{
  collation= dt.collation;
  derivation= dt.derivation;
}


void DTCollation::set(const CHARSET_INFO * const collation_arg,
                      Derivation derivation_arg)
{
  collation= collation_arg;
  derivation= derivation_arg;
}


void DTCollation::set(const CHARSET_INFO * const collation_arg)
{
  collation= collation_arg;
}


void DTCollation::set(Derivation derivation_arg)
{
  derivation= derivation_arg;
}


bool DTCollation::aggregate(DTCollation &dt, uint32_t flags)
{
  if (!my_charset_same(collation, dt.collation))
  {
    /*
      We do allow to use binary strings (like BLOBS)
      together with character strings.
      Binaries have more precedence than a character
      string of the same derivation.
    */
    if (collation == &my_charset_bin)
    {
      if (derivation <= dt.derivation)
        ; // Do nothing
      else
      {
        set(dt);
      }
    }
    else if (dt.collation == &my_charset_bin)
    {
      if (dt.derivation <= derivation)
      {
        set(dt);
      }
      else
      {
        // Do nothing
      }
    }
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
             collation->state & MY_CS_UNICODE &&
             (derivation < dt.derivation ||
              (derivation == dt.derivation &&
               !(dt.collation->state & MY_CS_UNICODE))))
    {
      // Do nothing
    }
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
             dt.collation->state & MY_CS_UNICODE &&
             (dt.derivation < derivation ||
              (dt.derivation == derivation &&
               !(collation->state & MY_CS_UNICODE))))
    {
      set(dt);
    }
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
             derivation < dt.derivation &&
             dt.derivation >= DERIVATION_SYSCONST)
    {
      // Do nothing;
    }
    else if ((flags & MY_COLL_ALLOW_COERCIBLE_CONV) &&
             dt.derivation < derivation &&
             derivation >= DERIVATION_SYSCONST)
    {
      set(dt);
    }
    else
    {
      // Cannot apply conversion
      set(0, DERIVATION_NONE);
      return true;
    }
  }
  else if (derivation < dt.derivation)
  {
    // Do nothing
  }
  else if (dt.derivation < derivation)
  {
    set(dt);
  }
  else
  {
    if (collation == dt.collation)
    {
      // Do nothing
    }
    else
    {
      if (derivation == DERIVATION_EXPLICIT)
      {
        set(0, DERIVATION_NONE);
        return true;
      }
      if (collation->state & MY_CS_BINSORT)
        return false;
      if (dt.collation->state & MY_CS_BINSORT)
      {
        set(dt);
        return false;
      }
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
      set(bin, DERIVATION_NONE);
    }
  }

  return false;
}


bool DTCollation::set(DTCollation &dt1, DTCollation &dt2, uint32_t flags)
{ set(dt1); return aggregate(dt2, flags); }


const char *DTCollation::derivation_name() const
{
  switch(derivation)
  {
  case DERIVATION_IGNORABLE: return "IGNORABLE";
  case DERIVATION_COERCIBLE: return "COERCIBLE";
  case DERIVATION_IMPLICIT:  return "IMPLICIT";
  case DERIVATION_SYSCONST:  return "SYSCONST";
  case DERIVATION_EXPLICIT:  return "EXPLICIT";
  case DERIVATION_NONE:      return "NONE";
  default: return "UNKNOWN";
  }
}


bool agg_item_collations(DTCollation &c, const char *fname,
                         Item **av, uint32_t count,
                         uint32_t flags, int item_sep)
{
  uint32_t i;
  Item **arg;
  c.set(av[0]->collation);
  for (i= 1, arg= &av[item_sep]; i < count; i++, arg++)
  {
    if (c.aggregate((*arg)->collation, flags))
    {
      my_coll_agg_error(av, count, fname, item_sep);
      return true;
    }
  }
  if ((flags & MY_COLL_DISALLOW_NONE) &&
      c.derivation == DERIVATION_NONE)
  {
    my_coll_agg_error(av, count, fname, item_sep);
    return true;
  }
  return false;
}


bool agg_item_collations_for_comparison(DTCollation &c, const char *fname,
                                        Item **av, uint32_t count,
                                        uint32_t flags)
{
  return (agg_item_collations(c, fname, av, count,
                              flags | MY_COLL_DISALLOW_NONE, 1));
}


bool agg_item_charsets(DTCollation &coll, const char *fname,
                       Item **args, uint32_t nargs, uint32_t flags,
                       int item_sep)
{
  Item **arg, *safe_args[2];

  memset(safe_args, 0, sizeof(safe_args));

  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
    return true;

  /*
    For better error reporting: save the first and the second argument.
    We need this only if the the number of args is 3 or 2:
    - for a longer argument list, "Illegal mix of collations"
      doesn't display each argument's characteristics.
    - if nargs is 1, then this error cannot happen.
  */
  if (nargs >=2 && nargs <= 3)
  {
    safe_args[0]= args[0];
    safe_args[1]= args[item_sep];
  }

  Session *session= current_session;
  bool res= false;
  uint32_t i;

  for (i= 0, arg= args; i < nargs; i++, arg+= item_sep)
  {
    Item* conv;
    uint32_t dummy_offset;
    if (!String::needs_conversion(0, (*arg)->collation.collation,
                                  coll.collation,
                                  &dummy_offset))
      continue;

    if (!(conv= (*arg)->safe_charset_converter(coll.collation)))
    {
      if (nargs >=2 && nargs <= 3)
      {
        /* restore the original arguments for better error message */
        args[0]= safe_args[0];
        args[item_sep]= safe_args[1];
      }
      my_coll_agg_error(args, nargs, fname, item_sep);
      res= true;
      break; // we cannot return here, we need to restore "arena".
    }
    if ((*arg)->type() == Item::FIELD_ITEM)
      ((Item_field *)(*arg))->no_const_subst= 1;
    /*
      If in statement prepare, then we create a converter for two
      constant items, do it once and then reuse it.
      If we're in execution of a prepared statement, arena is NULL,
      and the conv was created in runtime memory. This can be
      the case only if the argument is a parameter marker ('?'),
      because for all true constants the charset converter has already
      been created in prepare. In this case register the change for
      rollback.
    */
    session->change_item_tree(arg, conv);
    /*
      We do not check conv->fixed, because Item_func_conv_charset which can
      be return by safe_charset_converter can't be fixed at creation
    */
    conv->fix_fields(session, arg);
  }

  return res;
}


void my_coll_agg_error(DTCollation &c1,
                       DTCollation &c2, const char *fname)
{
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
           c1.collation->name,c1.derivation_name(),
           c2.collation->name,c2.derivation_name(),
           fname);
}


void my_coll_agg_error(DTCollation &c1,
                       DTCollation &c2,
                       DTCollation &c3,
                       const char *fname)
{
  my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
           c1.collation->name,c1.derivation_name(),
           c2.collation->name,c2.derivation_name(),
           c3.collation->name,c3.derivation_name(),
           fname);
}


void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
                       int item_sep)
{
  if (count == 2)
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
  else if (count == 3)
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
                      args[2*item_sep]->collation, fname);
  else
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
}

} /* namespace drizzled */