diff --git a/web/pgadmin/browser/templates/browser/js/node.js b/web/pgadmin/browser/templates/browser/js/node.js index 38f2722..6e16002 100644 --- a/web/pgadmin/browser/templates/browser/js/node.js +++ b/web/pgadmin/browser/templates/browser/js/node.js @@ -102,6 +102,34 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { }]); } } + // This will add options of scripts eg:'CREATE Script' + if (self.hasScriptTypes && _.isArray(self.hasScriptTypes) + && self.hasScriptTypes.length > 0) { + // For each script type create menu + _.each(self.hasScriptTypes, function(stype) { + + var type_label = S( + "{{ _("%%s Script") }}" + ).sprintf(stype.toUpperCase()).value(), + stype = stype.toLowerCase(); + + // Adding menu for each script type + pgAdmin.Browser.add_menus([{ + name: 'show_script_' + stype, node: self.type, module: self, + applies: ['object', 'context'], callback: 'show_script', + priority: 3, label: type_label, category: 'Scripts', + data: {'script': stype}, icon: 'fa fa-pencil' + }]); + }); + // If node has hasSQL then provide CREATE Script by default + } else if(self.hasSQL) { + pgAdmin.Browser.add_menus([{ + name: 'show_script_create', node: self.type, module: self, + applies: ['object', 'context'], callback: 'show_script', + priority: 3, label: 'CREATE Script', category: 'Scripts', + data: {'script': 'create'}, icon: 'fa fa-pencil' + }]); + } }, /////// // Generate a Backform view using the node's model type @@ -496,6 +524,44 @@ function($, _, S, pgAdmin, Menu, Backbone, Alertify, pgBrowser, Backform) { }, null).show() }, + // Callback for creating script(s) & opening them in Query editor + show_script: function(args, item) { + var scriptType = args.script, + obj = this, + t = pgBrowser.tree, + i = item || t.selected(), + d = i && i.length == 1 ? t.itemData(i) : undefined; + + if (!d) + return; + + /* + * Make sure - we're using the correct version of node + */ + obj = pgBrowser.Nodes[d._type]; + var objName = d.label; + + // URL for script type + if(scriptType == 'insert') { + sql_url = 'insert_sql'; + } else if(scriptType == 'update') { + sql_url = 'update_sql'; + } else if(scriptType == 'delete') { + sql_url = 'delete_sql'; + } else if(scriptType == 'select') { + sql_url = 'select_sql'; + } else if(scriptType == 'exec') { + sql_url = 'exec_sql'; + } else { + // By Default get CREATE SQL + sql_url = 'sql'; + } + + // Open data grid & pass the URL for fetching + pgAdmin.DataGrid.show_query_tool.apply( + this, [obj.generate_url(i, sql_url, d, true), i] + ); + }, // Callback called - when a node is selected in browser tree. selected: function(item, data, browser) { // Show the information about the selected node in the below panels, diff --git a/web/pgadmin/tools/datagrid/__init__.py b/web/pgadmin/tools/datagrid/__init__.py index 4edfed1..777255a 100644 --- a/web/pgadmin/tools/datagrid/__init__.py +++ b/web/pgadmin/tools/datagrid/__init__.py @@ -143,10 +143,15 @@ def panel(trans_id, is_query_tool, editor_title): is_query_tool: True if panel calls when query tool menu is clicked. editor_title: Title of the editor """ + # Let's fetch Script type URL from request + if request.args and request.args['query_url'] != '': + sURL = request.args['query_url'] + else: + sURL = None return render_template("datagrid/index.html", _=gettext, uniqueId=trans_id, - is_query_tool=is_query_tool, editor_title=editor_title) - + is_query_tool=is_query_tool, editor_title=editor_title, + script_type_url=sURL) @blueprint.route( '/initialize/query_tool//', diff --git a/web/pgadmin/tools/datagrid/templates/datagrid/index.html b/web/pgadmin/tools/datagrid/templates/datagrid/index.html index ddb9d8f..17831c7 100644 --- a/web/pgadmin/tools/datagrid/templates/datagrid/index.html +++ b/web/pgadmin/tools/datagrid/templates/datagrid/index.html @@ -153,8 +153,36 @@ try { } }); + // Fetch the SQL for Scripts (eg: CREATE/UPDATE/DELETE/SELECT) + var script_sql = ''; + {% if script_type_url%} + // Call AJAX only if script type url is present + $.ajax({ + url: '{{ script_type_url }}', + type:'GET', + async: false, + success: function(res) { + script_sql = res; + }, + error: function(jqx) { + var msg = jqx.responseText; + /* Error from the server */ + if (jqx.status == 410 || jqx.status == 500) { + try { + var data = $.parseJSON(jqx.responseText); + msg = data.errormsg; + } catch (e) {} + } + pgBrowser.report_error( + S('{{ _('Error fetching sql for script: "%%s"') }}') + .sprintf(msg) + .value(), msg); + } + }); + {% endif %} + // Start the query tool. - sqlEditorController.start({{ is_query_tool }}, "{{ editor_title }}"); + sqlEditorController.start({{ is_query_tool }}, "{{ editor_title }}", script_sql); }); } catch (err) { /* Show proper error dialog */ diff --git a/web/pgadmin/tools/datagrid/templates/datagrid/js/datagrid.js b/web/pgadmin/tools/datagrid/templates/datagrid/js/datagrid.js index 4bab281..773d285 100644 --- a/web/pgadmin/tools/datagrid/templates/datagrid/js/datagrid.js +++ b/web/pgadmin/tools/datagrid/templates/datagrid/js/datagrid.js @@ -346,9 +346,10 @@ define( }, // This is a callback function to show query tool when user click on menu item. - show_query_tool: function(data, i) { + show_query_tool: function(url, i) { var self = this, - d = pgAdmin.Browser.tree.itemData(i); + sURL = url || '', + d = pgAdmin.Browser.tree.itemData(i); if (d === undefined) { alertify.alert( 'Query tool Error', @@ -399,7 +400,8 @@ define( }); // Open the panel if frame is initialized - baseUrl = "{{ url_for('datagrid.index') }}" + "panel/" + res.data.gridTransId + "/true/" + grid_title; + baseUrl = "{{ url_for('datagrid.index') }}" + "panel/" + res.data.gridTransId + "/true/" + + grid_title + '?' + "query_url=" + encodeURI(sURL); var openQueryToolURL = function(j) { setTimeout(function() { var frameInitialized = j.data('frameInitialized'); diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js index ee533a1..c08dcd9 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js @@ -664,15 +664,14 @@ define( * call the render method of the grid view to render the backgrid * header and loading icon and start execution of the sql query. */ - start: function(is_query_tool, editor_title) { + start: function(is_query_tool, editor_title, script_sql) { var self = this; - self.is_query_tool = is_query_tool; self.items_per_page = 25; self.rows_affected = 0; self.marked_line_no = 0; - // We do not allow to call the start multiple times. + // We do not allow to call the start multiple times./ if (self.gridView) return; @@ -706,6 +705,9 @@ define( if (self.is_query_tool) { self.gridView.query_tool_obj.refresh(); + if(script_sql && script_sql !== '') { + self.gridView.query_tool_obj.setValue(script_sql); + } } else { self.gridView.query_tool_obj.setOption("readOnly",true);