我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用psycopg2.extensions.connection()。
def _record(self, msg, query, duration): query_data = None if self.query_threshold >= 0 and duration > self.query_threshold: query_data = self._get_data(query, duration) extra = collections.OrderedDict() extra['trailer'] = query_data[0] extra['duration'] = query_data[1] extra['connection'] = query_data[2] self.logger.info('slow ' + msg, extra=extra) def processor(data): if query_data is None: q, ms, conn = self._get_data(query, duration) else: q, ms, conn = query_data data['data']['query'] = q data['data']['duration'] = ms data['data']['connection'] = conn breadcrumb = dict( message=msg, category='sql', data={}, processor=processor) raven.breadcrumbs.record(**breadcrumb)
def test_subclass_commit(self): commits = [] class MyConn(ext.connection): def commit(self): commits.append(None) super(MyConn, self).commit() with self.connect(connection_factory=MyConn) as conn: curs = conn.cursor() curs.execute("insert into test_with values (10)") self.assertEqual(conn.status, ext.STATUS_READY) self.assertTrue(commits) curs = self.conn.cursor() curs.execute("select * from test_with") self.assertEqual(curs.fetchall(), [(10,)])
def test_connection_setup(self): cur = self.conn.cursor() sync_cur = self.sync_conn.cursor() del cur, sync_cur self.assertTrue(self.conn.async_) self.assertTrue(not self.sync_conn.async_) # the async connection should be autocommit self.assertTrue(self.conn.autocommit) self.assertEqual(self.conn.isolation_level, ext.ISOLATION_LEVEL_DEFAULT) # check other properties to be found on the connection self.assertTrue(self.conn.server_version) self.assertTrue(self.conn.protocol_version in (2, 3)) self.assertTrue(self.conn.encoding in ext.encodings)
def test_set_parameters_while_async(self): cur = self.conn.cursor() cur.execute("select 'c'") self.assertTrue(self.conn.isexecuting()) # getting transaction status works self.assertEqual(self.conn.get_transaction_status(), ext.TRANSACTION_STATUS_ACTIVE) self.assertTrue(self.conn.isexecuting()) # setting connection encoding should fail self.assertRaises(psycopg2.ProgrammingError, self.conn.set_client_encoding, "LATIN1") # same for transaction isolation self.assertRaises(psycopg2.ProgrammingError, self.conn.set_isolation_level, 1)
def as_string(self, context): # is it a connection or cursor? if isinstance(context, ext.connection): conn = context elif isinstance(context, ext.cursor): conn = context.connection else: raise TypeError("context must be a connection or a cursor") a = ext.adapt(self._wrapped) if hasattr(a, 'prepare'): a.prepare(conn) rv = a.getquoted() if sys.version_info[0] >= 3 and isinstance(rv, bytes): rv = rv.decode(ext.encodings[conn.encoding]) return rv
def test_subclass_commit(self): commits = [] class MyConn(ext.connection): def commit(self): commits.append(None) super(MyConn, self).commit() with self.connect(connection_factory=MyConn) as conn: curs = conn.cursor() curs.execute("insert into test_with values (10)") self.assertEqual(conn.status, ext.STATUS_READY) self.assert_(commits) curs = self.conn.cursor() curs.execute("select * from test_with") self.assertEqual(curs.fetchall(), [(10,)])
def test_subclass_rollback(self): rollbacks = [] class MyConn(ext.connection): def rollback(self): rollbacks.append(None) super(MyConn, self).rollback() try: with self.connect(connection_factory=MyConn) as conn: curs = conn.cursor() curs.execute("insert into test_with values (11)") 1 / 0 except ZeroDivisionError: pass else: self.assert_("exception not raised") self.assertEqual(conn.status, ext.STATUS_READY) self.assert_(rollbacks) curs = conn.cursor() curs.execute("select * from test_with") self.assertEqual(curs.fetchall(), [])
def test_connection_setup(self): cur = self.conn.cursor() sync_cur = self.sync_conn.cursor() del cur, sync_cur self.assert_(self.conn.async_) self.assert_(not self.sync_conn.async_) # the async connection should be autocommit self.assert_(self.conn.autocommit) self.assertEquals(self.conn.isolation_level, ext.ISOLATION_LEVEL_DEFAULT) # check other properties to be found on the connection self.assert_(self.conn.server_version) self.assert_(self.conn.protocol_version in (2, 3)) self.assert_(self.conn.encoding in ext.encodings)
def test_set_parameters_while_async(self): cur = self.conn.cursor() cur.execute("select 'c'") self.assertTrue(self.conn.isexecuting()) # getting transaction status works self.assertEquals(self.conn.get_transaction_status(), ext.TRANSACTION_STATUS_ACTIVE) self.assertTrue(self.conn.isexecuting()) # setting connection encoding should fail self.assertRaises(psycopg2.ProgrammingError, self.conn.set_client_encoding, "LATIN1") # same for transaction isolation self.assertRaises(psycopg2.ProgrammingError, self.conn.set_isolation_level, 1)
def connection_factory(tracer, service="postgres"): """ Return a connection factory class that will can be used to trace postgres queries. >>> factory = connection_factor(my_tracer, service="my_db_service") >>> conn = pyscopg2.connect(..., connection_factory=factory) """ tracer.set_service_info( service=service, app="postgres", app_type=AppTypes.db, ) return functools.partial(TracedConnection, datadog_tracer=tracer, datadog_service=service)
def __init__(self, *args, **kwargs): self._datadog_tracer = kwargs.pop("datadog_tracer", None) self._datadog_service = kwargs.pop("datadog_service", None) super(TracedConnection, self).__init__(*args, **kwargs) # add metadata (from the connection, string, etc) dsn = sql.parse_pg_dsn(self.dsn) self._datadog_tags = { net.TARGET_HOST: dsn.get("host"), net.TARGET_PORT: dsn.get("port"), db.NAME: dsn.get("dbname"), db.USER: dsn.get("user"), "db.application" : dsn.get("application_name"), } self._datadog_cursor_class = functools.partial(TracedCursor, datadog_tracer=self._datadog_tracer, datadog_service=self._datadog_service, datadog_tags=self._datadog_tags)
def get_task_status(task_id:str): """* What you can do """ try: db_connection = init_psql_db_connection() backend_handler = PgBackendDbHandler(db_connection) except: error_message = 'Failed to initialize DB connection!' response_body = {'message': 'Internal server error.', 'traceback': traceback.format_exc()} flask_app.logger.error(error_message) return jsonify(response_body), 500 else: pass task_status_record = backend_handler.get_task_status(task_id) if task_status_record is None: error_message = 'No task named = {}'.format(task_id) response_body = {'message': error_message, 'traceback': traceback.format_exc()} flask_app.logger.error(error_message) return jsonify(response_body), 500 else: return jsonify(task_status_record), 200
def test_subclass_rollback(self): rollbacks = [] class MyConn(ext.connection): def rollback(self): rollbacks.append(None) super(MyConn, self).rollback() try: with self.connect(connection_factory=MyConn) as conn: curs = conn.cursor() curs.execute("insert into test_with values (11)") 1 / 0 except ZeroDivisionError: pass else: self.assertTrue("exception not raised") self.assertEqual(conn.status, ext.STATUS_READY) self.assertTrue(rollbacks) curs = conn.cursor() curs.execute("select * from test_with") self.assertEqual(curs.fetchall(), [])
def test_subclass_rollback(self): rollbacks = [] class MyConn(ext.connection): def rollback(self): rollbacks.append(None) super(MyConn, self).rollback() try: with self.connect(connection_factory=MyConn) as conn: curs = conn.cursor() curs.execute("insert into test_with values (11)") 1/0 except ZeroDivisionError: pass else: self.assert_("exception not raised") self.assertEqual(conn.status, ext.STATUS_READY) self.assert_(rollbacks) curs = conn.cursor() curs.execute("select * from test_with") self.assertEqual(curs.fetchall(), [])
def test_subclass_rollback(self): rollbacks = [] class MyConn(ext.connection): def rollback(self): rollbacks.append(None) super(MyConn, self).rollback() try: with self.connect(connection_factory=MyConn) as conn: curs = conn.cursor() curs.execute("insert into test_with values (11)") 1/0 except ZeroDivisionError: pass else: self.assertTrue("exception not raised") self.assertEqual(conn.status, ext.STATUS_READY) self.assertTrue(rollbacks) curs = conn.cursor() curs.execute("select * from test_with") self.assertEqual(curs.fetchall(), [])
def _get_data(self, query, t): if callable(query): query = query() query = prettify_sql(query) query = FILTERED if query is None else query duration = float(round(t, 3)) connection = get_safe_connection_string(self) return query, duration, connection
def execute(self, query, vars=None): timestamp = time.time() try: return super(TaliskerCursor, self).execute(query, vars) finally: duration = (time.time() - timestamp) * 1000 if vars is None: query = None self.connection._record('query', query, duration)
def callproc(self, procname, vars=None): timestamp = time.time() try: return super(TaliskerCursor, self).callproc(procname, vars) finally: duration = (time.time() - timestamp) * 1000 # no query parameters, cannot safely record self.connection._record( 'stored proc: {}'.format(procname), None, duration)
def test_cursor_closed_attribute(self): conn = self.conn curs = conn.cursor() self.assertEqual(curs.closed, False) curs.close() self.assertEqual(curs.closed, True) # Closing the connection closes the cursor: curs = conn.cursor() conn.close() self.assertEqual(curs.closed, True)