~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
20
#ifndef DRIZZLED_DTCOLLATION_H
21
#define DRIZZLED_DTCOLLATION_H
22
23
#include <drizzled/definitions.h>
24
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
25
namespace drizzled
26
{
27
584.4.1 by Monty Taylor
Split out DTCollation.
28
class Item;
29
typedef struct charset_info_st CHARSET_INFO;
30
31
32
class DTCollation {
33
public:
34
  const CHARSET_INFO *collation;
35
  enum Derivation derivation;
36
37
  DTCollation();
38
  DTCollation(const CHARSET_INFO * const collation_arg,
39
              Derivation derivation_arg);
40
  void set(DTCollation &dt);
41
  void set(const CHARSET_INFO * const collation_arg,
42
           Derivation derivation_arg);
43
  void set(const CHARSET_INFO * const collation_arg);
44
  void set(Derivation derivation_arg);
45
  bool set(DTCollation &dt1, DTCollation &dt2, uint32_t flags= 0);
46
47
/**
48
  Aggregate two collations together taking
49
  into account their coercibility (aka derivation):.
50
51
  0 == DERIVATION_EXPLICIT  - an explicitly written COLLATE clause @n
52
  1 == DERIVATION_NONE      - a mix of two different collations @n
53
  2 == DERIVATION_IMPLICIT  - a column @n
54
  3 == DERIVATION_COERCIBLE - a string constant.
55
56
  The most important rules are:
57
  -# If collations are the same:
58
  chose this collation, and the strongest derivation.
59
  -# If collations are different:
60
  - Character sets may differ, but only if conversion without
61
  data loss is possible. The caller provides flags whether
62
  character set conversion attempts should be done. If no
63
  flags are substituted, then the character sets must be the same.
64
  Currently processed flags are:
65
  MY_COLL_ALLOW_SUPERSET_CONV  - allow conversion to a superset
66
  MY_COLL_ALLOW_COERCIBLE_CONV - allow conversion of a coercible value
67
  - two EXPLICIT collations produce an error, e.g. this is wrong:
68
  CONCAT(expr1 collate latin1_swedish_ci, expr2 collate latin1_german_ci)
69
  - the side with smaller derivation value wins,
70
  i.e. a column is stronger than a string constant,
71
  an explicit COLLATE clause is stronger than a column.
72
  - if derivations are the same, we have DERIVATION_NONE,
73
  we'll wait for an explicit COLLATE clause which possibly can
74
  come from another argument later: for example, this is valid,
75
  but we don't know yet when collecting the first two arguments:
76
     @code
77
       CONCAT(latin1_swedish_ci_column,
78
              latin1_german1_ci_column,
79
              expr COLLATE latin1_german2_ci)
80
  @endcode
81
*/
82
83
  bool aggregate(DTCollation &dt, uint32_t flags= 0);
84
85
  const char *derivation_name() const;
86
87
};
88
89
90
bool agg_item_collations(DTCollation &c, const char *name,
91
                         Item **items, uint32_t nitems,
92
                         uint32_t flags, int item_sep);
93
bool agg_item_collations_for_comparison(DTCollation &c, const char *name,
94
                                        Item **items, uint32_t nitems,
95
                                        uint32_t flags);
96
1578.6.4 by Brian Aker
Factor out conversion of charset (since we don't have more then one...).
97
/*
98
99
 @note In Drizzle we have just one charset, so no conversion is required (though collation may).
100
584.4.1 by Monty Taylor
Split out DTCollation.
101
  Collect arguments' character sets together.
102
103
  We allow to apply automatic character set conversion in some cases.
104
  The conditions when conversion is possible are:
105
  - arguments A and B have different charsets
106
  - A wins according to coercibility rules
107
    (i.e. a column is stronger than a string constant,
108
     an explicit COLLATE clause is stronger than a column)
109
  - character set of A is either superset for character set of B,
110
    or B is a string constant which can be converted into the
111
    character set of A without data loss.
112
113
  If all of the above is true, then it's possible to convert
114
  B into the character set of A, and then compare according
115
  to the collation of A.
116
117
  For functions with more than two arguments:
118
  @code
119
    collect(A,B,C) ::= collect(collect(A,B),C)
120
  @endcode
121
  Since this function calls Session::change_item_tree() on the passed Item **
122
  pointers, it is necessary to pass the original Item **'s, not copies.
123
  Otherwise their values will not be properly restored (see BUG#20769).
124
  If the items are not consecutive (eg. args[2] and args[5]), use the
125
  item_sep argument, ie.
126
  @code
127
    agg_item_charsets(coll, fname, &args[2], 2, flags, 3)
128
  @endcode
129
*/
130
bool agg_item_charsets(DTCollation &c, const char *name,
131
                       Item **items, uint32_t nitems, uint32_t flags,
132
                       int item_sep);
133
134
135
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, const char *fname);
136
void my_coll_agg_error(DTCollation &c1, DTCollation &c2, DTCollation &c3,
137
                       const char *fname);
138
void my_coll_agg_error(Item** args, uint32_t count, const char *fname,
139
                       int item_sep);
140
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
141
} /* namespace drizzled */
142
584.4.1 by Monty Taylor
Split out DTCollation.
143
#endif /* DRIZZLED_DTCOLLATION_H */