File: execute_routine_wizard.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 (273 lines) | stat: -rw-r--r-- 9,452 bytes parent folder | download
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
/* 
 * Copyright (c) 2014, 2016, 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 "execute_routine_wizard.h"

#include "base/string_utilities.h"

#include "mforms/button.h"
#include "mforms/box.h"
#include "mforms/table.h"
#include "mforms/label.h"
#include "mforms/textentry.h"
#include "mforms/scrollpanel.h"

#include "grtdb/db_object_helpers.h"

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

ExecuteRoutineWizard::ExecuteRoutineWizard(db_mysql_RoutineRef routine, const std::string &sql_mode)
  : Form(NULL)
{
  _sql_mode = sql_mode;
  _routine = routine;
  _catalog = db_mysql_CatalogRef::cast_from(_routine->owner()->owner());

 // set_managed();
  set_title(base::strfmt(_("Call stored %s %s.%s"), routine->routineType().c_str(), routine->owner()->name().c_str(), routine->name().c_str()));

  mforms::Box *content = mforms::manage(new mforms::Box(false));
  content->set_padding(12);
  content->set_spacing(20);

  _cancel_button = mforms::manage(new mforms::Button());
  _cancel_button->set_text(_("Cancel"));

  _execxute_button = mforms::manage(new mforms::Button());
  _execxute_button->set_text(_("Execute"));

  mforms::Box *button_bar = mforms::manage(new mforms::Box(true));
  button_bar->set_spacing(12);
  mforms::Utilities::add_end_ok_cancel_buttons(button_bar, _execxute_button, _cancel_button);

  content->add_end(button_bar, false, true);

  mforms::Label *title = mforms::manage(new mforms::Label());
  title->set_text(base::strfmt(_("Enter values for parameters of your %s and click <Execute> to create "
    "an SQL editor and run the call:"), routine->routineType().c_str()));
  title->set_wrap_text(true);
  content->add(title, false, true);

  mforms::ScrollPanel *scroll_box = mforms::manage(new mforms::ScrollPanel());
  content->add(scroll_box, true, true);

  // Create a table with a row for each IN and IN/OUT parameter.
  mforms::Table *table = mforms::manage(new mforms::Table());
  table->set_padding(5, 0, 5, 0);
  table->set_column_spacing(4);
  table->set_row_spacing(4);
  table->set_column_count(4);

  // Need an intermediate container as we have to make the scrollbox expand, but don't want
  // the table expand as well.
  mforms::Box *container = mforms::manage(new mforms::Box(false));
  container->add(table, false, true);
  scroll_box->add(container);

  grt::ListRef<db_mysql_RoutineParam> parameters = routine->params();
  table->set_row_count((int)parameters->count());
  for (int i = 0; i < (int)parameters->count(); ++i)
  {
    db_mysql_RoutineParamRef parameter = parameters[i];

    // Skip pure out parameters.
    if (routine->routineType() == "procedure" && parameter->paramType() == "out")
      continue;

    mforms::Label *text = mforms::manage(new mforms::Label(parameter->name()));
    text->set_style(mforms::BoldStyle);
    text->set_text_align(mforms::MiddleRight);
    table->add(text, 0, 1, i, i + 1);

    mforms::TextEntry *value_entry = mforms::manage(new mforms::TextEntry());
    _edits.push_back(value_entry);
    value_entry->set_size(100, -1);
    table->add(value_entry, 1, 2, i, i + 1, mforms::VFillFlag);

    if (!parameter->paramType().empty())
    {
      text = mforms::manage(new mforms::Label("[" + base::toupper(parameter->paramType()) + "]"));
      text->set_text_align(mforms::MiddleLeft);
      text->set_color("#376BA5");
      table->add(text, 2, 3, i, i + 1, mforms::VFillFlag);
    }

    text = mforms::manage(new mforms::Label(parameter->datatype()));
    text->set_style(mforms::InfoCaptionStyle);
    text->set_text_align(mforms::MiddleLeft);
    table->add(text, 3, 4, i, i + 1);
  }

  set_content(content);
  set_size(500, std::min(800, 160 + (int)parameters->count() * 30));
}

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

bool ExecuteRoutineWizard::needs_quoting(const std::string &type)
{
  // Parse type to see if it needs quoting.
  grt::ListRef<db_SimpleDatatype> default_type_list;
  grt::ListRef<db_SimpleDatatype> type_list;
  GrtVersionRef target_version;
  if (_catalog.is_valid())
  {
    default_type_list = _catalog->simpleDatatypes();
    type_list = default_type_list;
    target_version = _catalog->version();
  }

  db_UserDatatypeRef userType;
  db_SimpleDatatypeRef simpleType;
  int precision = bec::EMPTY_COLUMN_PRECISION;
  int scale = bec::EMPTY_COLUMN_SCALE;
  int length = bec::EMPTY_COLUMN_LENGTH;
  std::string datatypeExplicitParams;

  // Since we work with code directly from the server parsing should always succeed.
  // But just in case there's an unexpected error assume quoting is needed.
  if (!bec::parse_type_definition(type, target_version, type_list, grt::ListRef<db_UserDatatype>(),
    default_type_list, simpleType, userType, precision, scale, length, datatypeExplicitParams))
    return true;

  return simpleType->needsQuotes() != 0;
}

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

bool is_quoted(const std::string &text)
{
  std::string text_ = base::trim(text);
  if (text_.size() < 2)
    return false;

  if (text_[0] == '"' || text_[0] == '\'')
  {
    char quote_char = text_[0];
    if (text_[text.size() - 1] == quote_char)
      return true;
  }

  return false;
}

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

std::string ExecuteRoutineWizard::run()
{
  // Generate sql for the caller, so it can be run in an editor.
  std::string result;

  // If there are no input parameters, we don't need to ask the user for anything.
  grt::ListRef<db_mysql_RoutineParam> parameters = _routine->params();
  if (!_edits.empty())
  {
    if (!run_modal(_execxute_button, _cancel_button))
      return "";
  }

  std::string schema_name = base::quote_identifier_if_needed(*_routine->owner()->name(), '`');
  std::string routine_name = base::quote_identifier_if_needed(*_routine->name(), '`');
  if (base::tolower(_routine->routineType()) == "procedure")
  {
    std::string parameters_list;
    std::string variables_list;
    int edit_index = 0;

    for (size_t i = 0; i < parameters->count(); ++i)
    {
      db_mysql_RoutineParamRef parameter = parameters[i];
      bool quote = needs_quoting(parameter->datatype());
      if (base::tolower(parameter->paramType()) == "in")
      {
        // A pure input parameter. Just add it to the parameter list.
        if (!parameters_list.empty())
          parameters_list += ", ";

        std::string value = _edits[edit_index++]->get_string_value();

        // Don't quote if the user already did.
        if (quote && is_quoted(value))
          quote = false;
        if (quote)
          parameters_list += "'" + value + "'";
        else
          parameters_list += value;
      }
      else
      {
        // Out or in/out parameter.
        // Since we cannot use DECLARE outside stored programs we use SET to define a variable
        // that can take the output of the call. Need to set a dummy value, however.
        std::string parameter_name = base::quote_identifier_if_needed(*parameter->name(), '`');
        result += "set @" + parameter_name + " = ";
        
        std::string value = "0";
        if (base::tolower(parameter->paramType()) == "inout")
          value = _edits[edit_index++]->get_string_value();

        if (quote && is_quoted(value))
          quote = false;
        if (quote)
          result += "'" + value + "';\n";
        else
          result += value + ";\n";

        if (!parameters_list.empty())
          parameters_list += ", ";
        parameters_list += "@" + parameter_name;

        if (!variables_list.empty())
          variables_list += ", ";
        variables_list += "@" + parameter_name;
      }
    }

    result += "call " + schema_name + "." + routine_name + "(" + parameters_list + ");\n";
    if (!variables_list.empty())
      result += "select " + variables_list + ";\n";

  }
  else
  {
    std::string parameter_list;

    for (size_t i = 0; i < _edits.size(); ++i)
    {
      // For functions there's a 1:1 relationship between input edits and parameters.
      db_mysql_RoutineParamRef parameter = parameters[i];
      if (!parameter_list.empty())
        parameter_list += ", ";

      if (needs_quoting(parameter->datatype()))
        parameter_list += "'" + _edits[i]->get_string_value() + "'";
      else
        parameter_list += _edits[i]->get_string_value();
    }

    result = "select " + schema_name + "." + routine_name + "(" + parameter_list + ");\n";
  }

  for (size_t i = 0; i < _edits.size(); ++i)
    _edits[i]->release();

  return result;
}

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