def get_sql(self, scid, tid, clid, data, is_sql=False): """ This function will generate sql from model data """ data = column_utils.convert_length_precision_to_string(data) if clid is not None: sql = render_template( "/".join([self.template_path, self._PROPERTIES_SQL]), tid=tid, clid=clid, show_sys_objects=self.blueprint.show_system_objects) status, res = self.conn.execute_dict(sql) if not status: return internal_server_error(errormsg=res) elif len(res['rows']) == 0: return gone( gettext("Could not find the column on the server.")) old_data = dict(res['rows'][0]) is_view_only = True if 'is_view_only' in old_data and old_data[ 'is_view_only'] else False if 'seqcycle' in old_data and old_data['seqcycle'] is False: old_data['seqcycle'] = None # We will add table & schema as well old_data = column_utils.column_formatter(self.conn, tid, clid, old_data) self._check_type(data, old_data) self._parse_acl_to_db_parsing(data, old_data) sql = render_template("/".join( [self.template_path, self._UPDATE_SQL]), data=data, o_data=old_data, conn=self.conn, is_view_only=is_view_only) else: is_error, errmsg, sql = self._get_sql_for_create(data, is_sql) if is_error: return errmsg return sql, data['name'] if 'name' in data else old_data['name']
def properties(self, gid, sid, did, scid, tid, clid): """ This function will show the properties of the selected schema node. Args: gid: Server Group ID sid: Server ID did: Database ID scid: Schema ID scid: Schema ID tid: Table ID clid: Column ID Returns: JSON of selected schema node """ SQL = render_template( "/".join([self.template_path, self._PROPERTIES_SQL]), tid=tid, clid=clid, show_sys_objects=self.blueprint.show_system_objects ) status, res = self.conn.execute_dict(SQL) if not status: return internal_server_error(errormsg=res) if len(res['rows']) == 0: return gone(self.not_found_error_msg()) # Making copy of output for future use data = dict(res['rows'][0]) data = column_utils.column_formatter(self.conn, tid, clid, data) return ajax_response( response=data, status=200 )
def sql(self, gid, sid, did, scid, tid, clid): """ This function will generates reverse engineered sql for schema object Args: gid: Server Group ID sid: Server ID did: Database ID scid: Schema ID tid: Table ID clid: Column ID """ try: SQL = render_template( "/".join([self.template_path, 'properties.sql']), tid=tid, clid=clid, show_sys_objects=self.blueprint.show_system_objects) status, res = self.conn.execute_dict(SQL) if not status: return internal_server_error(errormsg=res) if len(res['rows']) == 0: return gone( gettext("Could not find the column on the server.")) data = dict(res['rows'][0]) # We do not want to display length as -1 in create query if 'attlen' in data and data['attlen'] == -1: data['attlen'] = '' # Adding parent into data dict, will be using it while creating sql data['schema'] = self.schema data['table'] = self.table # check type for '[]' in it if 'cltype' in data: data['cltype'], data['hasSqrBracket'] = \ column_utils.type_formatter(data['cltype']) # We will add table & schema as well # Passing edit_types_list param so that it does not fetch # edit types. It is not required here. data = column_utils.column_formatter(self.conn, tid, clid, data, []) SQL, name = self.get_sql(scid, tid, None, data, is_sql=True) if not isinstance(SQL, str): return SQL sql_header = u"-- Column: {0}\n\n-- ".format( self.qtIdent(self.conn, data['schema'], data['table'], data['name'])) sql_header += render_template("/".join( [self.template_path, 'delete.sql']), data=data, conn=self.conn) SQL = sql_header + '\n\n' + SQL return ajax_response(response=SQL.strip('\n')) except Exception as e: return internal_server_error(errormsg=str(e))
def get_sql(self, scid, tid, clid, data, is_sql=False): """ This function will genrate sql from model data """ data = column_utils.convert_length_precision_to_string(data) if clid is not None: SQL = render_template( "/".join([self.template_path, 'properties.sql']), tid=tid, clid=clid, show_sys_objects=self.blueprint.show_system_objects) status, res = self.conn.execute_dict(SQL) if not status: return internal_server_error(errormsg=res) if len(res['rows']) == 0: return gone( gettext("Could not find the column on the server.")) old_data = dict(res['rows'][0]) # We will add table & schema as well old_data = column_utils.column_formatter(self.conn, tid, clid, old_data) # check type for '[]' in it if 'cltype' in old_data: old_data['cltype'], old_data['hasSqrBracket'] = \ column_utils.type_formatter(old_data['cltype']) if 'cltype' in data and data['cltype'] != old_data['cltype']: length, precision, typeval = \ self.get_length_precision(data['cltype']) # if new datatype does not have length or precision # then we cannot apply length or precision of old # datatype to new one. if not length: old_data['attlen'] = -1 if not precision: old_data['attprecision'] = None # If name is not present in data then # we will fetch it from old data, we also need schema & table name if 'name' not in data: data['name'] = old_data['name'] # Convert acl coming from client in db parsing format key = 'attacl' if key in data and data[key] is not None: if 'added' in data[key]: data[key]['added'] = parse_priv_to_db( data[key]['added'], self.acl) if 'changed' in data[key]: data[key]['changed'] = parse_priv_to_db( data[key]['changed'], self.acl) if 'deleted' in data[key]: data[key]['deleted'] = parse_priv_to_db( data[key]['deleted'], self.acl) SQL = render_template("/".join([self.template_path, 'update.sql']), data=data, o_data=old_data, conn=self.conn) else: required_args = ['name', 'cltype'] for arg in required_args: if arg not in data: return gettext('-- definition incomplete') # We will convert privileges coming from client required # in server side format if 'attacl' in data: data['attacl'] = parse_priv_to_db(data['attacl'], self.acl) # If the request for new object which do not have did SQL = render_template("/".join([self.template_path, 'create.sql']), data=data, conn=self.conn, is_sql=is_sql) return SQL, data['name'] if 'name' in data else old_data['name']