File: MGSchemaBrowserHelper.cc

package info (click to toggle)
mysql-gui-tools 5.0r12-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 105,540 kB
  • ctags: 50,897
  • sloc: sql: 348,439; pascal: 285,780; cpp: 94,578; ansic: 90,768; objc: 33,761; sh: 25,629; xml: 10,924; yacc: 10,755; java: 9,986; php: 2,806; python: 2,068; makefile: 1,945; perl: 3
file content (216 lines) | stat: -rw-r--r-- 6,013 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
/* Copyright (C) 2004 MySQL AB

   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; either version 2 of the License, or
   (at your option) any later version.

   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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */


#include "MGSchemaBrowserHelper.h"
#include "MGTableEditor.h"
#include "myg_utils.h"
#include "myg_gtkutils.h"
#include "mygpriv.h"

#include "MGTableEditor.h"

#include <gtkmm/messagedialog.h>


MGSchemaBrowserHelper::MGSchemaBrowserHelper(MGTableBrowserList *browser)
  : _editor(0), _browser(browser), _mysql(0)
{
  
}


void MGSchemaBrowserHelper::set_mysql(MYSQL *mysql)
{
  _mysql= mysql;
}


bool MGSchemaBrowserHelper::confirm(const Glib::ustring &message)
{
  Gtk::MessageDialog dlg(message, false, Gtk::MESSAGE_WARNING, Gtk::BUTTONS_OK_CANCEL,
                         true);

  return dlg.run() == Gtk::RESPONSE_OK;
}


void MGSchemaBrowserHelper::create_schema()
{
  Glib::ustring query, name;

  if (myg_ask_string(_("Create Schema"),
                     _("<b>Create Schema</b>\nEnter a name for the new schema."), name))
  {
    MYX_LIB_ERROR error;
    long long int affected_rows= 0L;

    myx_query_execute_direct(_mysql, ufmt("CREATE DATABASE `%s`", name.c_str()).c_str(), &error, &affected_rows);

    if (error != MYX_NO_ERROR)
      myg_show_xlib_error(_("Error creating schema."), error);
    else
      _signal_refresh.emit();
  }
}


void MGSchemaBrowserHelper::create_table()
{
  Glib::ustring catalog, schema;
  Gtk::TreeIter it= _browser->get_selected();
  if (it)
  {
    catalog= _browser->get_catalog(it);
    schema= _browser->get_schema(it);

    if (!_editor)
    {
      _editor= new MGTableEditor(true);
      _editor->signal_close().connect(sigc::mem_fun(*this,&MGSchemaBrowserHelper::table_editor_closed));
    }
    _editor->new_table(_mysql, catalog, schema, _browser->get_catalogs());
  }
}


void MGSchemaBrowserHelper::edit_table()
{
  Gtk::TreeIter iter= _browser->get_selected();
  if (iter)
  {    
    Gtk::TreeRow row= *iter;
    Glib::ustring catalog, schema;
    Glib::ustring table= _browser->get_table(iter);

    catalog= _browser->get_catalog(iter);
    schema= _browser->get_schema(iter);

    if (!_editor)
    {
      _editor= new MGTableEditor(true);
      _editor->signal_close().connect(sigc::mem_fun(*this,&MGSchemaBrowserHelper::table_editor_closed));
    }
    _editor->show_table(_mysql, catalog, schema, table,
                        _browser->get_catalogs());
  }
}


void MGSchemaBrowserHelper::table_editor_closed()
{
  delete _editor;
  _editor= 0;
  
  _signal_refresh.emit();
}


void MGSchemaBrowserHelper::edit_object()
{
  Gtk::TreeIter iter= _browser->get_selected();
  if (iter) 
  {
    switch (_browser->get_type(iter))
    {
    case MGTableBrowserList::Table:
      edit_table();
      break;
    default:
      break;
    }
  }
}


void MGSchemaBrowserHelper::drop_object()
{
  bool is_func;
  Gtk::TreeIter iter= _browser->get_selected();
  if (iter) 
  {
    Glib::ustring query, what, schema;
    schema= _browser->get_schema(iter);
    
    switch (_browser->get_type(iter))
    {
    case MGTableBrowserList::Schema:
      what= schema;
      if (confirm(ufmt(_("This will permanently drop (delete) the Schema '%s' and all its tables.\nPlease confirm."),
                       what.c_str())))
      {
        query= ufmt("DROP DATABASE `%s`", what.c_str());
      }
      break;
    case MGTableBrowserList::Table:
      what= _browser->get_table(iter);
      if (_browser->is_view(iter))
      {
        if (confirm(ufmt(_("This will permanently drop (delete) the View '%s'.\nPlease confirm."),
                         what.c_str())))
        {
          query= ufmt("DROP VIEW `%s`.`%s`", schema.c_str(), what.c_str());
        }
      }
      else
      {
        if (confirm(ufmt(_("This will permanently drop (delete) the Table '%s'.\nPlease confirm."),
                         what.c_str())))
        {
          query= ufmt("DROP TABLE `%s`.`%s`", schema.c_str(), what.c_str());
        }
      }
      break;
    case MGTableBrowserList::SP:
      //what= _browser->get_table(iter);
      what= _browser->get_procedure(iter, is_func);
      if (confirm(ufmt(_("This will permanently drop (delete) the Procedure/Function '%s'.\nPlease confirm."),
                       what.c_str())))
      {
        if(is_func)
        {
          query= ufmt("DROP FUNCTION `%s`.`%s`", schema.c_str(), what.c_str());
        }
        else
        {
          query= ufmt("DROP PROCEDURE `%s`.`%s`", schema.c_str(), what.c_str());
        }
      }
      break;
    default:
      break;
    }

    if (!query.empty())
    {
      MYX_LIB_ERROR error;
      long long int affected_rows= 0L;

      myx_query_execute_direct(_mysql, 
        "/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;", 
          &error, &affected_rows);
      myx_query_execute_direct(_mysql, query.c_str(), &error, &affected_rows);
      myx_query_execute_direct(_mysql, 
        "/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;", 
        &error, &affected_rows);

      if (error != MYX_NO_ERROR)
        myg_show_xlib_error(ufmt(_("Error executing drop of %s."), what.c_str()), error);
      else
        _signal_refresh.emit();
    }
  }
}