~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

Merged trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
49
49
using namespace std;
50
50
using namespace drizzled;
51
51
 
52
 
/* Functions defined in this file */
 
52
/* Functions defined in this cursor */
53
53
 
54
54
void open_table_error(TableShare *share, int error, int db_errno,
55
55
                      myf errortype, int errarg);
161
161
    field_type= DRIZZLE_TYPE_VARCHAR;
162
162
    break;
163
163
  case message::Table::Field::DECIMAL:
164
 
    field_type= DRIZZLE_TYPE_NEWDECIMAL;
 
164
    field_type= DRIZZLE_TYPE_DECIMAL;
165
165
    break;
166
166
  case message::Table::Field::ENUM:
167
167
    field_type= DRIZZLE_TYPE_ENUM;
231
231
                                    system_charset_info);
232
232
    }
233
233
    break;
234
 
  case DRIZZLE_TYPE_NEWDECIMAL:
 
234
  case DRIZZLE_TYPE_DECIMAL:
235
235
    default_item= new Item_decimal(default_value->c_str(),
236
236
                                   default_value->length(),
237
237
                                   system_charset_info);
525
525
        interval_parts+= field_options.field_value_size();
526
526
      }
527
527
      break;
528
 
    case DRIZZLE_TYPE_NEWDECIMAL:
 
528
    case DRIZZLE_TYPE_DECIMAL:
529
529
      {
530
530
        message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
531
531
 
775
775
    }
776
776
 
777
777
    uint8_t decimals= 0;
778
 
    if (field_type == DRIZZLE_TYPE_NEWDECIMAL
 
778
    if (field_type == DRIZZLE_TYPE_DECIMAL
779
779
        || field_type == DRIZZLE_TYPE_DOUBLE)
780
780
    {
781
781
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
820
820
    temp_table.s->db_low_byte_first= 1; //Cursor->low_byte_first();
821
821
    temp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
822
822
 
 
823
    uint32_t field_length;
 
824
 
 
825
    switch (field_type)
 
826
    {
 
827
    case DRIZZLE_TYPE_DOUBLE:
 
828
    {
 
829
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
 
830
      if (!fo.has_precision() && !fo.has_scale())
 
831
      {
 
832
        field_length= DBL_DIG+7;
 
833
      }
 
834
      else
 
835
      {
 
836
        field_length= fo.precision();
 
837
      }
 
838
      if (field_length < decimals &&
 
839
          decimals != NOT_FIXED_DEC)
 
840
      {
 
841
        my_error(ER_M_BIGGER_THAN_D, MYF(0), pfield.name().c_str());
 
842
        error= 1;
 
843
        goto err;
 
844
      }
 
845
      break;
 
846
    }
 
847
    case DRIZZLE_TYPE_DECIMAL:
 
848
    {
 
849
      message::Table::Field::NumericFieldOptions fo= pfield.numeric_options();
 
850
 
 
851
      field_length= my_decimal_precision_to_length(fo.precision(), fo.scale(),
 
852
                                                   false);
 
853
      break;
 
854
    }
 
855
    default:
 
856
      field_length= pfield.options().length();
 
857
      break;
 
858
    }
 
859
 
823
860
    Field* f= make_field(share,
824
861
                         &share->mem_root,
825
862
                         record + field_offsets[fieldnr] + data_offset,
826
 
                         pfield.options().length(),
 
863
                         field_length,
827
864
                         pfield.has_constraints() && pfield.constraints().is_nullable() ? true : false,
828
865
                         null_pos,
829
866
                         null_bit_pos,
927
964
  share->last_null_bit_pos= null_bit_pos;
928
965
 
929
966
  free(field_offsets);
 
967
  field_offsets= NULL;
930
968
  free(field_pack_length);
 
969
  field_pack_length= NULL;
931
970
 
932
 
  if (! (handler_file= share->db_type()->getCursor(share, session.mem_root)))
 
971
  if (! (handler_file= share->db_type()->getCursor(*share, session.mem_root)))
933
972
    abort(); // FIXME
934
973
 
935
974
  /* Fix key stuff */
1126
1165
  return (0);
1127
1166
 
1128
1167
err:
 
1168
  if (field_offsets)
 
1169
    free(field_offsets);
 
1170
  if (field_pack_length)
 
1171
    free(field_pack_length);
 
1172
 
1129
1173
  share->error= error;
1130
1174
  share->open_errno= my_errno;
1131
1175
  share->errarg= 0;
1138
1182
}
1139
1183
 
1140
1184
/*
1141
 
  Read table definition from a binary / text based .frm file
 
1185
  Read table definition from a binary / text based .frm cursor
1142
1186
 
1143
1187
  SYNOPSIS
1144
1188
  open_table_def()
1155
1199
   0    ok
1156
1200
   1    Error (see open_table_error)
1157
1201
   2    Error (see open_table_error)
1158
 
   3    Wrong data in .frm file
 
1202
   3    Wrong data in .frm cursor
1159
1203
   4    Error (see open_table_error)
1160
1204
   5    Error (see open_table_error: charset unavailable)
1161
1205
   6    Unknown .frm version
1234
1278
   0    ok
1235
1279
   1    Error (see open_table_error)
1236
1280
   2    Error (see open_table_error)
1237
 
   3    Wrong data in .frm file
 
1281
   3    Wrong data in .frm cursor
1238
1282
   4    Error (see open_table_error)
1239
1283
   5    Error (see open_table_error: charset unavailable)
1240
1284
   7    Table definition has changed in engine
1263
1307
  /* Allocate Cursor */
1264
1308
  if (!(prgflag & OPEN_FRM_FILE_ONLY))
1265
1309
  {
1266
 
    if (!(outparam->file= share->db_type()->getCursor(share, &outparam->mem_root)))
 
1310
    if (!(outparam->cursor= share->db_type()->getCursor(*share, &outparam->mem_root)))
1267
1311
      goto err;
1268
1312
  }
1269
1313
  else
1398
1442
  if (db_stat && open_mode != OTM_ALTER)
1399
1443
  {
1400
1444
    int ha_err;
1401
 
    if ((ha_err= (outparam->file->
 
1445
    if ((ha_err= (outparam->cursor->
1402
1446
                  ha_open(outparam, share->normalized_path.str,
1403
1447
                          (db_stat & HA_READ_ONLY ? O_RDONLY : O_RDWR),
1404
1448
                          (db_stat & HA_OPEN_TEMPORARY ? HA_OPEN_TMP_TABLE :
1409
1453
    {
1410
1454
      /* Set a flag if the table is crashed and it can be auto. repaired */
1411
1455
      share->crashed= ((ha_err == HA_ERR_CRASHED_ON_USAGE) &&
1412
 
                       outparam->file->auto_repair() &&
 
1456
                       outparam->cursor->auto_repair() &&
1413
1457
                       !(ha_open_flags & HA_OPEN_FOR_REPAIR));
1414
1458
 
1415
1459
      switch (ha_err)
1417
1461
        case HA_ERR_NO_SUCH_TABLE:
1418
1462
          /*
1419
1463
            The table did not exists in storage engine, use same error message
1420
 
            as if the .frm file didn't exist
 
1464
            as if the .frm cursor didn't exist
1421
1465
          */
1422
1466
          error= 1;
1423
1467
          my_errno= ENOENT;
1425
1469
        case EMFILE:
1426
1470
          /*
1427
1471
            Too many files opened, use same error message as if the .frm
1428
 
            file can't open
 
1472
            cursor can't open
1429
1473
           */
1430
1474
          error= 1;
1431
1475
          my_errno= EMFILE;
1432
1476
          break;
1433
1477
        default:
1434
 
          outparam->file->print_error(ha_err, MYF(0));
 
1478
          outparam->cursor->print_error(ha_err, MYF(0));
1435
1479
          error_reported= true;
1436
1480
          if (ha_err == HA_ERR_TABLE_DEF_CHANGED)
1437
1481
            error= 7;
1452
1496
 err:
1453
1497
  if (!error_reported && !(prgflag & DONT_GIVE_ERROR))
1454
1498
    share->open_table_error(error, my_errno, 0);
1455
 
  delete outparam->file;
1456
 
  outparam->file= 0;                            // For easier error checking
 
1499
  delete outparam->cursor;
 
1500
  outparam->cursor= 0;                          // For easier error checking
1457
1501
  outparam->db_stat= 0;
1458
1502
  free_root(&outparam->mem_root, MYF(0));       // Safe to call on zeroed root
1459
1503
  free((char*) outparam->alias);
1480
1524
  int error= 0;
1481
1525
 
1482
1526
  if (db_stat)
1483
 
    error= file->close();
 
1527
    error= cursor->close();
1484
1528
  free((char*) alias);
1485
1529
  alias= NULL;
1486
1530
  if (field)
1489
1533
      delete *ptr;
1490
1534
    field= 0;
1491
1535
  }
1492
 
  delete file;
1493
 
  file= 0;                              /* For easier errorchecking */
 
1536
  delete cursor;
 
1537
  cursor= 0;                            /* For easier errorchecking */
1494
1538
  if (free_share)
1495
1539
  {
1496
1540
    if (s->tmp_table == NO_TMP_TABLE)
1516
1560
}
1517
1561
 
1518
1562
 
1519
 
        /* error message when opening a form file */
 
1563
        /* error message when opening a form cursor */
1520
1564
 
1521
1565
void TableShare::open_table_error(int pass_error, int db_errno, int pass_errarg)
1522
1566
{
1538
1582
    break;
1539
1583
  case 2:
1540
1584
  {
1541
 
    Cursor *file= 0;
 
1585
    Cursor *cursor= 0;
1542
1586
    const char *datext= "";
1543
1587
 
1544
1588
    if (db_type() != NULL)
1545
1589
    {
1546
 
      if ((file= db_type()->getCursor(this, current_session->mem_root)))
 
1590
      if ((cursor= db_type()->getCursor(*this, current_session->mem_root)))
1547
1591
      {
1548
1592
        if (!(datext= *db_type()->bas_ext()))
1549
1593
          datext= "";
1553
1597
      ER_FILE_USED : ER_CANT_OPEN_FILE;
1554
1598
    sprintf(buff,"%s%s", normalized_path.str,datext);
1555
1599
    my_error(err_no,errortype, buff, db_errno);
1556
 
    delete file;
 
1600
    delete cursor;
1557
1601
    break;
1558
1602
  }
1559
1603
  case 5:
1889
1933
void Table::prepare_for_position()
1890
1934
{
1891
1935
 
1892
 
  if ((file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
1936
  if ((cursor->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
1893
1937
      s->primary_key < MAX_KEY)
1894
1938
  {
1895
1939
    mark_columns_used_by_index_no_reset(s->primary_key);
1912
1956
{
1913
1957
  MyBitmap *bitmap= &tmp_set;
1914
1958
 
1915
 
  (void) file->extra(HA_EXTRA_KEYREAD);
 
1959
  (void) cursor->extra(HA_EXTRA_KEYREAD);
1916
1960
  bitmap->clearAll();
1917
1961
  mark_columns_used_by_index_no_reset(index, bitmap);
1918
1962
  column_bitmaps_set(bitmap, bitmap);
1935
1979
{
1936
1980
 
1937
1981
  key_read= 0;
1938
 
  (void) file->extra(HA_EXTRA_NO_KEYREAD);
 
1982
  (void) cursor->extra(HA_EXTRA_NO_KEYREAD);
1939
1983
  default_column_bitmaps();
1940
1984
  return;
1941
1985
}
2020
2064
    mark_columns_used_by_index_no_reset(s->primary_key);
2021
2065
 
2022
2066
  /* If we the engine wants all predicates we mark all keys */
2023
 
  if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
 
2067
  if (cursor->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
2024
2068
  {
2025
2069
    Field **reg_field;
2026
2070
    for (reg_field= field ; *reg_field ; reg_field++)
2067
2111
  else
2068
2112
    mark_columns_used_by_index_no_reset(s->primary_key);
2069
2113
 
2070
 
  if (file->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
 
2114
  if (cursor->ha_table_flags() & HA_REQUIRES_KEY_COLUMNS_FOR_DELETE)
2071
2115
  {
2072
2116
    /* Mark all used key columns for read */
2073
2117
    Field **reg_field;
2551
2595
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
2552
2596
  {
2553
2597
    share->storage_engine= myisam_engine;
2554
 
    table->file= share->db_type()->getCursor(share, &table->mem_root);
 
2598
    table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
2555
2599
    if (group &&
2556
 
        (param->group_parts > table->file->max_key_parts() ||
2557
 
         param->group_length > table->file->max_key_length()))
 
2600
        (param->group_parts > table->cursor->max_key_parts() ||
 
2601
         param->group_length > table->cursor->max_key_length()))
2558
2602
      using_unique_constraint=1;
2559
2603
  }
2560
2604
  else
2561
2605
  {
2562
2606
    share->storage_engine= heap_engine;
2563
 
    table->file= share->db_type()->getCursor(share, &table->mem_root);
 
2607
    table->cursor= share->db_type()->getCursor(*share, &table->mem_root);
2564
2608
  }
2565
 
  if (!table->file)
 
2609
  if (!table->cursor)
2566
2610
    goto err;
2567
2611
 
2568
2612
 
3041
3085
bool Table::open_tmp_table()
3042
3086
{
3043
3087
  int error;
3044
 
  if ((error=file->ha_open(this, s->table_name.str,O_RDWR,
 
3088
  if ((error=cursor->ha_open(this, s->table_name.str,O_RDWR,
3045
3089
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
3046
3090
  {
3047
 
    file->print_error(error,MYF(0));
 
3091
    cursor->print_error(error,MYF(0));
3048
3092
    db_stat= 0;
3049
3093
    return true;
3050
3094
  }
3051
 
  (void) file->extra(HA_EXTRA_QUICK);           /* Faster */
 
3095
  (void) cursor->extra(HA_EXTRA_QUICK);         /* Faster */
3052
3096
  return false;
3053
3097
}
3054
3098
 
3101
3145
      goto err;
3102
3146
 
3103
3147
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
3104
 
    if (keyinfo->key_length >= file->max_key_length() ||
3105
 
        keyinfo->key_parts > file->max_key_parts() ||
 
3148
    if (keyinfo->key_length >= cursor->max_key_length() ||
 
3149
        keyinfo->key_parts > cursor->max_key_parts() ||
3106
3150
        share->uniques)
3107
3151
    {
3108
3152
      /* Can't create a key; Make a unique constraint instead of a key */
3177
3221
                       &create_info,
3178
3222
                       HA_CREATE_TMP_TABLE)))
3179
3223
  {
3180
 
    file->print_error(error,MYF(0));
 
3224
    cursor->print_error(error,MYF(0));
3181
3225
    db_stat= 0;
3182
3226
    goto err;
3183
3227
  }
3200
3244
  // Release latches since this can take a long time
3201
3245
  plugin::StorageEngine::releaseTemporaryLatches(session);
3202
3246
 
3203
 
  if (file)
 
3247
  if (cursor)
3204
3248
  {
3205
3249
    if (db_stat)
3206
 
      file->closeMarkForDelete(s->table_name.str);
 
3250
      cursor->closeMarkForDelete(s->table_name.str);
3207
3251
 
3208
3252
    s->db_type()->doDropTable(*session, s->table_name.str);
3209
3253
 
3210
 
    delete file;
 
3254
    delete cursor;
3211
3255
  }
3212
3256
 
3213
3257
  /* free blobs */
3237
3281
  if (table->s->db_type() != heap_engine ||
3238
3282
      error != HA_ERR_RECORD_FILE_FULL)
3239
3283
  {
3240
 
    table->file->print_error(error,MYF(0));
 
3284
    table->cursor->print_error(error,MYF(0));
3241
3285
    return true;
3242
3286
  }
3243
3287
 
3248
3292
  share= *table->s;
3249
3293
  new_table.s= &share;
3250
3294
  new_table.s->storage_engine= myisam_engine;
3251
 
  if (!(new_table.file= new_table.s->db_type()->getCursor(&share, &new_table.mem_root)))
 
3295
  if (!(new_table.cursor= new_table.s->db_type()->getCursor(share, &new_table.mem_root)))
3252
3296
    return true;                                // End of memory
3253
3297
 
3254
3298
  save_proc_info=session->get_proc_info();
3260
3304
    goto err2;
3261
3305
  if (new_table.open_tmp_table())
3262
3306
    goto err1;
3263
 
  if (table->file->indexes_are_disabled())
3264
 
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
3265
 
  table->file->ha_index_or_rnd_end();
3266
 
  table->file->ha_rnd_init(1);
 
3307
  if (table->cursor->indexes_are_disabled())
 
3308
    new_table.cursor->ha_disable_indexes(HA_KEY_SWITCH_ALL);
 
3309
  table->cursor->ha_index_or_rnd_end();
 
3310
  table->cursor->ha_rnd_init(1);
3267
3311
  if (table->no_rows)
3268
3312
  {
3269
 
    new_table.file->extra(HA_EXTRA_NO_ROWS);
 
3313
    new_table.cursor->extra(HA_EXTRA_NO_ROWS);
3270
3314
    new_table.no_rows=1;
3271
3315
  }
3272
3316
 
3273
3317
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
3274
 
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
 
3318
  new_table.cursor->extra(HA_EXTRA_WRITE_CACHE);
3275
3319
 
3276
3320
  /*
3277
3321
    copy all old rows from heap table to MyISAM table
3278
3322
    This is the only code that uses record[1] to read/write but this
3279
3323
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
3280
3324
  */
3281
 
  while (!table->file->rnd_next(new_table.record[1]))
 
3325
  while (!table->cursor->rnd_next(new_table.record[1]))
3282
3326
  {
3283
 
    write_err= new_table.file->ha_write_row(new_table.record[1]);
 
3327
    write_err= new_table.cursor->ha_write_row(new_table.record[1]);
3284
3328
    if (write_err)
3285
3329
      goto err;
3286
3330
  }
3287
3331
  /* copy row that filled HEAP table */
3288
 
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
 
3332
  if ((write_err=new_table.cursor->ha_write_row(table->record[0])))
3289
3333
  {
3290
 
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
 
3334
    if (new_table.cursor->is_fatal_error(write_err, HA_CHECK_DUP) ||
3291
3335
        !ignore_last_dupp_key_error)
3292
3336
      goto err;
3293
3337
  }
3294
3338
 
3295
3339
  /* remove heap table and change to use myisam table */
3296
 
  (void) table->file->ha_rnd_end();
3297
 
  (void) table->file->close();                  // This deletes the table !
3298
 
  delete table->file;
3299
 
  table->file= NULL;
 
3340
  (void) table->cursor->ha_rnd_end();
 
3341
  (void) table->cursor->close();                  // This deletes the table !
 
3342
  delete table->cursor;
 
3343
  table->cursor= NULL;
3300
3344
  new_table.s= table->s;                       // Keep old share
3301
3345
  *table= new_table;
3302
3346
  *table->s= share;
3303
3347
 
3304
 
  table->file->change_table_ptr(table, table->s);
 
3348
  table->cursor->change_table_ptr(table, table->s);
3305
3349
  table->use_all_columns();
3306
3350
  if (save_proc_info)
3307
3351
  {
3313
3357
  return false;
3314
3358
 
3315
3359
 err:
3316
 
  table->file->print_error(write_err, MYF(0));
3317
 
  (void) table->file->ha_rnd_end();
3318
 
  (void) new_table.file->close();
 
3360
  table->cursor->print_error(write_err, MYF(0));
 
3361
  (void) table->cursor->ha_rnd_end();
 
3362
  (void) new_table.cursor->close();
3319
3363
 err1:
3320
3364
  new_table.s->db_type()->doDropTable(*session, new_table.s->table_name.str);
3321
3365
 err2:
3322
 
  delete new_table.file;
 
3366
  delete new_table.cursor;
3323
3367
  session->set_proc_info(save_proc_info);
3324
3368
  table->mem_root= new_table.mem_root;
3325
3369
  return true;
3473
3517
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
3474
3518
    errmsg_printf(ERRMSG_LVL_ERROR, _("Got error %d when reading table '%s'"),
3475
3519
                    error, s->path.str);
3476
 
  file->print_error(error,MYF(0));
 
3520
  cursor->print_error(error,MYF(0));
3477
3521
 
3478
3522
  return 1;
3479
3523
}