63
64
#include "drizzled/field/str.h"
64
65
#include "drizzled/field/num.h"
65
66
#include "drizzled/field/blob.h"
66
#include "drizzled/field/boolean.h"
67
67
#include "drizzled/field/enum.h"
68
68
#include "drizzled/field/null.h"
69
69
#include "drizzled/field/date.h"
70
70
#include "drizzled/field/decimal.h"
71
71
#include "drizzled/field/real.h"
72
72
#include "drizzled/field/double.h"
73
#include "drizzled/field/int32.h"
74
#include "drizzled/field/int64.h"
75
#include "drizzled/field/size.h"
73
#include "drizzled/field/long.h"
74
#include "drizzled/field/int64_t.h"
76
75
#include "drizzled/field/num.h"
77
#include "drizzled/field/time.h"
78
#include "drizzled/field/epoch.h"
76
#include "drizzled/field/timestamp.h"
79
77
#include "drizzled/field/datetime.h"
80
#include "drizzled/field/microtime.h"
81
78
#include "drizzled/field/varstring.h"
82
#include "drizzled/field/uuid.h"
84
#include "drizzled/plugin/storage_engine.h"
86
#include "drizzled/definition/cache.h"
88
#include <drizzled/refresh_version.h>
90
80
using namespace std;
95
85
extern size_t table_def_size;
98
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
87
/*****************************************************************************
88
Functions to handle table definition cach (TableShare)
89
*****************************************************************************/
92
Mark that we are not using table share anymore.
99
If ref_count goes to zero and (we have done a refresh or if we have
100
already too many open table shares) then delete the definition.
103
void TableShare::release(TableShare *share)
105
bool to_be_deleted= false;
106
safe_mutex_assert_owner(LOCK_open.native_handle);
109
if (!--share->ref_count)
116
TableIdentifier identifier(share->getSchemaName(), share->getTableName());
117
plugin::EventObserver::deregisterTableEvents(*share);
119
definition::Cache::singleton().erase(identifier);
125
void TableShare::release(TableSharePtr &share)
127
bool to_be_deleted= false;
128
safe_mutex_assert_owner(LOCK_open.native_handle);
131
if (!--share->ref_count)
138
TableIdentifier identifier(share->getSchemaName(), share->getTableName());
139
plugin::EventObserver::deregisterTableEvents(*share);
141
definition::Cache::singleton().erase(identifier);
147
void TableShare::release(TableIdentifier &identifier)
149
TableSharePtr share= definition::Cache::singleton().find(identifier);
152
share->version= 0; // Mark for delete
153
if (share->ref_count == 0)
156
plugin::EventObserver::deregisterTableEvents(*share);
157
definition::Cache::singleton().erase(identifier);
163
static TableSharePtr foundTableShare(TableSharePtr share)
166
We found an existing table definition. Return it if we didn't get
167
an error when reading the table definition from file.
170
/* We must do a lock to ensure that the structure is initialized */
173
/* Table definition contained an error */
174
share->open_table_error(share->error, share->open_errno, share->errarg);
176
return TableSharePtr();
179
share->incrementTableCount();
185
Get TableShare for a table.
188
session Thread handle
189
table_list Table that should be opened
191
key_length Length of key
192
error out: Error code from open_table_def()
195
Get a table definition from the table definition cache.
196
If it doesn't exist, create a new from the table definition file.
199
We must have wrlock on LOCK_open when we come here
200
(To be changed later)
207
TableSharePtr TableShare::getShareCreate(Session *session,
208
TableIdentifier &identifier,
215
/* Read table definition from cache */
216
if ((share= definition::Cache::singleton().find(identifier)))
217
return foundTableShare(share);
219
share.reset(new TableShare(message::Table::STANDARD, identifier));
222
Lock mutex to be able to read table definition from file without
227
bool ret= definition::Cache::singleton().insert(identifier, share);
230
return TableSharePtr();
232
if (share->open_table_def(*session, identifier))
234
*in_error= share->error;
235
definition::Cache::singleton().erase(identifier);
237
return TableSharePtr();
239
share->ref_count++; // Mark in use
241
plugin::EventObserver::registerTableEvents(*share);
250
Check if table definition exits in cache
253
get_cached_table_share()
255
table_name Table name
259
# TableShare for table
261
TableSharePtr TableShare::getShare(TableIdentifier &identifier)
263
safe_mutex_assert_owner(LOCK_open.native_handle);
265
return definition::Cache::singleton().find(identifier);
268
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
270
enum_field_types field_type;
272
switch(proto_field_type)
102
274
case message::Table::Field::INTEGER:
103
return DRIZZLE_TYPE_LONG;
275
field_type= DRIZZLE_TYPE_LONG;
105
277
case message::Table::Field::DOUBLE:
106
return DRIZZLE_TYPE_DOUBLE;
108
case message::Table::Field::EPOCH:
109
if (field.has_time_options() and field.time_options().microseconds())
110
return DRIZZLE_TYPE_MICROTIME;
112
return DRIZZLE_TYPE_TIMESTAMP;
278
field_type= DRIZZLE_TYPE_DOUBLE;
280
case message::Table::Field::TIMESTAMP:
281
field_type= DRIZZLE_TYPE_TIMESTAMP;
114
283
case message::Table::Field::BIGINT:
115
return DRIZZLE_TYPE_LONGLONG;
284
field_type= DRIZZLE_TYPE_LONGLONG;
117
286
case message::Table::Field::DATETIME:
118
return DRIZZLE_TYPE_DATETIME;
287
field_type= DRIZZLE_TYPE_DATETIME;
120
289
case message::Table::Field::DATE:
121
return DRIZZLE_TYPE_DATE;
290
field_type= DRIZZLE_TYPE_DATE;
123
292
case message::Table::Field::VARCHAR:
124
return DRIZZLE_TYPE_VARCHAR;
293
field_type= DRIZZLE_TYPE_VARCHAR;
126
295
case message::Table::Field::DECIMAL:
127
return DRIZZLE_TYPE_DECIMAL;
296
field_type= DRIZZLE_TYPE_DECIMAL;
129
298
case message::Table::Field::ENUM:
130
return DRIZZLE_TYPE_ENUM;
299
field_type= DRIZZLE_TYPE_ENUM;
132
301
case message::Table::Field::BLOB:
133
return DRIZZLE_TYPE_BLOB;
135
case message::Table::Field::UUID:
136
return DRIZZLE_TYPE_UUID;
138
case message::Table::Field::BOOLEAN:
139
return DRIZZLE_TYPE_BOOLEAN;
141
case message::Table::Field::TIME:
142
return DRIZZLE_TYPE_TIME;
302
field_type= DRIZZLE_TYPE_BLOB;
305
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
148
312
static Item *default_value_item(enum_field_types field_type,
368
539
path.str= (char *)"";
369
540
normalized_path.str= path.str;
370
541
path.length= normalized_path.length= 0;
372
std::string tb_name(identifier.getTableName());
373
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
374
assert(strcmp(tb_name.c_str(), table_name.str) == 0);
542
assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
376
543
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
380
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
547
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
381
548
table_category(TABLE_UNKNOWN_CATEGORY),
382
549
found_next_number_field(NULL),
383
550
timestamp_field(NULL),
385
552
mem_root(TABLE_ALLOC_BLOCK_SIZE),
389
table_name(NULL_LEX_STRING),
390
path(NULL_LEX_STRING),
391
normalized_path(NULL_LEX_STRING),
394
556
timestamp_offset(0),
396
558
stored_rec_length(0),
398
_table_message(NULL),
399
561
storage_engine(NULL),
400
562
tmp_table(identifier.getType()),
403
565
last_null_bit_pos(0),
405
567
rec_buff_length(0),
576
777
table_name.str= db.str + db.length + 1;
577
778
table_name.length= strlen(table_name.str);
579
getTableMessage()->set_name(identifier_arg.getTableName());
580
getTableMessage()->set_schema(identifier_arg.getSchemaName());
780
table_proto->set_name(identifier_arg.getTableName());
781
table_proto->set_schema(identifier_arg.getSchemaName());
583
bool TableShare::parse_table_proto(Session& session, message::Table &table)
784
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
585
drizzled::error_t local_error= EE_OK;
587
788
if (! table.IsInitialized())
589
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
590
table.name().empty() ? " " : table.name().c_str(),
591
table.InitializationErrorString().c_str());
790
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
593
791
return ER_CORRUPT_TABLE_DEFINITION;
596
setTableMessage(table);
794
setTableProto(new(nothrow) message::Table(table));
598
796
storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
599
797
assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
779
984
keys_for_keyread.reset();
780
985
set_prefix(keys_in_use, keys);
782
_field_size= table.field_size();
987
fields= table.field_size();
784
setFields(_field_size + 1);
785
_fields[_field_size]= NULL;
989
setFields(fields + 1);
787
992
uint32_t local_null_fields= 0;
790
std::vector<uint32_t> field_offsets;
791
std::vector<uint32_t> field_pack_length;
995
vector<uint32_t> field_offsets;
996
vector<uint32_t> field_pack_length;
793
field_offsets.resize(_field_size);
794
field_pack_length.resize(_field_size);
998
field_offsets.resize(fields);
999
field_pack_length.resize(fields);
796
1001
uint32_t interval_count= 0;
797
1002
uint32_t interval_parts= 0;
799
1004
uint32_t stored_columns_reclength= 0;
801
for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1006
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
803
1008
message::Table::Field pfield= table.field(fieldnr);
804
if (pfield.constraints().is_nullable()) // Historical reference
808
else if (not pfield.constraints().is_notnull())
1009
if (pfield.constraints().is_nullable())
1010
local_null_fields++;
813
enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
1012
enum_field_types drizzle_field_type=
1013
proto_field_type_to_drizzle_type(pfield.type());
815
1015
field_offsets[fieldnr]= stored_columns_reclength;
1164
1371
case DRIZZLE_TYPE_LONGLONG:
1166
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1167
field_length= MAX_BIGINT_WIDTH+sign_len;
1170
case DRIZZLE_TYPE_UUID:
1171
field_length= field::Uuid::max_string_length();
1173
case DRIZZLE_TYPE_BOOLEAN:
1174
field_length= field::Boolean::max_string_length();
1176
case DRIZZLE_TYPE_MICROTIME:
1177
field_length= field::Microtime::max_string_length();
1179
case DRIZZLE_TYPE_TIMESTAMP:
1180
field_length= field::Epoch::max_string_length();
1182
case DRIZZLE_TYPE_TIME:
1183
field_length= field::Time::max_string_length();
1372
field_length= MAX_BIGINT_WIDTH;
1185
1374
case DRIZZLE_TYPE_NULL:
1186
1375
abort(); // Programming error
1189
bool is_not_null= false;
1191
if (not pfield.constraints().is_nullable())
1195
else if (pfield.constraints().is_notnull())
1200
Field* f= make_field(pfield,
1201
record + field_offsets[fieldnr] + data_offset,
1209
MTYP_TYPENR(unireg_type),
1210
((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1211
getTableMessage()->field(fieldnr).name().c_str());
1213
_fields[fieldnr]= f;
1215
// Insert post make_field code here.
1218
case DRIZZLE_TYPE_BLOB:
1219
case DRIZZLE_TYPE_VARCHAR:
1220
case DRIZZLE_TYPE_DOUBLE:
1221
case DRIZZLE_TYPE_DECIMAL:
1222
case DRIZZLE_TYPE_TIMESTAMP:
1223
case DRIZZLE_TYPE_TIME:
1224
case DRIZZLE_TYPE_DATETIME:
1225
case DRIZZLE_TYPE_MICROTIME:
1226
case DRIZZLE_TYPE_DATE:
1227
case DRIZZLE_TYPE_ENUM:
1228
case DRIZZLE_TYPE_LONG:
1229
case DRIZZLE_TYPE_LONGLONG:
1230
case DRIZZLE_TYPE_NULL:
1231
case DRIZZLE_TYPE_UUID:
1232
case DRIZZLE_TYPE_BOOLEAN:
1378
Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1380
pfield.constraints().is_nullable(),
1386
(Field::utype) MTYP_TYPENR(unireg_type),
1387
((field_type == DRIZZLE_TYPE_ENUM) ?
1388
&intervals[interval_nr++]
1390
getTableProto()->field(fieldnr).name().c_str());
1236
1394
// This needs to go, we should be setting the "use" on the field so that
1237
1395
// it does not reference the share/table.
1525
1706
6 Unknown .frm version
1528
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1709
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1530
drizzled::error_t local_error= EE_OK;
1532
message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1534
if (table and table->IsInitialized())
1717
message::Table table;
1719
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1721
if (local_error != EEXIST)
1536
if (parse_table_proto(session, *table))
1723
if (local_error > 0)
1538
local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1539
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1543
setTableCategory(TABLE_CATEGORY_USER);
1730
if (not table.IsInitialized())
1547
else if (table and not table->IsInitialized())
1549
local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1550
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1554
local_error= ER_TABLE_UNKNOWN;
1555
my_error(ER_TABLE_UNKNOWN, identifier);
1558
return static_cast<int>(local_error);
1738
local_error= parse_table_proto(session, table);
1740
setTableCategory(TABLE_CATEGORY_USER);
1743
if (local_error && !error_given)
1746
open_table_error(error, (open_errno= errno), 0);
1966
2131
false /* is_unsigned */);
1967
case DRIZZLE_TYPE_UUID:
1968
return new (&mem_root) field::Uuid(ptr,
1973
case DRIZZLE_TYPE_BOOLEAN:
1974
return new (&mem_root) field::Boolean(ptr,
1980
2132
case DRIZZLE_TYPE_LONG:
1981
return new (&mem_root) field::Int32(ptr,
2133
return new (&mem_root) Field_long(ptr,
2140
false /* is_unsigned */);
1987
2141
case DRIZZLE_TYPE_LONGLONG:
1991
return new (&mem_root) field::Size(ptr,
1999
return new (&mem_root) field::Int64(ptr,
2006
case DRIZZLE_TYPE_MICROTIME:
2007
return new (&mem_root) field::Microtime(ptr,
2142
return new (&mem_root) Field_int64_t(ptr,
2149
false /* is_unsigned */);
2013
2150
case DRIZZLE_TYPE_TIMESTAMP:
2014
return new (&mem_root) field::Epoch(ptr,
2020
case DRIZZLE_TYPE_TIME:
2021
return new (&mem_root) field::Time(ptr,
2151
return new (&mem_root) Field_timestamp(ptr,
2027
2159
case DRIZZLE_TYPE_DATE:
2028
2160
return new (&mem_root) Field_date(ptr,