~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/table.cc

  • Committer: Brian Aker
  • Date: 2010-10-28 01:45:34 UTC
  • mfrom: (1878.5.8 catalogs)
  • Revision ID: brian@tangent.org-20101028014534-b6qp4wp6crj60h7k
Merge in catalog tree.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1421
1421
 
1422
1422
/****************************************************************************/
1423
1423
 
1424
 
/**
1425
 
  Create a reduced Table object with properly set up Field list from a
1426
 
  list of field definitions.
1427
 
 
1428
 
    The created table doesn't have a table Cursor associated with
1429
 
    it, has no keys, no group/distinct, no copy_funcs array.
1430
 
    The sole purpose of this Table object is to use the power of Field
1431
 
    class to read/write data to/from table->getInsertRecord(). Then one can store
1432
 
    the record in any container (RB tree, hash, etc).
1433
 
    The table is created in Session mem_root, so are the table's fields.
1434
 
    Consequently, if you don't BLOB fields, you don't need to free it.
1435
 
 
1436
 
  @param session         connection handle
1437
 
  @param field_list  list of column definitions
1438
 
 
1439
 
  @return
1440
 
    0 if out of memory, Table object in case of success
1441
 
*/
1442
 
 
1443
 
Table *Session::create_virtual_tmp_table(List<CreateField> &field_list)
1444
 
{
1445
 
  uint32_t field_count= field_list.elements;
1446
 
  uint32_t blob_count= 0;
1447
 
  Field **field;
1448
 
  CreateField *cdef;                           /* column definition */
1449
 
  uint32_t record_length= 0;
1450
 
  uint32_t null_count= 0;                 /* number of columns which may be null */
1451
 
  uint32_t null_pack_length;              /* NULL representation array length */
1452
 
 
1453
 
  table::Instance *table= getInstanceTable(); // This will not go into the tableshare cache, so no key is used.
1454
 
  table->getMutableShare()->setFields(field_count + 1);
1455
 
  table->setFields(table->getMutableShare()->getFields(true));
1456
 
  field= table->getMutableShare()->getFields(true);
1457
 
  table->getMutableShare()->blob_field.resize(field_count+1);
1458
 
  table->getMutableShare()->fields= field_count;
1459
 
  table->getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
1460
 
  table->setup_tmp_table_column_bitmaps();
1461
 
 
1462
 
  table->in_use= this;           /* field->reset() may access table->in_use */
1463
 
 
1464
 
  /* Create all fields and calculate the total length of record */
1465
 
  List_iterator_fast<CreateField> it(field_list);
1466
 
  while ((cdef= it++))
1467
 
  {
1468
 
    *field= table->getMutableShare()->make_field(NULL,
1469
 
                                                 cdef->length,
1470
 
                                                 (cdef->flags & NOT_NULL_FLAG) ? false : true,
1471
 
                                                 (unsigned char *) ((cdef->flags & NOT_NULL_FLAG) ? 0 : ""),
1472
 
                                                 (cdef->flags & NOT_NULL_FLAG) ? 0 : 1,
1473
 
                                                 cdef->decimals,
1474
 
                                                 cdef->sql_type,
1475
 
                                                 cdef->charset,
1476
 
                                                 cdef->unireg_check,
1477
 
                                                 cdef->interval,
1478
 
                                                 cdef->field_name);
1479
 
    if (!*field)
1480
 
      goto error;
1481
 
    (*field)->init(table);
1482
 
    record_length+= (*field)->pack_length();
1483
 
    if (! ((*field)->flags & NOT_NULL_FLAG))
1484
 
      null_count++;
1485
 
 
1486
 
    if ((*field)->flags & BLOB_FLAG)
1487
 
      table->getMutableShare()->blob_field[blob_count++]= (uint32_t) (field - table->getFields());
1488
 
 
1489
 
    field++;
1490
 
  }
1491
 
  *field= NULL;                             /* mark the end of the list */
1492
 
  table->getMutableShare()->blob_field[blob_count]= 0;            /* mark the end of the list */
1493
 
  table->getMutableShare()->blob_fields= blob_count;
1494
 
 
1495
 
  null_pack_length= (null_count + 7)/8;
1496
 
  table->getMutableShare()->setRecordLength(record_length + null_pack_length);
1497
 
  table->getMutableShare()->rec_buff_length= ALIGN_SIZE(table->getMutableShare()->getRecordLength() + 1);
1498
 
  table->record[0]= (unsigned char*)alloc(table->getMutableShare()->rec_buff_length);
1499
 
  if (not table->getInsertRecord())
1500
 
    goto error;
1501
 
 
1502
 
  if (null_pack_length)
1503
 
  {
1504
 
    table->null_flags= (unsigned char*) table->getInsertRecord();
1505
 
    table->getMutableShare()->null_fields= null_count;
1506
 
    table->getMutableShare()->null_bytes= null_pack_length;
1507
 
  }
1508
 
  {
1509
 
    /* Set up field pointers */
1510
 
    unsigned char *null_pos= table->getInsertRecord();
1511
 
    unsigned char *field_pos= null_pos + table->getMutableShare()->null_bytes;
1512
 
    uint32_t null_bit= 1;
1513
 
 
1514
 
    for (field= table->getFields(); *field; ++field)
1515
 
    {
1516
 
      Field *cur_field= *field;
1517
 
      if ((cur_field->flags & NOT_NULL_FLAG))
1518
 
        cur_field->move_field(field_pos);
1519
 
      else
1520
 
      {
1521
 
        cur_field->move_field(field_pos, (unsigned char*) null_pos, null_bit);
1522
 
        null_bit<<= 1;
1523
 
        if (null_bit == (1 << 8))
1524
 
        {
1525
 
          ++null_pos;
1526
 
          null_bit= 1;
1527
 
        }
1528
 
      }
1529
 
      cur_field->reset();
1530
 
 
1531
 
      field_pos+= cur_field->pack_length();
1532
 
    }
1533
 
  }
1534
 
 
1535
 
  return table;
1536
 
 
1537
 
error:
1538
 
  for (field= table->getFields(); *field; ++field)
1539
 
  {
1540
 
    delete *field;                         /* just invokes field destructor */
1541
 
  }
1542
 
  return 0;
1543
 
}
1544
 
 
1545
1424
void Table::column_bitmaps_set(boost::dynamic_bitset<>& read_set_arg,
1546
1425
                               boost::dynamic_bitset<>& write_set_arg)
1547
1426
{