File: wb_editor_storednote.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,273 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 "base/util_functions.h"
#include "base/string_utilities.h"

#include "wb_editor_storednote.h"

#include "grts/structs.workbench.physical.h"
#include "grt/exceptions.h"

#include "mforms/code_editor.h"
#include "mforms/toolbar.h"

#include "grtsqlparser/mysql_parser_services.h"

using namespace base;
using namespace parser;


static struct {
  const char *label;
  const char *name;
} inclusion_positions[] = {
  { "Do not include", "" },
  { "Top of script", "top_file" },
  { "Before DDL", "before_ddl" },
  { "After DDL", "after_ddl" },
  { "Before Inserts", "before_inserts" },
  { "After Inserts", "after_inserts" },  
  { "Bottom of script", "bottom_file" },
  { NULL, NULL }
};


StoredNoteEditorBE::StoredNoteEditorBE(bec::GRTManager *grtm, const GrtStoredNoteRef &note)
: bec::BaseEditor(grtm, note), _note(note)
{
  _ignored_object_fields_for_ui_refresh.insert("lastChangeDate");
}


bool StoredNoteEditorBE::is_script()
{
  return _note.is_instance(db_Script::static_class_name());
}


MySQLEditor::Ref StoredNoteEditorBE::get_sql_editor()
{
  if (!_sql_editor)
  {
    workbench_physical_ModelRef model(workbench_physical_ModelRef::cast_from(_note->owner()));
    MySQLParserServices::Ref services = MySQLParserServices::get(get_grt());
    ParserContext::Ref context = services->createParserContext(model->catalog()->characterSets(), model->catalog()->version(), false);
    ParserContext::Ref autocomplete_context = services->createParserContext(model->catalog()->characterSets(), model->catalog()->version(), false);
    _sql_editor = MySQLEditor::create(get_grt(), context, autocomplete_context);

    scoped_connect(_sql_editor->text_change_signal(),
      boost::bind(&StoredNoteEditorBE::do_partial_ui_refresh, this, (int)BaseEditor::RefreshTextChanged));

    if (is_script())
    {
      mforms::ToolBar *tbar = _sql_editor->get_toolbar();
      mforms::ToolBarItem *item;
      db_ScriptRef script(db_ScriptRef::cast_from(_note));

      std::string syncvalue = inclusion_positions[0].label, fwvalue = inclusion_positions[0].label;
      std::vector<std::string> sync_choices, fw_choices;
      for (int i = 0; inclusion_positions[i].label != NULL; i++)
      {
        if (strcmp(inclusion_positions[i].name, "after_inserts") != 0 && strcmp(inclusion_positions[i].name, "before_inserts") != 0)
          sync_choices.push_back(inclusion_positions[i].label);
        fw_choices.push_back(inclusion_positions[i].label);
        if (strcmp(inclusion_positions[i].name, script->synchronizeScriptPosition().c_str()) == 0)
          syncvalue = inclusion_positions[i].label;
        if (strcmp(inclusion_positions[i].name, script->forwardEngineerScriptPosition().c_str()) == 0)
          fwvalue = inclusion_positions[i].label;
      }

      item = mforms::manage(new mforms::ToolBarItem(mforms::ExpanderItem));
      tbar->add_item(item);

      item = mforms::manage(new mforms::ToolBarItem(mforms::LabelItem));
      item->set_text(_("Synchronization:"));
      tbar->add_item(item);

      item = mforms::manage(new mforms::ToolBarItem(mforms::SelectorItem));
      item->set_selector_items(sync_choices);
      item->set_name("syncscript");
      item->set_tooltip(_("Position to insert this in synchronization output scripts"));
      item->signal_activated()->connect(boost::bind(&StoredNoteEditorBE::changed_selector, this, item));
      item->set_text(syncvalue);
      tbar->add_item(item);

      item = mforms::manage(new mforms::ToolBarItem(mforms::LabelItem));
      item->set_text(_("Forward Engineering:"));
      tbar->add_item(item);

      item = mforms::manage(new mforms::ToolBarItem(mforms::SelectorItem));
      item->set_selector_items(fw_choices);
      item->set_name("forwardscript");
      item->signal_activated()->connect(boost::bind(&StoredNoteEditorBE::changed_selector, this, item));
      item->set_tooltip(_("Position to insert this in forward engineering output scripts"));
      item->set_text(fwvalue);
      tbar->add_item(item);
    }

    // Remove syntax highlighting if this is a note.
    if (!is_script())
    {
      _sql_editor->get_editor_control()->set_language(mforms::LanguageNone);
      _sql_editor->set_sql_check_enabled(false);
    }
  }
  return _sql_editor;
}


void StoredNoteEditorBE::changed_selector(mforms::ToolBarItem *item)
{
  std::string value = item->get_text();
  std::string s;
  for (int i = 0; inclusion_positions[i].label != NULL; i++)
    if (strcmp(inclusion_positions[i].label, value.c_str()) == 0)
    {
      s = inclusion_positions[i].name;
      break;
    }

  bec::AutoUndoEdit undo(this);
  if (item->get_name() == "syncscript")
  {
    db_ScriptRef::cast_from(_note)->synchronizeScriptPosition(s);
    undo.end(base::strfmt(_("Change sync output position for %s"), get_name().c_str()));
  }
  else
  {
    db_ScriptRef::cast_from(_note)->forwardEngineerScriptPosition(s);
    undo.end(base::strfmt(_("Change forward eng. output position for %s"), get_name().c_str()));
  }
}


void StoredNoteEditorBE::set_text(grt::StringRef text)
{  
  //XXX replace this using module wrapper class
  grt::Module *module= get_grt()->get_module("Workbench");
  if (!module)
    throw std::runtime_error("Workbench module not found");

  grt::BaseListRef args(get_grt());

  args.ginsert(_note->filename());
  args.ginsert(text);

  module->call_function("setAttachedFileContents", args);
  
  _note->lastChangeDate(base::fmttime(0, DATETIME_FMT));
}


grt::StringRef StoredNoteEditorBE::get_text(bool &isutf8)
{
  grt::Module *module= get_grt()->get_module("Workbench");
  if (!module)
    throw std::runtime_error("Workbench module not found");

  grt::BaseListRef args(get_grt());

  args.ginsert(_note->filename());

  grt::StringRef value(grt::StringRef::cast_from(module->call_function("getAttachedFileContents", args)));

  if (!g_utf8_validate(value.c_str(), (gssize)strlen(value.c_str()), NULL))
  {
    isutf8= false;
    return "";
  }
  isutf8= true;

  return value;
}


void StoredNoteEditorBE::set_name(const std::string &name)
{
  if (_note->name() != name)
  {
    workbench_physical_ModelRef model(workbench_physical_ModelRef::cast_from(_note->owner()));

    if (!model.is_valid())
      throw std::logic_error("Note owner not set");

    grt::ListRef<GrtStoredNote> notes(model->notes());
    for (size_t c= notes.count(), i= 0; i < c; i++)
    {
      GrtStoredNoteRef note(notes[i]);

      if (note != _note && *note->name() == name)
        throw bec::validation_error(_("Duplicate note name."));
    }

    bec::AutoUndoEdit undo(this, _note, "name");
    _note->name(name);
    undo.end(strfmt(_("Rename '%s' to '%s'"), _note->name().c_str(), name.c_str()));
  }
}


std::string StoredNoteEditorBE::get_name()
{
  return _note->name();
}

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

std::string StoredNoteEditorBE::get_title()
{
  std::string result = is_script() ? base::strfmt("%s - Script", get_name().c_str()) : base::strfmt("%s - Stored Note", get_name().c_str());
  if (is_editor_dirty())
    result += "*";
  
  return result;
}

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

/**
 * Loads the note text from the GRT into the editor.
 */
void StoredNoteEditorBE::load_text()
{
  bool isUTF8;

  grt::StringRef text = get_text(isUTF8);
  MySQLEditor::Ref editor = get_sql_editor();
  mforms::CodeEditor* code_editor = editor->get_editor_control();
  if (isUTF8)
    code_editor->set_text_keeping_state(text.c_str());
  else
    code_editor->set_text(_("Data is not UTF8 encoded and cannot be displayed."));
  code_editor->reset_dirty();
}

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

void StoredNoteEditorBE::commit_changes()
{
  MySQLEditor::Ref editor = get_sql_editor();
  mforms::CodeEditor* code_editor = editor->get_editor_control();
  if (code_editor->is_dirty())
  {
    std::pair<const char*, size_t> text = code_editor->get_text_ptr();
    set_text(grt::StringRef(text.first));

    // Note: the dirty state of the editor does *not* indicate the dirty state of the note object
    //       or even the model file. Instead it only reflects if the text differs from the content
    //       of the note object. WBContext instead keeps track of changed attachments.
    code_editor->reset_dirty();
  }
}

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