File: fetch_schema_names_multi_page.h

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 (244 lines) | stat: -rw-r--r-- 8,258 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
/*
 * Copyright (c) 2009, 2013, 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/wizard_progress_page.h"

class FetchSchemaNamesSourceTargetProgressPage : public WizardProgressPage
{
public:
  FetchSchemaNamesSourceTargetProgressPage(WizardForm *form, MultiSourceSelectPage *source_page, const char *name= "fetchNames")
  : WizardProgressPage(form, name, true), _source_page(source_page), _source_dbconn(0), _target_dbconn(0)
  {
    set_title(_("Retrieve Source and Target Schema Names"));
    set_short_title(_("Get Source and Target"));

    set_status_text("");
  }

  void set_load_schemata_slot(DbConnection *sdbc, const boost::function<std::vector<std::string> ()> &sslot,
                              DbConnection *tdbc, const boost::function<std::vector<std::string> ()> &tslot)
  {
    _source_dbconn= sdbc;
    _target_dbconn= tdbc;
    _load_source_schemata= sslot;
    _load_target_schemata= tslot;
  }

  void set_model_catalog(db_CatalogRef catalog)
  {
    _model_catalog = catalog;
  }

protected:

  bool perform_connect(bool source)
  {
    DbConnection *dbc = source ? _source_dbconn : _target_dbconn;
    db_mgmt_ConnectionRef conn = dbc->get_connection();

    execute_grt_task(boost::bind(&FetchSchemaNamesSourceTargetProgressPage::do_connect, this, _1, dbc), false);

    return true;
  }

  grt::ValueRef do_connect(grt::GRT *grt, DbConnection *dbc)
  {
    if (!dbc)
      throw std::logic_error("must call set_db_connection() 1st");
    dbc->test_connection();

    return grt::ValueRef();
  }


  bool perform_fetch(bool source)
  {
    execute_grt_task(boost::bind(&FetchSchemaNamesSourceTargetProgressPage::do_fetch, this, _1, source),
                     false);
    return true;
  }

  static bool collate(const std::string &a, const std::string &b)
  {
    return g_utf8_collate(a.c_str(), b.c_str()) < 0;
  }


  grt::ValueRef do_fetch(grt::GRT *grt, bool source)
  {
    std::vector<std::string> schema_names= source ? _load_source_schemata() : _load_target_schemata();

    // order the schema names alphabetically
    std::sort(schema_names.begin(), schema_names.end(), std::ptr_fun(&FetchSchemaNamesSourceTargetProgressPage::collate));

    grt::StringListRef list(grt);
    for (std::vector<std::string>::const_iterator iter= schema_names.begin();
         iter != schema_names.end(); ++iter)
      list.insert(*iter);

    if (source)
      values().set("schemata", list);
    else
      values().set("targetSchemata", list);

    _finished++;

    return grt::ValueRef();
  }

  bool perform_script_fetch(bool source)
  {
    std::string path = values().get_string(source ? "left_source_file" : "right_source_file");
    db_CatalogRef catalog = parse_catalog_from_file(path);

    grt::StringListRef schemata(catalog.get_grt());
    for (size_t i = 0; i < catalog->schemata().count(); i++)
      schemata.insert(catalog->schemata()[i]->name());

    if (source)
    {
      values().set("left_file_catalog", catalog);
      values().set("schemata", schemata);
    }
    else
    {
      values().set("right_file_catalog", catalog);
      values().set("targetSchemata", schemata);
    }

    _finished++;

    return true;
  }

  db_CatalogRef parse_catalog_from_file(const std::string &filename)
  {
    workbench_physical_ModelRef pm= workbench_physical_ModelRef::cast_from(_model_catalog->owner());

    db_mysql_CatalogRef cat(_model_catalog.get_grt());
    cat->version(pm->rdbms()->version());
    grt::replace_contents(cat->simpleDatatypes(), pm->rdbms()->simpleDatatypes());

    cat->name("default");
    cat->oldName("default");

    GError *file_error= NULL;
    char *sql_input_script= NULL;
    gsize sql_input_script_length= 0;

    if (!g_file_get_contents(filename.c_str(), &sql_input_script, &sql_input_script_length, &file_error))
    {
      std::string file_error_msg("Error reading input file: ");
      file_error_msg.append(file_error->message);
      throw std::runtime_error(file_error_msg);
    }

    SqlFacade::Ref sql_parser= SqlFacade::instance_for_rdbms(pm->rdbms());
    sql_parser->parseSqlScriptString(cat, sql_input_script);
    g_free(sql_input_script);
    
    return cat;
  }

  bool perform_model_fetch(bool source)
  {
    {
      db_CatalogRef catalog(_model_catalog);
      grt::StringListRef names(wizard()->grtm()->get_grt());
      for (size_t i = 0; i < catalog->schemata().count(); i++)
        names.insert(catalog->schemata()[i]->name());
      values().set(source ? "schemata" : "targetSchemata", names);
    }
    _finished++;
    return true;
  }

  virtual void enter(bool advancing)
  {
    if (advancing)
    {
      clear_tasks();
      switch (_source_page->get_left_source())
      {
        case DataSourceSelector::ServerSource:
          add_async_task(_("Connect to Source DBMS"),
                         boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_connect, this, true),
                         _("Connecting to Source DBMS..."));

          add_async_task(_("Retrieve Schema List from Source Database"),
                         boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_fetch, this, true),
                         _("Retrieving schema list from source database..."));
          break;
        case DataSourceSelector::FileSource:
          add_task(_("Retrieve database objects from source file"),
                   boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_script_fetch, this, true),
                   _("Retrieving objects from selected source file..."));
          break;
        case DataSourceSelector::ModelSource:
          add_task(_("Load schemas from source model"), boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_model_fetch, this, true),
                   _("Loading schemas from source model..."));
          break;
      }
      switch (_source_page->get_right_source())
      {
        case DataSourceSelector::ServerSource:
          add_async_task(_("Connect to Target DBMS"),
                         boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_connect, this, false),
                         _("Connecting to Target DBMS..."));

          add_async_task(_("Retrieve Schema List from Target Database"),
                         boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_fetch, this, false),
                         _("Retrieving schema list from target database..."));
          break;
        case DataSourceSelector::FileSource:
          add_task(_("Retrieve database objects from target file"),
                   boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_script_fetch, this, false),
                   _("Retrieving objects from selected target file..."));
          break;
        case DataSourceSelector::ModelSource:
          add_task(_("Load schemas from target model"), boost::bind(&FetchSchemaNamesSourceTargetProgressPage::perform_model_fetch, this, false),
                   _("Loading schemas from target model..."));
          break;
      }
      end_adding_tasks(_("Execution Completed Successfully"));

      _finished= 0;
      reset_tasks();
    }

    WizardProgressPage::enter(advancing);
  }

  virtual bool allow_next()
  {
    return _finished == 2;
  }

private:
  MultiSourceSelectPage *_source_page;

  db_CatalogRef _model_catalog;

  DbConnection *_source_dbconn;
  DbConnection *_target_dbconn;
  boost::function<std::vector<std::string> () > _load_source_schemata;
  boost::function<std::vector<std::string> () > _load_target_schemata;
  
  int _finished;
};