~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/key.cc

  • Committer: Padraig O'Sullivan
  • Date: 2009-03-21 01:02:23 UTC
  • mto: (960.2.5 mordred)
  • mto: This revision was merged to the branch mainline in revision 961.
  • Revision ID: osullivan.padraig@gmail.com-20090321010223-j8cph7eeyt1u3xol
Fixed function object to ensure it correctly returns a boolean type since
memcmp returns an integer. Added some more comments.

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 "drizzled/server_includes.h"
 
20
#include "drizzled/table.h"
 
21
#include "drizzled/key.h"
 
22
#include "drizzled/field/blob.h"
 
23
 
 
24
#include <string>
 
25
 
 
26
using namespace std;
20
27
 
21
28
/*
22
29
  Search after a key that starts with 'field'
90
97
}
91
98
 
92
99
 
93
 
/**
94
 
  Copy part of a record that forms a key or key prefix to a buffer.
95
 
 
96
 
    The function takes a complete table record (as e.g. retrieved by
97
 
    handler::index_read()), and a description of an index on the same table,
98
 
    and extracts the first key_length bytes of the record which are part of a
99
 
    key into to_key. If length == 0 then copy all bytes from the record that
100
 
    form a key.
101
 
 
102
 
  @param to_key      buffer that will be used as a key
103
 
  @param from_record full record to be copied from
104
 
  @param key_info    descriptor of the index
105
 
  @param key_length  specifies length of all keyparts that will be copied
106
 
*/
107
 
 
108
100
void key_copy(unsigned char *to_key, unsigned char *from_record, KEY *key_info,
109
101
              unsigned int key_length)
110
102
{
144
136
}
145
137
 
146
138
 
 
139
void key_copy(basic_string<unsigned char> &to_key,
 
140
              unsigned char *from_record, KEY *key_info,
 
141
              unsigned int key_length)
 
142
{
 
143
  uint32_t length;
 
144
  KEY_PART_INFO *key_part;
 
145
 
 
146
  if (key_length == 0)
 
147
    key_length= key_info->key_length;
 
148
  for (key_part= key_info->key_part; (int) key_length > 0; key_part++)
 
149
  {
 
150
    if (key_part->null_bit)
 
151
    {
 
152
      to_key.push_back(test(from_record[key_part->null_offset] &
 
153
                       key_part->null_bit) ? '1' : '0');
 
154
      key_length--;
 
155
    }
 
156
    if (key_part->key_part_flag & HA_BLOB_PART ||
 
157
        key_part->key_part_flag & HA_VAR_LENGTH_PART)
 
158
    {
 
159
      key_length-= HA_KEY_BLOB_LENGTH;
 
160
      length= cmin((uint16_t)key_length, key_part->length);
 
161
      key_part->field->get_key_image(to_key, length, Field::itRAW);
 
162
      to_key.append(HA_KEY_BLOB_LENGTH, '0');
 
163
    }
 
164
    else
 
165
    {
 
166
      length= cmin((uint16_t)key_length, key_part->length);
 
167
      Field *field= key_part->field;
 
168
      uint32_t bytes= field->get_key_image(to_key, length, Field::itRAW);
 
169
      if (bytes < length)
 
170
        to_key.append(length-bytes, ' ');
 
171
    }
 
172
    key_length-= length;
 
173
  }
 
174
}
 
175
 
 
176
 
147
177
/**
148
178
  Zero the null components of key tuple.
149
179
*/
262
292
  const unsigned char *key_end= key + key_length;;
263
293
 
264
294
  for (key_part=table->key_info[idx].key_part;
265
 
       key < key_end ; 
 
295
       key < key_end ;
266
296
       key_part++, key+= store_length)
267
297
  {
268
298
    uint32_t length;
270
300
 
271
301
    if (key_part->null_bit)
272
302
    {
273
 
      if (*key != test(table->record[0][key_part->null_offset] & 
 
303
      if (*key != test(table->record[0][key_part->null_offset] &
274
304
                       key_part->null_bit))
275
305
        return 1;
276
306
      if (*key)
285
315
        return 1;
286
316
      continue;
287
317
    }
288
 
    length= cmin((uint) (key_end-key), store_length);
 
318
    length= cmin((uint32_t) (key_end-key), store_length);
289
319
    if (!(key_part->key_type & (FIELDFLAG_NUMBER+FIELDFLAG_BINARY+
290
320
                                FIELDFLAG_PACK)))
291
321
    {
312
342
/*
313
343
  unpack key-fields from record to some buffer.
314
344
 
315
 
  This is used mainly to get a good error message.  We temporary 
 
345
  This is used mainly to get a good error message.  We temporary
316
346
  change the column bitmap so that all columns are readable.
317
347
 
318
348
  @param
361
391
          Align, returning not more than "char_length" characters.
362
392
        */
363
393
        uint32_t charpos, char_length= key_part->length / cs->mbmaxlen;
364
 
        if ((charpos= my_charpos(cs, tmp.ptr(),
365
 
                                 tmp.ptr() + tmp.length(),
 
394
        if ((charpos= my_charpos(cs, tmp.c_ptr(),
 
395
                                 tmp.c_ptr() + tmp.length(),
366
396
                                 char_length)) < key_part->length)
367
397
          tmp.length(charpos);
368
398
      }
369
 
      
 
399
 
370
400
      if (key_part->length < field->pack_length())
371
401
        tmp.length(cmin(tmp.length(),(uint32_t)key_part->length));
372
402
      to->append(tmp);
545
575
  } while (!result && ++i < key_parts);
546
576
  return(result);
547
577
}
 
578
 
 
579
Key::Key(const Key &rhs, MEM_ROOT *mem_root)
 
580
  :type(rhs.type),
 
581
  key_create_info(rhs.key_create_info),
 
582
  columns(rhs.columns, mem_root),
 
583
  name(rhs.name),
 
584
  generated(rhs.generated)
 
585
{
 
586
  list_copy_and_replace_each_value(columns, mem_root);
 
587
}