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>
33#include <drizzled/key_part_info.h>
38Singular::Singular(Session *session, std::list<CreateField>& field_list) :
39 _share(message::Table::INTERNAL),
40 _has_variable_width(false)
42 uint32_t field_count= field_list.size();
43 uint32_t blob_count= 0;
44 uint32_t record_length= 0;
45 uint32_t null_count= 0;
46 uint32_t null_pack_length;
48 getMutableShare()->setFields(field_count + 1);
49 setFields(getMutableShare()->getFields(
true));
50 Field** field_arg= getMutableShare()->getFields(
true);
51 getMutableShare()->blob_field.resize(field_count+1);
52 getMutableShare()->setFieldSize(field_count);
53 getMutableShare()->blob_ptr_size= portable_sizeof_char_ptr;
54 setup_tmp_table_column_bitmaps();
59 message::Table::Field null_field;
60 for (CreateField& it : field_list)
62 *field_arg= getMutableShare()->make_field(null_field,
65 (it.flags & NOT_NULL_FLAG) ?
false :
true,
66 (
unsigned char *) ((it.flags & NOT_NULL_FLAG) ? 0 :
""),
67 (it.flags & NOT_NULL_FLAG) ? 0 : 1,
74 it.flags & UNSIGNED_FLAG ?
true :
false);
75 (*field_arg)->init(
this);
76 record_length+= (*field_arg)->pack_length();
77 if (! ((*field_arg)->flags & NOT_NULL_FLAG))
80 if ((*field_arg)->flags & BLOB_FLAG)
81 getMutableShare()->blob_field[blob_count++]= (uint32_t) (field_arg - getFields());
86 getMutableShare()->blob_field[blob_count]= 0;
87 getMutableShare()->blob_fields= blob_count;
89 null_pack_length= (null_count + 7)/8;
90 getMutableShare()->setRecordLength(record_length + null_pack_length);
91 getMutableShare()->rec_buff_length= ALIGN_SIZE(getMutableShare()->getRecordLength() + 1);
92 record[0]= session->mem.alloc(getMutableShare()->rec_buff_length);
96 null_flags= getInsertRecord();
97 getMutableShare()->null_fields= null_count;
98 getMutableShare()->null_bytes= null_pack_length;
102 unsigned char *null_pos= getInsertRecord();
103 unsigned char *field_pos= null_pos + getMutableShare()->null_bytes;
104 uint32_t null_bit= 1;
106 for (field_arg= getFields(); *field_arg; ++field_arg)
108 Field *cur_field= *field_arg;
109 if ((cur_field->flags & NOT_NULL_FLAG))
110 cur_field->move_field(field_pos);
113 cur_field->move_field(field_pos, (
unsigned char*) null_pos, null_bit);
115 if (null_bit == (1 << 8))
123 field_pos+= cur_field->pack_length();
128bool Singular::open_tmp_table()
130 if (
int error= cursor->ha_open(getShare()->getTableIdentifier(), O_RDWR, HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE))
132 print_error(error, MYF(0));
136 (void) cursor->extra(HA_EXTRA_QUICK);
170bool Singular::create_myisam_tmp_table(KeyInfo *keyinfo,
171 MI_COLUMNDEF *start_recinfo,
172 MI_COLUMNDEF **recinfo,
177 MI_UNIQUEDEF uniquedef;
179 if (getShare()->sizeKeys())
181 bool using_unique_constraint=
false;
182 HA_KEYSEG *seg=
new (mem()) HA_KEYSEG[keyinfo->key_parts];
184 memset(seg, 0,
sizeof(*seg) * keyinfo->key_parts);
185 if (keyinfo->key_length >= cursor->getEngine()->max_key_length() ||
186 keyinfo->key_parts > cursor->getEngine()->max_key_parts() ||
190 getMutableShare()->keys= 0;
191 getMutableShare()->uniques= 1;
192 using_unique_constraint=
true;
193 memset(&uniquedef, 0,
sizeof(uniquedef));
194 uniquedef.keysegs=keyinfo->key_parts;
196 uniquedef.null_are_equal=1;
199 memset(*recinfo, 0,
sizeof(**recinfo));
200 (*recinfo)->type= FIELD_CHECK;
201 (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
203 getMutableShare()->setRecordLength(getShare()->getRecordLength() + MI_UNIQUE_HASH_LENGTH);
208 memset(&keydef, 0,
sizeof(keydef));
209 keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
210 keydef.keysegs= keyinfo->key_parts;
213 for (uint32_t i= 0; i < keyinfo->key_parts ; i++,seg++)
215 Field *key_field=keyinfo->key_part[i].field;
217 seg->language= key_field->charset()->number;
218 seg->length= keyinfo->key_part[i].length;
219 seg->start= keyinfo->key_part[i].offset;
220 if (key_field->flags & BLOB_FLAG)
222 seg->type= ((keyinfo->key_part[i].key_type & 1 ) ?
223 HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
224 seg->bit_start= (uint8_t)(key_field->pack_length() - getShare()->blob_ptr_size);
225 seg->flag= HA_BLOB_PART;
230 seg->type= keyinfo->key_part[i].type;
232 if (!(key_field->flags & NOT_NULL_FLAG))
234 seg->null_bit= key_field->null_bit;
235 seg->null_pos= (uint32_t) (key_field->null_ptr - getInsertRecord());
241 if (! using_unique_constraint)
242 keydef.flag|= HA_NULL_ARE_EQUAL;
246 MI_CREATE_INFO create_info;
248 if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
250 create_info.data_file_length= ~(uint64_t) 0;
252 if ((error= mi_create(getShare()->getTableName(), getShare()->sizeKeys(), &keydef,
253 (uint32_t) (*recinfo-start_recinfo),
255 getShare()->uniques, &uniquedef,
257 HA_CREATE_TMP_TABLE)))
259 print_error(error, MYF(0));
264 in_use->status_var.created_tmp_disk_tables++;
265 getMutableShare()->db_record_offset= 1;
277void Singular::setup_tmp_table_column_bitmaps()
279 uint32_t field_count= getShare()->sizeFields();
281 def_read_set.resize(field_count);
282 def_write_set.resize(field_count);
283 tmp_set.resize(field_count);
284 getMutableShare()->all_set.resize(field_count);
285 getMutableShare()->all_set.set();
288 default_column_bitmaps();
293 const char* save_proc_info= in_use->get_proc_info();
294 in_use->set_proc_info(
"removing tmp table");
297 plugin::TransactionalStorageEngine::releaseTemporaryLatches(in_use);
302 cursor->closeMarkForDelete();
304 drizzled::error_t ignored;
305 plugin::StorageEngine::dropTable(*in_use, *getShare()->getEngine(), getShare()->getTableIdentifier(), ignored);
310 for (Field **ptr= getFields(); *ptr; ptr++)
316 mem().free_root(MYF(0));
317 in_use->set_proc_info(save_proc_info);
TODO: Rename this file - func.h is stupid.