~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/key.cc

Removed DBUG symbols and fixed TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
 
17
17
/* Functions to handle keys and fields in forms */
18
18
 
19
 
#include <drizzled/server_includes.h>
 
19
#include "mysql_priv.h"
20
20
 
21
21
/*
22
22
  Search after a key that starts with 'field'
45
45
       key_length is set to length of key before (not including) field
46
46
*/
47
47
 
48
 
int find_ref_key(KEY *key, uint32_t key_count, unsigned char *record, Field *field,
49
 
                 uint32_t *key_length, uint32_t *keypart)
 
48
int find_ref_key(KEY *key, uint key_count, uchar *record, Field *field,
 
49
                 uint *key_length, uint *keypart)
50
50
{
51
51
  register int i;
52
52
  register KEY *key_info;
53
 
  uint32_t fieldpos;
 
53
  uint fieldpos;
54
54
 
55
55
  fieldpos= field->offset(record);
56
56
 
71
71
       i < (int) key_count ;
72
72
       i++, key_info++)
73
73
  {
74
 
    uint32_t j;
 
74
    uint j;
75
75
    KEY_PART_INFO *key_part;
76
76
    *key_length=0;
77
77
    for (j=0, key_part=key_info->key_part ;
105
105
  @param key_length  specifies length of all keyparts that will be copied
106
106
*/
107
107
 
108
 
void key_copy(unsigned char *to_key, unsigned char *from_record, KEY *key_info,
109
 
              unsigned int key_length)
 
108
void key_copy(uchar *to_key, uchar *from_record, KEY *key_info,
 
109
              uint key_length)
110
110
{
111
 
  uint32_t length;
 
111
  uint length;
112
112
  KEY_PART_INFO *key_part;
113
113
 
114
114
  if (key_length == 0)
125
125
        key_part->key_part_flag & HA_VAR_LENGTH_PART)
126
126
    {
127
127
      key_length-= HA_KEY_BLOB_LENGTH;
128
 
      length= cmin((uint16_t)key_length, key_part->length);
 
128
      length= min(key_length, key_part->length);
129
129
      key_part->field->get_key_image(to_key, length, Field::itRAW);
130
130
      to_key+= HA_KEY_BLOB_LENGTH;
131
131
    }
132
132
    else
133
133
    {
134
 
      length= cmin((uint16_t)key_length, key_part->length);
 
134
      length= min(key_length, key_part->length);
135
135
      Field *field= key_part->field;
136
 
      const CHARSET_INFO * const cs= field->charset();
137
 
      uint32_t bytes= field->get_key_image(to_key, length, Field::itRAW);
 
136
      CHARSET_INFO *cs= field->charset();
 
137
      uint bytes= field->get_key_image(to_key, length, Field::itRAW);
138
138
      if (bytes < length)
139
139
        cs->cset->fill(cs, (char*) to_key + bytes, length - bytes, ' ');
140
140
    }
148
148
  Zero the null components of key tuple.
149
149
*/
150
150
 
151
 
void key_zero_nulls(unsigned char *tuple, KEY *key_info)
 
151
void key_zero_nulls(uchar *tuple, KEY *key_info)
152
152
{
153
153
  KEY_PART_INFO *key_part= key_info->key_part;
154
154
  KEY_PART_INFO *key_part_end= key_part + key_info->key_parts;
155
155
  for (; key_part != key_part_end; key_part++)
156
156
  {
157
157
    if (key_part->null_bit && *tuple)
158
 
      memset(tuple+1, 0, key_part->store_length-1);
 
158
      bzero(tuple+1, key_part->store_length-1);
159
159
    tuple+= key_part->store_length;
160
160
  }
161
161
}
173
173
  @param key_length  specifies length of all keyparts that will be restored
174
174
*/
175
175
 
176
 
void key_restore(unsigned char *to_record, unsigned char *from_key, KEY *key_info,
177
 
                 uint16_t key_length)
 
176
void key_restore(uchar *to_record, uchar *from_key, KEY *key_info,
 
177
                 uint key_length)
178
178
{
179
 
  uint32_t length;
 
179
  uint length;
180
180
  KEY_PART_INFO *key_part;
181
181
 
182
182
  if (key_length == 0)
185
185
  }
186
186
  for (key_part= key_info->key_part ; (int) key_length > 0 ; key_part++)
187
187
  {
188
 
    unsigned char used_uneven_bits= 0;
 
188
    uchar used_uneven_bits= 0;
189
189
    if (key_part->null_bit)
190
190
    {
191
191
      if (*from_key++)
203
203
        Maybe this branch is to be removed, but now we
204
204
        have to ignore GCov compaining.
205
205
      */
206
 
      uint32_t blob_length= uint2korr(from_key);
 
206
      uint blob_length= uint2korr(from_key);
207
207
      Field_blob *field= (Field_blob*) key_part->field;
208
208
      from_key+= HA_KEY_BLOB_LENGTH;
209
209
      key_length-= HA_KEY_BLOB_LENGTH;
214
214
    else if (key_part->key_part_flag & HA_VAR_LENGTH_PART)
215
215
    {
216
216
      Field *field= key_part->field;
 
217
      my_bitmap_map *old_map;
217
218
      my_ptrdiff_t ptrdiff= to_record - field->table->record[0];
218
219
      field->move_field_offset(ptrdiff);
219
220
      key_length-= HA_KEY_BLOB_LENGTH;
220
 
      length= cmin(key_length, key_part->length);
 
221
      length= min(key_length, key_part->length);
 
222
      old_map= dbug_tmp_use_all_columns(field->table, field->table->write_set);
221
223
      field->set_key_image(from_key, length);
 
224
      dbug_tmp_restore_column_map(field->table->write_set, old_map);
222
225
      from_key+= HA_KEY_BLOB_LENGTH;
223
226
      field->move_field_offset(-ptrdiff);
224
227
    }
225
228
    else
226
229
    {
227
 
      length= cmin(key_length, key_part->length);
 
230
      length= min(key_length, key_part->length);
228
231
      /* skip the byte with 'uneven' bits, if used */
229
232
      memcpy(to_record + key_part->offset, from_key + used_uneven_bits
230
233
             , (size_t) length - used_uneven_bits);
238
241
/**
239
242
  Compare if a key has changed.
240
243
 
241
 
  @param table          Table
 
244
  @param table          TABLE
242
245
  @param key            key to compare to row
243
246
  @param idx            Index used
244
247
  @param key_length     Length of key
255
258
    1   Key has changed
256
259
*/
257
260
 
258
 
bool key_cmp_if_same(Table *table,const unsigned char *key,uint32_t idx,uint32_t key_length)
 
261
bool key_cmp_if_same(TABLE *table,const uchar *key,uint idx,uint key_length)
259
262
{
260
 
  uint32_t store_length;
 
263
  uint store_length;
261
264
  KEY_PART_INFO *key_part;
262
 
  const unsigned char *key_end= key + key_length;;
 
265
  const uchar *key_end= key + key_length;;
263
266
 
264
267
  for (key_part=table->key_info[idx].key_part;
265
268
       key < key_end ; 
266
269
       key_part++, key+= store_length)
267
270
  {
268
 
    uint32_t length;
 
271
    uint length;
269
272
    store_length= key_part->store_length;
270
273
 
271
274
    if (key_part->null_bit)
285
288
        return 1;
286
289
      continue;
287
290
    }
288
 
    length= cmin((uint) (key_end-key), store_length);
 
291
    length= min((uint) (key_end-key), store_length);
289
292
    if (!(key_part->key_type & (FIELDFLAG_NUMBER+FIELDFLAG_BINARY+
290
293
                                FIELDFLAG_PACK)))
291
294
    {
292
 
      const CHARSET_INFO * const cs= key_part->field->charset();
293
 
      uint32_t char_length= key_part->length / cs->mbmaxlen;
294
 
      const unsigned char *pos= table->record[0] + key_part->offset;
 
295
      CHARSET_INFO *cs= key_part->field->charset();
 
296
      uint char_length= key_part->length / cs->mbmaxlen;
 
297
      const uchar *pos= table->record[0] + key_part->offset;
295
298
      if (length > char_length)
296
299
      {
297
300
        char_length= my_charpos(cs, pos, pos + length, char_length);
298
301
        set_if_smaller(char_length, length);
299
302
      }
300
303
      if (cs->coll->strnncollsp(cs,
301
 
                                (const unsigned char*) key, length,
302
 
                                (const unsigned char*) pos, char_length, 0))
 
304
                                (const uchar*) key, length,
 
305
                                (const uchar*) pos, char_length, 0))
303
306
        return 1;
304
307
      continue;
305
308
    }
323
326
     idx        Key number
324
327
*/
325
328
 
326
 
void key_unpack(String *to,Table *table,uint32_t idx)
 
329
void key_unpack(String *to,TABLE *table,uint idx)
327
330
{
328
331
  KEY_PART_INFO *key_part,*key_part_end;
329
332
  Field *field;
330
333
  String tmp;
 
334
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->read_set);
331
335
 
332
336
  to->length(0);
333
337
  for (key_part=table->key_info[idx].key_part,key_part_end=key_part+
347
351
    }
348
352
    if ((field=key_part->field))
349
353
    {
350
 
      const CHARSET_INFO * const cs= field->charset();
 
354
      CHARSET_INFO *cs= field->charset();
351
355
      field->val_str(&tmp);
352
356
      if (cs->mbmaxlen > 1 &&
353
357
          table->field[key_part->fieldnr - 1]->field_length !=
360
364
          which can break a multi-byte characters in the middle.
361
365
          Align, returning not more than "char_length" characters.
362
366
        */
363
 
        uint32_t charpos, char_length= key_part->length / cs->mbmaxlen;
 
367
        uint charpos, char_length= key_part->length / cs->mbmaxlen;
364
368
        if ((charpos= my_charpos(cs, tmp.ptr(),
365
369
                                 tmp.ptr() + tmp.length(),
366
370
                                 char_length)) < key_part->length)
368
372
      }
369
373
      
370
374
      if (key_part->length < field->pack_length())
371
 
        tmp.length(cmin(tmp.length(),(uint32_t)key_part->length));
 
375
        tmp.length(min(tmp.length(),key_part->length));
372
376
      to->append(tmp);
373
377
    }
374
378
    else
375
379
      to->append(STRING_WITH_LEN("???"));
376
380
  }
377
 
 
 
381
  dbug_tmp_restore_column_map(table->read_set, old_map);
378
382
  return;
379
383
}
380
384
 
384
388
 
385
389
  SYNOPSIS
386
390
    is_key_used()
387
 
      table   Table object with which keys and fields are associated.
 
391
      table   TABLE object with which keys and fields are associated.
388
392
      idx     Key to be checked.
389
393
      fields  Bitmap of fields to be checked.
390
394
 
391
395
  NOTE
392
 
    This function uses Table::tmp_set bitmap so the caller should care
 
396
    This function uses TABLE::tmp_set bitmap so the caller should care
393
397
    about saving/restoring its state if it also uses this bitmap.
394
398
 
395
399
  RETURN VALUE
397
401
    FALSE  Otherwise
398
402
*/
399
403
 
400
 
bool is_key_used(Table *table, uint32_t idx, const MY_BITMAP *fields)
 
404
bool is_key_used(TABLE *table, uint idx, const MY_BITMAP *fields)
401
405
{
402
406
  bitmap_clear_all(&table->tmp_set);
403
407
  table->mark_columns_used_by_index_no_reset(idx, &table->tmp_set);
429
433
    -   1               Key is larger than range
430
434
*/
431
435
 
432
 
int key_cmp(KEY_PART_INFO *key_part, const unsigned char *key, uint32_t key_length)
 
436
int key_cmp(KEY_PART_INFO *key_part, const uchar *key, uint key_length)
433
437
{
434
 
  uint32_t store_length;
 
438
  uint store_length;
435
439
 
436
 
  for (const unsigned char *end=key + key_length;
 
440
  for (const uchar *end=key + key_length;
437
441
       key < end;
438
442
       key+= store_length, key_part++)
439
443
  {
490
494
    and return the result of the comparison.
491
495
*/
492
496
 
493
 
int key_rec_cmp(void *key, unsigned char *first_rec, unsigned char *second_rec)
 
497
int key_rec_cmp(void *key, uchar *first_rec, uchar *second_rec)
494
498
{
495
499
  KEY *key_info= (KEY*)key;
496
 
  uint32_t key_parts= key_info->key_parts, i= 0;
 
500
  uint key_parts= key_info->key_parts, i= 0;
497
501
  KEY_PART_INFO *key_part= key_info->key_part;
498
 
  unsigned char *rec0= key_part->field->ptr - key_part->offset;
 
502
  uchar *rec0= key_part->field->ptr - key_part->offset;
499
503
  my_ptrdiff_t first_diff= first_rec - rec0, sec_diff= second_rec - rec0;
500
504
  int result= 0;
501
505