2
* Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
4
* This program is free software; you can redistribute it and/or
5
* modify it under the terms of the GNU General Public License as
6
* published by the Free Software Foundation; version 2 of the
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
#ifndef _WB_LIVE_SCHEMA_TREE_H_
21
#define _WB_LIVE_SCHEMA_TREE_H_
23
#include "grt/tree_model.h"
25
#include "workbench/wb_backend_public_interface.h"
26
#include "base/string_utilities.h"
27
#include <glib/gpattern.h>
31
class MYSQLWBBACKEND_PUBLIC_FUNC LiveSchemaTree : public bec::TreeModel
34
static const short COLUMN_DATA = 0x01;
35
static const short TRIGGER_DATA = 0x02;
36
static const short INDEX_DATA = 0x04;
37
static const short FK_DATA = 0x08;
48
// One entry in a list that describes a single object.
57
struct MYSQLWBBACKEND_PUBLIC_FUNC ColumnNode
59
ColumnNode() : is_pk(false), is_fk(false), is_id(false) {}
62
std::string default_value;
68
struct MYSQLWBBACKEND_PUBLIC_FUNC FKColumnNode
71
std::string referenced_column;
74
struct MYSQLWBBACKEND_PUBLIC_FUNC FKNode
77
unsigned char update_rule;
78
unsigned char delete_rule;
79
std::string referenced_table;
80
std::vector<FKColumnNode> columns;
83
struct MYSQLWBBACKEND_PUBLIC_FUNC IndexColumnNode
89
struct MYSQLWBBACKEND_PUBLIC_FUNC IndexNode
92
std::vector<IndexColumnNode> columns;
97
struct MYSQLWBBACKEND_PUBLIC_FUNC TriggerNode
100
unsigned char event_manipulation;
101
unsigned char timing;
105
struct MYSQLWBBACKEND_PUBLIC_FUNC ObjectNode
113
virtual int get_loaded_mask() { return 0; }
114
virtual bool operator <(const ObjectNode &node) const { return name < node.name; }
115
virtual void copy(const ObjectNode &node, bool full);
116
virtual ObjectNode& operator=(const ObjectNode &node);
119
struct MYSQLWBBACKEND_PUBLIC_FUNC ViewNode: public ObjectNode
121
std::vector<ColumnNode> columns;
122
bool columns_load_error;
124
bool columns_expanded;
126
virtual int get_loaded_mask() { return columns_loaded ? COLUMN_DATA : 0; }
127
virtual void copy(const ObjectNode &node, bool full);
128
virtual ObjectNode& operator=(const ObjectNode &node);
129
virtual ViewNode& operator=(const ViewNode &node);
132
struct MYSQLWBBACKEND_PUBLIC_FUNC TableNode: public ViewNode
134
std::vector<TriggerNode> triggers;
135
std::vector<IndexNode> indexes;
136
std::vector<FKNode> foreign_keys;
138
bool triggers_loaded;
140
bool foreign_keys_loaded;
142
bool triggers_expanded;
143
bool indexes_expanded;
144
bool foreign_keys_expanded;
147
virtual int get_loaded_mask();
148
virtual void copy(const ObjectNode &node, bool full);
149
virtual ObjectNode& operator=(const ObjectNode &node);
150
virtual TableNode& operator=(const TableNode &node);
151
IndexNode *get_index(const std::string &name);
152
FKNode *get_foreign_key(const std::string &name);
155
struct MYSQLWBBACKEND_PUBLIC_FUNC SchemaNode
158
std::vector<TableNode> tables;
159
std::vector<ViewNode> views;
160
std::vector<ObjectNode> routines;
164
bool tables_expanded;
166
bool routines_expanded;
169
bool operator == (const SchemaNode &s) const { return name == s.name; }
170
void add_node(ObjectType type, std::string name);
171
void delete_node(std::string name, ObjectType type);
172
void sort_nodes(ObjectType type);
173
bool filter_data(ObjectType object_type, GPatternSpec* schema_pattern, GPatternSpec* object_pattern, SchemaNode& target) const;
177
typedef boost::function<void (SchemaNode)> SchemaContentArrivedSlot;
178
typedef boost::function<void (SchemaNode, ObjectNode*, ObjectType)> ObjectDetailsArrivedSlot;
181
virtual std::list<std::string> fetch_schema_list() = 0;
182
virtual bool fetch_schema_contents(bec::NodeId, const std::string &, bool include_column_data, const SchemaContentArrivedSlot&) = 0;
183
virtual bool fetch_object_details(ObjectType, const std::string&, const std::string&, short, const ObjectDetailsArrivedSlot&) = 0;
188
virtual void tree_refresh() = 0;
189
virtual void tree_activate_objects(const std::string&, const std::vector<ChangeRecord>&) = 0;
190
virtual void tree_alter_objects(const std::vector<ChangeRecord>&) = 0;
191
virtual void tree_create_object(ObjectType, const std::string&, const std::string&) = 0;
192
virtual void tree_drop_objects(const std::vector<ChangeRecord>&) = 0;
196
typedef boost::signals2::signal<int (const std::string&)> SqlEditorTextInsertSignal;
197
typedef boost::function<bec::MenuItemList (std::string, std::string, std::string)> ObjectPluginMenuItemsSlot;
198
typedef boost::function<bool (std::string, std::string, std::string, std::string)> CallObjectPluginMenuItemsSlot;
200
SqlEditorTextInsertSignal sql_editor_text_insert_signal;
201
ObjectPluginMenuItemsSlot object_plugin_items_slot;
202
CallObjectPluginMenuItemsSlot call_object_plugin_items_slot;
205
boost::weak_ptr<FetchDelegate> _fetch_delegate;
206
boost::weak_ptr<Delegate> _delegate;
208
std::vector<SchemaNode> _schemata;
209
std::string _active_schema;
210
int _schemata_vector_serial;
212
bool _case_sensitive_identifiers;
214
void schema_contents_arrived(const SchemaNode &node, int index, int serial);
215
void object_details_arrived(const SchemaNode &schema_node, const ObjectNode *obj_node, ObjectType obj_type, int schema_index, int obj_index, int serial);
216
void load_table_details(const bec::NodeId &index_node, int fetch_mask);
218
bool identifiers_equal(const std::string &a, const std::string &b);
220
bool do_expand_node(const bec::NodeId &node, bool dont_fetch);
222
LiveSchemaTree(grt::GRT* grtm);
223
~LiveSchemaTree() { clean_filter(); }
224
void set_delegate(boost::shared_ptr<Delegate> delegate);
225
void set_fetch_delegate(boost::shared_ptr<FetchDelegate> delegate);
226
std::string get_filter_wildcard(const std::string& filter);
227
bool filter_data(ObjectType object_type, const std::string& filter, LiveSchemaTree& target);
228
bool filter_data(LiveSchemaTree& target);
230
ObjectNode * get_object_node_by_index(const bec::NodeId &index_node, ObjectType *object_type= NULL);
231
ObjectNode * get_object_node_by_type_and_name(const std::string &schema_name, ObjectType obj_type, std::string name, bec::NodeId *index_ret = 0);
232
ObjectNode * get_object_node_by_type_and_name(SchemaNode *schema_node, ObjectType obj_type, std::string name, bec::NodeId *index_ret = 0);
233
ObjectNode * get_object_node_by_type_and_index(SchemaNode *schema_node, ObjectType obj_type, size_t index);
234
void insert_node(SchemaNode *schema_node, ObjectType obj_type, ObjectNode *obj_node);
235
static unsigned char internalize_token(const std::string& token);
236
static std::string externalize_token(unsigned char c);
238
void set_active_schema(const std::string &schema);
239
int get_index_of_schema(const std::string &schema_name) const;
241
void set_case_sensitive_identifiers(bool flag);
243
virtual bool is_expandable(const bec::NodeId &node);
244
virtual bool is_expanded(const bec::NodeId &node);
245
virtual bool expand_node(const bec::NodeId &node);
246
virtual void collapse_node(const bec::NodeId &node);
248
virtual void refresh();
249
virtual void refresh_ex(bool hard_refresh);
250
virtual void refresh_ex(const std::list<std::string> &schema_list, bool hard_refresh);
251
virtual int count_children(const bec::NodeId &node);
252
virtual bec::NodeId get_child(const bec::NodeId &node, int index);
254
void update_live_object_state(ObjectType type, const std::string &schema_name, const std::string &old_obj_name, const std::string &new_obj_name);
256
virtual std::string get_field_description(const bec::NodeId &node, int column);
257
virtual bool get_field(const bec::NodeId &node, int column, std::string &value);
258
#if !defined(__APPLE__)
259
virtual bool get_field(const bec::NodeId &node, int column, int &value);
261
virtual bec::IconId get_field_icon(const bec::NodeId &node, int column, bec::IconSize size);
263
bec::NodeId get_node_for_schema(const std::string &name);
264
bec::NodeId get_node_for_object(const std::string &schema_name, ObjectType type, const std::string& name);
266
virtual bool activate_node(const bec::NodeId &node);
268
virtual bool activate_popup_item_for_nodes(const std::string &name, const std::vector<bec::NodeId> &orig_nodes);
269
virtual bec::MenuItemList get_popup_items_for_nodes(const std::vector<bec::NodeId> &nodes);
271
bool is_schema_contents_enabled() const;
272
void is_schema_contents_enabled(bool value);
274
void set_base(LiveSchemaTree *base) { _base = base; }
275
void set_filter(std::string filter, ObjectType type);
278
virtual bool is_highlighted(const bec::NodeId& node);
281
const std::vector<SchemaNode> &get_schemata() { return _schemata; }
283
bool _is_schema_contents_enabled;
284
bool _auto_fetch_columns;
286
LiveSchemaTree *_base;
288
ObjectType _filter_type;
289
GPatternSpec* _schema_pattern;
290
GPatternSpec* _object_pattern;
292
static const char* _schema_tokens[15];