-
Notifications
You must be signed in to change notification settings - Fork 4k
/
Copy pathcharacter_sets.cc
181 lines (148 loc) · 7.57 KB
/
character_sets.cc
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
/* Copyright (c) 2014, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
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, version 2.0, 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 "sql/dd/impl/tables/character_sets.h"
#include <assert.h>
#include <stddef.h>
#include <new>
#include <set>
#include <vector>
#include "my_sys.h"
#include "mysql/strings/m_ctype.h"
#include "sql/dd/cache/dictionary_client.h" // dd::cache::Dictionary_...
#include "sql/dd/dd.h" // dd::create_object
#include "sql/dd/impl/cache/storage_adapter.h" // Storage_adapter
#include "sql/dd/impl/raw/object_keys.h" // Global_name_key
#include "sql/dd/impl/raw/raw_record.h"
#include "sql/dd/impl/tables/dd_properties.h" // TARGET_DD_VERSION
#include "sql/dd/impl/types/charset_impl.h" // dd::Charset_impl
#include "sql/dd/impl/types/object_table_definition_impl.h"
#include "sql/dd/object_id.h"
#include "sql/dd/types/charset.h"
#include "sql/item_create.h"
#include "sql/sql_class.h" // THD
namespace dd {
namespace tables {
const Character_sets &Character_sets::instance() {
static Character_sets *s_instance = new Character_sets();
return *s_instance;
}
///////////////////////////////////////////////////////////////////////////
const CHARSET_INFO *Character_sets::name_collation() {
return &my_charset_utf8mb3_general_ci;
}
///////////////////////////////////////////////////////////////////////////
Character_sets::Character_sets() {
m_target_def.set_table_name("character_sets");
m_target_def.add_field(FIELD_ID, "FIELD_ID",
"id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT");
m_target_def.add_field(FIELD_NAME, "FIELD_NAME",
"name VARCHAR(64) NOT NULL COLLATE " +
String_type(name_collation()->m_coll_name));
m_target_def.add_field(FIELD_DEFAULT_COLLATION_ID,
"FIELD_DEFAULT_COLLATION_ID",
"default_collation_id BIGINT UNSIGNED NOT NULL");
m_target_def.add_field(FIELD_COMMENT, "FIELD_COMMENT",
"comment VARCHAR(2048)"
" COLLATE utf8mb3_general_ci NOT NULL");
m_target_def.add_field(FIELD_MB_MAX_LENGTH, "FIELD_MB_MAX_LENGTH",
"mb_max_length INT UNSIGNED NOT NULL");
m_target_def.add_field(FIELD_OPTIONS, "FIELD_OPTIONS", "options MEDIUMTEXT");
m_target_def.add_index(INDEX_PK_ID, "INDEX_PK_ID", "PRIMARY KEY(id)");
m_target_def.add_index(INDEX_UK_NAME, "INDEX_UK_NAME", "UNIQUE KEY(name)");
m_target_def.add_index(INDEX_UK_DEFAULT_COLLATION_ID,
"INDEX_UK_DEFAULT_COLLATION_ID",
"UNIQUE KEY(default_collation_id)");
m_target_def.add_foreign_key(FK_DEFAULT_COLLATION_ID,
"FK_DEFAULT_COLLATION_ID",
"FOREIGN KEY (default_collation_id) "
"REFERENCES collations(id)");
}
///////////////////////////////////////////////////////////////////////////
// The table is populated when the server is started, unless it is
// started in read only mode.
bool Character_sets::populate(THD *thd) const {
// Obtain a list of the previously stored charsets.
cache::Dictionary_client::Auto_releaser releaser(thd->dd_client());
std::vector<const Charset *> prev_cset;
if (thd->dd_client()->fetch_global_components(&prev_cset)) return true;
std::set<Object_id> prev_cset_ids;
for (const Charset *cset : prev_cset) prev_cset_ids.insert(cset->id());
// We have an outer loop identifying the primary collations, i.e.,
// the collations which are default for some character set. Then,
// the character set of each primary collation is stored in an entry
// in the dd.character_sets table. This means that if there are
// collations referring to a character set which has no default
// collation, we will not have an entry for this character set in
// the dd.character_sets table. This also means that a given character
// set can have only one primary collation, since character set identity
// is given by the character set name, and we have a unique key on the
// character set name in the dd.character_sets table. Populating the
// dd.collations table follows a similar pattern, but has an additional
// inner loop adding the actual collations referring to the character
// sets. Each character set is stored with the id (primary key) of its
// corresponding primary collation as the id (primary key).
Charset_impl *new_charset = create_object<Charset_impl>();
bool error = false;
for (int internal_charset_id = 0;
internal_charset_id < MY_ALL_CHARSETS_SIZE && !error;
internal_charset_id++) {
const CHARSET_INFO *cs = all_charsets[internal_charset_id];
if (cs && (cs->state & MY_CS_PRIMARY) && (cs->state & MY_CS_AVAILABLE) &&
!(cs->state & MY_CS_HIDDEN)) {
// Remove the id from the set of non-updated old ids.
prev_cset_ids.erase(cs->number);
// The character set is stored on the same id as its primary collation
new_charset->set_id(cs->number);
new_charset->set_name(cs->csname);
new_charset->set_default_collation_id(cs->number);
new_charset->set_mb_max_length(cs->mbmaxlen);
new_charset->set_comment(cs->comment ? cs->comment : "");
// If the charset exists, it will be updated; otherwise,
// it will be inserted.
error = cache::Storage_adapter::instance()->store(
thd, static_cast<Charset *>(new_charset));
}
}
delete new_charset;
// The remaining ids in the prev_cset_ids set were not updated, and must
// therefore be deleted from the DD since they are not supported anymore.
for (std::set<Object_id>::const_iterator del_it = prev_cset_ids.begin();
del_it != prev_cset_ids.end(); ++del_it) {
const Charset *del_cset = nullptr;
if (thd->dd_client()->acquire(*del_it, &del_cset)) return true;
assert(del_cset);
if (thd->dd_client()->drop(del_cset)) return true;
}
return error;
}
///////////////////////////////////////////////////////////////////////////
/* purecov: begin deadcode */
Charset *Character_sets::create_entity_object(const Raw_record &) const {
return new (std::nothrow) Charset_impl();
}
/* purecov: end */
///////////////////////////////////////////////////////////////////////////
bool Character_sets::update_object_key(Global_name_key *key,
const String_type &charset_name) {
key->update(FIELD_NAME, charset_name, name_collation());
return false;
}
///////////////////////////////////////////////////////////////////////////
} // namespace tables
} // namespace dd