Пример #1
0
class Commands:
    """Helper class, contains command for bind events, menu and buttons."""
    def __init__(self, drawer, dumper):
        """Initilizing commands class."""
        self.drawer = drawer
        self.dumper = dumper
        self.message = Message(self.drawer)

    def db_browse(self, event):
        """Change file database sqlite."""
        self.drawer.path = self.drawer.db_ctrl.GetPath()

    def sql_browse(self, event):
        """Change file dump file for save."""
        self.drawer.path_sql = self.drawer.sql_ctrl.GetPath()

    def connect(self, event):
        """Connect sqlite database."""
        if self.drawer.but_connect.GetValue():
            self.dumper.start(self.drawer.path)
            self.drawer.tables.Set(self.dumper.get_tables_names())
            self.drawer.but_add_table.Enable()
            self.drawer.but_insert.Enable()
            self.drawer.but_update.Enable()
            self.drawer.but_delete.Enable()
            self.drawer.but_save.Enable()
            self.drawer.but_connect.SetLabel('Отключить')
        else:
            self.dumper.finish()
            self.drawer.tables.Set([])
            self.drawer.but_add_table.Disable()
            self.drawer.but_insert.Disable()
            self.drawer.but_update.Disable()
            self.drawer.but_delete.Disable()
            self.drawer.but_save.Disable()
            self.drawer.but_connect.SetLabel('Подключить')
        self.drawer.Layout()

    def save(self, event):
        """Save dump sql database in file."""
        if '' == self.drawer.path_sql:
            self.message.error('Ошибка', 'Имя файла дампа не указано')
            return
        self.dumper.dump(self.drawer.path_sql)
        if self.drawer.mysql.GetValue():
            self.dumper.fix_mysql(self.drawer.path_sql)
        if self.drawer.drop_table.GetValue():
            self.dumper.add_drop_tables(self.drawer.path_sql)
        if self.drawer.del_quotes.GetValue():
            self.dumper.del_quotes_around_table_name(self.drawer.path_sql)
        if self.drawer.use_db.GetValue():
            self.dumper.use_database_name(self.drawer.path_sql)
        elif self.drawer.add_db.GetValue():
            self.dumper.add_database_name(self.drawer.path_sql)
        self.message.information('Сохранение', 'Файл дампа успешно сохранен')

    def about(self, event):
        """Run about dialog."""
        About(self.drawer, 'О программе...', 'Конструктор дампов базы данных',
              '1.0', 'Руслан Долованюк').ShowModal()

    def close(self, event):
        """Close event for button close."""
        self.drawer.Close(True)

    def close_window(self, event):
        """Close window event."""
        self.drawer.Destroy()
Пример #2
0
class AddTableDialog(wx.Dialog):
    """Create interface dialog add table."""
    def __init__(self, parent):
        """Initialize interface."""
        super().__init__(parent, wx.ID_ANY, 'Добавление таблицы')
        self.SetExtraStyle(wx.WS_EX_VALIDATE_RECURSIVELY)
        self.command = parent.command
        self.message = Message(self)
        self.params = []
        self.param_names = []

        box_table = wx.StaticBox(self, wx.ID_ANY, 'Наименование таблицы')
        self.table = wx.TextCtrl(self, wx.ID_ANY, validator=TableValidator())
        box_columns = wx.StaticBox(self, wx.ID_ANY, 'Столбцы')
        box_name = wx.StaticBox(box_columns, wx.ID_ANY, 'Имя')
        self.name = wx.TextCtrl(box_name, wx.ID_ANY)
        box_type = wx.StaticBox(box_columns, wx.ID_ANY, 'Тип')
        self.type = wx.Choice(box_type,
                              wx.ID_ANY,
                              choices=parent.command.dumper.get_param_types())
        self.not_null = wx.CheckBox(box_columns, wx.ID_ANY, 'NOT NULL')
        but_add = wx.Button(box_columns, wx.ID_ANY, 'Добавить')
        self.columns = wx.ListBox(self,
                                  wx.ID_ANY,
                                  choices=self.param_names,
                                  style=wx.LB_SINGLE | wx.LB_HSCROLL)
        self.but_del = wx.Button(self, wx.ID_ANY, 'Удалить')
        but_save = wx.Button(self, wx.ID_OK, 'Сохранить')
        but_cancel = wx.Button(self, wx.ID_CANCEL, 'Отмена')

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer_panels = wx.GridSizer(rows=1, cols=2, hgap=5, vgap=5)
        sizer_left = wx.BoxSizer(wx.VERTICAL)
        sizer_table = wx.StaticBoxSizer(box_table, wx.HORIZONTAL)
        sizer_table.Add(self.table, 1, wx.EXPAND | wx.ALL, 5)
        sizer_left.Add(sizer_table, 0, wx.EXPAND | wx.ALL)
        sizer_columns = wx.StaticBoxSizer(box_columns, wx.VERTICAL)
        sizer_name = wx.StaticBoxSizer(box_name, wx.HORIZONTAL)
        sizer_name.Add(self.name, 1, wx.EXPAND | wx.ALL, 5)
        sizer_columns.Add(sizer_name, 0, wx.EXPAND | wx.ALL)
        sizer_type = wx.StaticBoxSizer(box_type, wx.HORIZONTAL)
        sizer_type.Add(self.type, 1, wx.EXPAND | wx.ALL, 5)
        sizer_columns.Add(sizer_type, 0, wx.EXPAND | wx.ALL)
        sizer_columns.Add(self.not_null, 0, wx.EXPAND | wx.ALL, 5)
        sizer_columns.Add(but_add, 0, wx.ALIGN_CENTER, 5)
        sizer_left.Add(sizer_columns, 1, wx.EXPAND | wx.ALL)
        sizer_panels.Add(sizer_left, 1, wx.EXPAND | wx.ALL)
        sizer_right = wx.BoxSizer(wx.VERTICAL)
        sizer_right.Add(self.columns, 1, wx.EXPAND | wx.ALL, 5)
        sizer_right.Add(self.but_del, 0, wx.ALIGN_CENTER, 5)
        sizer_panels.Add(sizer_right, 1, wx.EXPAND | wx.ALL)
        sizer.Add(sizer_panels, 1, wx.EXPAND | wx.ALL)
        sizer_but = wx.GridSizer(rows=1, cols=2, hgap=5, vgap=5)
        sizer_but.Add(but_save, 0, wx.ALIGN_LEFT | wx.ALIGN_CENTER_VERTICAL)
        sizer_but.Add(but_cancel, 0, wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL)
        sizer.Add(sizer_but, 0, wx.EXPAND | wx.ALL)
        self.SetSizer(sizer)

        self.Bind(wx.EVT_BUTTON, self.add_column, but_add)
        self.Bind(wx.EVT_BUTTON, self.del_column, self.but_del)
        self.Bind(wx.EVT_LISTBOX, self.sel_column, self.columns)

        but_save.SetDefault()
        self.not_null.SetValue(True)
        self.type.SetSelection(0)
        self.but_del.Disable()
        self.Layout()

    def add_column(self, event):
        """Add column in list params."""
        typ = self.command.dumper.get_param_types()[self.type.GetSelection()]
        name = self.name.GetValue()
        if '' == name:
            self.message.error('Ошибка имени столбца',
                               'Имя столбца не указано')
        elif name in self.param_names:
            self.message.error('Ошибка имени столбца',
                               'Данное имя уже существует')
        elif 'id' == name:
            self.message.error('Ошибка имени столбца',
                               'Данное имя зарезервировано')
        else:
            not_null = ' NOT NULL' if self.not_null.GetValue() else ''
            param = name + ' ' + typ + not_null
            self.params.append(param)
            self.param_names.append(name)
            self.columns.Set(self.param_names)
            self.name.SetValue('')
            self.type.SetSelection(0)
            self.not_null.SetValue(True)
            self.Layout()

    def sel_column(self, event):
        """Change selection in columns list."""
        self.but_del.Enable()

    def del_column(self, event):
        """Delete column from list."""
        index = self.columns.GetSelection()
        self.param_names.pop(index)
        self.columns.Set(self.param_names)
        self.but_del.Disable()
        self.Layout()