~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
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
31
namespace drizzled
32
{
584.4.1 by Monty Taylor
Split out DTCollation.
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) &&
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
107
             collation->state & MY_CS_UNICODE &&
108
             (derivation < dt.derivation ||
109
              (derivation == dt.derivation &&
110
               !(dt.collation->state & MY_CS_UNICODE))))
584.4.1 by Monty Taylor
Split out DTCollation.
111
    {
112
      // Do nothing
113
    }
114
    else if ((flags & MY_COLL_ALLOW_SUPERSET_CONV) &&
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
115
             dt.collation->state & MY_CS_UNICODE &&
116
             (dt.derivation < derivation ||
117
              (dt.derivation == derivation &&
118
               !(collation->state & MY_CS_UNICODE))))
584.4.1 by Monty Taylor
Split out DTCollation.
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
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
137
      set(0, DERIVATION_NONE);
138
      return true;
584.4.1 by Monty Taylor
Split out DTCollation.
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
      {
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
159
        set(0, DERIVATION_NONE);
160
        return true;
584.4.1 by Monty Taylor
Split out DTCollation.
161
      }
162
      if (collation->state & MY_CS_BINSORT)
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
163
        return false;
584.4.1 by Monty Taylor
Split out DTCollation.
164
      if (dt.collation->state & MY_CS_BINSORT)
165
      {
166
        set(dt);
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
167
        return false;
584.4.1 by Monty Taylor
Split out DTCollation.
168
      }
862 by Brian Aker
Remove charset directory code.
169
      const CHARSET_INFO * const bin= get_charset_by_csname(collation->csname, MY_CS_BINSORT);
584.4.1 by Monty Taylor
Split out DTCollation.
170
      set(bin, DERIVATION_NONE);
171
    }
172
  }
1022.1.3 by Brian Aker
Force UTF8 (remove the bits for looking for ascii).
173
174
  return false;
584.4.1 by Monty Taylor
Split out DTCollation.
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
  if (agg_item_collations(coll, fname, args, nargs, flags, item_sep))
236
    return true;
237
1578.6.4 by Brian Aker
Factor out conversion of charset (since we don't have more then one...).
238
  return false;
584.4.1 by Monty Taylor
Split out DTCollation.
239
}
240
241
242
void my_coll_agg_error(DTCollation &c1,
243
                       DTCollation &c2, const char *fname)
244
{
245
  my_error(ER_CANT_AGGREGATE_2COLLATIONS,MYF(0),
246
           c1.collation->name,c1.derivation_name(),
247
           c2.collation->name,c2.derivation_name(),
248
           fname);
249
}
250
251
252
void my_coll_agg_error(DTCollation &c1,
253
                       DTCollation &c2,
254
                       DTCollation &c3,
255
                       const char *fname)
256
{
257
  my_error(ER_CANT_AGGREGATE_3COLLATIONS,MYF(0),
258
           c1.collation->name,c1.derivation_name(),
259
           c2.collation->name,c2.derivation_name(),
260
           c3.collation->name,c3.derivation_name(),
261
           fname);
262
}
263
264
265
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
266
                       int item_sep)
267
{
268
  if (count == 2)
269
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation, fname);
270
  else if (count == 3)
271
    my_coll_agg_error(args[0]->collation, args[item_sep]->collation,
272
                      args[2*item_sep]->collation, fname);
273
  else
274
    my_error(ER_CANT_AGGREGATE_NCOLLATIONS,MYF(0),fname);
275
}
276
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
277
} /* namespace drizzled */