Initial version for generationg the CREATE/SELECT/EXEC scripts for the

selected node.
This commit is contained in:
Murtuza Zabuawala
2016-05-16 00:25:31 +05:30
committed by Ashesh Vashi
parent c34e62207a
commit 5ca63fba48
8 changed files with 261 additions and 19 deletions

View File

@@ -173,6 +173,12 @@ class FunctionView(PGChildNodeView, DataTypeReader):
* types(gid, sid, did, scid, fnid=None):
- Returns Data Types.
* select_sql(gid, sid, did, scid, fnid):
- Returns sql for Script
* exec_sql(gid, sid, did, scid, fnid):
- Returns sql for Script
"""
node_type = blueprint.node_type
@@ -203,7 +209,9 @@ class FunctionView(PGChildNodeView, DataTypeReader):
'module.js': [{}, {}, {'get': 'module_js'}],
'get_types': [{'get': 'types'}, {'get': 'types'}],
'get_languages': [{'get': 'get_languages'}, {'get': 'get_languages'}],
'vopts': [{}, {'get': 'variable_options'}]
'vopts': [{}, {'get': 'variable_options'}],
'select_sql': [{'get': 'select_sql'}],
'exec_sql': [{'get': 'exec_sql'}]
})
@property
@@ -1198,6 +1206,74 @@ It may have been removed by another user or moved to another schema.
status=200
)
@check_precondition
def select_sql(self, gid, sid, did, scid, fnid):
"""
This function returns sql for select script call.
Args:
gid: Server Group Id
sid: Server Id
did: Database Id
scid: Schema Id
doid: Function Id
"""
# Fetch the function definition.
SQL = render_template("/".join([self.sql_template_path,
'get_definition.sql']), fnid=fnid, scid=scid)
status, res = self.conn.execute_2darray(SQL)
if not status:
return internal_server_error(errormsg=res)
func_def, name = res['rows'][0]
# Fetch only arguments
args = name[name.rfind('('):].strip('(').strip(')').split(',')
# Remove unwanted spaces from arguments
args = [arg.strip(' ') for arg in args]
# Remove duplicate and then format arguments
for arg in list(set(args)):
formatted_arg = '\n\t<' + arg + '>'
name = name.replace(arg, formatted_arg)
name = name.replace(')', '\n)')
sql = "SELECT {0}".format(name)
return ajax_response(response=sql)
@check_precondition
def exec_sql(self, gid, sid, did, scid, fnid):
"""
This function returns sql for exec script call.
Args:
gid: Server Group Id
sid: Server Id
did: Database Id
scid: Schema Id
doid: Function Id
"""
resp_data = self._fetch_properties(gid, sid, did, scid, fnid)
name = resp_data['pronamespace'] + "." + resp_data['name_with_args']
# Fetch only arguments
args = name[name.rfind('('):].strip('(').strip(')').split(',')
# Remove unwanted spaces from arguments
args = [arg.strip(' ') for arg in args]
# Remove duplicate and then format arguments
for arg in list(set(args)):
formatted_arg = '\n\t<' + arg + '>'
name = name.replace(arg, formatted_arg)
name = name.replace(')', '\n)')
sql = "EXEC {0}".format(name)
return ajax_response(response=sql)
FunctionView.register_node_view(blueprint)

View File

@@ -108,6 +108,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify) {
collection_type: 'coll-function',
hasSQL: true,
hasDepends: true,
hasScriptTypes: ['create', 'select'],
parent_type: ['schema', 'catalog'],
Init: function(args) {
/* Avoid mulitple registration of menus */

View File

@@ -28,6 +28,7 @@ function($, _, S, pgAdmin, pgBrowser, alertify, Function) {
collection_type: 'coll-procedure',
hasSQL: true,
hasDepends: true,
hasScriptTypes: ['create', 'exec'],
parent_type: ['schema'],
Init: function() {
/* Avoid mulitple registration of menus */

View File

@@ -105,6 +105,60 @@ 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',
enable: this.check_user_permission
}]);
});
// 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',
enable: this.check_user_permission
}]);
}
},
///////
// Checks if Script Type is allowed to user
// First check if role node & create role allowed
// Otherwise test rest of database objects
// if no permission matched then do not allow create script
///////
check_user_permission: function(itemData, item, data) {
var node = pgBrowser.Nodes[itemData._type],
parentData = node.getTreeNodeHierarchy(item);
if ( _.indexOf(['create','insert','update', 'delete'], data.script) != -1) {
if (itemData.type == 'role' &&
parentData.server.user.can_create_role) {
return true;
} else if (parentData.server.user.is_superuser ||
parentData.server.user.can_create_db ||
(parentData.schema && parentData.schema.can_create)) {
return true;
} else {
return false;
}
} else {
return true;
}
},
///////
// Generate a Backform view using the node's model type
@@ -499,6 +553,43 @@ 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,