1
by brian
clean slate |
1 |
/* Copyright (C) 2000-2003 MySQL AB
|
2 |
||
3 |
This program is free software; you can redistribute it and/or modify
|
|
4 |
it under the terms of the GNU General Public License as published by
|
|
5 |
the Free Software Foundation; version 2 of the License.
|
|
6 |
||
7 |
This program is distributed in the hope that it will be useful,
|
|
8 |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
GNU General Public License for more details.
|
|
11 |
||
12 |
You should have received a copy of the GNU General Public License
|
|
13 |
along with this program; if not, write to the Free Software
|
|
1802.10.2
by Monty Taylor
Update all of the copyright headers to include the correct address. |
14 |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
|
1
by brian
clean slate |
15 |
|
16 |
||
17 |
/* create and drop of databases */
|
|
1241.9.36
by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h. |
18 |
#include "config.h" |
1259.3.7
by Monty Taylor
Fixed OSX build error. |
19 |
|
20 |
#include <fcntl.h> |
|
21 |
#include <sys/stat.h> |
|
22 |
#include <sys/types.h> |
|
23 |
||
24 |
#include <set> |
|
318
by Brian Aker
Modified sql_db to now use Google Proto buffers instead of MySQL type. |
25 |
#include <string> |
26 |
#include <fstream> |
|
1259.3.7
by Monty Taylor
Fixed OSX build error. |
27 |
|
988.1.1
by Jay Pipes
Changes libserialize to libdrizzledmessage per ML discussion. All GPB messages are now in the drizzled::message namespace. |
28 |
#include <drizzled/message/schema.pb.h> |
1271.5.3
by Tim Penhey
change the include files |
29 |
#include "drizzled/error.h" |
538
by Monty Taylor
Moved gettext.h into drizzled in anticipation of the new client lib. |
30 |
#include <drizzled/gettext.h> |
1241.9.57
by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined. |
31 |
#include <drizzled/my_hash.h> |
1241.9.64
by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal. |
32 |
#include "drizzled/internal/m_string.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. |
33 |
#include <drizzled/session.h> |
34 |
#include <drizzled/db.h> |
|
35 |
#include <drizzled/sql_base.h> |
|
670.2.4
by Monty Taylor
Removed more stuff from the headers. |
36 |
#include <drizzled/lock.h> |
722.4.1
by Mark Atwood
integrate errmsg plugin into sql_print_* functions |
37 |
#include <drizzled/errmsg_print.h> |
1336.2.2
by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers. |
38 |
#include <drizzled/transaction_services.h> |
1089.10.1
by Stewart Smith
fix SHOW CREATE DATABASE for default collation. Move database metadata reading code around to be a bit more sane. |
39 |
#include <drizzled/message/schema.pb.h> |
1241.9.12
by Monty Taylor
Trims more out of server_includes.h. |
40 |
#include "drizzled/sql_table.h" |
1273.20.3
by Brian Aker
Fixing charset return. |
41 |
#include "drizzled/plugin/storage_engine.h" |
1317.2.1
by Monty Taylor
Removed an unused variable. |
42 |
#include "drizzled/plugin/authorization.h" |
1241.9.28
by Monty Taylor
Removed global_charset_info.h from server_includes.h |
43 |
#include "drizzled/global_charset_info.h" |
1241.9.31
by Monty Taylor
Moved global pthread variables into their own header. |
44 |
#include "drizzled/pthread_globals.h" |
1241.9.57
by Monty Taylor
Oy. Bigger change than I normally like - but this stuff is all intertwined. |
45 |
#include "drizzled/charset.h" |
1241.9.28
by Monty Taylor
Removed global_charset_info.h from server_includes.h |
46 |
|
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
47 |
#include <boost/thread/mutex.hpp> |
48 |
||
1241.9.64
by Monty Taylor
Moved remaining non-public portions of mysys and mystrings to drizzled/internal. |
49 |
#include "drizzled/internal/my_sys.h" |
988.1.6
by Jay Pipes
Removed old protobuf_replicator plugin, fixed up db.cc and other files to use new |
50 |
|
1
by brian
clean slate |
51 |
#define MAX_DROP_TABLE_Q_LEN 1024
|
52 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
53 |
using namespace std; |
54 |
||
55 |
namespace drizzled |
|
56 |
{
|
|
57 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
58 |
static void change_db_impl(Session *session); |
59 |
static void change_db_impl(Session *session, SchemaIdentifier &schema_identifier); |
|
820.1.11
by Stewart Smith
re-introduce db.opt, but with parsing it from disk instead of in process cache with mutex. |
60 |
|
1
by brian
clean slate |
61 |
/*
|
62 |
Create a database
|
|
63 |
||
64 |
SYNOPSIS
|
|
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
65 |
create_db()
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
66 |
session Thread handler
|
1
by brian
clean slate |
67 |
db Name of database to create
|
68 |
Function assumes that this is already validated.
|
|
69 |
create_info Database create options (like character set)
|
|
70 |
||
71 |
SIDE-EFFECTS
|
|
72 |
1. Report back to client that command succeeded (my_ok)
|
|
73 |
2. Report errors to client
|
|
74 |
3. Log event to binary log
|
|
75 |
||
76 |
RETURN VALUES
|
|
51.1.51
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
77 |
false ok
|
78 |
true Error
|
|
1
by brian
clean slate |
79 |
|
80 |
*/
|
|
81 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
82 |
bool create_db(Session *session, const message::Schema &schema_message, const bool is_if_not_exists) |
1
by brian
clean slate |
83 |
{
|
1336.2.2
by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers. |
84 |
TransactionServices &transaction_services= TransactionServices::singleton(); |
1039.1.3
by Brian Aker
Remove dead bits from db create/rm |
85 |
bool error= false; |
1235.4.14
by Stewart Smith
use message::Schema in mysql_create_db instead of HA_CREATE_INFO |
86 |
|
1
by brian
clean slate |
87 |
/*
|
88 |
Do not create database if another thread is holding read lock.
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
89 |
Wait for global read lock before acquiring session->catalog()->schemaLock().
|
1
by brian
clean slate |
90 |
After wait_if_global_read_lock() we have protection against another
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
91 |
global read lock. If we would acquire session->catalog()->schemaLock() first,
|
1
by brian
clean slate |
92 |
another thread could step in and get the global read lock before we
|
93 |
reach wait_if_global_read_lock(). If this thread tries the same as we
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
94 |
(admin a db), it would then go and wait on session->catalog()->schemaLock()...
|
1
by brian
clean slate |
95 |
Furthermore wait_if_global_read_lock() checks if the current thread
|
96 |
has the global read lock and refuses the operation with
|
|
97 |
ER_CANT_UPDATE_WITH_READLOCK if applicable.
|
|
98 |
*/
|
|
1910.2.3
by Brian Aker
Second pass on move code to global lock encapsulation. |
99 |
if (session->wait_if_global_read_lock(false, true)) |
1
by brian
clean slate |
100 |
{
|
1273.19.25
by Brian Aker
createSchema() now works via SE interface. |
101 |
return false; |
1
by brian
clean slate |
102 |
}
|
103 |
||
1273.19.28
by Brian Aker
More cleanup on ALTER SCHEMA. Hey! MySQL never had errors on half of it... |
104 |
assert(schema_message.has_name()); |
105 |
assert(schema_message.has_collation()); |
|
106 |
||
1273.19.25
by Brian Aker
createSchema() now works via SE interface. |
107 |
// @todo push this lock down into the engine
|
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
108 |
{
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
109 |
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock()); |
1
by brian
clean slate |
110 |
|
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
111 |
// Check to see if it exists already.
|
112 |
SchemaIdentifier schema_identifier(schema_message.name()); |
|
113 |
if (plugin::StorageEngine::doesSchemaExist(schema_identifier)) |
|
114 |
{
|
|
115 |
if (not is_if_not_exists) |
|
116 |
{
|
|
2041.3.15
by Brian Aker
Cleanup error usage around identifier usage. |
117 |
my_error(ER_DB_CREATE_EXISTS, schema_identifier); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
118 |
error= true; |
119 |
}
|
|
120 |
else
|
|
121 |
{
|
|
122 |
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE, |
|
123 |
ER_DB_CREATE_EXISTS, ER(ER_DB_CREATE_EXISTS), |
|
124 |
schema_message.name().c_str()); |
|
125 |
session->my_ok(); |
|
126 |
}
|
|
127 |
}
|
|
1856.2.8
by Joseph Daly
working alter, drop, create schema |
128 |
else if (not plugin::StorageEngine::createSchema(schema_message)) // Try to create it |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
129 |
{
|
130 |
my_error(ER_CANT_CREATE_DB, MYF(0), schema_message.name().c_str(), errno); |
|
1273.19.25
by Brian Aker
createSchema() now works via SE interface. |
131 |
error= true; |
132 |
}
|
|
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
133 |
else // Created ! |
1273.19.25
by Brian Aker
createSchema() now works via SE interface. |
134 |
{
|
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
135 |
transaction_services.createSchema(session, schema_message); |
136 |
session->my_ok(1); |
|
1273.19.25
by Brian Aker
createSchema() now works via SE interface. |
137 |
}
|
138 |
}
|
|
1910.2.2
by Brian Aker
First pass through the global lock refactor merge. |
139 |
session->startWaitingGlobalReadLock(); |
1273.19.25
by Brian Aker
createSchema() now works via SE interface. |
140 |
|
1039.1.3
by Brian Aker
Remove dead bits from db create/rm |
141 |
return error; |
1
by brian
clean slate |
142 |
}
|
143 |
||
144 |
||
145 |
/* db-name is already validated when we come here */
|
|
146 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
147 |
bool alter_db(Session *session, const message::Schema &schema_message) |
1
by brian
clean slate |
148 |
{
|
1336.2.2
by Jay Pipes
NO CODE CHANGES - Simply moves pieces of ReplicationServices to TransactionServices. Preparation for making the ReplicationServices component only responsible for communication between replicators, appliers, publishers, and subscribers. |
149 |
TransactionServices &transaction_services= TransactionServices::singleton(); |
1
by brian
clean slate |
150 |
|
151 |
/*
|
|
152 |
Do not alter database if another thread is holding read lock.
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
153 |
Wait for global read lock before acquiring session->catalog()->schemaLock().
|
1
by brian
clean slate |
154 |
After wait_if_global_read_lock() we have protection against another
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
155 |
global read lock. If we would acquire session->catalog()->schemaLock() first,
|
1
by brian
clean slate |
156 |
another thread could step in and get the global read lock before we
|
157 |
reach wait_if_global_read_lock(). If this thread tries the same as we
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
158 |
(admin a db), it would then go and wait on session->catalog()->schemaLock()...
|
1
by brian
clean slate |
159 |
Furthermore wait_if_global_read_lock() checks if the current thread
|
160 |
has the global read lock and refuses the operation with
|
|
161 |
ER_CANT_UPDATE_WITH_READLOCK if applicable.
|
|
162 |
*/
|
|
1910.2.3
by Brian Aker
Second pass on move code to global lock encapsulation. |
163 |
if ((session->wait_if_global_read_lock(false, true))) |
1273.19.26
by Brian Aker
Move Alter schema to SE interface. |
164 |
return false; |
1235.4.16
by Stewart Smith
use Schema proto for ALTER DATABASE/SCHEMA as well. Fix 'ALTER DATABASE COLLATE = foo'. Remove now obsolete fill_schema_message. HA_CREATE_INFO is no longer used in CREATE or ALTER SCHEMA. |
165 |
|
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
166 |
bool success; |
167 |
{
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
168 |
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock()); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
169 |
|
170 |
SchemaIdentifier schema_idenifier(schema_message.name()); |
|
171 |
if (not plugin::StorageEngine::doesSchemaExist(schema_idenifier)) |
|
172 |
{
|
|
2041.3.15
by Brian Aker
Cleanup error usage around identifier usage. |
173 |
my_error(ER_SCHEMA_DOES_NOT_EXIST, schema_idenifier); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
174 |
return false; |
175 |
}
|
|
176 |
||
177 |
/* Change options if current database is being altered. */
|
|
178 |
success= plugin::StorageEngine::alterSchema(schema_message); |
|
179 |
||
180 |
if (success) |
|
181 |
{
|
|
1921.4.13
by Brian Aker
Fix issue where session info might not be correct. |
182 |
transaction_services.rawStatement(session, *session->getQueryString()); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
183 |
session->my_ok(1); |
184 |
}
|
|
185 |
else
|
|
186 |
{
|
|
2041.3.15
by Brian Aker
Cleanup error usage around identifier usage. |
187 |
my_error(ER_ALTER_SCHEMA, schema_idenifier); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
188 |
}
|
189 |
}
|
|
1910.2.2
by Brian Aker
First pass through the global lock refactor merge. |
190 |
session->startWaitingGlobalReadLock(); |
1273.19.26
by Brian Aker
Move Alter schema to SE interface. |
191 |
|
192 |
return success; |
|
1
by brian
clean slate |
193 |
}
|
194 |
||
195 |
||
196 |
/*
|
|
197 |
Drop all tables in a database and the database itself
|
|
198 |
||
199 |
SYNOPSIS
|
|
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
200 |
rm_db()
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
201 |
session Thread handle
|
1
by brian
clean slate |
202 |
db Database name in the case given by user
|
203 |
It's already validated and set to lower case
|
|
204 |
(if needed) when we come here
|
|
205 |
if_exists Don't give error if database doesn't exists
|
|
206 |
silent Don't generate errors
|
|
207 |
||
208 |
RETURN
|
|
51.1.51
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
209 |
false ok (Database dropped)
|
1
by brian
clean slate |
210 |
ERROR Error
|
211 |
*/
|
|
212 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
213 |
bool rm_db(Session *session, SchemaIdentifier &schema_identifier, const bool if_exists) |
1
by brian
clean slate |
214 |
{
|
2041.3.6
by Brian Aker
Cleanup of drop table. |
215 |
bool error= false; |
1
by brian
clean slate |
216 |
|
217 |
/*
|
|
218 |
Do not drop database if another thread is holding read lock.
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
219 |
Wait for global read lock before acquiring session->catalog()->schemaLock().
|
1
by brian
clean slate |
220 |
After wait_if_global_read_lock() we have protection against another
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
221 |
global read lock. If we would acquire session->catalog()->schemaLock() first,
|
1
by brian
clean slate |
222 |
another thread could step in and get the global read lock before we
|
223 |
reach wait_if_global_read_lock(). If this thread tries the same as we
|
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
224 |
(admin a db), it would then go and wait on session->catalog()->schemaLock()...
|
1
by brian
clean slate |
225 |
Furthermore wait_if_global_read_lock() checks if the current thread
|
226 |
has the global read lock and refuses the operation with
|
|
227 |
ER_CANT_UPDATE_WITH_READLOCK if applicable.
|
|
228 |
*/
|
|
1910.2.3
by Brian Aker
Second pass on move code to global lock encapsulation. |
229 |
if (session->wait_if_global_read_lock(false, true)) |
1
by brian
clean slate |
230 |
{
|
2041.3.4
by Brian Aker
remove goto |
231 |
return true; |
1
by brian
clean slate |
232 |
}
|
233 |
||
2041.3.4
by Brian Aker
remove goto |
234 |
do
|
1
by brian
clean slate |
235 |
{
|
2060.4.2
by Brian Aker
A few small fixes, plus move the schema lock to the actual catalog. |
236 |
boost::mutex::scoped_lock scopedLock(session->catalog().schemaLock()); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
237 |
|
238 |
/* See if the schema exists */
|
|
239 |
if (not plugin::StorageEngine::doesSchemaExist(schema_identifier)) |
|
1309.1.8
by Brian Aker
Modest update to drop schema. |
240 |
{
|
1954.2.1
by Brian Aker
getSQLPath() modified to take a string so that we can const the table |
241 |
std::string path; |
242 |
schema_identifier.getSQLPath(path); |
|
243 |
||
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
244 |
if (if_exists) |
245 |
{
|
|
246 |
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE, |
|
247 |
ER_DB_DROP_EXISTS, ER(ER_DB_DROP_EXISTS), |
|
1954.2.1
by Brian Aker
getSQLPath() modified to take a string so that we can const the table |
248 |
path.c_str()); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
249 |
}
|
250 |
else
|
|
251 |
{
|
|
2041.3.6
by Brian Aker
Cleanup of drop table. |
252 |
error= true; |
2041.3.15
by Brian Aker
Cleanup error usage around identifier usage. |
253 |
my_error(ER_DB_DROP_EXISTS, schema_identifier); |
2041.3.4
by Brian Aker
remove goto |
254 |
break; |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
255 |
}
|
1309.1.8
by Brian Aker
Modest update to drop schema. |
256 |
}
|
257 |
else
|
|
1
by brian
clean slate |
258 |
{
|
2041.3.10
by Brian Aker
Put more of drop schema behind interface. |
259 |
error= plugin::StorageEngine::dropSchema(*session, schema_identifier); |
1
by brian
clean slate |
260 |
}
|
261 |
||
2041.3.4
by Brian Aker
remove goto |
262 |
} while (0); |
1685.2.14
by Brian Aker
Use boost for the main lock we use in schema creation/alteration. |
263 |
|
2041.3.11
by Brian Aker
Only change schemas if we DROP the schema. |
264 |
/*
|
265 |
If this database was the client's selected database, we silently
|
|
266 |
change the client's selected database to nothing (to have an empty
|
|
267 |
SELECT DATABASE() in the future). For this we free() session->db and set
|
|
268 |
it to 0.
|
|
269 |
*/
|
|
270 |
if (not error and schema_identifier.compare(*session->schema())) |
|
271 |
change_db_impl(session); |
|
272 |
||
1910.2.2
by Brian Aker
First pass through the global lock refactor merge. |
273 |
session->startWaitingGlobalReadLock(); |
1309.1.8
by Brian Aker
Modest update to drop schema. |
274 |
|
1259.3.3
by Monty Taylor
Removed last use of my_dir. |
275 |
return error; |
1
by brian
clean slate |
276 |
}
|
277 |
||
278 |
/**
|
|
279 |
@brief Change the current database and its attributes unconditionally.
|
|
280 |
||
520.1.22
by Brian Aker
Second pass of thd cleanup |
281 |
@param session thread handle
|
1
by brian
clean slate |
282 |
@param new_db_name database name
|
51.1.51
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
283 |
@param force_switch if force_switch is false, then the operation will fail if
|
1
by brian
clean slate |
284 |
|
285 |
- new_db_name is NULL or empty;
|
|
286 |
||
287 |
- OR new database name is invalid
|
|
288 |
(check_db_name() failed);
|
|
289 |
||
290 |
- OR user has no privilege on the new database;
|
|
291 |
||
292 |
- OR new database does not exist;
|
|
293 |
||
51.1.51
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
294 |
if force_switch is true, then
|
1
by brian
clean slate |
295 |
|
296 |
- if new_db_name is NULL or empty, the current
|
|
297 |
database will be NULL, @@collation_database will
|
|
298 |
be set to @@collation_server, the operation will
|
|
299 |
succeed.
|
|
300 |
||
301 |
- if new database name is invalid
|
|
302 |
(check_db_name() failed), the current database
|
|
303 |
will be NULL, @@collation_database will be set to
|
|
304 |
@@collation_server, but the operation will fail;
|
|
305 |
||
306 |
- user privileges will not be checked
|
|
520.1.21
by Brian Aker
THD -> Session rename |
307 |
(Session::db_access however is updated);
|
1
by brian
clean slate |
308 |
|
309 |
TODO: is this really the intention?
|
|
310 |
(see sp-security.test).
|
|
311 |
||
312 |
- if new database does not exist,the current database
|
|
313 |
will be NULL, @@collation_database will be set to
|
|
314 |
@@collation_server, a warning will be thrown, the
|
|
315 |
operation will succeed.
|
|
316 |
||
317 |
@details The function checks that the database name corresponds to a
|
|
318 |
valid and existent database, checks access rights and changes the current
|
|
319 |
database with database attributes (@@collation_database session variable,
|
|
520.1.21
by Brian Aker
THD -> Session rename |
320 |
Session::db_access).
|
1
by brian
clean slate |
321 |
|
322 |
This function is not the only way to switch the database that is
|
|
323 |
currently employed. When the replication slave thread switches the
|
|
520.1.22
by Brian Aker
Second pass of thd cleanup |
324 |
database before executing a query, it calls session->set_db directly.
|
1
by brian
clean slate |
325 |
However, if the query, in turn, uses a stored routine, the stored routine
|
326 |
will use this function, even if it's run on the slave.
|
|
327 |
||
328 |
This function allocates the name of the database on the system heap: this
|
|
329 |
is necessary to be able to uniformly change the database from any module
|
|
330 |
of the server. Up to 5.0 different modules were using different memory to
|
|
331 |
store the name of the database, and this led to memory corruption:
|
|
332 |
a stack pointer set by Stored Procedures was used by replication after
|
|
333 |
the stack address was long gone.
|
|
334 |
||
335 |
@return Operation status
|
|
51.1.51
by Jay Pipes
Removed/replaced DBUG symbols and standardized TRUE/FALSE |
336 |
@retval false Success
|
337 |
@retval true Error
|
|
1
by brian
clean slate |
338 |
*/
|
339 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
340 |
bool change_db(Session *session, SchemaIdentifier &schema_identifier) |
1
by brian
clean slate |
341 |
{
|
342 |
||
2008.1.1
by Brian Aker
Adding user identifier that makes use of a shared ptr to handle concurrency |
343 |
if (not plugin::Authorization::isAuthorized(session->user(), schema_identifier)) |
1317.2.2
by Monty Taylor
Prevent unauthorized users from changing schema. |
344 |
{
|
345 |
/* Error message is set in isAuthorized */
|
|
346 |
return true; |
|
347 |
}
|
|
348 |
||
1578.4.11
by Brian Aker
PAss through the code removing current_session |
349 |
if (not check_db_name(session, schema_identifier)) |
1
by brian
clean slate |
350 |
{
|
1954.2.1
by Brian Aker
getSQLPath() modified to take a string so that we can const the table |
351 |
std::string path; |
352 |
schema_identifier.getSQLPath(path); |
|
2041.3.15
by Brian Aker
Cleanup error usage around identifier usage. |
353 |
my_error(ER_WRONG_DB_NAME, schema_identifier); |
1
by brian
clean slate |
354 |
|
1014.3.3
by Brian Aker
Formating fix. |
355 |
return true; |
1
by brian
clean slate |
356 |
}
|
357 |
||
1415
by Brian Aker
Mass overhaul to use schema_identifier. |
358 |
if (not plugin::StorageEngine::doesSchemaExist(schema_identifier)) |
1
by brian
clean slate |
359 |
{
|
2041.3.15
by Brian Aker
Cleanup error usage around identifier usage. |
360 |
my_error(ER_BAD_DB_ERROR, schema_identifier); |
1309.1.9
by Brian Aker
Baby steps. |
361 |
|
362 |
/* The operation failed. */
|
|
363 |
||
364 |
return true; |
|
1
by brian
clean slate |
365 |
}
|
366 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
367 |
change_db_impl(session, schema_identifier); |
1
by brian
clean slate |
368 |
|
1014.3.3
by Brian Aker
Formating fix. |
369 |
return false; |
1
by brian
clean slate |
370 |
}
|
371 |
||
1273
by Brian Aker
Revert db patch. |
372 |
/**
|
373 |
@brief Internal implementation: switch current database to a valid one.
|
|
374 |
||
375 |
@param session Thread context.
|
|
376 |
@param new_db_name Name of the database to switch to. The function will
|
|
377 |
take ownership of the name (the caller must not free
|
|
378 |
the allocated memory). If the name is NULL, we're
|
|
379 |
going to switch to NULL db.
|
|
380 |
@param new_db_charset Character set of the new database.
|
|
381 |
*/
|
|
382 |
||
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
383 |
static void change_db_impl(Session *session, SchemaIdentifier &schema_identifier) |
1235.4.18
by Stewart Smith
replace check_db_dir_existence() with class DatabasePathName and a ::exists() method. |
384 |
{
|
1273
by Brian Aker
Revert db patch. |
385 |
/* 1. Change current database in Session. */
|
386 |
||
1415
by Brian Aker
Mass overhaul to use schema_identifier. |
387 |
#if 0
|
1273
by Brian Aker
Revert db patch. |
388 |
if (new_db_name == NULL)
|
389 |
{
|
|
390 |
/*
|
|
391 |
Session::set_db() does all the job -- it frees previous database name and
|
|
392 |
sets the new one.
|
|
393 |
*/
|
|
394 |
||
395 |
session->set_db(NULL, 0);
|
|
396 |
}
|
|
397 |
else
|
|
1415
by Brian Aker
Mass overhaul to use schema_identifier. |
398 |
#endif
|
1273
by Brian Aker
Revert db patch. |
399 |
{
|
400 |
/*
|
|
401 |
Here we already have a copy of database name to be used in Session. So,
|
|
402 |
we just call Session::reset_db(). Since Session::reset_db() does not releases
|
|
403 |
the previous database name, we should do it explicitly.
|
|
404 |
*/
|
|
405 |
||
1685.2.12
by Brian Aker
This fixes the lower casing of names from Schema even when we should not. |
406 |
session->set_db(schema_identifier.getSchemaName()); |
1273
by Brian Aker
Revert db patch. |
407 |
}
|
1235.4.18
by Stewart Smith
replace check_db_dir_existence() with class DatabasePathName and a ::exists() method. |
408 |
}
|
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
409 |
|
2026.2.1
by Monty Taylor
Renamed things prefixed mysql_ or mysqld_ |
410 |
static void change_db_impl(Session *session) |
1415
by Brian Aker
Mass overhaul to use schema_identifier. |
411 |
{
|
412 |
session->set_db(string()); |
|
413 |
}
|
|
414 |
||
1280.1.10
by Monty Taylor
Put everything in drizzled into drizzled namespace. |
415 |
} /* namespace drizzled */ |