1136
1126
return(found_old_table);
1140
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1141
static inline uint32_t tmpkeyval(Session *session __attribute__((unused)),
1144
return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1149
Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1150
creates one DROP TEMPORARY Table binlog event for each pseudo-thread
1153
void close_temporary_tables(Session *session)
1158
/* Assume session->options has OPTION_QUOTE_SHOW_CREATE */
1159
bool was_quote_show= true;
1161
if (!session->temporary_tables)
1164
if (!drizzle_bin_log.is_open() || true )
1167
for (table= session->temporary_tables; table; table= tmp_next)
1169
tmp_next= table->next;
1170
close_temporary(table, 1, 1);
1172
session->temporary_tables= 0;
1176
/* Better add "if exists", in case a RESET MASTER has been done */
1177
const char stub[]= "DROP /*!40005 TEMPORARY */ Table IF EXISTS ";
1178
uint32_t stub_len= sizeof(stub) - 1;
1180
String s_query= String(buf, sizeof(buf), system_charset_info);
1181
bool found_user_tables= false;
1183
memcpy(buf, stub, stub_len);
1186
Insertion sort of temp tables by pseudo_thread_id to build ordered list
1187
of sublists of equal pseudo_thread_id
1190
for (prev_table= session->temporary_tables, table= prev_table->next;
1192
prev_table= table, table= table->next)
1194
Table *prev_sorted /* same as for prev_table */, *sorted;
1195
if (is_user_table(table))
1197
if (!found_user_tables)
1198
found_user_tables= true;
1199
for (prev_sorted= NULL, sorted= session->temporary_tables; sorted != table;
1200
prev_sorted= sorted, sorted= sorted->next)
1202
if (!is_user_table(sorted) ||
1203
tmpkeyval(session, sorted) > tmpkeyval(session, table))
1205
/* move into the sorted part of the list from the unsorted */
1206
prev_table->next= table->next;
1207
table->next= sorted;
1210
prev_sorted->next= table;
1214
session->temporary_tables= table;
1223
/* We always quote db,table names though it is slight overkill */
1224
if (found_user_tables &&
1225
!(was_quote_show= test(session->options & OPTION_QUOTE_SHOW_CREATE)))
1227
session->options |= OPTION_QUOTE_SHOW_CREATE;
1230
/* scan sorted tmps to generate sequence of DROP */
1231
for (table= session->temporary_tables; table; table= next)
1233
if (is_user_table(table))
1235
my_thread_id save_pseudo_thread_id= session->variables.pseudo_thread_id;
1236
/* Set pseudo_thread_id to be that of the processed table */
1237
session->variables.pseudo_thread_id= tmpkeyval(session, table);
1239
Loop forward through all tables within the sublist of
1240
common pseudo_thread_id to create single DROP query.
1242
for (s_query.length(stub_len);
1243
table && is_user_table(table) &&
1244
tmpkeyval(session, table) == session->variables.pseudo_thread_id;
1248
We are going to add 4 ` around the db/table names and possible more
1249
due to special characters in the names
1251
append_identifier(session, &s_query, table->s->db.str, strlen(table->s->db.str));
1252
s_query.append('.');
1253
append_identifier(session, &s_query, table->s->table_name.str,
1254
strlen(table->s->table_name.str));
1255
s_query.append(',');
1257
close_temporary(table, 1, 1);
1259
session->clear_error();
1260
Query_log_event qinfo(session, s_query.ptr(),
1261
s_query.length() - 1 /* to remove trailing ',' */,
1264
Imagine the thread had created a temp table, then was doing a
1265
SELECT, and the SELECT was killed. Then it's not clever to
1266
mark the statement above as "killed", because it's not really
1267
a statement updating data, and there are 99.99% chances it
1268
will succeed on slave. If a real update (one updating a
1269
persistent table) was killed on the master, then this real
1270
update will be logged with error_code=killed, rightfully
1271
causing the slave to stop.
1273
qinfo.error_code= 0;
1274
drizzle_bin_log.write(&qinfo);
1275
session->variables.pseudo_thread_id= save_pseudo_thread_id;
1280
close_temporary(table, 1, 1);
1283
if (!was_quote_show)
1284
session->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
1285
session->temporary_tables=0;
1289
1130
Find table in list.