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/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/varstring.h"
81
#include "drizzled/field/uuid.h"
83
#include "drizzled/definition/cache.h"
91
85
using namespace std;
96
90
extern size_t table_def_size;
99
static enum_field_types proto_field_type_to_drizzle_type(const message::Table::Field &field)
92
/*****************************************************************************
93
Functions to handle table definition cach (TableShare)
94
*****************************************************************************/
97
Mark that we are not using table share anymore.
104
If ref_count goes to zero and (we have done a refresh or if we have
105
already too many open table shares) then delete the definition.
108
void TableShare::release(TableShare *share)
110
bool to_be_deleted= false;
111
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
114
if (!--share->ref_count)
122
definition::Cache::singleton().erase(share->getCacheKey());
126
void TableShare::release(TableShare::shared_ptr &share)
128
bool to_be_deleted= false;
129
safe_mutex_assert_owner(table::Cache::singleton().mutex().native_handle);
132
if (!--share->ref_count)
140
definition::Cache::singleton().erase(share->getCacheKey());
144
void TableShare::release(const TableIdentifier &identifier)
146
TableShare::shared_ptr share= definition::Cache::singleton().find(identifier.getKey());
149
share->version= 0; // Mark for delete
150
if (share->ref_count == 0)
152
definition::Cache::singleton().erase(identifier.getKey());
158
static TableShare::shared_ptr foundTableShare(TableShare::shared_ptr share)
161
We found an existing table definition. Return it if we didn't get
162
an error when reading the table definition from file.
165
/* We must do a lock to ensure that the structure is initialized */
168
/* Table definition contained an error */
169
share->open_table_error(share->error, share->open_errno, share->errarg);
171
return TableShare::shared_ptr();
174
share->incrementTableCount();
180
Get TableShare for a table.
183
session Thread handle
184
table_list Table that should be opened
186
key_length Length of key
187
error out: Error code from open_table_def()
190
Get a table definition from the table definition cache.
191
If it doesn't exist, create a new from the table definition file.
194
We must have wrlock on table::Cache::singleton().mutex() when we come here
195
(To be changed later)
202
TableShare::shared_ptr TableShare::getShareCreate(Session *session,
203
const TableIdentifier &identifier,
206
TableShare::shared_ptr share;
210
/* Read table definition from cache */
211
if ((share= definition::Cache::singleton().find(identifier.getKey())))
212
return foundTableShare(share);
214
share.reset(new TableShare(message::Table::STANDARD, identifier));
216
if (share->open_table_def(*session, identifier))
218
in_error= share->error;
220
return TableShare::shared_ptr();
222
share->ref_count++; // Mark in use
224
plugin::EventObserver::registerTableEvents(*share);
226
bool ret= definition::Cache::singleton().insert(identifier.getKey(), share);
229
return TableShare::shared_ptr();
234
static enum_field_types proto_field_type_to_drizzle_type(uint32_t proto_field_type)
236
enum_field_types field_type;
238
switch(proto_field_type)
103
240
case message::Table::Field::INTEGER:
104
return DRIZZLE_TYPE_LONG;
241
field_type= DRIZZLE_TYPE_LONG;
106
243
case message::Table::Field::DOUBLE:
107
return DRIZZLE_TYPE_DOUBLE;
244
field_type= DRIZZLE_TYPE_DOUBLE;
109
246
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;
247
field_type= DRIZZLE_TYPE_TIMESTAMP;
115
249
case message::Table::Field::BIGINT:
116
return DRIZZLE_TYPE_LONGLONG;
250
field_type= DRIZZLE_TYPE_LONGLONG;
118
252
case message::Table::Field::DATETIME:
119
return DRIZZLE_TYPE_DATETIME;
253
field_type= DRIZZLE_TYPE_DATETIME;
121
255
case message::Table::Field::DATE:
122
return DRIZZLE_TYPE_DATE;
256
field_type= DRIZZLE_TYPE_DATE;
124
258
case message::Table::Field::VARCHAR:
125
return DRIZZLE_TYPE_VARCHAR;
259
field_type= DRIZZLE_TYPE_VARCHAR;
127
261
case message::Table::Field::DECIMAL:
128
return DRIZZLE_TYPE_DECIMAL;
262
field_type= DRIZZLE_TYPE_DECIMAL;
130
264
case message::Table::Field::ENUM:
131
return DRIZZLE_TYPE_ENUM;
265
field_type= DRIZZLE_TYPE_ENUM;
133
267
case message::Table::Field::BLOB:
134
return DRIZZLE_TYPE_BLOB;
268
field_type= DRIZZLE_TYPE_BLOB;
136
270
case message::Table::Field::UUID:
137
return DRIZZLE_TYPE_UUID;
139
case message::Table::Field::BOOLEAN:
140
return DRIZZLE_TYPE_BOOLEAN;
271
field_type= DRIZZLE_TYPE_UUID;
142
273
case message::Table::Field::TIME:
143
return DRIZZLE_TYPE_TIME;
274
field_type= DRIZZLE_TYPE_TIME;
278
abort(); // Programming error
149
284
static Item *default_value_item(enum_field_types field_type,
577
725
table_name.str= db.str + db.length + 1;
578
726
table_name.length= strlen(table_name.str);
580
getTableMessage()->set_name(identifier_arg.getTableName());
581
getTableMessage()->set_schema(identifier_arg.getSchemaName());
728
table_proto->set_name(identifier_arg.getTableName());
729
table_proto->set_schema(identifier_arg.getSchemaName());
584
bool TableShare::parse_table_proto(Session& session, message::Table &table)
732
int TableShare::inner_parse_table_proto(Session& session, message::Table &table)
586
drizzled::error_t local_error= EE_OK;
588
736
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());
738
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), table.InitializationErrorString().c_str());
594
739
return ER_CORRUPT_TABLE_DEFINITION;
597
setTableMessage(table);
742
setTableProto(new(nothrow) message::Table(table));
599
744
storage_engine= plugin::StorageEngine::findByName(session, table.engine().name());
600
745
assert(storage_engine); // We use an assert() here because we should never get this far and still have no suitable engine.
621
766
table_charset= get_charset(table_options.collation_id());
623
if (not table_charset)
625
my_error(ER_CORRUPT_TABLE_DEFINITION_UNKNOWN_COLLATION, MYF(0),
626
table_options.collation().c_str(),
627
table.name().c_str());
771
snprintf(errmsg, sizeof(errmsg),
772
_("Table %s has invalid/unknown collation: %d,%s"),
774
table_options.collation_id(),
775
table_options.collation().c_str());
629
return ER_CORRUPT_TABLE_DEFINITION; // Historical
778
my_error(ER_CORRUPT_TABLE_DEFINITION, MYF(0), errmsg);
779
return ER_CORRUPT_TABLE_DEFINITION;
632
782
db_record_offset= 1;
784
blob_ptr_size= portable_sizeof_char_ptr; // more bonghits.
634
786
keys= table.indexes_size();
791
943
std::vector<uint32_t> field_offsets;
792
944
std::vector<uint32_t> field_pack_length;
794
field_offsets.resize(_field_size);
795
field_pack_length.resize(_field_size);
946
field_offsets.resize(fields);
947
field_pack_length.resize(fields);
797
949
uint32_t interval_count= 0;
798
950
uint32_t interval_parts= 0;
800
952
uint32_t stored_columns_reclength= 0;
802
for (unsigned int fieldnr= 0; fieldnr < _field_size; fieldnr++)
954
for (unsigned int fieldnr= 0; fieldnr < fields; fieldnr++)
804
956
message::Table::Field pfield= table.field(fieldnr);
805
if (pfield.constraints().is_nullable()) // Historical reference
809
else if (not pfield.constraints().is_notnull())
957
if (pfield.constraints().is_nullable())
814
enum_field_types drizzle_field_type= proto_field_type_to_drizzle_type(pfield);
960
enum_field_types drizzle_field_type=
961
proto_field_type_to_drizzle_type(pfield.type());
816
963
field_offsets[fieldnr]= stored_columns_reclength;
1526
1678
6 Unknown .frm version
1529
int TableShare::open_table_def(Session& session, const identifier::Table &identifier)
1681
int TableShare::open_table_def(Session& session, const 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())
1689
message::table::shared_ptr table;
1691
local_error= plugin::StorageEngine::getTableDefinition(session, identifier, table);
1693
if (local_error != EEXIST)
1537
if (parse_table_proto(session, *table))
1695
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);
1702
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);
1710
local_error= parse_table_proto(session, *table);
1712
setTableCategory(TABLE_CATEGORY_USER);
1715
if (local_error && !error_given)
1718
open_table_error(error, (open_errno= errno), 0);