def test_async_cancel(self): async_conn = psycopg2.connect(dsn, async=True) self.assertRaises(psycopg2.OperationalError, async_conn.cancel) extras.wait_select(async_conn) cur = async_conn.cursor() cur.execute("select pg_sleep(10000)") self.assertTrue(async_conn.isexecuting()) async_conn.cancel() self.assertRaises(psycopg2.extensions.QueryCanceledError, extras.wait_select, async_conn) cur.execute("select 1") extras.wait_select(async_conn) self.assertEqual(cur.fetchall(), [(1,)])
def pg_connect(self): """A generic function to connect to PostgreSQL using Psycopg2""" try: self.conn = psycopg2.connect(self.dsn) if self.conn: self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) wait_select(self.conn) self.cur = self.conn.cursor() self.server_version = self.conn.server_version if (self.server_version >= 90000 and 'application_name=' not in self.dsn): try: self.cur.execute('SET application_name TO %s',(self.application,)) self.conn.commit() except psycopg2.Error as e: self.logs.logger.error('Could not define the application_name parameter: - %s', e) except psycopg2.Error as e: raise e
async def search_player(self, ctx, username): self.bot.db.execute( "SELECT * FROM track WHERE guild_id = %s AND username ILIKE %s", (ctx.guild.id, username)) wait_select(self.bot.db.connection) return self.bot.db.fetchone()
async def update_changes(self, new_plays_list, user_id): self.bot.db.execute( "UPDATE top_scores " "SET scores = %s " "WHERE user_id = %s", (new_plays_list, user_id)) wait_select(self.bot.db.connection)
async def total_unique_tracking(self): self.bot.db.execute("SELECT COUNT (DISTINCT username) FROM track") wait_select(self.bot.db.connection) return self.bot.db.fetchone()[0]
async def remove_scores(self, username): self.bot.db.execute( "DELETE FROM top_scores " "WHERE username ILIKE %s", (username, )) wait_select(self.bot.db.connection)
async def get_info(self, ctx): self.bot.db.execute( "SELECT username, user_id, pp_rank " "FROM latest_score WHERE discord_user_id = %s", (ctx.author.id, )) wait_select(self.bot.db.connection) return self.bot.db.fetchone()
def _ensure_cursor(self): if self.cursor is None: if self.async_conn: wait_select(self.conn) self.cursor = self.conn.cursor()
def wait(self): """ Block until any pending operation is done. """ wait_select(self.conn) self.current_query = None
def execute(self, query): self._ensure_cursor() if self. async is True: wait_select(self.conn)