~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_base.cc

  • Committer: Brian Aker
  • Date: 2009-01-17 00:52:59 UTC
  • Revision ID: brian@gir-3.local-20090117005259-goyecyq0tpi9irnb
Pass through on refactoring functions to clases.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
 
49
49
 
50
50
/**
51
 
  return true if the table was created explicitly.
52
 
*/
53
 
inline bool is_user_table(Table * table)
54
 
{
55
 
  const char *name= table->s->table_name.str;
56
 
  return strncmp(name, TMP_FILE_PREFIX, TMP_FILE_PREFIX_LENGTH);
57
 
}
58
 
 
59
 
 
60
 
/**
61
51
  @defgroup Data_Dictionary Data Dictionary
62
52
  @{
63
53
*/
601
591
{                                               // Free all structures
602
592
  free_io_cache(table);
603
593
  if (table->file)                              // Not true if name lock
604
 
    closefrm(table, 1);                 // close file
 
594
    table->closefrm(true);                      // close file
605
595
  return;
606
596
}
607
597
 
1136
1126
  return(found_old_table);
1137
1127
}
1138
1128
 
1139
 
 
1140
 
/* close_temporary_tables' internal, 4 is due to uint4korr definition */
1141
 
static inline uint32_t  tmpkeyval(Session *session __attribute__((unused)),
1142
 
                              Table *table)
1143
 
{
1144
 
  return uint4korr(table->s->table_cache_key.str + table->s->table_cache_key.length - 4);
1145
 
}
1146
 
 
1147
 
 
1148
 
/*
1149
 
  Close all temporary tables created by 'CREATE TEMPORARY TABLE' for thread
1150
 
  creates one DROP TEMPORARY Table binlog event for each pseudo-thread
1151
 
*/
1152
 
 
1153
 
void close_temporary_tables(Session *session)
1154
 
{
1155
 
  Table *table;
1156
 
  Table *next= NULL;
1157
 
  Table *prev_table;
1158
 
  /* Assume session->options has OPTION_QUOTE_SHOW_CREATE */
1159
 
  bool was_quote_show= true;
1160
 
 
1161
 
  if (!session->temporary_tables)
1162
 
    return;
1163
 
 
1164
 
  if (!drizzle_bin_log.is_open() || true )
1165
 
  {
1166
 
    Table *tmp_next;
1167
 
    for (table= session->temporary_tables; table; table= tmp_next)
1168
 
    {
1169
 
      tmp_next= table->next;
1170
 
      close_temporary(table, 1, 1);
1171
 
    }
1172
 
    session->temporary_tables= 0;
1173
 
    return;
1174
 
  }
1175
 
 
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;
1179
 
  char buf[256];
1180
 
  String s_query= String(buf, sizeof(buf), system_charset_info);
1181
 
  bool found_user_tables= false;
1182
 
 
1183
 
  memcpy(buf, stub, stub_len);
1184
 
 
1185
 
  /*
1186
 
    Insertion sort of temp tables by pseudo_thread_id to build ordered list
1187
 
    of sublists of equal pseudo_thread_id
1188
 
  */
1189
 
 
1190
 
  for (prev_table= session->temporary_tables, table= prev_table->next;
1191
 
       table;
1192
 
       prev_table= table, table= table->next)
1193
 
  {
1194
 
    Table *prev_sorted /* same as for prev_table */, *sorted;
1195
 
    if (is_user_table(table))
1196
 
    {
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)
1201
 
      {
1202
 
        if (!is_user_table(sorted) ||
1203
 
            tmpkeyval(session, sorted) > tmpkeyval(session, table))
1204
 
        {
1205
 
          /* move into the sorted part of the list from the unsorted */
1206
 
          prev_table->next= table->next;
1207
 
          table->next= sorted;
1208
 
          if (prev_sorted)
1209
 
          {
1210
 
            prev_sorted->next= table;
1211
 
          }
1212
 
          else
1213
 
          {
1214
 
            session->temporary_tables= table;
1215
 
          }
1216
 
          table= prev_table;
1217
 
          break;
1218
 
        }
1219
 
      }
1220
 
    }
1221
 
  }
1222
 
 
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)))
1226
 
  {
1227
 
    session->options |= OPTION_QUOTE_SHOW_CREATE;
1228
 
  }
1229
 
 
1230
 
  /* scan sorted tmps to generate sequence of DROP */
1231
 
  for (table= session->temporary_tables; table; table= next)
1232
 
  {
1233
 
    if (is_user_table(table))
1234
 
    {
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);
1238
 
      /*
1239
 
        Loop forward through all tables within the sublist of
1240
 
        common pseudo_thread_id to create single DROP query.
1241
 
      */
1242
 
      for (s_query.length(stub_len);
1243
 
           table && is_user_table(table) &&
1244
 
             tmpkeyval(session, table) == session->variables.pseudo_thread_id;
1245
 
           table= next)
1246
 
      {
1247
 
        /*
1248
 
          We are going to add 4 ` around the db/table names and possible more
1249
 
          due to special characters in the names
1250
 
        */
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(',');
1256
 
        next= table->next;
1257
 
        close_temporary(table, 1, 1);
1258
 
      }
1259
 
      session->clear_error();
1260
 
      Query_log_event qinfo(session, s_query.ptr(),
1261
 
                            s_query.length() - 1 /* to remove trailing ',' */,
1262
 
                            0, false);
1263
 
      /*
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.
1272
 
      */
1273
 
      qinfo.error_code= 0;
1274
 
      drizzle_bin_log.write(&qinfo);
1275
 
      session->variables.pseudo_thread_id= save_pseudo_thread_id;
1276
 
    }
1277
 
    else
1278
 
    {
1279
 
      next= table->next;
1280
 
      close_temporary(table, 1, 1);
1281
 
    }
1282
 
  }
1283
 
  if (!was_quote_show)
1284
 
    session->options&= ~OPTION_QUOTE_SHOW_CREATE; /* restore option */
1285
 
  session->temporary_tables=0;
1286
 
}
1287
 
 
1288
1129
/*
1289
1130
  Find table in list.
1290
1131
 
1557
1398
  handlerton *table_type= table->s->db_type();
1558
1399
 
1559
1400
  free_io_cache(table);
1560
 
  closefrm(table, 0);
 
1401
  table->closefrm(false);
1561
1402
 
1562
1403
  if (delete_table)
1563
1404
    rm_temporary_table(table_type, table->s->path.str);
1588
1429
  TableList table_list;
1589
1430
 
1590
1431
  if (!(key=(char*) alloc_root(&share->mem_root, MAX_DBKEY_LENGTH)))
1591
 
    return(1);                          /* purecov: inspected */
 
1432
    return true;                                /* purecov: inspected */
1592
1433
 
1593
1434
  table_list.db= (char*) db;
1594
1435
  table_list.table_name= (char*) table_name;
1595
1436
  key_length= create_table_def_key(session, key, &table_list, 1);
1596
1437
  share->set_table_cache_key(key, key_length);
1597
 
  return(0);
 
1438
 
 
1439
  return false;
1598
1440
}
1599
1441
 
1600
1442
 
2510
2352
  tmp.prev=             table->prev;
2511
2353
 
2512
2354
  if (table->file)
2513
 
    closefrm(table, 1);         // close file, free everything
 
2355
    table->closefrm(true);              // close file, free everything
2514
2356
 
2515
2357
  *table= tmp;
2516
2358
  table->default_column_bitmaps();
3126
2968
       errmsg_printf(ERRMSG_LVL_ERROR, _("Couldn't repair table: %s.%s"), share->db.str,
3127
2969
                       share->table_name.str);
3128
2970
       if (entry->file)
3129
 
        closefrm(entry, 0);
 
2971
        entry->closefrm(false);
3130
2972
       error=1;
3131
2973
     }
3132
2974
     else
3170
3012
                          "to write 'DELETE FROM `%s`.`%s`' to the binary log"),
3171
3013
                        table_list->db, table_list->table_name);
3172
3014
        my_error(ER_OUTOFMEMORY, MYF(0), query_buf_size);
3173
 
        closefrm(entry, 0);
 
3015
        entry->closefrm(false);
3174
3016
        goto err;
3175
3017
      }
3176
3018
    }