~ubuntu-branches/ubuntu/quantal/mysql-workbench/quantal

« back to all changes in this revision

Viewing changes to backend/wbprivate/sqlide/wb_live_schema_tree.h

  • Committer: Package Import Robot
  • Author(s): Dmitry Smirnov
  • Date: 2012-03-01 21:57:30 UTC
  • Revision ID: package-import@ubuntu.com-20120301215730-o7y8av8y38n162ro
Tags: upstream-5.2.38+dfsg
ImportĀ upstreamĀ versionĀ 5.2.38+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (c) 2009, 2011, Oracle and/or its affiliates. All rights reserved.
 
3
 *
 
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
 
7
 * License.
 
8
 * 
 
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.
 
13
 * 
 
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
 
17
 * 02110-1301  USA
 
18
 */
 
19
 
 
20
#ifndef _WB_LIVE_SCHEMA_TREE_H_
 
21
#define _WB_LIVE_SCHEMA_TREE_H_
 
22
 
 
23
#include "grt/tree_model.h"
 
24
#include "grtpp.h"
 
25
#include "workbench/wb_backend_public_interface.h"
 
26
#include "base/string_utilities.h"
 
27
#include <glib/gpattern.h>
 
28
 
 
29
namespace wb
 
30
{
 
31
  class MYSQLWBBACKEND_PUBLIC_FUNC LiveSchemaTree : public bec::TreeModel
 
32
  {
 
33
  public:
 
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;
 
38
 
 
39
    enum ObjectType
 
40
    {
 
41
      Schema,
 
42
      Table,
 
43
      View,
 
44
      Routine,
 
45
      Any
 
46
    };
 
47
 
 
48
    // One entry in a list that describes a single object.
 
49
    struct ChangeRecord
 
50
    {
 
51
      ObjectType type;
 
52
      std::string schema;
 
53
      std::string name;
 
54
      std::string detail;
 
55
    };
 
56
 
 
57
    struct MYSQLWBBACKEND_PUBLIC_FUNC ColumnNode
 
58
    {
 
59
      ColumnNode() : is_pk(false), is_fk(false), is_id(false) {}
 
60
      std::string name;
 
61
      std::string type;
 
62
      std::string default_value;
 
63
      bool is_pk;
 
64
      bool is_fk;
 
65
      bool is_id;
 
66
    };
 
67
 
 
68
    struct MYSQLWBBACKEND_PUBLIC_FUNC FKColumnNode
 
69
    {
 
70
      std::string name;
 
71
      std::string referenced_column;
 
72
    };
 
73
 
 
74
    struct MYSQLWBBACKEND_PUBLIC_FUNC FKNode
 
75
    {
 
76
      std::string name;
 
77
      unsigned char update_rule;
 
78
      unsigned char delete_rule;
 
79
      std::string referenced_table;
 
80
      std::vector<FKColumnNode> columns;
 
81
    };
 
82
 
 
83
    struct MYSQLWBBACKEND_PUBLIC_FUNC IndexColumnNode
 
84
    {
 
85
      std::string name;
 
86
      bool nullable;
 
87
    };
 
88
 
 
89
    struct MYSQLWBBACKEND_PUBLIC_FUNC IndexNode
 
90
    {
 
91
      std::string name;
 
92
      std::vector<IndexColumnNode> columns;
 
93
      bool unique;
 
94
      unsigned char type;
 
95
    };
 
96
 
 
97
    struct MYSQLWBBACKEND_PUBLIC_FUNC TriggerNode
 
98
    {
 
99
      std::string name;
 
100
      unsigned char event_manipulation;
 
101
      unsigned char timing;
 
102
    };
 
103
 
 
104
    
 
105
    struct MYSQLWBBACKEND_PUBLIC_FUNC ObjectNode
 
106
    {
 
107
      ObjectNode();
 
108
      std::string name;
 
109
      std::string details;
 
110
      bool fetched;
 
111
      bool fetching;
 
112
      bool expanded;
 
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);
 
117
    };
 
118
 
 
119
    struct MYSQLWBBACKEND_PUBLIC_FUNC ViewNode: public ObjectNode
 
120
    {
 
121
      std::vector<ColumnNode> columns;
 
122
      bool columns_load_error;
 
123
      bool columns_loaded;
 
124
      bool columns_expanded;
 
125
      ViewNode();
 
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);
 
130
    };
 
131
    
 
132
    struct MYSQLWBBACKEND_PUBLIC_FUNC TableNode: public ViewNode
 
133
    {
 
134
      std::vector<TriggerNode> triggers;
 
135
      std::vector<IndexNode> indexes;
 
136
      std::vector<FKNode> foreign_keys;
 
137
 
 
138
      bool triggers_loaded;
 
139
      bool indexes_loaded;
 
140
      bool foreign_keys_loaded;
 
141
 
 
142
      bool triggers_expanded;
 
143
      bool indexes_expanded;
 
144
      bool foreign_keys_expanded;
 
145
 
 
146
      TableNode();
 
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);
 
153
    };
 
154
 
 
155
    struct MYSQLWBBACKEND_PUBLIC_FUNC SchemaNode
 
156
    {
 
157
      std::string name;
 
158
      std::vector<TableNode> tables;
 
159
      std::vector<ViewNode> views;
 
160
      std::vector<ObjectNode> routines;
 
161
      bool fetched;
 
162
      bool fetching;
 
163
      bool expanded;
 
164
      bool tables_expanded;
 
165
      bool views_expanded;
 
166
      bool routines_expanded;
 
167
 
 
168
      SchemaNode();
 
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;
 
174
    };
 
175
 
 
176
 
 
177
    typedef boost::function<void (SchemaNode)> SchemaContentArrivedSlot;
 
178
    typedef boost::function<void (SchemaNode, ObjectNode*, ObjectType)> ObjectDetailsArrivedSlot;
 
179
    struct FetchDelegate
 
180
    {
 
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;
 
184
    };
 
185
    
 
186
    struct Delegate
 
187
    {
 
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;
 
193
    };
 
194
    
 
195
    
 
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;
 
199
 
 
200
    SqlEditorTextInsertSignal sql_editor_text_insert_signal;
 
201
    ObjectPluginMenuItemsSlot object_plugin_items_slot;
 
202
    CallObjectPluginMenuItemsSlot call_object_plugin_items_slot;
 
203
 
 
204
  protected:
 
205
    boost::weak_ptr<FetchDelegate> _fetch_delegate;
 
206
    boost::weak_ptr<Delegate> _delegate;
 
207
    grt::GRT* _grt;
 
208
    std::vector<SchemaNode> _schemata;
 
209
    std::string _active_schema;
 
210
    int _schemata_vector_serial;
 
211
    bool _rebuilding;
 
212
    bool _case_sensitive_identifiers;
 
213
 
 
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);
 
217
    
 
218
    bool identifiers_equal(const std::string &a, const std::string &b);
 
219
 
 
220
    bool do_expand_node(const bec::NodeId &node, bool dont_fetch);
 
221
  public:
 
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);
 
229
 
 
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);
 
237
 
 
238
    void set_active_schema(const std::string &schema);
 
239
    int get_index_of_schema(const std::string &schema_name) const;
 
240
    
 
241
    void set_case_sensitive_identifiers(bool flag);
 
242
 
 
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);
 
247
 
 
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);
 
253
    
 
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);
 
255
 
 
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);
 
260
#endif
 
261
    virtual bec::IconId get_field_icon(const bec::NodeId &node, int column, bec::IconSize size);
 
262
    
 
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);
 
265
    
 
266
    virtual bool activate_node(const bec::NodeId &node);
 
267
    
 
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);
 
270
 
 
271
    bool is_schema_contents_enabled() const;
 
272
    void is_schema_contents_enabled(bool value);
 
273
 
 
274
    void set_base(LiveSchemaTree *base) { _base = base; }
 
275
    void set_filter(std::string filter, ObjectType type);
 
276
    void clean_filter();
 
277
 
 
278
    virtual bool is_highlighted(const bec::NodeId& node);
 
279
    
 
280
    
 
281
    const std::vector<SchemaNode> &get_schemata() { return _schemata; }
 
282
  private:
 
283
    bool _is_schema_contents_enabled;
 
284
    bool _auto_fetch_columns;
 
285
 
 
286
    LiveSchemaTree *_base;
 
287
    std::string _filter;
 
288
    ObjectType _filter_type;
 
289
    GPatternSpec* _schema_pattern;
 
290
    GPatternSpec* _object_pattern;
 
291
 
 
292
    static const char* _schema_tokens[15];
 
293
  };
 
294
};
 
295
 
 
296
#endif