~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Brian Aker
  • Date: 2010-02-19 23:42:02 UTC
  • mto: (1273.19.18 fix_is)
  • mto: This revision was merged to the branch mainline in revision 1309.
  • Revision ID: brian@gir.tangent.org-20100219234202-boogaw8j3folts51
Basic engine with test.

Show diffs side-by-side

added added

removed removed

Lines of Context:
458
458
  {
459
459
    char buff[160];
460
460
    if (ignore)
461
 
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
461
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
462
462
              (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
463
463
    else
464
 
      snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
464
      sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
465
465
              (ulong) (info.deleted + info.updated), (ulong) session->cuted_fields);
466
466
    session->row_count_func= info.copied + info.deleted + info.updated;
467
467
    session->my_ok((ulong) session->row_count_func,
1343
1343
  }
1344
1344
  char buff[160];
1345
1345
  if (info.ignore)
1346
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
1346
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1347
1347
            (ulong) (info.records - info.copied), (ulong) session->cuted_fields);
1348
1348
  else
1349
 
    snprintf(buff, sizeof(buff), ER(ER_INSERT_INFO), (ulong) info.records,
 
1349
    sprintf(buff, ER(ER_INSERT_INFO), (ulong) info.records,
1350
1350
            (ulong) (info.deleted+info.updated), (ulong) session->cuted_fields);
1351
1351
  session->row_count_func= info.copied + info.deleted + info.updated;
1352
1352
 
1454
1454
 
1455
1455
static Table *create_table_from_items(Session *session, HA_CREATE_INFO *create_info,
1456
1456
                                      TableList *create_table,
1457
 
                                      message::Table &table_proto,
 
1457
                                      message::Table *table_proto,
1458
1458
                                      AlterInfo *alter_info,
1459
1459
                                      List<Item> *items,
1460
1460
                                      bool is_if_not_exists,
1461
 
                                      DRIZZLE_LOCK **lock,
1462
 
                                      TableIdentifier &identifier)
 
1461
                                      DRIZZLE_LOCK **lock)
1463
1462
{
1464
1463
  Table tmp_table;              // Used during 'CreateField()'
1465
1464
  TableShare share;
1471
1470
  Field *tmp_field;
1472
1471
  bool not_used;
1473
1472
 
1474
 
  if (not (identifier.isTmp()) && create_table->table->db_stat)
 
1473
  bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
 
1474
 
 
1475
  if (!(lex_identified_temp_table) &&
 
1476
      create_table->table->db_stat)
1475
1477
  {
1476
1478
    /* Table already exists and was open at openTablesLock() stage. */
1477
1479
    if (is_if_not_exists)
1493
1495
 
1494
1496
  tmp_table.s->db_create_options=0;
1495
1497
  tmp_table.s->blob_ptr_size= portable_sizeof_char_ptr;
1496
 
 
1497
 
  if (not table_proto.engine().name().compare("MyISAM"))
1498
 
    tmp_table.s->db_low_byte_first= true;
1499
 
  else if (not table_proto.engine().name().compare("MEMORY"))
1500
 
    tmp_table.s->db_low_byte_first= true;
1501
 
 
 
1498
  tmp_table.s->db_low_byte_first=
 
1499
        test(create_info->db_type == myisam_engine ||
 
1500
             create_info->db_type == heap_engine);
1502
1501
  tmp_table.null_row= false;
1503
1502
  tmp_table.maybe_null= false;
1504
1503
 
1525
1524
    alter_info->create_list.push_back(cr_field);
1526
1525
  }
1527
1526
 
 
1527
  TableIdentifier identifier(create_table->db,
 
1528
                             create_table->table_name,
 
1529
                             lex_identified_temp_table ?  TEMP_TABLE :
 
1530
                             NO_TMP_TABLE);
 
1531
 
 
1532
 
1528
1533
  /*
1529
1534
    Create and lock table.
1530
1535
 
1533
1538
    should not cause deadlocks or races.
1534
1539
  */
1535
1540
  {
1536
 
    if (not mysql_create_table_no_lock(session,
1537
 
                                       identifier,
1538
 
                                       create_info,
1539
 
                                       table_proto,
1540
 
                                       alter_info,
1541
 
                                       false,
1542
 
                                       select_field_count,
1543
 
                                       is_if_not_exists))
 
1541
    if (!mysql_create_table_no_lock(session,
 
1542
                                    identifier,
 
1543
                                    create_info,
 
1544
                                    table_proto,
 
1545
                                    alter_info,
 
1546
                                    false,
 
1547
                                    select_field_count,
 
1548
                                    is_if_not_exists))
1544
1549
    {
1545
 
      if (create_info->table_existed && not identifier.isTmp())
 
1550
      if (create_info->table_existed &&
 
1551
          !(lex_identified_temp_table))
1546
1552
      {
1547
1553
        /*
1548
1554
          This means that someone created table underneath server
1553
1559
        return NULL;
1554
1560
      }
1555
1561
 
1556
 
      if (not identifier.isTmp())
 
1562
      if (!(lex_identified_temp_table))
1557
1563
      {
1558
1564
        pthread_mutex_lock(&LOCK_open); /* CREATE TABLE... has found that the table already exists for insert and is adapting to use it */
1559
1565
        if (session->reopen_name_locked_table(create_table, false))
1566
1572
      }
1567
1573
      else
1568
1574
      {
1569
 
        if (not (table= session->openTable(create_table, (bool*) 0,
1570
 
                                           DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
1571
 
            not create_info->table_existed)
 
1575
        if (!(table= session->openTable(create_table, (bool*) 0,
 
1576
                                         DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
 
1577
            !create_info->table_existed)
1572
1578
        {
1573
1579
          /*
1574
1580
            This shouldn't happen as creation of temporary table should make
1593
1599
      *lock= 0;
1594
1600
    }
1595
1601
 
1596
 
    if (not create_info->table_existed)
1597
 
      session->drop_open_table(table, identifier);
 
1602
    if (!create_info->table_existed)
 
1603
      session->drop_open_table(table, create_table->db, create_table->table_name);
1598
1604
    return NULL;
1599
1605
  }
1600
1606
 
1605
1611
int
1606
1612
select_create::prepare(List<Item> &values, Select_Lex_Unit *u)
1607
1613
{
 
1614
  bool lex_identified_temp_table= (table_proto->type() == message::Table::TEMPORARY);
 
1615
 
1608
1616
  DRIZZLE_LOCK *extra_lock= NULL;
1609
1617
  /*
1610
 
    For replication, the CREATE-SELECT statement is written
1611
 
    in two pieces: the first transaction messsage contains 
1612
 
    the CREATE TABLE statement as a CreateTableStatement message
1613
 
    necessary to create the table.
1614
 
    
1615
 
    The second transaction message contains all the InsertStatement
1616
 
    and associated InsertRecords that should go into the table.
 
1618
    For row-based replication, the CREATE-SELECT statement is written
 
1619
    in two pieces: the first one contain the CREATE TABLE statement
 
1620
    necessary to create the table and the second part contain the rows
 
1621
    that should go into the table.
 
1622
 
 
1623
    For non-temporary tables, the start of the CREATE-SELECT
 
1624
    implicitly commits the previous transaction, and all events
 
1625
    forming the statement will be stored the transaction cache. At end
 
1626
    of the statement, the entire statement is committed as a
 
1627
    transaction, and all events are written to the binary log.
 
1628
 
 
1629
    On the master, the table is locked for the duration of the
 
1630
    statement, but since the CREATE part is replicated as a simple
 
1631
    statement, there is no way to lock the table for accesses on the
 
1632
    slave.  Hence, we have to hold on to the CREATE part of the
 
1633
    statement until the statement has finished.
1617
1634
   */
1618
1635
 
1619
1636
  unit= u;
1620
1637
 
1621
 
  if (not (table= create_table_from_items(session, create_info, create_table,
1622
 
                                          table_proto,
1623
 
                                          alter_info, &values,
1624
 
                                          is_if_not_exists,
1625
 
                                          &extra_lock, identifier)))
 
1638
  /*
 
1639
    Start a statement transaction before the create if we are using
 
1640
    row-based replication for the statement.  If we are creating a
 
1641
    temporary table, we need to start a statement transaction.
 
1642
  */
 
1643
 
 
1644
  if (!(table= create_table_from_items(session, create_info, create_table,
 
1645
                                       table_proto,
 
1646
                                       alter_info, &values,
 
1647
                                       is_if_not_exists,
 
1648
                                       &extra_lock)))
1626
1649
    return(-1);                         // abort() deletes table
1627
1650
 
1628
1651
  if (extra_lock)
1629
1652
  {
1630
1653
    assert(m_plock == NULL);
1631
1654
 
1632
 
    if (identifier.isTmp())
 
1655
    if (lex_identified_temp_table)
1633
1656
      m_plock= &m_lock;
1634
1657
    else
1635
1658
      m_plock= &session->extra_lock;
1758
1781
  {
1759
1782
    table->cursor->extra(HA_EXTRA_NO_IGNORE_DUP_KEY);
1760
1783
    table->cursor->extra(HA_EXTRA_WRITE_CANNOT_REPLACE);
1761
 
    if (not create_info->table_existed)
1762
 
      session->drop_open_table(table, identifier);
 
1784
    if (!create_info->table_existed)
 
1785
      session->drop_open_table(table, create_table->db, create_table->table_name);
1763
1786
    table= NULL;                                    // Safety
1764
1787
  }
1765
1788
}