def _check_rename_db_or_change_table_space(self, data, conn, all_ids): for action in ["rename_database", "tablespace"]: sql = self.get_offline_sql(all_ids['gid'], all_ids['sid'], data, all_ids['did'], action) sql = sql.strip('\n').strip(' ') if sql and sql != "": status, msg = conn.execute_scalar(sql) if not status: # In case of error from server while rename it, # reconnect to the database with old name again. self.conn = self.manager.connection( database=data['old_name'], auto_reconnect=True ) status, errmsg = self.conn.connect() if not status: current_app.logger.error( 'Could not reconnected to database(#{0}).\n' 'Error: {1}'.format(all_ids['did'], errmsg) ) return True, msg QueryHistory.update_history_dbname( current_user.id, all_ids['sid'], data['old_name'], data['name']) return False, ''
def get_query_history(trans_id): """ This method returns query history for user/server/database Args: sid: server id did: database id """ status, error_msg, conn, trans_obj, session_ob = \ check_transaction_status(trans_id) return QueryHistory.get(current_user.id, trans_obj.sid, conn.db)
def add_query_history(trans_id): """ This method adds to query history for user/server/database Args: sid: server id did: database id """ status, error_msg, conn, trans_obj, session_ob = \ check_transaction_status(trans_id) return QueryHistory.save(current_user.id, trans_obj.sid, conn.db, request=request)
def update(self, gid, sid, did): """Update the database.""" data = request.form if request.form else json.loads(request.data, encoding='utf-8') # Generic connection for offline updates conn = self.manager.connection(conn_id='db_offline_update') status, errmsg = conn.connect() if not status: current_app.logger.error( "Could not create database connection for offline updates\n" "Err: {0}".format(errmsg)) return internal_server_error(errmsg) if did is not None: # Fetch the name of database for comparison status, rset = self.conn.execute_dict( render_template("/".join([self.template_path, 'nodes.sql']), did=did, conn=self.conn, last_system_oid=0)) if not status: return internal_server_error(errormsg=rset) if len(rset['rows']) == 0: return gone(_('Could not find the database on the server.')) data['old_name'] = (rset['rows'][0])['name'] if 'name' not in data: data['name'] = data['old_name'] # Release any existing connection from connection manager # to perform offline operation self.manager.release(did=did) for action in ["rename_database", "tablespace"]: SQL = self.get_offline_sql(gid, sid, data, did, action) SQL = SQL.strip('\n').strip(' ') if SQL and SQL != "": status, msg = conn.execute_scalar(SQL) if not status: # In case of error from server while rename it, # reconnect to the database with old name again. self.conn = self.manager.connection( database=data['old_name'], auto_reconnect=True) status, errmsg = self.conn.connect() if not status: current_app.logger.error( 'Could not reconnected to database(#{0}).\n' 'Error: {1}'.format(did, errmsg)) return internal_server_error(errormsg=msg) QueryHistory.update_history_dbname(current_user.id, sid, data['old_name'], data['name']) # Make connection for database again if self._db['datallowconn']: self.conn = self.manager.connection(database=data['name'], auto_reconnect=True) status, errmsg = self.conn.connect() if not status: current_app.logger.error( 'Could not connected to database(#{0}).\n' 'Error: {1}'.format(did, errmsg)) return internal_server_error(errmsg) SQL = self.get_online_sql(gid, sid, data, did) SQL = SQL.strip('\n').strip(' ') if SQL and SQL != "": status, msg = self.conn.execute_scalar(SQL) if not status: return internal_server_error(errormsg=msg) # Release any existing connection from connection manager # used for offline updates self.manager.release(conn_id="db_offline_update") # Fetch the new data again after update for proper node # generation status, rset = self.conn.execute_dict( render_template("/".join([self.template_path, 'nodes.sql']), did=did, conn=self.conn, last_system_oid=0)) if not status: return internal_server_error(errormsg=rset) if len(rset['rows']) == 0: return gone(_("Could not find the database on the server.")) res = rset['rows'][0] canDrop = canDisConn = True if self.manager.db == res['name']: canDrop = canDisConn = False return jsonify(node=self.blueprint.generate_browser_node( did, sid, res['name'], icon="pg-icon-{0}".format(self.node_type ) if self._db['datallowconn'] and self.conn.connected() else "icon-database-not-connected", connected=self.conn.connected( ) if self._db['datallowconn'] else False, tablespace=res['spcname'], allowConn=res['datallowconn'], canCreate=res['cancreate'], canDisconn=canDisConn, canDrop=canDrop, inode=True if res['datallowconn'] else False))