23 #include <sys/types.h>
27 #include <drizzled/session.h>
28 #include <plugin/myisam/myisam.h>
29 #include <drizzled/plugin/transactional_storage_engine.h>
30 #include <drizzled/statistics_variables.h>
31 #include <drizzled/table.h>
32 #include <drizzled/create_field.h>
37 Singular::Singular(Session *session, std::list<CreateField>& field_list) :
38 _share(message::Table::INTERNAL),
39 _has_variable_width(false)
41 uint32_t field_count= field_list.size();
42 uint32_t blob_count= 0;
43 uint32_t record_length= 0;
44 uint32_t null_count= 0;
45 uint32_t null_pack_length;
47 getMutableShare()->setFields(field_count + 1);
48 setFields(getMutableShare()->getFields(
true));
49 Field** field_arg= getMutableShare()->getFields(
true);
50 getMutableShare()->blob_field.resize(field_count+1);
51 getMutableShare()->setFieldSize(field_count);
52 getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
53 setup_tmp_table_column_bitmaps();
58 message::Table::Field null_field;
59 BOOST_FOREACH(CreateField& it, field_list)
61 *field_arg= getMutableShare()->make_field(null_field,
64 (it.flags & NOT_NULL_FLAG) ?
false :
true,
65 (
unsigned char *) ((it.flags & NOT_NULL_FLAG) ? 0 :
""),
66 (it.flags & NOT_NULL_FLAG) ? 0 : 1,
73 it.flags & UNSIGNED_FLAG ?
true :
false);
74 (*field_arg)->init(
this);
75 record_length+= (*field_arg)->pack_length();
76 if (! ((*field_arg)->flags & NOT_NULL_FLAG))
79 if ((*field_arg)->flags & BLOB_FLAG)
80 getMutableShare()->blob_field[blob_count++]= (uint32_t) (field_arg - getFields());
85 getMutableShare()->blob_field[blob_count]= 0;
86 getMutableShare()->blob_fields= blob_count;
88 null_pack_length= (null_count + 7)/8;
89 getMutableShare()->setRecordLength(record_length + null_pack_length);
90 getMutableShare()->rec_buff_length= ALIGN_SIZE(getMutableShare()->getRecordLength() + 1);
91 record[0]= session->mem.alloc(getMutableShare()->rec_buff_length);
95 null_flags= getInsertRecord();
96 getMutableShare()->null_fields= null_count;
97 getMutableShare()->null_bytes= null_pack_length;
101 unsigned char *null_pos= getInsertRecord();
102 unsigned char *field_pos= null_pos + getMutableShare()->null_bytes;
103 uint32_t null_bit= 1;
105 for (field_arg= getFields(); *field_arg; ++field_arg)
107 Field *cur_field= *field_arg;
108 if ((cur_field->flags & NOT_NULL_FLAG))
109 cur_field->move_field(field_pos);
112 cur_field->move_field(field_pos, (
unsigned char*) null_pos, null_bit);
114 if (null_bit == (1 << 8))
122 field_pos+= cur_field->pack_length();
127 bool Singular::open_tmp_table()
129 if (
int error= cursor->ha_open(getShare()->getTableIdentifier(), O_RDWR, HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE))
131 print_error(error, MYF(0));
135 (void) cursor->extra(HA_EXTRA_QUICK);
169 bool Singular::create_myisam_tmp_table(KeyInfo *keyinfo,
170 MI_COLUMNDEF *start_recinfo,
171 MI_COLUMNDEF **recinfo,
178 if (getShare()->sizeKeys())
180 bool using_unique_constraint=
false;
183 memset(seg, 0,
sizeof(*seg) * keyinfo->key_parts);
184 if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
185 keyinfo->key_parts > cursor->getEngine()->max_key_parts() ||
189 getMutableShare()->keys= 0;
190 getMutableShare()->uniques= 1;
191 using_unique_constraint=
true;
192 memset(&uniquedef, 0,
sizeof(uniquedef));
193 uniquedef.keysegs=keyinfo->key_parts;
195 uniquedef.null_are_equal=1;
198 memset(*recinfo, 0,
sizeof(**recinfo));
199 (*recinfo)->type= FIELD_CHECK;
200 (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
202 getMutableShare()->setRecordLength(getShare()->getRecordLength() + MI_UNIQUE_HASH_LENGTH);
207 memset(&keydef, 0,
sizeof(keydef));
208 keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
209 keydef.keysegs= keyinfo->key_parts;
212 for (uint32_t i= 0; i < keyinfo->key_parts ; i++,seg++)
214 Field *key_field=keyinfo->key_part[i].field;
216 seg->language= key_field->charset()->number;
217 seg->length= keyinfo->key_part[i].length;
218 seg->start= keyinfo->key_part[i].offset;
219 if (key_field->flags & BLOB_FLAG)
221 seg->type= ((keyinfo->key_part[i].key_type & 1 ) ?
222 HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
223 seg->bit_start= (uint8_t)(key_field->pack_length() - getShare()->blob_ptr_size);
224 seg->flag= HA_BLOB_PART;
229 seg->type= keyinfo->key_part[i].type;
231 if (!(key_field->flags & NOT_NULL_FLAG))
233 seg->null_bit= key_field->null_bit;
234 seg->null_pos= (uint32_t) (key_field->null_ptr - getInsertRecord());
240 if (! using_unique_constraint)
241 keydef.flag|= HA_NULL_ARE_EQUAL;
247 if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
249 create_info.data_file_length= ~(uint64_t) 0;
251 if ((error= mi_create(getShare()->getTableName(), getShare()->sizeKeys(), &keydef,
252 (uint32_t) (*recinfo-start_recinfo),
254 getShare()->uniques, &uniquedef,
256 HA_CREATE_TMP_TABLE)))
258 print_error(error, MYF(0));
263 in_use->status_var.created_tmp_disk_tables++;
264 getMutableShare()->db_record_offset= 1;
276 void Singular::setup_tmp_table_column_bitmaps()
278 uint32_t field_count= getShare()->sizeFields();
280 def_read_set.resize(field_count);
281 def_write_set.resize(field_count);
282 tmp_set.resize(field_count);
283 getMutableShare()->all_set.resize(field_count);
284 getMutableShare()->all_set.set();
287 default_column_bitmaps();
290 Singular::~Singular()
292 const char* save_proc_info= in_use->get_proc_info();
293 in_use->set_proc_info(
"removing tmp table");
301 cursor->closeMarkForDelete();
303 drizzled::error_t ignored;
304 plugin::StorageEngine::dropTable(*in_use, *getShare()->getEngine(), getShare()->getTableIdentifier(), ignored);
309 for (Field **ptr= getFields(); *ptr; ptr++)
315 mem().free_root(MYF(0));
316 in_use->set_proc_info(save_proc_info);