コード例 #1
0
ファイル: sqlide_grt.py プロジェクト: Vescatur/DatabaseTest
def showInspector(editor, selection):
    schema_insp = []
    table_insp = []
    table_insp_idx = []
    for s in selection:
        if s.type == "db.Schema":
            schema_insp.append(s.schemaName)
        elif (s.type == "db.Table") or (s.type == "db.View"):
            table_insp.append((s.schemaName, s.name))
        elif s.type == "db.Index":
            table_insp_idx.append((s.schemaName, s.owner.name))
        else:
            log_error("Unsupported inspector type: %s\n" % s.type)
            
    if len(schema_insp):
        show_schema_manager(editor, schema_insp, False)
    if len(table_insp):
        show_table_inspector(editor, table_insp)
    if len(table_insp_idx):
        show_table_inspector(editor, table_insp_idx, "indexes")
    return 0
コード例 #2
0
def handleLiveTreeContextMenu(name, sender, args):
    menu = mforms.fromgrt(args['menu'])
    selection = args['selection']

    # Add extra menu items to the SQL editor live schema tree context menu

    node_type = None
    mixed = False
    selection_type = None
    object_selected = False
    object_selected_count = 0
    column_selected = False
    if len(selection) == 1:
        object_type_caption = {
            'db.Schema': "Schema",
            'db.Table': "Table",
            'db.View': "View",
            'db.Function': "Function",
            'db.StoredProcedure': "Stored Procedure",
            'db.Index': "Index",
            'db.Trigger': "Trigger",
            'db.ForeignKey': "Foreign Key",
        }
    else:
        object_type_caption = {
            'db.Schema': "Schemas",
            'db.Table': "Tables",
            'db.View': "Views",
            'db.Function': "Functions",
            'db.StoredProcedure': "Stored Procedures",
            'db.Index': "Indexes",
            'db.Trigger': "Triggers",
            'db.ForeignKey': "Foreign Keys",
        }
    unique_tables = set()
    selection_db_type = None
    for s in selection:
        if s.type != node_type:
            if node_type is None:
                node_type = s.type
            else:
                mixed = True
        if s.type == 'columns':
            selection_type = s.type
            column_selected = True
            unique_tables.add(s.name)
        elif s.type in ['tables', 'views', 'functions', 'storedProcedures']:
            selection_type = s.type
            object_selected = False
            if s.type == 'tables':
                selection_db_type = 'db.Table'
            elif s.type == 'views':
                selection_db_type = 'db.View'
            elif s.type == 'functions':
                selection_db_type = 'db.Function'
            elif s.type == 'storedProcedures':
                selection_db_type = 'db.StoredProcedure'
        elif s.type in ('db.Schema', 'db.Table', 'db.View', 'db.Function',
                        'db.StoredProcedure'):
            selection_type = s.type
            selection_db_type = s.type
            object_selected = True
            object_selected_count += 1
            if s.type == 'db.Table':
                unique_tables.add(s.name)
        elif s.type in ('db.Index', 'db.Trigger'):
            selection_type = s.type
            object_selected = False
        elif s.type == ('db.Column'):
            selection_type = s.type
            if s.owner:
                unique_tables.add(s.owner.name)
                selection_type = s.owner.type + ':' + s.type
            column_selected = True
        else:
            print "Unhandled type", s.type

    if mixed:
        selection_type = None
        pass
    else:
        item = menu.find_item('builtins_separator')
        if item:
            index = menu.get_item_index(item) + 1
        else:
            index = 0
        needs_separator = False

        if False and object_selected and selection_type == 'db.Table':
            item = mforms.newMenuItem(_("Export Data to CSV File..."))
            item.set_name("export_table_csv")
            menu.insert_item(index - 1, item)
            index += 1
            item = mforms.newMenuItem(_("Import Data from CSV File..."))
            item.set_name("import_table_csv")
            menu.insert_item(index - 1, item)
            index += 1
            item = mforms.newMenuItem(_("Dump Data to SQL File..."))
            item.set_name("dump_table")
            menu.insert_item(index - 1, item)
            index += 1

        if object_selected or column_selected:
            # Add menu items for generating code
            code_items = [
                # Caption, callback name, supported node types, enable condition, mixed node types allowed
                (_("Name"), 'name_short', ['db.Schema'], len(selection) > 0,
                 False),
                (_("Name (short)"), 'name_short', [
                    'tables', 'views', 'functions', 'storedProcedures',
                    'db.Table', 'db.Table:db.Column', 'db.View:db.Column',
                    'db.View', 'db.StoredProcedure', 'db.Function'
                ], len(selection) > 0, True),
                (_("Name (long)"), 'name_long', [
                    'tables', 'views', 'functions', 'storedProcedures',
                    'db.Table', 'db.Table:db.Column', 'db.View:db.Column',
                    'db.View', 'db.StoredProcedure', 'db.Function'
                ], len(selection) > 0, True),
                (_("Select All Statement"), 'select_all_statement',
                 ['db.Table', 'db.View'], len(selection) > 0, False),
                (_("Select All Statement"), 'select_all_statement',
                 ['columns'], len(selection) == 1
                 and selection[0].type == 'columns', False),
                (_("Select Columns Statement"), 'select_columns_statement',
                 ['db.Table:db.Column',
                  'db.View:db.Column'], len(unique_tables) == 1, False),
                (_("Insert Statement"), 'insert_all_statement',
                 ['db.Table', 'columns'], len(selection) > 0, False),
                (_("Insert Statement"), 'insert_columns_statement',
                 ['db.Table:db.Column'], len(selection) > 0, False),
                (_("Update Statement"), 'update_all_statement',
                 ['db.Table', 'columns'], len(selection) > 0, False),
                (_("Update Statement"), 'update_columns_statement',
                 ['db.Table:db.Column'], len(selection) > 0, False),
                (_("Delete Statement"), 'delete_statement', ['db.Table'],
                 len(selection) > 0, False),
                (_("Create Statement"), 'create_statement', [
                    'db.Schema', 'db.Table', 'db.View', 'db.StoredProcedure',
                    'db.Function'
                ], len(selection) > 0, False),
                (_("Procedure Call"), 'call_procedure', ['db.StoredProcedure'],
                 len(selection) > 0, False),
                (_("Function Call"), 'call_function', ['db.Function'],
                 len(selection) > 0, False),
                (None, None, ['db.Table'], None, False),
                (_("Join Selected Tables"), 'build_joined_select',
                 ['db.Table'], len(selection) == 2, False),
                (_("Delete with References"), 'build_cascaded_delete',
                 ['db.Table'], len(selection) == 1, False),
                (_("Select Row References"), 'build_cascaded_select',
                 ['db.Table'], len(selection) == 1, False),
            ]

            copy_submenu = mforms.newMenuItem("Copy to Clipboard")
            copy_submenu.set_name("copy_to_clipboard")
            menu.insert_item(index, copy_submenu)
            index += 1

            send_submenu = mforms.newMenuItem("Send to SQL Editor")
            send_submenu.set_name("send_to_editor")
            menu.insert_item(index, send_submenu)
            index += 1

            gencopy = CodeGenerator(sender, selection, False)
            gensend = CodeGenerator(sender, selection, True)

            for caption, name, types, enabled, allow_mixed in code_items:
                if not allow_mixed and mixed:
                    continue
                if types and selection_type not in types:
                    continue
                if caption is None:
                    copy_submenu.add_separator()
                    send_submenu.add_separator()
                else:
                    item = copy_submenu.add_item_with_title(
                        caption, getattr(gencopy, name), name)
                    item.set_enabled(enabled)

                    item = send_submenu.add_item_with_title(
                        caption, getattr(gensend, name), name)
                    item.set_enabled(enabled)
            needs_separator = True
        else:
            needs_separator = False

        if selection_type in ('tables', 'views', 'functions',
                              'storedProcedures', 'db.Schema', 'db.Table',
                              'db.View', 'db.Function',
                              'db.StoredProcedure') and len(selection) == 1:
            if needs_separator:
                menu.insert_item(
                    index, mforms.newMenuItem("", mforms.SeparatorMenuItem))
                index += 1
                needs_separator = False

            item = mforms.newMenuItem(
                "Create %s..." %
                object_type_caption.get(selection_db_type, selection_db_type))
            item.add_clicked_callback(lambda: do_create_object(
                sender, selection[0].schemaName, selection_db_type))
            menu.insert_item(index, item)
            index += 1

            if selection_type in ('db.Table', 'tables'):
                item = mforms.newMenuItem("Create Table Like...")
                menu.insert_item(index, item)
                index += 1
                #        it = item.add_item_with_title("Selected Table", lambda: template_manager.create_table_like(sender, selection[0].schemaName, selection[0].name))
                #        if selection[0].type == 'tables':
                #            it.set_enabled(False)
                for templ in template_manager.templates:
                    it = item.add_item_with_title(
                        templ.name,
                        lambda templ=templ, schema=selection[0].schemaName:
                        template_manager.create_table_like_template(
                            sender, schema, templ))
                item.add_separator()
                it = item.add_item_with_title("Edit Templates...",
                                              template_manager.edit_templates)

        if object_selected:
            if needs_separator:
                menu.insert_item(
                    index, mforms.newMenuItem("", mforms.SeparatorMenuItem))
                index += 1
                needs_separator = False

            if object_selected_count == 1:
                item = mforms.newMenuItem(
                    "Alter %s..." %
                    object_type_caption.get(selection_type, selection_type))
            else:
                item = mforms.newMenuItem(
                    "Alter %i %s..." %
                    (object_selected_count,
                     object_type_caption.get(selection_type, selection_type)))
            item.add_clicked_callback(
                lambda: do_alter_object(sender, selection))
            menu.insert_item(index, item)
            index += 1
            needs_separator = True

        if selection_type == 'db.Table' and object_selected:
            item = mforms.newMenuItem("Table Maintenance...")
            item.add_clicked_callback(lambda: show_schema_manager(
                sender, set([obj.schemaName for obj in selection]), True))
            menu.insert_item(index, item)
            index += 1

        if object_selected:  # or selection_type in ('db.Index', 'db.Trigger'):  <-- enable this once we support selective refresh of LST on drop of these objects
            if needs_separator:
                menu.insert_item(
                    index, mforms.newMenuItem("", mforms.SeparatorMenuItem))
                index += 1
                needs_separator = False

            if object_selected_count == 1:
                item = mforms.newMenuItem(
                    "Drop %s..." %
                    object_type_caption.get(selection_type, selection_type))
            else:
                item = mforms.newMenuItem(
                    "Drop %i %s..." %
                    (object_selected_count,
                     object_type_caption.get(selection_type, selection_type)))
            item.add_clicked_callback(
                lambda: do_drop_object(sender, selection))
            menu.insert_item(index, item)
            index += 1

        if selection_type == 'db.Table' and object_selected:
            item = mforms.newMenuItem(
                "Truncate %s..." %
                ("Table" if object_selected_count == 1 else "%i Tables" %
                 object_selected_count))
            item.add_clicked_callback(
                lambda: do_truncate_table(sender, selection))
            menu.insert_item(index, item)
            index += 1

        if selection_type in ('db.Table', 'db.Schema', 'tables'):
            item = mforms.newMenuItem("Search Table Data...")
            item.add_clicked_callback(lambda: open_search(sender))
            insert_item_to_plugin_context_menu(menu, item)
コード例 #3
0
def handleLiveTreeContextMenu(name, sender, args):
    menu = mforms.fromgrt(args['menu'])
    selection = args['selection']

    # Add extra menu items to the SQL editor live schema tree context menu

    node_type = None
    mixed = False
    selection_type = None
    object_selected = False
    object_selected_count = 0
    column_selected = False
    if len(selection) == 1:
        object_type_caption = {
            'db.Schema' : "Schema",
            'db.Table' : "Table",
            'db.View' : "View",
            'db.Function' : "Function",
            'db.StoredProcedure' : "Stored Procedure",
            'db.Index' : "Index",
            'db.Trigger' : "Trigger",
            'db.ForeignKey' : "Foreign Key",
        }
    else:
        object_type_caption = {
            'db.Schema' : "Schemas",
            'db.Table' : "Tables",
            'db.View' : "Views",
            'db.Function' : "Functions",
            'db.StoredProcedure' : "Stored Procedures",
            'db.Index' : "Indexes",
            'db.Trigger' : "Triggers",
            'db.ForeignKey' : "Foreign Keys",
        }
    unique_tables = set()
    selection_db_type = None
    for s in selection:
        if s.type != node_type:
            if node_type is None:
                node_type = s.type
            else:
                mixed = True
        if s.type == 'columns':
            selection_type = s.type
            column_selected = True
            unique_tables.add(s.name)
        elif s.type in ['tables', 'views', 'functions', 'storedProcedures']:
            selection_type = s.type
            object_selected = False
            if s.type == 'tables':
                selection_db_type = 'db.Table'
            elif s.type == 'views':
                selection_db_type = 'db.View'
            elif s.type == 'functions':
                selection_db_type = 'db.Function'
            elif s.type == 'storedProcedures':
                selection_db_type = 'db.StoredProcedure'
        elif s.type in ('db.Schema', 'db.Table', 'db.View', 'db.Function', 'db.StoredProcedure'):
            selection_type = s.type
            selection_db_type = s.type
            object_selected = True
            object_selected_count += 1
            if s.type == 'db.Table':
                unique_tables.add(s.name)
        elif s.type in ('db.Index', 'db.Trigger'):
            selection_type = s.type
            object_selected = False
        elif s.type == ('db.Column'):
            selection_type = s.type
            if s.owner:
                unique_tables.add(s.owner.name)
                selection_type = s.owner.type+':'+s.type
            column_selected = True
        else:
            print "Unhandled type", s.type

    if mixed:
        selection_type = None
        pass
    else:
        item = menu.find_item('builtins_separator')
        if item:
            index = menu.get_item_index(item)+1
        else:
            index = 0
        needs_separator = False

        if False and object_selected and selection_type == 'db.Table':
            item = mforms.newMenuItem(_("Export Data to CSV File..."))
            item.set_name("export_table_csv")
            menu.insert_item(index-1, item)
            index += 1
            item = mforms.newMenuItem(_("Import Data from CSV File..."))
            item.set_name("import_table_csv")
            menu.insert_item(index-1, item)
            index += 1
            item = mforms.newMenuItem(_("Dump Data to SQL File..."))
            item.set_name("dump_table")
            menu.insert_item(index-1, item)
            index += 1

        if object_selected or column_selected:
            # Add menu items for generating code
            code_items = [
                          # Caption, callback name, supported node types, enable condition, mixed node types allowed
                          (_("Name"), 'name_short', ['db.Schema'], len(selection) > 0, False),
                          (_("Name (short)"), 'name_short', ['tables', 'views', 'functions', 'storedProcedures', 'db.Table', 'db.Table:db.Column', 'db.View:db.Column', 'db.View', 'db.StoredProcedure', 'db.Function'], len(selection) > 0, True),
                          (_("Name (long)"), 'name_long', ['tables', 'views', 'functions', 'storedProcedures', 'db.Table', 'db.Table:db.Column', 'db.View:db.Column', 'db.View', 'db.StoredProcedure', 'db.Function'], len(selection) > 0, True),
                          (_("Select All Statement"), 'select_all_statement', ['db.Table', 'db.View'], len(selection) > 0, False),
                          (_("Select All Statement"), 'select_all_statement', ['columns'], len(selection) == 1 and selection[0].type == 'columns', False),
                          (_("Select Columns Statement"), 'select_columns_statement', ['db.Table:db.Column', 'db.View:db.Column'], len(unique_tables) == 1, False),
                          (_("Insert Statement"), 'insert_all_statement', ['db.Table', 'columns'], len(selection) > 0, False),
                          (_("Insert Statement"), 'insert_columns_statement', ['db.Table:db.Column'], len(selection) > 0, False),
                          (_("Update Statement"), 'update_all_statement', ['db.Table', 'columns'], len(selection) > 0, False),
                          (_("Update Statement"), 'update_columns_statement', ['db.Table:db.Column'], len(selection) > 0, False),
                          (_("Delete Statement"), 'delete_statement', ['db.Table'], len(selection) > 0, False),
                          (_("Create Statement"), 'create_statement', ['db.Schema', 'db.Table', 'db.View', 'db.StoredProcedure', 'db.Function'], len(selection) > 0, False),
                          (_("Procedure Call"), 'call_procedure', ['db.StoredProcedure'], len(selection) > 0, False),
                          (_("Function Call"), 'call_function', ['db.Function'], len(selection) > 0, False),
                          (None, None, ['db.Table'], None, False),
                          (_("Join Selected Tables"), 'build_joined_select', ['db.Table'], len(selection) == 2, False),
                          (_("Delete with References"), 'build_cascaded_delete', ['db.Table'], len(selection) == 1, False),
                          (_("Select Row References"), 'build_cascaded_select', ['db.Table'], len(selection) == 1, False),
                          ]

            copy_submenu = mforms.newMenuItem("Copy to Clipboard")
            copy_submenu.set_name("copy_to_clipboard")
            menu.insert_item(index, copy_submenu)
            index += 1

            send_submenu = mforms.newMenuItem("Send to SQL Editor")
            send_submenu.set_name("send_to_editor")
            menu.insert_item(index, send_submenu)
            index += 1

            gencopy = CodeGenerator(sender, selection, False)
            gensend = CodeGenerator(sender, selection, True)

            for caption, name, types, enabled, allow_mixed in code_items:
                if not allow_mixed and mixed:
                    continue
                if types and selection_type not in types:
                    continue
                if caption is None:
                    copy_submenu.add_separator()
                    send_submenu.add_separator()
                else:
                    item = copy_submenu.add_item_with_title(caption, getattr(gencopy, name), name)
                    item.set_enabled(enabled)

                    item = send_submenu.add_item_with_title(caption, getattr(gensend, name), name)
                    item.set_enabled(enabled)
            needs_separator = True
        else:
            needs_separator = False

        if selection_type in ('tables', 'views', 'functions', 'storedProcedures', 'db.Schema', 'db.Table', 'db.View', 'db.Function', 'db.StoredProcedure') and len(selection) == 1:
            if needs_separator:
                menu.insert_item(index, mforms.newMenuItem("", mforms.SeparatorMenuItem))
                index += 1
                needs_separator = False

            item = mforms.newMenuItem("Create %s..." % object_type_caption.get(selection_db_type, selection_db_type))
            item.add_clicked_callback(lambda: do_create_object(sender, selection[0].schemaName, selection_db_type))
            menu.insert_item(index, item)
            index+= 1

            if selection_type in ('db.Table', 'tables'):
                item = mforms.newMenuItem("Create Table Like...")
                menu.insert_item(index, item)
                index += 1
                #        it = item.add_item_with_title("Selected Table", lambda: template_manager.create_table_like(sender, selection[0].schemaName, selection[0].name))
                #        if selection[0].type == 'tables':
                #            it.set_enabled(False)
                for templ in template_manager.templates:
                    it = item.add_item_with_title(templ.name, lambda templ=templ, schema=selection[0].schemaName: template_manager.create_table_like_template(sender, schema, templ))
                item.add_separator()
                it = item.add_item_with_title("Edit Templates...", template_manager.edit_templates)

        if object_selected:
            if needs_separator:
                menu.insert_item(index, mforms.newMenuItem("", mforms.SeparatorMenuItem))
                index += 1
                needs_separator = False

            if object_selected_count == 1:
                item = mforms.newMenuItem("Alter %s..." % object_type_caption.get(selection_type, selection_type))
            else:
                item = mforms.newMenuItem("Alter %i %s..." % (object_selected_count, object_type_caption.get(selection_type, selection_type)))
            item.add_clicked_callback(lambda: do_alter_object(sender, selection))
            menu.insert_item(index, item)
            index+= 1
            needs_separator = True

        if selection_type == 'db.Table' and object_selected:
            item = mforms.newMenuItem("Table Maintenance...")
            item.add_clicked_callback(lambda: show_schema_manager(sender, set([obj.schemaName for obj in selection]), True))
            menu.insert_item(index, item)
            index += 1

        if object_selected: # or selection_type in ('db.Index', 'db.Trigger'):  <-- enable this once we support selective refresh of LST on drop of these objects
            if needs_separator:
                menu.insert_item(index, mforms.newMenuItem("", mforms.SeparatorMenuItem))
                index += 1
                needs_separator = False

            if object_selected_count == 1:
                item = mforms.newMenuItem("Drop %s..." % object_type_caption.get(selection_type, selection_type))
            else:
                item = mforms.newMenuItem("Drop %i %s..." % (object_selected_count, object_type_caption.get(selection_type, selection_type)))
            item.add_clicked_callback(lambda: do_drop_object(sender, selection))
            menu.insert_item(index, item)
            index+= 1

        if selection_type == 'db.Table' and object_selected:
            item = mforms.newMenuItem("Truncate %s..." % ("Table" if object_selected_count == 1 else "%i Tables" % object_selected_count))
            item.add_clicked_callback(lambda: do_truncate_table(sender, selection))
            menu.insert_item(index, item)
            index += 1

        if selection_type in ('db.Table', 'db.Schema', 'tables'):
            item = mforms.newMenuItem("Search Table Data...")
            item.add_clicked_callback(lambda: open_search(sender))
            insert_item_to_plugin_context_menu(menu, item)
コード例 #4
0
def showSchemaInspector(editor, selection):
    show_schema_manager(editor, selection, False)
    return 0