~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_table.cc

Merge (with fix for dangling bit in merge)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1611
1611
                                message::Table *table_proto,
1612
1612
                                AlterInfo *alter_info,
1613
1613
                                bool internal_tmp_table,
1614
 
                                uint32_t select_field_count)
 
1614
                                uint32_t select_field_count, 
 
1615
                                bool is_if_not_exists)
1615
1616
{
1616
1617
  char          path[FN_REFLEN];
1617
1618
  uint32_t          path_length;
1620
1621
  Cursor        *cursor;
1621
1622
  bool          error= true;
1622
1623
  TableShare share;
 
1624
  bool lex_identified_temp_table= 
 
1625
    (table_proto->type() == drizzled::message::Table::TEMPORARY);
1623
1626
 
1624
1627
  /* Check for duplicate fields and check type of table to create */
1625
1628
  if (!alter_info->create_list.elements)
1629
1632
    return true;
1630
1633
  }
1631
1634
  assert(strcmp(table_name,table_proto->name().c_str())==0);
1632
 
  if (check_engine(session, table_name, create_info))
 
1635
  if (check_engine(session, table_name, 
 
1636
                   table_proto,
 
1637
                   create_info))
1633
1638
    return true;
1634
1639
  db_options= create_info->table_options;
1635
1640
  if (create_info->row_type == ROW_TYPE_DYNAMIC)
1650
1655
    goto err;
1651
1656
 
1652
1657
      /* Check if table exists */
1653
 
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
 
1658
  if (lex_identified_temp_table)
1654
1659
  {
1655
1660
    path_length= build_tmptable_filename(session, path, sizeof(path));
1656
1661
  }
1668
1673
  }
1669
1674
 
1670
1675
  /* Check if table already exists */
1671
 
  if ((create_info->options & HA_LEX_CREATE_TMP_TABLE) &&
 
1676
  if (lex_identified_temp_table &&
1672
1677
      session->find_temporary_table(db, table_name))
1673
1678
  {
1674
 
    if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
 
1679
    if (is_if_not_exists)
1675
1680
    {
1676
1681
      create_info->table_existed= 1;            // Mark that table existed
1677
1682
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1685
1690
  }
1686
1691
 
1687
1692
  pthread_mutex_lock(&LOCK_open); /* CREATE TABLE (some confussion on naming, double check) */
1688
 
  if (!internal_tmp_table && !(create_info->options & HA_LEX_CREATE_TMP_TABLE))
 
1693
  if (!internal_tmp_table && ! lex_identified_temp_table)
1689
1694
  {
1690
1695
    if (plugin::StorageEngine::getTableDefinition(*session, path, db, table_name, internal_tmp_table)==EEXIST)
1691
1696
    {
1692
 
      if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
 
1697
      if (is_if_not_exists)
1693
1698
      {
1694
1699
        error= false;
1695
1700
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1726
1731
    one else is attempting to discover the table. Since
1727
1732
    it's not on disk as a frm cursor, no one could be using it!
1728
1733
  */
1729
 
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
 
1734
  if (! lex_identified_temp_table)
1730
1735
  {
1731
 
    bool create_if_not_exists =
1732
 
      create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS;
1733
 
 
1734
1736
    char table_path[FN_REFLEN];
1735
1737
    uint32_t          table_path_length;
1736
1738
 
1744
1746
        /* Normal case, no table exists. we can go and create it */
1745
1747
        break;
1746
1748
      case EEXIST:
1747
 
        if (create_if_not_exists)
 
1749
        if (is_if_not_exists)
1748
1750
        {
1749
1751
          error= false;
1750
1752
          push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1772
1774
                       key_count, key_info_buffer))
1773
1775
    goto unlock_and_end;
1774
1776
 
1775
 
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
 
1777
  if (lex_identified_temp_table)
1776
1778
  {
1777
1779
    /* Open table and put in temporary table list */
1778
1780
    if (!(session->open_temporary_table(path, db, table_name)))
1789
1791
    - The binary log is not open.
1790
1792
    Otherwise, the statement shall be binlogged.
1791
1793
   */
1792
 
  if (!internal_tmp_table &&
1793
 
      ((!(create_info->options & HA_LEX_CREATE_TMP_TABLE))))
 
1794
  if (!internal_tmp_table && ! lex_identified_temp_table)
1794
1795
    write_bin_log(session, session->query, session->query_length);
1795
1796
  error= false;
1796
1797
unlock_and_end:
1812
1813
                        message::Table *table_proto,
1813
1814
                        AlterInfo *alter_info,
1814
1815
                        bool internal_tmp_table,
1815
 
                        uint32_t select_field_count)
 
1816
                        uint32_t select_field_count,
 
1817
                        bool is_if_not_exists)
1816
1818
{
1817
1819
  Table *name_lock= NULL;
1818
1820
  bool result;
 
1821
  bool lex_identified_temp_table=
 
1822
    (table_proto->type() == drizzled::message::Table::TEMPORARY);
1819
1823
 
1820
 
  if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
 
1824
  if (! lex_identified_temp_table)
1821
1825
  {
1822
1826
    if (session->lock_table_name_if_not_cached(db, table_name, &name_lock))
1823
1827
    {
1826
1830
    }
1827
1831
    if (name_lock == NULL)
1828
1832
    {
1829
 
      if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
 
1833
      if (is_if_not_exists)
1830
1834
      {
1831
1835
        push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_NOTE,
1832
1836
                            ER_TABLE_EXISTS_ERROR, ER(ER_TABLE_EXISTS_ERROR),
1847
1851
                                     table_proto,
1848
1852
                                     alter_info,
1849
1853
                                     internal_tmp_table,
1850
 
                                     select_field_count);
 
1854
                                     select_field_count,
 
1855
                                     is_if_not_exists);
1851
1856
 
1852
1857
unlock:
1853
1858
  if (name_lock)
2392
2397
 
2393
2398
bool mysql_create_like_table(Session* session, TableList* table, TableList* src_table,
2394
2399
                             drizzled::message::Table& create_table_proto,
2395
 
                             HA_CREATE_INFO *create_info)
 
2400
                             HA_CREATE_INFO *create_info,
 
2401
                             bool is_if_not_exists)
2396
2402
{
2397
2403
  Table *name_lock= 0;
2398
2404
  char src_path[FN_REFLEN], dst_path[FN_REFLEN];
2403
2409
  bool res= true;
2404
2410
  uint32_t not_used;
2405
2411
  message::Table src_proto;
 
2412
  bool lex_identified_temp_table=
 
2413
    (create_table_proto.type() == drizzled::message::Table::TEMPORARY);
2406
2414
 
2407
2415
  /*
2408
2416
    By opening source table we guarantee that it exists and no concurrent
2423
2431
    Check that destination tables does not exist. Note that its name
2424
2432
    was already checked when it was added to the table list.
2425
2433
  */
2426
 
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
 
2434
  if (lex_identified_temp_table)
2427
2435
  {
2428
2436
    if (session->find_temporary_table(db, table_name))
2429
2437
      goto table_exists;
2494
2502
 
2495
2503
    message::Table new_proto(src_proto);
2496
2504
 
 
2505
    if (lex_identified_temp_table)
 
2506
    {
 
2507
      new_proto.set_type(message::Table::TEMPORARY);
 
2508
    }
 
2509
    else
 
2510
    {
 
2511
      new_proto.set_type(message::Table::STANDARD);
 
2512
    }
 
2513
 
2497
2514
    if (create_info->used_fields & HA_CREATE_USED_ENGINE)
2498
2515
    {
2499
2516
      message::Table::StorageEngine *protoengine;
2540
2557
  }
2541
2558
  pthread_mutex_unlock(&LOCK_open);
2542
2559
 
2543
 
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE)
 
2560
  if (lex_identified_temp_table)
2544
2561
  {
2545
2562
    if (err || !session->open_temporary_table(dst_path, db, table_name))
2546
2563
    {
2574
2591
           4    temporary temporary Nothing
2575
2592
           ==== ========= ========= ==============================
2576
2593
    */
2577
 
    if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
 
2594
    if (! lex_identified_temp_table)
2578
2595
    {
2579
2596
      if (src_table->table->s->tmp_table)               // Case 2
2580
2597
      {
2598
2615
        }
2599
2616
        pthread_mutex_unlock(&LOCK_open);
2600
2617
 
2601
 
        int result= store_create_info(table, &query, create_info);
 
2618
        int result= store_create_info(table, &query, create_info, is_if_not_exists);
2602
2619
 
2603
2620
        assert(result == 0); // store_create_info() always return 0
2604
2621
        write_bin_log(session, query.ptr(), query.length());
2612
2629
  goto err;
2613
2630
 
2614
2631
table_exists:
2615
 
  if (create_info->options & HA_LEX_CREATE_IF_NOT_EXISTS)
 
2632
  if (is_if_not_exists)
2616
2633
  {
2617
2634
    char warn_buff[DRIZZLE_ERRMSG_SIZE];
2618
2635
    snprintf(warn_buff, sizeof(warn_buff),
2805
2822
}
2806
2823
 
2807
2824
bool check_engine(Session *session, const char *table_name,
2808
 
                         HA_CREATE_INFO *create_info)
 
2825
                  message::Table *create_proto,
 
2826
                  HA_CREATE_INFO *create_info)
2809
2827
{
2810
2828
  plugin::StorageEngine **new_engine= &create_info->db_type;
2811
2829
  plugin::StorageEngine *req_engine= *new_engine;
 
2830
  bool lex_identified_temp_table=  (create_proto->type() == drizzled::message::Table::TEMPORARY);
 
2831
 
2812
2832
  if (!req_engine->is_enabled())
2813
2833
  {
2814
2834
    string engine_name= req_engine->getName();
2826
2846
                       plugin::StorageEngine::resolveName(*new_engine).c_str(),
2827
2847
                       table_name);
2828
2848
  }
2829
 
  if (create_info->options & HA_LEX_CREATE_TMP_TABLE &&
 
2849
  if (lex_identified_temp_table &&
2830
2850
      (*new_engine)->check_flag(HTON_BIT_TEMPORARY_NOT_SUPPORTED))
2831
2851
  {
2832
2852
    if (create_info->used_fields & HA_CREATE_USED_ENGINE)
2839
2859
    }
2840
2860
    *new_engine= myisam_engine;
2841
2861
  }
2842
 
  if(!(create_info->options & HA_LEX_CREATE_TMP_TABLE)
 
2862
  if(! lex_identified_temp_table
2843
2863
     && (*new_engine)->check_flag(HTON_BIT_TEMPORARY_ONLY))
2844
2864
  {
2845
2865
    my_error(ER_ILLEGAL_HA_CREATE_OPTION, MYF(0),