File: db_sql_editor_log.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 (409 lines) | stat: -rw-r--r-- 12,257 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* 
 * Copyright (c) 2007, 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 "db_sql_editor_log.h"
#include "sqlide/recordset_data_storage.h"

#include "base/string_utilities.h"
#include "base/threading.h"
#include "base/wb_memory.h"
#include "base/file_functions.h"
#include "base/file_utilities.h"
#include "base/log.h"

#include "mforms/utilities.h"
#include "wb_sql_editor_form.h"
#include "wb_sql_editor_panel.h"
#include <boost/foreach.hpp>

DEFAULT_LOG_DOMAIN("SqlEditorLog");

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

// In the actions UI, truncate anything that's longer than this.
// In the actions log file we use the full string.
#define MAX_LOG_STATEMENT_TEXT 4098

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

DbSqlEditorLog::DbSqlEditorLog(SqlEditorForm *owner, GRTManager *grtm, int max_entry_count)
  : VarGridModel(grtm), _owner(owner), _max_entry_count(max_entry_count)
{
  reset();
  std::string log_dir = base::join_path(grtm->get_user_datadir().c_str(), "log", "");
  create_directory(log_dir, 0700);
  _log_file_name = base::join_path(log_dir.c_str(), sanitize_file_name("sql_actions_" + owner->get_session_name() + ".log").c_str(), "");

  _context_menu.add_item("Copy Row", "copy_row");
  _context_menu.add_item("Copy Action", "copy_action");
  _context_menu.add_item("Copy Response", "copy_message");
  _context_menu.add_item("Copy Duration", "copy_duration");
  _context_menu.add_separator();
  _context_menu.add_item("Append Selected Items to SQL script", "append_selected_items");
  _context_menu.add_item("Replace SQL Script With Selected Items", "replace_sql_script");
  _context_menu.add_separator();
  _context_menu.add_item("Clear", "clear");
  _context_menu.set_handler(boost::bind(&DbSqlEditorLog::handle_context_menu, this, _1));
  
  for (int i = 0; i < 8; i++)
    _context_menu.set_item_enabled(i, false);
}

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

std::string DbSqlEditorLog::get_selection_text(bool time, bool query, bool result, bool duration)
{  
  std::string sql;
  for (std::vector<int>::const_iterator end = _selection.end(), it = _selection.begin(); it != end; ++it)
  {
    std::string s;
    bool need_tab = false;
    
    if (!sql.empty())
      sql.append("\n");
    
    if (time)
    {
      get_field(*it, 2, s);
      need_tab = true;
    }
    if (query)
    {
      if (need_tab)
        sql.append(s).append("\t");
      need_tab = true;
      get_field(*it, 3, s);
    }
    if (result)
    {
      if (need_tab)
        sql.append(s).append("\t");
      need_tab = true;
      get_field(*it, 4, s);
    }
    if (duration)
    {
      if (need_tab)
        sql.append(s).append("\t");
      get_field(*it, 5, s);
    }
    sql.append(s).append("\n");
  }
  return sql;
}

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

void DbSqlEditorLog::handle_context_menu(const std::string &action)
{
  std::string sql;
  if (action == "copy_row")
  {
    sql = get_selection_text(true, true, true, true);
    mforms::Utilities::set_clipboard_text(sql);
  }
  else if (action == "copy_action")
  {
    sql = get_selection_text(false, true, false, false);
    mforms::Utilities::set_clipboard_text(sql);
  }
  else if (action == "copy_message")
  {
    sql = get_selection_text(false, false, true, false);
    mforms::Utilities::set_clipboard_text(sql);
  }
  else if (action == "copy_duration")
  {
    sql = get_selection_text(false, false, false, true);
    mforms::Utilities::set_clipboard_text(sql);
  }
  else if (action == "append_selected_items")
  {
    sql = get_selection_text(false, true, false, false);
    SqlEditorPanel *editor(_owner->active_sql_editor_panel());
    if (editor)
      editor->editor_be()->append_text(sql);
  }
  else if (action == "replace_sql_script")
  {
    sql = get_selection_text(false, true, false, false);
    SqlEditorPanel *editor(_owner->active_sql_editor_panel());
    if (editor)
      editor->editor_be()->sql(sql.c_str());
  }
  else if (action == "clear")
  {
    reset();
  }
}

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

void DbSqlEditorLog::set_selection(const std::vector<int> &selection)
{
  _selection = selection;
  bool has_selection = !selection.empty();
  for (int i = 0; i < 8; i++)
    _context_menu.set_item_enabled(i, has_selection);
}

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

void DbSqlEditorLog::reset()
{
  VarGridModel::reset();

  {
    base::RecMutexLock data_mutex(_data_mutex);
    _data.clear();
    _next_id = 1;
  }

  _readonly= true;

  add_column("", int()); // msg type (icon)
  add_column("#", int()); // sequence no.
  add_column("Time", std::string());
  add_column("Action", std::string());
  add_column("Message", std::string());
  add_column("Duration / Fetch", std::string());

  boost::shared_ptr<sqlite::connection> data_swap_db= this->data_swap_db();
  Recordset_data_storage::create_data_swap_tables(data_swap_db.get(), _column_names, _column_types);

  refresh_ui();
}

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

void DbSqlEditorLog::refresh()
{
  refresh_ui();
}

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

class MsgTypeIcons
{
public:
  MsgTypeIcons()
  {
    IconManager *icon_man= IconManager::get_instance();
    _error_icon= icon_man->get_icon_id("mini_error.png");
    _warning_icon= icon_man->get_icon_id("mini_warning.png");
    _info_icon= icon_man->get_icon_id("mini_notice.png");
    _ok_icon= icon_man->get_icon_id("mini_ok.png");
    _edit_icon= icon_man->get_icon_id("mini_edit.png");
  }
private:
  IconId _error_icon;
  IconId _warning_icon;
  IconId _info_icon;
  IconId _edit_icon;
  IconId _ok_icon;
public:
  IconId icon(DbSqlEditorLog::MessageType msg_type)
  {
    switch (msg_type)
    {
      case DbSqlEditorLog::BusyMsg: return 0;
      case DbSqlEditorLog::EditMsg: return _edit_icon;
      case DbSqlEditorLog::NoteMsg: return _info_icon;
      case DbSqlEditorLog::OKMsg: return _ok_icon;
      case DbSqlEditorLog::ErrorMsg: return _error_icon;
      case DbSqlEditorLog::WarningMsg: return _warning_icon;
    }
    return _info_icon;
  }
};

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

IconId DbSqlEditorLog::get_field_icon(const NodeId &node, ColumnId column, IconSize size)
{
  IconId icon= 0;

  static MsgTypeIcons msg_type_icons;
  switch (column)
  {
  case 0:
    Cell cell;
    if (get_cell(cell, node, column, false))
    {
      int msg_type= boost::get<int>(*cell);
      icon= msg_type_icons.icon((DbSqlEditorLog::MessageType)msg_type);
    }
    break;
  }

  return icon;
}

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

static std::string sanitize_text(const std::string &text)
{
  std::string output;
  for (std::string::const_iterator end = text.end(), ch = text.begin(); ch != end; ++ch)
  {
    if (*ch == '\n' || *ch == '\r' || *ch == '\t')
      output.push_back(' ');
    else
      output.push_back(*ch);
  }
  return output;
}

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

bool DbSqlEditorLog::get_field(const bec::NodeId &node, ColumnId column, std::string &value)
{
  if (VarGridModel::get_field(node, column, value))
  {
    if (column == 3)
      value = sanitize_text(base::truncate_text(value, MAX_LOG_STATEMENT_TEXT));
    else if (column == 4)
      value = sanitize_text(value);
    return true;
  }
  return false;
}

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

bool DbSqlEditorLog::get_field_description(const bec::NodeId &node, ColumnId column, std::string &value)
{
  return VarGridModel::get_field(node, column, value);
}

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

RowId DbSqlEditorLog::add_message(int msg_type, const std::string &context, const std::string &msg, const std::string &duration)
{
  if (msg.empty())
    return -1;

  std::string time = current_time();
  if (!_log_file_name.empty())
  {
    base::FILE_scope_ptr fp = base_fopen(_log_file_name.c_str(), "a");
    fprintf(fp, "[%u, %s] %s: %s\n",  _next_id, time.c_str(), context.c_str(), msg.c_str());
  }
  else
  {
    log_error("DbSqlEditorLog::add_message called with no log file name set\n");
    return -1;
  }

  {
    base::RecMutexLock data_mutex(_data_mutex);

    // Remove oldest messages if there are more than the maximum allowed number.
    if (_max_entry_count > -1 && _max_entry_count - 1 < (int)_row_count)
    {
      _data.erase(_data.begin(), _data.begin() + (_row_count - _max_entry_count + 1) * _column_count);
      _row_count = _max_entry_count - 1;
    }

    add_message_with_id(_next_id, time, msg_type, context, msg, duration);
  }

  return _next_id++;
}

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

void DbSqlEditorLog::set_message(RowId row, int msg_type, const std::string &context, const std::string &msg, const std::string &duration)
{
  std::string time = current_time();
  {
    base::FILE_scope_ptr fp = base_fopen(_log_file_name.c_str(), "a");
    fprintf(fp, "[%u, %s] %s: %s\n",  (unsigned)row, time.c_str(), context.c_str(), msg.c_str());
  }

  base::RecMutexLock data_mutex(_data_mutex);

  // Scan backwards for the given row id to find the right message entry.
  // Usually this will find the actual entry quickly, since normally add_message and set_message
  // aren't that much apart.
  // If the log has been cleared in the meantime however then simply add the message.
  if (_data.size() == 0)
  {
    add_message_with_id(row, time, msg_type, context, msg, duration);
    return;
  }

  Data::reverse_iterator cell= _data.rbegin() + _column_count - 2;
  while (cell != _data.rend())
  {
    unsigned id = (unsigned)boost::apply_visitor(_var_to_int, *cell);
    if (id == row)
    {
      *(cell + 1) = msg_type;
      --cell;
      *--cell = base::strip_text(context);
      *--cell = msg;
      *--cell = duration;
      break;
    }
    cell += _column_count;
  }
}

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

/**
 * This function does actually add the message and can also be called be set_message, if the 
 * there's no message with a given id anymore.
 */
void DbSqlEditorLog::add_message_with_id(RowId id, const std::string &time, int msg_type,
  const std::string &context, const std::string &msg, const std::string &duration)
{
  _data.reserve(_data.size() + _column_count);

  try
  {
    _data.push_back(msg_type);
    _data.push_back((int)id);
    _data.push_back(time);
    _data.push_back(base::strip_text(context));
    _data.push_back(msg);
    _data.push_back(duration);
  }
  catch(...)
  {
    _data.resize(_row_count * _column_count);
    throw;
  }

  ++_row_count;
  ++_data_frame_end;
}

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

mforms::Menu* DbSqlEditorLog::get_context_menu()
{
  return &_context_menu;
}

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