40
#include <drizzled/error.h>
41
#include <drizzled/gettext.h>
42
#include <drizzled/sql_base.h>
43
#include <drizzled/pthread_globals.h>
44
#include <drizzled/internal/my_pthread.h>
46
#include <drizzled/table.h>
47
#include <drizzled/table/shell.h>
49
#include <drizzled/session.h>
51
#include <drizzled/charset.h>
52
#include <drizzled/internal/m_string.h>
53
#include <drizzled/internal/my_sys.h>
55
#include <drizzled/item/string.h>
56
#include <drizzled/item/int.h>
57
#include <drizzled/item/decimal.h>
58
#include <drizzled/item/float.h>
59
#include <drizzled/item/null.h>
60
#include <drizzled/temporal.h>
62
#include <drizzled/field.h>
63
#include <drizzled/field/str.h>
64
#include <drizzled/field/num.h>
65
#include <drizzled/field/blob.h>
66
#include <drizzled/field/boolean.h>
67
#include <drizzled/field/enum.h>
68
#include <drizzled/field/null.h>
69
#include <drizzled/field/date.h>
70
#include <drizzled/field/decimal.h>
71
#include <drizzled/field/real.h>
72
#include <drizzled/field/double.h>
73
#include <drizzled/field/int32.h>
74
#include <drizzled/field/int64.h>
75
#include <drizzled/field/size.h>
76
#include <drizzled/field/num.h>
77
#include <drizzled/field/time.h>
78
#include <drizzled/field/epoch.h>
79
#include <drizzled/field/datetime.h>
80
#include <drizzled/field/microtime.h>
81
#include <drizzled/field/varstring.h>
82
#include <drizzled/field/uuid.h>
84
#include <drizzled/plugin/storage_engine.h>
86
#include <drizzled/definition/cache.h>
87
#include <drizzled/typelib.h>
89
#include <drizzled/refresh_version.h>
40
#include "drizzled/error.h"
41
#include "drizzled/gettext.h"
42
#include "drizzled/sql_base.h"
43
#include "drizzled/pthread_globals.h"
44
#include "drizzled/internal/my_pthread.h"
45
#include "drizzled/plugin/event_observer.h"
47
#include "drizzled/table.h"
48
#include "drizzled/table/shell.h"
50
#include "drizzled/session.h"
52
#include "drizzled/charset.h"
53
#include "drizzled/internal/m_string.h"
54
#include "drizzled/internal/my_sys.h"
56
#include "drizzled/item/string.h"
57
#include "drizzled/item/int.h"
58
#include "drizzled/item/decimal.h"
59
#include "drizzled/item/float.h"
60
#include "drizzled/item/null.h"
61
#include "drizzled/temporal.h"
63
#include "drizzled/field.h"
64
#include "drizzled/field/str.h"
65
#include "drizzled/field/num.h"
66
#include "drizzled/field/blob.h"
67
#include "drizzled/field/enum.h"
68
#include "drizzled/field/null.h"
69
#include "drizzled/field/date.h"
70
#include "drizzled/field/decimal.h"
71
#include "drizzled/field/real.h"
72
#include "drizzled/field/double.h"
73
#include "drizzled/field/long.h"
74
#include "drizzled/field/int64_t.h"
75
#include "drizzled/field/num.h"
76
#include "drizzled/field/timestamp.h"
77
#include "drizzled/field/datetime.h"
78
#include "drizzled/field/varstring.h"
91
80
using namespace std;
96
85
extern size_t table_def_size;
99
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)
103
274
case message::Table::Field::INTEGER:
104
return DRIZZLE_TYPE_LONG;
275
field_type= DRIZZLE_TYPE_LONG;
106
277
case message::Table::Field::DOUBLE:
107
return DRIZZLE_TYPE_DOUBLE;
109
case message::Table::Field::EPOCH:
110
if (field.has_time_options() and field.time_options().microseconds())
111
return DRIZZLE_TYPE_MICROTIME;
113
return DRIZZLE_TYPE_TIMESTAMP;
278
field_type= DRIZZLE_TYPE_DOUBLE;
280
case message::Table::Field::TIMESTAMP:
281
field_type= DRIZZLE_TYPE_TIMESTAMP;
115
283
case message::Table::Field::BIGINT:
116
return DRIZZLE_TYPE_LONGLONG;
284
field_type= DRIZZLE_TYPE_LONGLONG;
118
286
case message::Table::Field::DATETIME:
119
return DRIZZLE_TYPE_DATETIME;
287
field_type= DRIZZLE_TYPE_DATETIME;
121
289
case message::Table::Field::DATE:
122
return DRIZZLE_TYPE_DATE;
290
field_type= DRIZZLE_TYPE_DATE;
124
292
case message::Table::Field::VARCHAR:
125
return DRIZZLE_TYPE_VARCHAR;
293
field_type= DRIZZLE_TYPE_VARCHAR;
127
295
case message::Table::Field::DECIMAL:
128
return DRIZZLE_TYPE_DECIMAL;
296
field_type= DRIZZLE_TYPE_DECIMAL;
130
298
case message::Table::Field::ENUM:
131
return DRIZZLE_TYPE_ENUM;
299
field_type= DRIZZLE_TYPE_ENUM;
133
301
case message::Table::Field::BLOB:
134
return DRIZZLE_TYPE_BLOB;
136
case message::Table::Field::UUID:
137
return DRIZZLE_TYPE_UUID;
139
case message::Table::Field::BOOLEAN:
140
return DRIZZLE_TYPE_BOOLEAN;
142
case message::Table::Field::TIME:
143
return DRIZZLE_TYPE_TIME;
302
field_type= DRIZZLE_TYPE_BLOB;
305
field_type= DRIZZLE_TYPE_LONG; /* Set value to kill GCC warning */
149
312
static Item *default_value_item(enum_field_types field_type,
369
539
path.str= (char *)"";
370
540
normalized_path.str= path.str;
371
541
path.length= normalized_path.length= 0;
373
std::string tb_name(identifier.getTableName());
374
std::transform(tb_name.begin(), tb_name.end(), tb_name.begin(), ::tolower);
375
assert(strcmp(tb_name.c_str(), table_name.str) == 0);
542
assert(strcmp(identifier.getTableName().c_str(), table_name.str) == 0);
377
543
assert(strcmp(identifier.getSchemaName().c_str(), db.str) == 0);
381
TableShare::TableShare(const identifier::Table &identifier) : // Just used during createTable()
547
TableShare::TableShare(const TableIdentifier &identifier) : // Just used during createTable()
382
548
table_category(TABLE_UNKNOWN_CATEGORY),
383
549
found_next_number_field(NULL),
384
550
timestamp_field(NULL),
386
552
mem_root(TABLE_ALLOC_BLOCK_SIZE),
390
table_name(NULL_LEX_STRING),
391
path(NULL_LEX_STRING),
392
normalized_path(NULL_LEX_STRING),
395
556
timestamp_offset(0),
397
558
stored_rec_length(0),
399
_table_message(NULL),
400
561
storage_engine(NULL),
401
562
tmp_table(identifier.getType()),
404
565
last_null_bit_pos(0),
406
567
rec_buff_length(0),
577
777
table_name.str= db.str + db.length + 1;
578
778
table_name.length= strlen(table_name.str);
580
getTableMessage()->set_name(identifier_arg.getTableName());
581
getTableMessage()->set_schema(identifier_arg.getSchemaName());
780
table_proto->set_name(identifier_arg.getTableName());
781
table_proto->set_schema(identifier_arg.getSchemaName());
584
bool TableShare::parse_table_proto(Session& session, message::Table &table)
784
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
586
drizzled::error_t local_error= EE_OK;
588
788
if (! table.IsInitialized())
590
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0),
591
table.name().empty() ? " " : table.name().c_str(),
592
table.InitializationErrorString().c_str());
790
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
594
791
return ER_CORRUPT_TABLE_DEFINITION;
597
setTableMessage(table);
794
setTableProto(new(nothrow) message::Table(table));
599
796
storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
600
797
assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
780
984
keys_for_keyread.reset();
781
985
set_prefix(keys_in_use, keys);
783
_field_size= table.field_size();
987
fields= table.field_size();
785
setFields(_field_size + 1);
786
_fields[_field_size]= NULL;
989
setFields(fields + 1);
788
992
uint32_t local_null_fields= 0;
791
std::vector<uint32_t> field_offsets;
792
std::vector<uint32_t> field_pack_length;
995
vector<uint32_t> field_offsets;
996
vector<uint32_t> field_pack_length;
794
field_offsets.resize(_field_size);
795
field_pack_length.resize(_field_size);
998
field_offsets.resize(fields);
999
field_pack_length.resize(fields);
797
1001
uint32_t interval_count= 0;
798
1002
uint32_t interval_parts= 0;
800
1004
uint32_t stored_columns_reclength= 0;
802
for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
1006
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
804
1008
message::Table::Field pfield= table.field(fieldnr);
805
if (pfield.constraints().is_nullable()) // Historical reference
809
else if (not pfield.constraints().is_notnull())
1009
if (pfield.constraints().is_nullable())
1010
local_null_fields++;
814
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());
816
1015
field_offsets[fieldnr]= stored_columns_reclength;
1165
1371
case DRIZZLE_TYPE_LONGLONG:
1167
uint32_t sign_len= pfield.constraints().is_unsigned() ? 0 : 1;
1168
field_length= MAX_BIGINT_WIDTH+sign_len;
1171
case DRIZZLE_TYPE_UUID:
1172
field_length= field::Uuid::max_string_length();
1174
case DRIZZLE_TYPE_BOOLEAN:
1175
field_length= field::Boolean::max_string_length();
1177
case DRIZZLE_TYPE_MICROTIME:
1178
field_length= field::Microtime::max_string_length();
1180
case DRIZZLE_TYPE_TIMESTAMP:
1181
field_length= field::Epoch::max_string_length();
1183
case DRIZZLE_TYPE_TIME:
1184
field_length= field::Time::max_string_length();
1372
field_length= MAX_BIGINT_WIDTH;
1186
1374
case DRIZZLE_TYPE_NULL:
1187
1375
abort(); // Programming error
1190
bool is_not_null= false;
1192
if (not pfield.constraints().is_nullable())
1196
else if (pfield.constraints().is_notnull())
1201
Field* f= make_field(pfield,
1202
record + field_offsets[fieldnr] + data_offset,
1210
MTYP_TYPENR(unireg_type),
1211
((field_type == DRIZZLE_TYPE_ENUM) ? &intervals[interval_nr++] : (TYPELIB*) 0),
1212
getTableMessage()->field(fieldnr).name().c_str());
1214
_fields[fieldnr]= f;
1216
// Insert post make_field code here.
1219
case DRIZZLE_TYPE_BLOB:
1220
case DRIZZLE_TYPE_VARCHAR:
1221
case DRIZZLE_TYPE_DOUBLE:
1222
case DRIZZLE_TYPE_DECIMAL:
1223
case DRIZZLE_TYPE_TIMESTAMP:
1224
case DRIZZLE_TYPE_TIME:
1225
case DRIZZLE_TYPE_DATETIME:
1226
case DRIZZLE_TYPE_MICROTIME:
1227
case DRIZZLE_TYPE_DATE:
1228
case DRIZZLE_TYPE_ENUM:
1229
case DRIZZLE_TYPE_LONG:
1230
case DRIZZLE_TYPE_LONGLONG:
1231
case DRIZZLE_TYPE_NULL:
1232
case DRIZZLE_TYPE_UUID:
1233
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());
1237
1394
// This needs to go, we should be setting the "use" on the field so that
1238
1395
// it does not reference the share/table.
1526
1706
6 Unknown .frm version
1529
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1709
int TableShare::open_table_def(Session& session, TableIdentifier &identifier)
1531
drizzled::error_t local_error= EE_OK;
1533
message::table::shared_ptr table= plugin::StorageEngine::getTableMessage(session, identifier, local_error);
1535
if (table and table->IsInitialized())
1717
message::Table table;
1719
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1721
if (local_error != EEXIST)
1537
if (parse_table_proto(session, *table))
1723
if (local_error > 0)
1539
local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1540
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1544
setTableCategory(TABLE_CATEGORY_USER);
1730
if (not table.IsInitialized())
1548
else if (table and not table->IsInitialized())
1550
local_error= ER_CORRUPT_TABLE_DEFINITION_UNKNOWN;
1551
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN, identifier);
1555
local_error= ER_TABLE_UNKNOWN;
1556
my_error(ER_TABLE_UNKNOWN, identifier);
1559
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);
1958
2131
false /* is_unsigned */);
1959
case DRIZZLE_TYPE_UUID:
1960
return new (&mem_root) field::Uuid(ptr,
1965
case DRIZZLE_TYPE_BOOLEAN:
1966
return new (&mem_root) field::Boolean(ptr,
1972
2132
case DRIZZLE_TYPE_LONG:
1973
return new (&mem_root) field::Int32(ptr,
2133
return new (&mem_root) Field_long(ptr,
2140
false /* is_unsigned */);
1979
2141
case DRIZZLE_TYPE_LONGLONG:
1983
return new (&mem_root) field::Size(ptr,
1991
return new (&mem_root) field::Int64(ptr,
1998
case DRIZZLE_TYPE_MICROTIME:
1999
return new (&mem_root) field::Microtime(ptr,
2142
return new (&mem_root) Field_int64_t(ptr,
2149
false /* is_unsigned */);
2005
2150
case DRIZZLE_TYPE_TIMESTAMP:
2006
return new (&mem_root) field::Epoch(ptr,
2012
case DRIZZLE_TYPE_TIME:
2013
return new (&mem_root) field::Time(ptr,
2151
return new (&mem_root) Field_timestamp(ptr,
2018
2159
case DRIZZLE_TYPE_DATE:
2019
2160
return new (&mem_root) Field_date(ptr,
2023
2165
case DRIZZLE_TYPE_DATETIME:
2024
2166
return new (&mem_root) Field_datetime(ptr,
2028
2171
case DRIZZLE_TYPE_NULL:
2029
2172
return new (&mem_root) Field_null(ptr,
2176
default: // Impossible (Wrong version)
2037
void TableShare::refreshVersion()
2039
version= refresh_version;