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
80
#include "drizzled/definition/cache.h"
88
#include <drizzled/refresh_version.h>
90
82
using namespace std;
95
87
extern size_t table_def_size;
98
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
89
/*****************************************************************************
90
Functions to handle table definition cach (TableShare)
91
*****************************************************************************/
94
Mark that we are not using table share anymore.
101
If ref_count goes to zero and (we have done a refresh or if we have
102
already too many open table shares) then delete the definition.
105
void TableShare::release(TableShare *share)
107
bool to_be_deleted= false;
108
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
111
if (!--share->ref_count)
119
definition::Cache::singleton().erase(share->getCacheKey());
123
void TableShare::release(TableShare::shared_ptr &share)
125
bool to_be_deleted= false;
126
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
129
if (!--share->ref_count)
137
definition::Cache::singleton().erase(share->getCacheKey());
141
void TableShare::release(const TableIdentifier &identifier)
143
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
146
share->version= 0; // Mark for delete
147
if (share->ref_count == 0)
149
definition::Cache::singleton().erase(identifier.getKey());
155
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
158
We found an existing table definition. Return it if we didn't get
159
an error when reading the table definition from file.
162
/* We must do a lock to ensure that the structure is initialized */
165
/* Table definition contained an error */
166
share->open_table_error(share->error, share->open_errno, share->errarg);
168
return TableShare::shared_ptr();
171
share->incrementTableCount();
177
Get TableShare for a table.
180
session Thread handle
181
table_list Table that should be opened
183
key_length Length of key
184
error out: Error code from open_table_def()
187
Get a table definition from the table definition cache.
188
If it doesn't exist, create a new from the table definition file.
191
We must have wrlock on table::Cache::singleton().mutex() when we come here
192
(To be changed later)
199
TableShare::shared_ptr TableShare::getShareCreate(Session *session,
200
const TableIdentifier &identifier,
203
TableShare::shared_ptr share;
207
/* Read table definition from cache */
208
if ((share= definition::Cache::singleton().find(identifier.getKey())))
209
return foundTableShare(share);
211
share.reset(new TableShare(message::Table::STANDARD, identifier));
213
if (share->open_table_def(*session, identifier))
215
in_error= share->error;
217
return TableShare::shared_ptr();
219
share->ref_count++; // Mark in use
221
plugin::EventObserver::registerTableEvents(*share);
223
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
226
return TableShare::shared_ptr();
231
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
233
enum_field_types field_type;
235
switch(proto_field_type)
102
237
case message::Table::Field::INTEGER:
103
return DRIZZLE_TYPE_LONG;
238
field_type= DRIZZLE_TYPE_LONG;
105
240
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;
241
field_type= DRIZZLE_TYPE_DOUBLE;
243
case message::Table::Field::TIMESTAMP:
244
field_type= DRIZZLE_TYPE_TIMESTAMP;
114
246
case message::Table::Field::BIGINT:
115
return DRIZZLE_TYPE_LONGLONG;
247
field_type= DRIZZLE_TYPE_LONGLONG;
117
249
case message::Table::Field::DATETIME:
118
return DRIZZLE_TYPE_DATETIME;
250
field_type= DRIZZLE_TYPE_DATETIME;
120
252
case message::Table::Field::DATE:
121
return DRIZZLE_TYPE_DATE;
253
field_type= DRIZZLE_TYPE_DATE;
123
255
case message::Table::Field::VARCHAR:
124
return DRIZZLE_TYPE_VARCHAR;
256
field_type= DRIZZLE_TYPE_VARCHAR;
126
258
case message::Table::Field::DECIMAL:
127
return DRIZZLE_TYPE_DECIMAL;
259
field_type= DRIZZLE_TYPE_DECIMAL;
129
261
case message::Table::Field::ENUM:
130
return DRIZZLE_TYPE_ENUM;
262
field_type= DRIZZLE_TYPE_ENUM;
132
264
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;
265
field_type= DRIZZLE_TYPE_BLOB;
268
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
148
275
static Item *default_value_item(enum_field_types field_type,
576
716
table_name.str= db.str + db.length + 1;
577
717
table_name.length= strlen(table_name.str);
579
getTableMessage()->set_name(identifier_arg.getTableName());
580
getTableMessage()->set_schema(identifier_arg.getSchemaName());
719
table_proto->set_name(identifier_arg.getTableName());
720
table_proto->set_schema(identifier_arg.getSchemaName());
583
bool TableShare::parse_table_proto(Session& session, message::Table &table)
723
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
585
drizzled::error_t local_error= EE_OK;
587
727
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());
729
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
593
730
return ER_CORRUPT_TABLE_DEFINITION;
596
setTableMessage(table);
733
setTableProto(new(nothrow) message::Table(table));
598
735
storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
599
736
assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
620
757
table_charset= get_charset(table_options.collation_id());
622
if (not table_charset)
624
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
625
table_options.collation().c_str(),
626
table.name().c_str());
762
snprintf(errmsg, sizeof(errmsg),
763
_("Table %s has invalid/unknown collation: %d,%s"),
765
table_options.collation_id(),
766
table_options.collation().c_str());
628
return ER_CORRUPT_TABLE_DEFINITION; // Historical
769
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
770
return ER_CORRUPT_TABLE_DEFINITION;
631
773
db_record_offset= 1;
775
blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
633
777
keys= table.indexes_size();
790
934
std::vector<uint32_t> field_offsets;
791
935
std::vector<uint32_t> field_pack_length;
793
field_offsets.resize(_field_size);
794
field_pack_length.resize(_field_size);
937
field_offsets.resize(fields);
938
field_pack_length.resize(fields);
796
940
uint32_t interval_count= 0;
797
941
uint32_t interval_parts= 0;
799
943
uint32_t stored_columns_reclength= 0;
801
for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
945
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
803
947
message::Table::Field pfield= table.field(fieldnr);
804
if (pfield.constraints().is_nullable()) // Historical reference
808
else if (not pfield.constraints().is_notnull())
948
if (pfield.constraints().is_nullable())
813
enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
951
enum_field_types drizzle_field_type=
952
proto_field_type_to_drizzle_type(pfield.type());
815
954
field_offsets[fieldnr]= stored_columns_reclength;
1164
1310
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();
1311
field_length= MAX_BIGINT_WIDTH;
1185
1313
case DRIZZLE_TYPE_NULL:
1186
1314
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:
1317
Field* f= make_field(record + field_offsets[fieldnr] + data_offset,
1319
pfield.constraints().is_nullable(),
1325
MTYP_TYPENR(unireg_type),
1326
((field_type == DRIZZLE_TYPE_ENUM) ?
1327
&intervals[interval_nr++]
1329
getTableProto()->field(fieldnr).name().c_str());
1236
1333
// This needs to go, we should be setting the "use" on the field so that
1237
1334
// it does not reference the share/table.
1276
1374
f->setTable(NULL);
1277
1375
f->orig_table= NULL;
1279
f->setPosition(fieldnr);
1377
f->field_index= fieldnr;
1280
1378
f->comment= comment;
1281
if (not default_value &&
1282
not (f->unireg_check==Field::NEXT_NUMBER) &&
1379
if (! default_value &&
1380
! (f->unireg_check==Field::NEXT_NUMBER) &&
1283
1381
(f->flags & NOT_NULL_FLAG) &&
1284
(not f->is_timestamp()))
1382
(f->real_type() != DRIZZLE_TYPE_TIMESTAMP))
1286
1384
f->flags|= NO_DEFAULT_VALUE_FLAG;
1289
1387
if (f->unireg_check == Field::NEXT_NUMBER)
1290
found_next_number_field= &(_fields[fieldnr]);
1388
found_next_number_field= &(field[fieldnr]);
1390
if (timestamp_field == f)
1391
timestamp_field_offset= fieldnr;
1292
1393
if (use_hash) /* supposedly this never fails... but comments lie */
1294
const char *local_field_name= _fields[fieldnr]->field_name;
1295
name_hash.insert(make_pair(local_field_name, &(_fields[fieldnr])));
1395
const char *local_field_name= field[fieldnr]->field_name;
1396
name_hash.insert(make_pair(local_field_name, &(field[fieldnr])));
1299
1401
keyinfo= key_info;
1525
1645
6 Unknown .frm version
1528
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1648
int TableShare::open_table_def(Session& session, const 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())
1656
message::table::shared_ptr table;
1658
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1660
if (local_error != EEXIST)
1536
if (parse_table_proto(session, *table))
1662
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);
1669
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);
1677
local_error= parse_table_proto(session, *table);
1679
setTableCategory(TABLE_CATEGORY_USER);
1682
if (local_error && !error_given)
1685
open_table_error(error, (open_errno= errno), 0);
1966
2069
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
2070
case DRIZZLE_TYPE_LONG:
1981
return new (&mem_root) field::Int32(ptr,
2071
return new (&mem_root) Field_long(ptr,
2078
false /* is_unsigned */);
1987
2079
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,
2080
return new (&mem_root) Field_int64_t(ptr,
2087
false /* is_unsigned */);
2013
2088
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,
2089
return new (&mem_root) Field_timestamp(ptr,
2027
2097
case DRIZZLE_TYPE_DATE:
2028
2098
return new (&mem_root) Field_date(ptr,