File: editor_routinegroup.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 (186 lines) | stat: -rw-r--r-- 6,654 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
/* 
 * Copyright (c) 2007, 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 "grt/grt_dispatcher.h"
#include "editor_routinegroup.h"

#include "base/string_utilities.h"

using namespace grt;
using namespace bec;
using namespace base;

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

RoutineGroupEditorBE::RoutineGroupEditorBE(GRTManager *grtm, const db_RoutineGroupRef &group)
  : DBObjectEditorBE(grtm, group)
{
  // No specific query type setting for the group editor. We have to parse the full spectrum (especially for delimiter changes).
}

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

/**
 * For routine groups getting the SQL is different compared to any other object editor. We are not using sqlDefinition()
 * of the object being edited (in fact, it doesn't even have this member), but collect all SQL text from the individual
 * routine objects we have in the routines() collection.
 */
std::string RoutineGroupEditorBE::get_sql()
{
  grt::ListRef<db_Routine> routines = get_routine_group()->routines();
  if (!routines.is_valid())
    return "";
  
  std::string sql = "DELIMITER $$\n\n";

  typedef std::map<size_t, db_RoutineRef> OrderedRoutines;
  typedef std::list<db_RoutineRef> UnorderedRoutines;
  OrderedRoutines ordered_routines;
  UnorderedRoutines unordered_routines; // routines with duplicated sequence number. to upgrade old models smoothly, where sequence numbers are 0.

  // XXX: the sequence number idea is utter nonsense. If a routine is part in different groups
  //       this number may be different in each group. Find a replacement or maybe we don't need it at all.
  for(size_t i = 0; i < routines.count(); ++i)
  {
    db_RoutineRef routine = routines.get(i);
    size_t sequenceNumber = routine->sequenceNumber();
    if (ordered_routines.find(sequenceNumber) == ordered_routines.end())
      ordered_routines[sequenceNumber]= routine;
    else
      unordered_routines.push_back(routine);
  }

  for (OrderedRoutines::iterator i = ordered_routines.begin(); i != ordered_routines.end(); ++i)
    sql += base::trim(i->second->sqlDefinition()) + "$$\n\n";

  for (UnorderedRoutines::iterator i = unordered_routines.begin(); i != unordered_routines.end(); ++i)
    sql += base::trim((*i)->sqlDefinition()) + "$$\n\n";

  return sql;
}

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

std::vector<std::string> RoutineGroupEditorBE::get_routines_names()
{
  std::vector<std::string> result;
  grt::ListRef<db_Routine> routines = get_routine_group()->routines();
  if (!routines.is_valid())
    return result;

  // TODO: isn't the owner for all routines the same?
  for(size_t i = 0; i < routines.count(); ++i)
    result.push_back(*routines[i]->owner()->name() + "." + *routines[i]->name());

  return result;
}

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

std::string RoutineGroupEditorBE::get_routine_sql(db_RoutineRef routine)
{
  return routine->sqlDefinition();
}

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

void RoutineGroupEditorBE::delete_routine_with_name(const std::string& str)
{
  grt::ListRef<db_Routine> routines = get_routine_group()->routines();
  
  if (!routines.is_valid())
    return;

  for(size_t i = 0; i < routines.count(); i++)
  {
    std::string qname = *routines[i]->owner()->name() + "." + *routines[i]->name();
    if (base::same_string(str, qname, _parser_context->case_sensitive()))
    {
      AutoUndoEdit undo(this);
      routines.remove(i);
      undo.end(strfmt(_("Remove routine from routine group `%s`.%s`"), get_schema_name().c_str(), get_name().c_str()));

      return;
    }
  }
}

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

/**
 * Removes the routine at the given index from this group. Does nothing if index is out of range.
 */
void RoutineGroupEditorBE::remove_routine_by_index(size_t index)
{
  grt::ListRef<db_Routine> routines = get_routine_group()->routines();
  
  if (!routines.is_valid() || index > routines.count())
    return;

  AutoUndoEdit undo(this);
  routines.remove(index);
  undo.end(strfmt(_("Remove routine from routine group `%s`.%s`"), get_schema_name().c_str(), get_name().c_str()));
}

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

void RoutineGroupEditorBE::append_routine_with_id(const std::string &id)
{
  // First ensure no routine with the same id exists already.
  grt::ListRef<db_Routine> routines = get_routine_group()->routines();

  if (!routines.is_valid())
    return;

  for (size_t i = 0; i < routines.count(); ++i)
  {
    if (base::same_string(id, routines[i].id(), _parser_context->case_sensitive()))
      return;
  }

  routines = get_schema()->routines();
  for (size_t i = 0; i < routines.count(); ++i)
  {
    if (base::same_string(id, routines[i].id(), _parser_context->case_sensitive()))
    {
      AutoUndoEdit undo(this);
      get_routine_group()->routines().insert(routines[i]);
      undo.end(strfmt(_("Add routine to routine group `%s`.%s`"), get_schema_name().c_str(), get_name().c_str()));

      return;
    }
  }
}

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

std::string RoutineGroupEditorBE::get_title()
{
  return get_name() + " - Group";
}

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

void RoutineGroupEditorBE::open_editor_for_routine_at_index(size_t index)
{
  if (index < get_routine_group()->routines().count())
    get_grt_manager()->open_object_editor(get_routine_group()->routines()[index]);
}

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