~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_insert.cc

  • Committer: Monty Taylor
  • Date: 2008-08-09 02:24:34 UTC
  • mto: (266.1.8 codestyle)
  • mto: This revision was merged to the branch mainline in revision 279.
  • Revision ID: monty@inaugust.com-20080809022434-97na8pnugghskxip
Removed mystringslt conv lib and replaced it with a noinst lib.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
#define my_safe_alloca(size, min_length) my_alloca(size)
35
35
#define my_safe_afree(ptr, size, min_length) my_afree(ptr)
36
36
#else
37
 
#define my_safe_alloca(size, min_length) ((size <= min_length) ? my_alloca(size) : malloc(size))
38
 
#define my_safe_afree(ptr, size, min_length) if (size > min_length) free(ptr)
 
37
#define my_safe_alloca(size, min_length) ((size <= min_length) ? my_alloca(size) : my_malloc(size,MYF(0)))
 
38
#define my_safe_afree(ptr, size, min_length) if (size > min_length) my_free(ptr,MYF(0))
39
39
#endif
40
40
 
41
41
 
61
61
    -1          Error
62
62
*/
63
63
 
64
 
static int check_insert_fields(THD *thd, TableList *table_list,
 
64
static int check_insert_fields(THD *thd, TABLE_LIST *table_list,
65
65
                               List<Item> &fields, List<Item> &values,
66
66
                               bool check_unique,
67
67
                               table_map *map __attribute__((unused)))
68
68
{
69
 
  Table *table= table_list->table;
 
69
  TABLE *table= table_list->table;
70
70
 
71
71
  if (fields.elements == 0 && values.elements != 0)
72
72
  {
157
157
    -1          Error
158
158
*/
159
159
 
160
 
static int check_update_fields(THD *thd, TableList *insert_table_list,
 
160
static int check_update_fields(THD *thd, TABLE_LIST *insert_table_list,
161
161
                               List<Item> &update_fields,
162
162
                               table_map *map __attribute__((unused)))
163
163
{
164
 
  Table *table= insert_table_list->table;
 
164
  TABLE *table= insert_table_list->table;
165
165
  bool timestamp_mark= false;
166
166
 
167
167
  if (table->timestamp_field)
224
224
  end of dispatch_command().
225
225
*/
226
226
 
227
 
bool mysql_insert(THD *thd,TableList *table_list,
 
227
bool mysql_insert(THD *thd,TABLE_LIST *table_list,
228
228
                  List<Item> &fields,
229
229
                  List<List_item> &values_list,
230
230
                  List<Item> &update_fields,
236
236
  bool transactional_table, joins_freed= false;
237
237
  bool changed;
238
238
  bool was_insert_delayed= (table_list->lock_type ==  TL_WRITE_DELAYED);
239
 
  uint32_t value_count;
 
239
  uint value_count;
240
240
  ulong counter = 1;
241
241
  uint64_t id;
242
242
  COPY_INFO info;
243
 
  Table *table= 0;
 
243
  TABLE *table= 0;
244
244
  List_iterator_fast<List_item> its(values_list);
245
245
  List_item *values;
246
246
  Name_resolution_context *context;
348
348
  thd->cuted_fields = 0L;
349
349
  table->next_number_field=table->found_next_number_field;
350
350
 
 
351
#ifdef HAVE_REPLICATION
351
352
  if (thd->slave_thread &&
352
353
      (info.handle_duplicates == DUP_UPDATE) &&
353
354
      (table->next_number_field != NULL) &&
354
355
      rpl_master_has_bug(&active_mi->rli, 24432))
355
356
    goto abort;
 
357
#endif
356
358
 
357
359
  error=0;
358
360
  thd_proc_info(thd, "update");
433
435
  {
434
436
    /*
435
437
      Do not do this release if this is a delayed insert, it would steal
436
 
      auto_inc values from the delayed_insert thread as they share Table.
 
438
      auto_inc values from the delayed_insert thread as they share TABLE.
437
439
    */
438
440
    table->file->ha_release_auto_increment();
439
441
    if (table->file->ha_end_bulk_insert() && !error)
549
551
    ::my_ok(thd, (ulong) thd->row_count_func, id, buff);
550
552
  }
551
553
  thd->abort_on_warning= 0;
552
 
  DRIZZLE_INSERT_END();
 
554
  MYSQL_INSERT_END();
553
555
  return(false);
554
556
 
555
557
abort:
558
560
  if (!joins_freed)
559
561
    free_underlaid_joins(thd, &thd->lex->select_lex);
560
562
  thd->abort_on_warning= 0;
561
 
  DRIZZLE_INSERT_END();
 
563
  MYSQL_INSERT_END();
562
564
  return(true);
563
565
}
564
566
 
579
581
     true  ERROR
580
582
*/
581
583
 
582
 
static bool mysql_prepare_insert_check_table(THD *thd, TableList *table_list,
 
584
static bool mysql_prepare_insert_check_table(THD *thd, TABLE_LIST *table_list,
583
585
                                             List<Item> &fields __attribute__((unused)),
584
586
                                             bool select_insert)
585
587
{
634
636
    true  error
635
637
*/
636
638
 
637
 
bool mysql_prepare_insert(THD *thd, TableList *table_list,
638
 
                          Table *table, List<Item> &fields, List_item *values,
 
639
bool mysql_prepare_insert(THD *thd, TABLE_LIST *table_list,
 
640
                          TABLE *table, List<Item> &fields, List_item *values,
639
641
                          List<Item> &update_fields, List<Item> &update_values,
640
642
                          enum_duplicates duplic,
641
643
                          COND **where __attribute__((unused)),
734
736
 
735
737
  if (!select_insert)
736
738
  {
737
 
    TableList *duplicate;
 
739
    TABLE_LIST *duplicate;
738
740
    if ((duplicate= unique_table(thd, table_list, table_list->next_global, 1)))
739
741
    {
740
742
      update_non_unique_table_error(table_list, "INSERT", duplicate);
741
743
      return(true);
742
744
    }
 
745
    select_lex->first_execution= 0;
743
746
  }
744
747
  if (duplic == DUP_UPDATE || duplic == DUP_REPLACE)
745
748
    table->prepare_for_position();
749
752
 
750
753
        /* Check if there is more uniq keys after field */
751
754
 
752
 
static int last_uniq_key(Table *table,uint32_t keynr)
 
755
static int last_uniq_key(TABLE *table,uint keynr)
753
756
{
754
757
  while (++keynr < table->s->keys)
755
758
    if (table->key_info[keynr].flags & HA_NOSAME)
785
788
*/
786
789
 
787
790
 
788
 
int write_record(THD *thd, Table *table,COPY_INFO *info)
 
791
int write_record(THD *thd, TABLE *table,COPY_INFO *info)
789
792
{
790
793
  int error;
791
794
  char *key=0;
803
806
  {
804
807
    while ((error=table->file->ha_write_row(table->record[0])))
805
808
    {
806
 
      uint32_t key_nr;
 
809
      uint key_nr;
807
810
      /*
808
811
        If we do more than one iteration of this loop, from the second one the
809
812
        row will have an explicit value in the autoinc field, which was set at
869
872
            goto err;
870
873
          }
871
874
        }
872
 
        key_copy((unsigned char*) key,table->record[0],table->key_info+key_nr,0);
 
875
        key_copy((uchar*) key,table->record[0],table->key_info+key_nr,0);
873
876
        if ((error=(table->file->index_read_idx_map(table->record[1],key_nr,
874
 
                                                    (unsigned char*) key, HA_WHOLE_KEY,
 
877
                                                    (uchar*) key, HA_WHOLE_KEY,
875
878
                                                    HA_READ_KEY_EXACT))))
876
879
          goto err;
877
880
      }
899
902
        info->touched++;
900
903
        if ((table->file->ha_table_flags() & HA_PARTIAL_COLUMN_READ &&
901
904
             !bitmap_is_subset(table->write_set, table->read_set)) ||
902
 
            table->compare_record())
 
905
            compare_record(table))
903
906
        {
904
907
          if ((error=table->file->ha_update_row(table->record[1],
905
908
                                                table->record[0])) &&
1031
1034
  Check that all fields with arn't null_fields are used
1032
1035
******************************************************************************/
1033
1036
 
1034
 
int check_that_all_fields_are_given_values(THD *thd, Table *entry,
1035
 
                                           TableList *table_list)
 
1037
int check_that_all_fields_are_given_values(THD *thd, TABLE *entry,
 
1038
                                           TABLE_LIST *table_list)
1036
1039
{
1037
1040
  int err= 0;
1038
1041
  MY_BITMAP *write_set= entry->write_set;
1082
1085
{
1083
1086
  LEX *lex= thd->lex;
1084
1087
  SELECT_LEX *select_lex= &lex->select_lex;
 
1088
  TABLE_LIST *first_select_leaf_table;
1085
1089
  
1086
1090
 
1087
1091
  /*
1116
1120
  assert(select_lex->leaf_tables != 0);
1117
1121
  lex->leaf_tables_insert= select_lex->leaf_tables;
1118
1122
  /* skip all leaf tables belonged to view where we are insert */
1119
 
  select_lex->leaf_tables= select_lex->leaf_tables->next_leaf;
 
1123
  for (first_select_leaf_table= select_lex->leaf_tables->next_leaf;
 
1124
       first_select_leaf_table &&
 
1125
       first_select_leaf_table->belong_to_view &&
 
1126
       first_select_leaf_table->belong_to_view ==
 
1127
       lex->leaf_tables_insert->belong_to_view;
 
1128
       first_select_leaf_table= first_select_leaf_table->next_leaf)
 
1129
  {}
 
1130
  select_lex->leaf_tables= first_select_leaf_table;
1120
1131
  return(false);
1121
1132
}
1122
1133
 
1123
1134
 
1124
 
select_insert::select_insert(TableList *table_list_par, Table *table_par,
 
1135
select_insert::select_insert(TABLE_LIST *table_list_par, TABLE *table_par,
1125
1136
                             List<Item> *fields_par,
1126
1137
                             List<Item> *update_fields,
1127
1138
                             List<Item> *update_values,
1215
1226
      while ((item= li++))
1216
1227
      {
1217
1228
        item->transform(&Item::update_value_transformer,
1218
 
                        (unsigned char*)lex->current_select);
 
1229
                        (uchar*)lex->current_select);
1219
1230
      }
1220
1231
    }
1221
1232
 
1258
1269
  restore_record(table,s->default_values);              // Get empty record
1259
1270
  table->next_number_field=table->found_next_number_field;
1260
1271
 
 
1272
#ifdef HAVE_REPLICATION
1261
1273
  if (thd->slave_thread &&
1262
1274
      (info.handle_duplicates == DUP_UPDATE) &&
1263
1275
      (table->next_number_field != NULL) &&
1264
1276
      rpl_master_has_bug(&active_mi->rli, 24432))
1265
1277
    return(1);
 
1278
#endif
1266
1279
 
1267
1280
  thd->cuted_fields=0;
1268
1281
  if (info.ignore || info.handle_duplicates != DUP_ERROR)
1386
1399
    fill_record(thd, table->field, values, 1);
1387
1400
}
1388
1401
 
1389
 
void select_insert::send_error(uint32_t errcode,const char *err)
 
1402
void select_insert::send_error(uint errcode,const char *err)
1390
1403
{
1391
1404
  
1392
1405
 
1514
1527
***************************************************************************/
1515
1528
 
1516
1529
/*
1517
 
  Create table from lists of fields and items (or just return Table
 
1530
  Create table from lists of fields and items (or just return TABLE
1518
1531
  object for pre-opened existing table).
1519
1532
 
1520
1533
  SYNOPSIS
1522
1535
      thd          in     Thread object
1523
1536
      create_info  in     Create information (like MAX_ROWS, ENGINE or
1524
1537
                          temporary table flag)
1525
 
      create_table in     Pointer to TableList object providing database
 
1538
      create_table in     Pointer to TABLE_LIST object providing database
1526
1539
                          and name for table to be created or to be open
1527
1540
      alter_info   in/out Initial list of columns and indexes for the table
1528
1541
                          to be created
1529
1542
      items        in     List of items which should be used to produce rest
1530
1543
                          of fields for the table (corresponding fields will
1531
1544
                          be added to the end of alter_info->create_list)
1532
 
      lock         out    Pointer to the DRIZZLE_LOCK object for table created
 
1545
      lock         out    Pointer to the MYSQL_LOCK object for table created
1533
1546
                          (or open temporary table) will be returned in this
1534
1547
                          parameter. Since this table is not included in
1535
1548
                          THD::lock caller is responsible for explicitly
1540
1553
    This function behaves differently for base and temporary tables:
1541
1554
    - For base table we assume that either table exists and was pre-opened
1542
1555
      and locked at open_and_lock_tables() stage (and in this case we just
1543
 
      emit error or warning and return pre-opened Table object) or special
 
1556
      emit error or warning and return pre-opened TABLE object) or special
1544
1557
      placeholder was put in table cache that guarantees that this table
1545
1558
      won't be created or opened until the placeholder will be removed
1546
1559
      (so there is an exclusive lock on this table).
1551
1564
    SELECT it should be changed before it can be used in other contexts.
1552
1565
 
1553
1566
  RETURN VALUES
1554
 
    non-zero  Pointer to Table object for table created or opened
 
1567
    non-zero  Pointer to TABLE object for table created or opened
1555
1568
    0         Error
1556
1569
*/
1557
1570
 
1558
 
static Table *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
1559
 
                                      TableList *create_table,
 
1571
static TABLE *create_table_from_items(THD *thd, HA_CREATE_INFO *create_info,
 
1572
                                      TABLE_LIST *create_table,
1560
1573
                                      Alter_info *alter_info,
1561
1574
                                      List<Item> *items,
1562
 
                                      DRIZZLE_LOCK **lock,
 
1575
                                      MYSQL_LOCK **lock,
1563
1576
                                      TABLEOP_HOOKS *hooks)
1564
1577
{
1565
 
  Table tmp_table;              // Used during 'Create_field()'
 
1578
  TABLE tmp_table;              // Used during 'Create_field()'
1566
1579
  TABLE_SHARE share;
1567
 
  Table *table= 0;
1568
 
  uint32_t select_field_count= items->elements;
 
1580
  TABLE *table= 0;
 
1581
  uint select_field_count= items->elements;
1569
1582
  /* Add selected items to field list */
1570
1583
  List_iterator_fast<Item> it(*items);
1571
1584
  Item *item;
1599
1612
  tmp_table.s->db_low_byte_first= 
1600
1613
        test(create_info->db_type == myisam_hton ||
1601
1614
             create_info->db_type == heap_hton);
1602
 
  tmp_table.null_row= false;
1603
 
  tmp_table.maybe_null= false;
 
1615
  tmp_table.null_row=tmp_table.maybe_null=0;
1604
1616
 
1605
1617
  while ((item=it++))
1606
1618
  {
1638
1650
    binlog when a HEAP table is opened for the first time since startup, must
1639
1651
    not be written: 1) it would be wrong (imagine we're in CREATE SELECT: we
1640
1652
    don't want to delete from it) 2) it would be written before the CREATE
1641
 
    Table, which is a wrong order. So we keep binary logging disabled when we
 
1653
    TABLE, which is a wrong order. So we keep binary logging disabled when we
1642
1654
    open_table().
1643
1655
  */
1644
1656
  {
1662
1674
 
1663
1675
      if (!(create_info->options & HA_LEX_CREATE_TMP_TABLE))
1664
1676
      {
1665
 
        pthread_mutex_lock(&LOCK_open);
 
1677
        VOID(pthread_mutex_lock(&LOCK_open));
1666
1678
        if (reopen_name_locked_table(thd, create_table, false))
1667
1679
        {
1668
1680
          quick_rm_table(create_info->db_type, create_table->db,
1671
1683
        }
1672
1684
        else
1673
1685
          table= create_table->table;
1674
 
        pthread_mutex_unlock(&LOCK_open);
 
1686
        VOID(pthread_mutex_unlock(&LOCK_open));
1675
1687
      }
1676
1688
      else
1677
1689
      {
1678
 
        if (!(table= open_table(thd, create_table, (bool*) 0,
1679
 
                                DRIZZLE_OPEN_TEMPORARY_ONLY)) &&
 
1690
        if (!(table= open_table(thd, create_table, thd->mem_root, (bool*) 0,
 
1691
                                MYSQL_OPEN_TEMPORARY_ONLY)) &&
1680
1692
            !create_info->table_existed)
1681
1693
        {
1682
1694
          /*
1696
1708
  table->reginfo.lock_type=TL_WRITE;
1697
1709
  hooks->prelock(&table, 1);                    // Call prelock hooks
1698
1710
  if (! ((*lock)= mysql_lock_tables(thd, &table, 1,
1699
 
                                    DRIZZLE_LOCK_IGNORE_FLUSH, &not_used)) ||
 
1711
                                    MYSQL_LOCK_IGNORE_FLUSH, &not_used)) ||
1700
1712
        hooks->postlock(&table, 1))
1701
1713
  {
1702
1714
    if (*lock)
1716
1728
int
1717
1729
select_create::prepare(List<Item> &values, SELECT_LEX_UNIT *u)
1718
1730
{
1719
 
  DRIZZLE_LOCK *extra_lock= NULL;
 
1731
  MYSQL_LOCK *extra_lock= NULL;
1720
1732
  
1721
1733
 
1722
1734
  TABLEOP_HOOKS *hook_ptr= NULL;
1740
1752
   */
1741
1753
  class MY_HOOKS : public TABLEOP_HOOKS {
1742
1754
  public:
1743
 
    MY_HOOKS(select_create *x, TableList *create_table,
1744
 
             TableList *select_tables)
 
1755
    MY_HOOKS(select_create *x, TABLE_LIST *create_table,
 
1756
             TABLE_LIST *select_tables)
1745
1757
      : ptr(x), all_tables(*create_table)
1746
1758
      {
1747
1759
        all_tables.next_global= select_tables;
1748
1760
      }
1749
1761
 
1750
1762
  private:
1751
 
    virtual int do_postlock(Table **tables, uint32_t count)
 
1763
    virtual int do_postlock(TABLE **tables, uint count)
1752
1764
    {
1753
1765
      THD *thd= const_cast<THD*>(ptr->get_thd());
1754
1766
      if (int error= decide_logging_format(thd, &all_tables))
1755
1767
        return error;
1756
1768
 
1757
 
      Table const *const table = *tables;
 
1769
      TABLE const *const table = *tables;
1758
1770
      if (thd->current_stmt_binlog_row_based  &&
1759
1771
          !table->s->tmp_table &&
1760
1772
          !ptr->get_create_info()->table_existed)
1765
1777
    }
1766
1778
 
1767
1779
    select_create *ptr;
1768
 
    TableList all_tables;
 
1780
    TABLE_LIST all_tables;
1769
1781
  };
1770
1782
 
1771
1783
  MY_HOOKS hooks(this, create_table, select_tables);
1836
1848
}
1837
1849
 
1838
1850
void
1839
 
select_create::binlog_show_create_table(Table **tables, uint32_t count)
 
1851
select_create::binlog_show_create_table(TABLE **tables, uint count)
1840
1852
{
1841
1853
  /*
1842
1854
    Note 1: In RBR mode, we generate a CREATE TABLE statement for the
1861
1873
  char buf[2048];
1862
1874
  String query(buf, sizeof(buf), system_charset_info);
1863
1875
  int result;
1864
 
  TableList tmp_table_list;
 
1876
  TABLE_LIST tmp_table_list;
1865
1877
 
1866
1878
  memset(&tmp_table_list, 0, sizeof(tmp_table_list));
1867
1879
  tmp_table_list.table = *tables;
1882
1894
}
1883
1895
 
1884
1896
 
1885
 
void select_create::send_error(uint32_t errcode,const char *err)
 
1897
void select_create::send_error(uint errcode,const char *err)
1886
1898
{
1887
1899
  
1888
1900