File: schema_matching_page.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (277 lines) | stat: -rw-r--r-- 8,496 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/* 
 * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; version 2 of the
 * License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "schema_matching_page.h"
#include "grt/icon_manager.h"
#include "grts/structs.db.h"
#include "mforms/selector.h"

#include "db_plugin_be.h"

class SchemaMatchingPage::OverridePanel : public mforms::Box
{
public:
  OverridePanel()
  : mforms::Box(true)
  {
    set_spacing(8);
    _button.set_text("Override Target");
    _button.signal_clicked()->connect(boost::bind(&OverridePanel::override, this));
    add(mforms::manage(new mforms::Label("Override target schema to be synchronized with:")), false, true);
    add(&_selector, true, true);
    add(&_button, false, true);
  }

  void override()
  {
    std::string s = _selector.get_string_value();
    _node->set_string(2, s);
    _node->set_string(3, "overriden");
  }

  void set_schemas(const std::list<std::string> &schema_names)
  {
    _selector.add_items(schema_names);
  }

  void set_active(mforms::TreeNodeRef node)
  {
    _node = node;
    _selector.set_value(node->get_string(2));
  }
private:
  mforms::TreeNodeRef _node;
  mforms::Selector _selector;
  mforms::Button _button;
};

static void select_all(mforms::TreeNodeView *tree, SchemaMatchingPage *page)
{
  for (int i= 0; i < tree->count(); i++)
    tree->node_at_row(i)->set_bool(0, true);
  page->validate();
}


static void unselect_all(mforms::TreeNodeView *tree, SchemaMatchingPage *page)
{
  for (int i= 0; i < tree->count(); i++)
    tree->node_at_row(i)->set_bool(0, false);
  page->validate();
}


SchemaMatchingPage::SchemaMatchingPage(grtui::WizardForm *form, const char *name,
                                       const std::string &left_name,
                                       const std::string &right_name,
                                       bool unselect_by_default)
: WizardPage(form, name), _header(true), _tree(mforms::TreeFlatList), _unselect_by_default(unselect_by_default)
{
  _header.set_spacing(4);

  _image.set_image(bec::IconManager::get_instance()->get_icon_path("db.Schema.32x32.png"));
  _header.add(&_image, false);

  _label.set_text_align(mforms::MiddleLeft);
  _label.set_text(_("Select the Schemata to be Synchronized:"));
  _label.set_style(mforms::BoldStyle);
  _header.add(&_label, true, true);

  add(&_header, false, false);

  set_short_title(_("Select Schemata"));
  set_title(_("Select the Schemata to be Synchronized"));

  _menu.add_item_with_title("Select All", boost::bind(select_all, &_tree, this));
  _menu.add_item_with_title("Unselect All", boost::bind(unselect_all, &_tree, this));

  _tree.add_column(mforms::CheckColumnType, "", 20, true);
  _tree.add_column(mforms::IconStringColumnType, left_name, 150, false);
  _tree.add_column(mforms::StringColumnType, right_name, 150, false);
  _tree.add_column(mforms::IconStringColumnType, "", 300, false);
  _tree.end_columns();
  _tree.set_context_menu(&_menu);
  _tree.set_cell_edit_handler(boost::bind(&SchemaMatchingPage::cell_edited, this, _1, _2, _3));
  scoped_connect(_tree.signal_changed(), boost::bind(&SchemaMatchingPage::selection_changed, this));

  add(&_tree, true, true);

  _override = mforms::manage(new OverridePanel());
  add(_override, false, true);

  add(&_missing_label, false, true);

  _missing_label.show(false);
  _missing_label.set_style(mforms::SmallHelpTextStyle);
}

void SchemaMatchingPage::cell_edited(mforms::TreeNodeRef node, int column, const std::string &value)
{
  if (column == 0)
  {
    node->set_bool(column, value != "0");
    validate();
  }
}

bool SchemaMatchingPage::allow_next()
{
  int c = _tree.count();
  for (int i = 0; i < c; i++)
  {
    mforms::TreeNodeRef node(_tree.root_node()->get_child(i));
    if (node->get_bool(0))
      return true;
  }
  return false;
}

void SchemaMatchingPage::leave(bool advancing)
{
  if (advancing)
  {
    grt::StringListRef unlist(_form->grtm()->get_grt());
    grt::StringListRef list(_form->grtm()->get_grt());
    grt::StringListRef orig_list(_form->grtm()->get_grt());

    int c = _tree.count();
    for (int i = 0; i < c; i++)
    {
      mforms::TreeNodeRef node(_tree.node_at_row(i));
      if (node->get_bool(0))
      {
        list.insert(node->get_string(2));
        orig_list.insert(node->get_string(1));
      }
      else
        unlist.insert(node->get_string(2));
    }
    values().set("unSelectedSchemata", unlist);
    values().set("selectedSchemata", list);
    values().set("selectedOriginalSchemata", orig_list);
  }
  WizardPage::leave(advancing);
}


std::map<std::string, std::string> SchemaMatchingPage::get_mapping()
{
  std::map<std::string, std::string> mapping;
  int c = _tree.count();
  for (int i = 0; i < c; i++)
  {
    mforms::TreeNodeRef node(_tree.node_at_row(i));
    if (node->get_bool(0))
    {
      if (node->get_string(1) != node->get_string(2) && !node->get_string(2).empty())
        mapping[node->get_string(1)] = node->get_string(2);
    }
  }
  return mapping;
}

void SchemaMatchingPage::enter(bool advancing)
{
  if (advancing)
  {
    int missing = 0;
    _tree.clear();

    {
      grt::IntegerRef server_case_sensitive(grt::IntegerRef::cast_from(values().get("server_is_case_sensitive")));
      // list of schemas from source (usually model).. must be filled by caller before this is reached
      grt::StringListRef db_list(grt::StringListRef::cast_from(values().get("schemata")));
      // list of schemas from target (usually DB or script).. must be filled by caller before this is reached
      grt::StringListRef target_db_list(grt::StringListRef::cast_from(values().get("targetSchemata")));



      std::list<std::string> db_schema_names;
      for (grt::StringListRef::const_iterator j= target_db_list.begin(); j != target_db_list.end(); ++j)
        db_schema_names.push_back(*j);
      db_schema_names.sort(boost::bind(base::same_string, _1, _2, true));

      _override->set_schemas(db_schema_names);

      std::vector<std::string> sorted_names;
      for (grt::StringListRef::const_iterator sname= db_list.begin(); sname != db_list.end(); ++sname)
        sorted_names.push_back(*sname);
      std::sort(sorted_names.begin(), sorted_names.end(), boost::bind(base::same_string, _1, _2, true));

      // check for schemas that exist only in the model and not in DB
      for (std::vector<std::string>::const_iterator sname = sorted_names.begin(); sname != sorted_names.end(); ++sname)
      {
        mforms::TreeNodeRef node = _tree.add_node();

        std::string target_name;
        bool found_name = false;

        node->set_icon_path(1, "db.Schema.16x16.png");
        node->set_string(1, *sname);

        // check if the target has the list of schemas we want
        for (grt::StringListRef::const_iterator j= target_db_list.begin(); j != target_db_list.end(); ++j)
        {

          if (base::same_string(*j, *sname, server_case_sensitive == 1))
          {
            found_name = true;
            target_name = *j;
          }
        }

        if (!found_name)
        {
          node->set_bool(0, false);
          node->set_string(2, *sname);
          node->set_string(3, _("schema not found in target"));
          missing++;
        }
        else
        {
          if (!_unselect_by_default)
            node->set_bool(0, true);
          node->set_string(2, target_name);
        }
      }
    }

    if (missing > 0)
    {
      _missing_label.set_text(_("The schemata from your model are missing from the target.\nIf you are creating them for the first time use the Forward Engineer function."));
      _missing_label.show(true);
    }

    selection_changed();
  }
}


void SchemaMatchingPage::selection_changed()
{
  mforms::TreeNodeRef sel(_tree.get_selected_node());
  if (sel)
  {
    _override->set_enabled(true);
    _override->set_active(sel);
  }
  else
    _override->set_enabled(false);
}