File: merge_model.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 (206 lines) | stat: -rw-r--r-- 7,062 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
/* 
 * Copyright (c) 2011, 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 "merge_model.h"
#include "grtpp.h"
#include "grtpp_util.h"

#include "grts/structs.workbench.h"
#include "grts/structs.db.h"
#include "grts/structs.workbench.model.h"

#include "base/wb_iterators.h"
#include "base/string_utilities.h"

#include "sqlide/recordset_table_inserts_storage.h"

#include "mforms/form.h"
#include "mforms/box.h"
#include "mforms/button.h"
#include "mforms/listbox.h"

#include "schema_select_form.h"

struct tolower_pred
{
	std::string tolower(const std::string& arg){return base::tolower(arg);};
};


template<class T,class TOwner>
void copy_additional_data(T obj,std::string new_name,TOwner new_owner)
{
  update_ids(obj);
};

template<class TOwner>
void copy_additional_data(workbench_physical_DiagramRef obj,std::string old_name,TOwner new_owner)
{
  grt::BaseListRef args(obj->get_grt());
  grt::Module *module= obj->get_grt()->get_module("Workbench");
  grt::StringRef img_file_path(grt::StringRef::cast_from(module->call_function("getTempDir", args)));
    update_ids(obj);
    grt::ListRef<model_Figure> figures= obj->figures();
    for (size_t i = 0; i < figures.count(); ++i)
        if (workbench_model_ImageFigureRef::can_wrap(figures[i]))
        {
            workbench_model_ImageFigureRef image = workbench_model_ImageFigureRef::cast_from(figures[i]);
            std::string img_path = img_file_path;
            img_path.append("/").append(image->filename());
            image->setImageFile(img_path);
        }
}

template<class TOwner>
void copy_additional_data(db_TableRef obj,std::string old_name,TOwner new_owner)
{

  bec::GRTManager *grtm = bec::GRTManager::get_instance_for(obj->get_grt());
  grt::BaseListRef args(obj->get_grt());
  grt::Module *module= obj->get_grt()->get_module("Workbench");
  grt::StringRef db_file_path(grt::StringRef::cast_from(module->call_function("getDbFilePath", args)));

  Recordset_table_inserts_storage::Ref input_storage= Recordset_table_inserts_storage::create_with_path(grtm,db_file_path);
  input_storage->table(obj);

  Recordset::Ref rs= Recordset::create(grtm);
  rs->data_storage(input_storage);
  rs->reset();

  //update IDs, INSERTs will go the table with ne IDs
  update_ids(obj);
  Recordset_table_inserts_storage::Ref output_storage= Recordset_table_inserts_storage::create(grtm);
  output_storage->table(obj);
  //Trigger creation of table to store INSERTs data
  output_storage->unserialize(Recordset::create(grtm));


  output_storage->serialize(rs);
  std::string table_inserts_sql= output_storage->sql_script();
};

template<class T>
void merge_list(grt::ListRef<T> list1, const grt::ListRef<T> &list2, const GrtObjectRef& dst_owner)
{
  std::set<std::string> existing_names;
  
  for (size_t c = list1.count(), i = 0; i < c; i++)
    existing_names.insert(base::tolower(*list1[i]->name()));

  for (size_t c = list2.count(), i= 0; i < c; i++)
  {
    if (!GrtObjectRef::can_wrap(list2[i]))
      continue;
    std::string name = list2[i]->name();

    std::set<std::string>::const_iterator (std::set<std::string>::*findFn)(const std::string&) const = &std::set<std::string>::find;
    std::string new_name = grt::get_name_suggestion(
            boost::bind(
                    std::not_equal_to<std::set<std::string>::const_iterator>(),
                            boost::bind(findFn, &existing_names, 
                              boost::bind(&tolower_pred::tolower,tolower_pred(),_1)
                            ),
                            existing_names.end()
            ),name,false);
    GrtObjectRef new_obj = list2[i];
    new_obj->owner(dst_owner);
    if (new_name != name)
    {
      new_obj->name(new_name);
      existing_names.insert(base::tolower(new_name));
    }
    list1.insert(grt::ListRef<T>::value_type::cast_from(new_obj));
    copy_additional_data(grt::Ref<T>::cast_from(new_obj),name,dst_owner);
  }
}

template<class T>
void update_list(grt::ListRef<T> list)
{
  for (size_t c = list.count(), i = 0; i < c; i++)
  {
    grt::Ref<T> obj = grt::Ref<T>::cast_from(list[i]);
    copy_additional_data(obj,obj->name(),obj->owner());
  }
}

void merge_schema(//workbench_DocumentRef document,
                  const db_SchemaRef& target_schema,
                  const db_SchemaRef& schema)
{
  merge_list(target_schema->tables(), schema->tables(),GrtObjectRef::cast_from(target_schema));
  merge_list(target_schema->views(), schema->views(),target_schema);
  merge_list(target_schema->routines(), schema->routines(),target_schema);
  merge_list(target_schema->routineGroups(),schema->routineGroups(),target_schema);
}

void update_schema(db_SchemaRef& schema)
{
  update_list(schema->tables());
  update_list(schema->views());
  update_list(schema->routines());
}

void merge_catalog (grt::Module *module, db_CatalogRef& dest_cat,const db_CatalogRef src_cat)
{
  if (src_cat->schemata().count() == 1)
  {
    if(dest_cat->schemata().count() == 1)
    {//Merge to exiting schema regardless of name
      merge_schema(dest_cat->schemata().get(0),src_cat->schemata().get(0));
      return;
    }
    else
    {
        SchemaSelectionForm form(module,dest_cat->schemata(),src_cat->schemata().get(0));
        if (!form.run())
            return;
        //If selection is not valid procceed to regular merge and include diagram
        if (form.get_selection().is_valid())
        {
            merge_schema(form.get_selection(),src_cat->schemata().get(0));
            return;        
        }
      //Ask where to merge    
    }
  }

  for(size_t sz= src_cat->schemata().count(), i= 0; i < sz; i++)
  {
    bool merged = false;
    for(size_t sz= dest_cat->schemata().count(), j= 0; j < sz; j++)
      if(strcmp(src_cat->schemata().get(i)->name().c_str(),dest_cat->schemata().get(j)->name().c_str()) == 0)
      {
        merge_schema(dest_cat->schemata().get(j),src_cat->schemata().get(i));
        merged = true;
        break;
      }
    if (merged)
      continue;
    db_SchemaRef schema = src_cat->schemata().get(i);
    schema->owner(dest_cat);
    dest_cat->schemata().insert(schema);
    update_schema(schema);
  }
};

void merge_diagrams (grt::ListRef<workbench_physical_Diagram>& dest_diagrams, const grt::ListRef<workbench_physical_Diagram>& src_diagrams, const GrtObjectRef& dst_owner)
{
  merge_list(dest_diagrams,src_diagrams,dst_owner);
};