File: diff_reporting.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 (280 lines) | stat: -rw-r--r-- 9,606 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
278
279
280
/* 
 * Copyright (c) 2009, 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 "grtui/grt_wizard_plugin.h"
#include "grtui/wizard_view_text_page.h"

#include "db_mysql_diff_reporting.h"
#include "backend/db_plugin_be.h"
#include "base/string_utilities.h"
#include "mforms/treenodeview.h"

using namespace grtui;
using namespace mforms;
using namespace base;

#include "grtui/connection_page.h"
#include "frontend/multi_source_selector_page.h"
#include "frontend/fetch_schema_names_multi_page.h"
#include "frontend/fetch_schema_contents_multi_page.h"


class MultiSchemaSelectionPage : public WizardPage
{
public:
  MultiSchemaSelectionPage(WizardForm *form, const char *name)
  : WizardPage(form, name), _body(true), _left(mforms::TreeFlatList), _right(mforms::TreeFlatList)
  {
    set_title("Select Schemas from Source and Target to be Compared");
    set_short_title("Select Schemas");

    add(&_body, true, true);

    _body.set_spacing(12);
    _body.set_homogeneous(true);
    _body.add(&_left, true, true);
    _body.add(&_right, true, true);

    _left.add_column(mforms::IconStringColumnType, "Source Schema", 300, false);
    _left.end_columns();
    _left.signal_changed()->connect(boost::bind(&MultiSchemaSelectionPage::validate, this));

    _right.add_column(mforms::IconStringColumnType, "Target Schema", 300, false);
    _right.end_columns();
    _right.signal_changed()->connect(boost::bind(&MultiSchemaSelectionPage::validate, this));
  }

  virtual void enter(bool advancing)
  {
    if (advancing)
    {
      std::string icon = bec::IconManager::get_instance()->get_icon_path("db.Schema.16x16.png");
      grt::StringListRef schemata(grt::StringListRef::cast_from(values().get("schemata")));
      grt::StringListRef targetSchemata(grt::StringListRef::cast_from(values().get("targetSchemata")));

      _left.clear();
      for (grt::StringListRef::const_iterator i = schemata.begin(); i != schemata.end(); ++i)
      {
        mforms::TreeNodeRef node = _left.add_node();
        node->set_string(0, *i);
        node->set_icon_path(0, icon);
      }

      _right.clear();
      for (grt::StringListRef::const_iterator i = targetSchemata.begin(); i != targetSchemata.end(); ++i)
      {
        mforms::TreeNodeRef node = _right.add_node();
        node->set_string(0, *i);
        node->set_icon_path(0, icon);
      }
    }
  }

  virtual void leave(bool advancing)
  {
    if (advancing)
    {
      {
        grt::StringListRef slist(values().get_grt());
        slist.insert(grt::StringRef(_left.get_selected_node()->get_string(0)));
        values().set("selectedOriginalSchemata", slist);
      }
      {
        grt::StringListRef slist(values().get_grt());
        slist.insert(grt::StringRef(_right.get_selected_node()->get_string(0)));
        values().set("selectedSchemata", slist);
      }
    }
  }

  virtual bool allow_next()
  {
    return _left.get_selected_node() && _right.get_selected_node();
  }

protected:
  mforms::Box _body;
  mforms::TreeNodeView _left;
  mforms::TreeNodeView _right;
};


//--------------------------------------------------------------------------------
  
class ViewResultPage : public ViewTextPage
{
public:
  ViewResultPage(WizardForm *form)
    : ViewTextPage(form, "viewdiff", (ViewTextPage::Buttons)(ViewTextPage::SaveButton|ViewTextPage::CopyButton), "Text Files (*.txt)|*.txt")
  {
    set_short_title(_("Differences Report"));
    set_title(_("Differences Found in Catalog Comparison"));
    _text.set_language(mforms::LanguageNone);
  }

  void set_generate_text_slot(const boost::function<std::string ()> &slot)
  {
    _generate= slot;
  }

  virtual void enter(bool advancing)
  {
    if (advancing)
      _text.set_value(_generate());
  }

  virtual bool allow_cancel() { return false; }
  virtual bool next_closes_wizard() { return true; }

protected:  
  boost::function<std::string ()> _generate;
};


//--------------------------------------------------------------------------------

class WbPluginDiffReport : public WizardPlugin
{
public:
  WbPluginDiffReport(grt::Module *module)
    : WizardPlugin(module), _be(grtm())
  {
    set_name("diff_report_wizard");
    add_page(mforms::manage(_source_page= new MultiSourceSelectPage(this, false)));

    _left_db.grtm(grtm(), true);
    _right_db.grtm(grtm(), true);

    ConnectionPage *connect;
    // Pick source connection (optional)
    add_page(mforms::manage(connect= new ConnectionPage(this, "connect_source", "db.mysql.compareSchema:left_source_connection")));
    connect->set_db_connection(_left_db.db_conn());
    connect->set_title(std::string("Source Database: ").append(connect->get_title()));
    connect->set_short_title("Source Database");
    // Pick target connection (optional)
    add_page(mforms::manage(connect= new ConnectionPage(this, "connect_target", "db.mysql.compareSchema:right_source_connection")));
    connect->set_db_connection(_right_db.db_conn());
    connect->set_title(std::string("Target Database: ").append(connect->get_title()));
    connect->set_short_title("Target Database");

    // Fetch names from source and target if they're DBs, reveng script if they're files
    FetchSchemaNamesSourceTargetProgressPage *fetch_names_page;
    add_page(mforms::manage(fetch_names_page= new FetchSchemaNamesSourceTargetProgressPage(this, _source_page, "fetch_names")));
    fetch_names_page->set_load_schemata_slot(_left_db.db_conn(), boost::bind(&WbPluginDiffReport::load_schemata, this, &_left_db),
                                             _right_db.db_conn(), boost::bind(&WbPluginDiffReport::load_schemata, this, &_right_db));
    fetch_names_page->set_model_catalog(_be.get_model_catalog());

    // Pick what to synchronize
    _schema_pick_page = new MultiSchemaSelectionPage(this, "pick_schemata");
    add_page(mforms::manage(_schema_pick_page));

    // Fetch contents from source and target, if they come from the database.. otherwise the schemas are already loaded (optional)
    FetchSchemaContentsSourceTargetProgressPage *fetch_schema_page;
    add_page(mforms::manage(fetch_schema_page= new FetchSchemaContentsSourceTargetProgressPage(this, _source_page, "fetch_schema")));
    fetch_schema_page->set_db_plugin(&_left_db, &_right_db);

    ViewResultPage *page;
    add_page(mforms::manage(page= new ViewResultPage(this)));
    page->set_generate_text_slot(boost::bind(&WbPluginDiffReport::generate_report, this));

    set_title(_("Compare and Report Differences in Catalogs"));
  }

  std::vector<std::string> load_schemata(Db_plugin *db)
  {
    std::vector<std::string> names;
    db->load_schemata(names);
//    _be.set_db_options(db->load_db_options());
    return names;
  }

  std::string generate_report()
  {
    db_CatalogRef left_catalog, right_catalog;

    if (_source_page->get_left_source() == DataSourceSelector::ServerSource)
      left_catalog= _left_db.db_catalog();
    else if (_source_page->get_left_source() == DataSourceSelector::FileSource)
      left_catalog = db_CatalogRef::cast_from(values().get("left_file_catalog"));
    else if (_source_page->get_left_source() == DataSourceSelector::ModelSource)
      left_catalog = _be.get_model_catalog();

    if (_source_page->get_right_source() == DataSourceSelector::ServerSource)
      right_catalog = _right_db.db_catalog();
    else if (_source_page->get_right_source() == DataSourceSelector::FileSource)
      right_catalog = db_CatalogRef::cast_from(values().get("right_file_catalog"));
    else if (_source_page->get_right_source() == DataSourceSelector::ModelSource)
      right_catalog = _be.get_model_catalog();

    std::string report;
    try
    {
      report = _be.generate_report(db_mysql_CatalogRef::cast_from(left_catalog), db_mysql_CatalogRef::cast_from(right_catalog));
    }
    catch (const std::exception &exc)
    {
      report = base::strfmt("Error generating report: %s", exc.what());
    }
    return report;
  }


  virtual WizardPage *get_next_page(WizardPage *current)
  {
    std::string curid= current->get_id();
    std::string nextid;
    
    if (curid == "source")
    {
      if (_source_page->get_left_source() == DataSourceSelector::ServerSource)
        nextid= "connect_source";
      else if (_source_page->get_right_source() == DataSourceSelector::ServerSource)
        nextid= "connect_target";
      else
        nextid= "fetch_names";
    }

    if (!nextid.empty())
      return get_page_with_id(nextid);
    else
      return WizardForm::get_next_page(current);
  }


protected:
  DbMySQLDiffReporting _be;
  Db_plugin _left_db;
  Db_plugin _right_db;
  MultiSourceSelectPage *_source_page;
  MultiSchemaSelectionPage *_schema_pick_page;
  
  std::vector<std::string> load_schemas(Db_plugin *db)
  {
    std::vector<std::string> names;
    db->load_schemata(names);
    return names;
  }
};


WizardPlugin *createWbPluginDiffReport(grt::Module *module)
{
  return new WbPluginDiffReport(module);
}