本文整理汇总了Python中psycopg2.extensions.STATUS_IN_TRANSACTION属性的典型用法代码示例。如果您正苦于以下问题:Python extensions.STATUS_IN_TRANSACTION属性的具体用法?Python extensions.STATUS_IN_TRANSACTION怎么用?Python extensions.STATUS_IN_TRANSACTION使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类psycopg2.extensions
的用法示例。
在下文中一共展示了extensions.STATUS_IN_TRANSACTION属性的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _get_json_oids
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def _get_json_oids(conn_or_curs, name='json'):
# lazy imports
from psycopg2.extensions import STATUS_IN_TRANSACTION
from psycopg2.extras import _solve_conn_curs
conn, curs = _solve_conn_curs(conn_or_curs)
# Store the transaction status of the connection to revert it after use
conn_status = conn.status
# column typarray not available before PG 8.3
typarray = conn.server_version >= 80300 and "typarray" or "NULL"
# get the oid for the hstore
curs.execute(
"SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;"
% typarray, (name,))
r = curs.fetchone()
# revert the status of the connection as before the command
if (conn_status != STATUS_IN_TRANSACTION and not conn.autocommit):
conn.rollback()
if not r:
raise conn.ProgrammingError("%s data type not found" % name)
return r
示例2: get_oids
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def get_oids(self, conn_or_curs):
"""Return the lists of OID of the hstore and hstore[] types.
"""
conn, curs = _solve_conn_curs(conn_or_curs)
# Store the transaction status of the connection to revert it after use
conn_status = conn.status
# column typarray not available before PG 8.3
typarray = conn.server_version >= 80300 and "typarray" or "NULL"
rv0, rv1 = [], []
# get the oid for the hstore
curs.execute("""\
SELECT t.oid, %s
FROM pg_type t JOIN pg_namespace ns
ON typnamespace = ns.oid
WHERE typname = 'hstore';
""" % typarray)
for oids in curs:
rv0.append(oids[0])
rv1.append(oids[1])
# revert the status of the connection as before the command
if (conn_status != _ext.STATUS_IN_TRANSACTION
and not conn.autocommit):
conn.rollback()
return tuple(rv0), tuple(rv1)
示例3: close
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def close(self):
"""
Close the connection to PostgreSQL
"""
if self._conn:
# If the connection is still alive, rollback and close it
if not self._conn.closed:
if self._conn.status == STATUS_IN_TRANSACTION:
self._conn.rollback()
self._conn.close()
# Remove the connection from the live connections list
self._conn = None
_live_connections.remove(self)
示例4: _get_json_oids
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def _get_json_oids(conn_or_curs, name='json'):
# lazy imports
from psycopg2.extensions import STATUS_IN_TRANSACTION
from psycopg2.extras import _solve_conn_curs
conn, curs = _solve_conn_curs(conn_or_curs)
# Store the transaction status of the connection to revert it after use
conn_status = conn.status
# column typarray not available before PG 8.3
typarray = conn.server_version >= 80300 and "typarray" or "NULL"
# get the oid for the hstore
curs.execute(
"SELECT t.oid, %s FROM pg_type t WHERE t.typname = %%s;"
% typarray, (name,))
r = curs.fetchone()
# revert the status of the connection as before the command
if (conn_status != STATUS_IN_TRANSACTION and not conn.autocommit):
conn.rollback()
if not r:
raise conn.ProgrammingError("%s data type not found" % name)
return r
示例5: test_initial_transaction_state
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def test_initial_transaction_state(self):
from psycopg2.extensions import STATUS_IN_TRANSACTION
engine = engines.testing_engine()
with engine.connect() as conn:
ne_(conn.connection.status, STATUS_IN_TRANSACTION)
示例6: _from_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def _from_db(self, name, conn_or_curs):
"""Return a `CompositeCaster` instance for the type *name*.
Raise `ProgrammingError` if the type is not found.
"""
conn, curs = _solve_conn_curs(conn_or_curs)
# Store the transaction status of the connection to revert it after use
conn_status = conn.status
# Use the correct schema
if '.' in name:
schema, tname = name.split('.', 1)
else:
tname = name
schema = 'public'
# column typarray not available before PG 8.3
typarray = conn.server_version >= 80300 and "typarray" or "NULL"
# get the type oid and attributes
curs.execute("""\
SELECT t.oid, %s, attname, atttypid
FROM pg_type t
JOIN pg_namespace ns ON typnamespace = ns.oid
JOIN pg_attribute a ON attrelid = typrelid
WHERE typname = %%s AND nspname = %%s
AND attnum > 0 AND NOT attisdropped
ORDER BY attnum;
""" % typarray, (tname, schema))
recs = curs.fetchall()
# revert the status of the connection as before the command
if (conn_status != _ext.STATUS_IN_TRANSACTION
and not conn.autocommit):
conn.rollback()
if not recs:
raise psycopg2.ProgrammingError(
"PostgreSQL type '%s' not found" % name)
type_oid = recs[0][0]
array_oid = recs[0][1]
type_attrs = [(r[2], r[3]) for r in recs]
return self(tname, type_oid, type_attrs,
array_oid=array_oid, schema=schema)
示例7: create_dbapi_connection_scope
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def create_dbapi_connection_scope(autocommit=False):
# This method's contextmanager just checks the connection out, sets autocommit if desired, and
# returns the connection to the pool when the contextmanager exits
# It does not perform transaction control! create_dbapi_session_scope is more useful since it provides
# an automatic COMMIT/ROLLBACK mechanism, and also returns a DBAPI Session instead of Connection.
# https://docs.sqlalchemy.org/en/13/core/connections.html#sqlalchemy.engine.Engine.raw_connection
pool_connection = DBManager.engine.raw_connection()
# Quoting from the SQLAlchemy docs:
# > The returned object is a proxied version of the DBAPI connection object used by the underlying driver in use.
# To be able to use the connection as a contextmanager for transaction control,
# we need the actual underlying connection, the SQLAlchemy proxy will not work for this.
raw_connection = pool_connection.connection
try:
if autocommit:
# The parameter "pool_pre_ping=True" from create_engine() above makes SQLAlchemy issue a
# "SELECT 1" to test the connection works before returning it from the connection pool.
# Even the simple "SELECT 1" has automatically begun a transaction, and the connection
# will now be "idle in transaction".
#
# Because we can only enable autocommit on "idle" connections, we need to ROLLBACK if a transaction
# is ongoing first:
if raw_connection.status == STATUS_IN_TRANSACTION:
raw_connection.rollback()
# http://initd.org/psycopg/docs/connection.html#connection.autocommit
raw_connection.autocommit = True
try:
yield raw_connection
finally:
# We want to reset the connection to a clean state in the end, so we don't return connections
# to the pool that are in an ongoing transaction, or whose transaction is aborted
# (typical error message: "ERROR: current transaction is aborted,
# commands ignored until end of transaction block")
if raw_connection.status == STATUS_IN_TRANSACTION:
raw_connection.rollback()
finally:
if autocommit:
# Because the connection is returned to the pool, we want to reset the autocommit state on it
raw_connection.autocommit = False
# Finally release connection back to the pool (notice we use .close() on the pool connection,
# not raw_connection which would close the connection for good)
pool_connection.close()
示例8: _from_db
# 需要导入模块: from psycopg2 import extensions [as 别名]
# 或者: from psycopg2.extensions import STATUS_IN_TRANSACTION [as 别名]
def _from_db(self, name, conn_or_curs):
"""Return a `CompositeCaster` instance for the type *name*.
Raise `ProgrammingError` if the type is not found.
"""
conn, curs = _solve_conn_curs(conn_or_curs)
# Store the transaction status of the connection to revert it after use
conn_status = conn.status
# Use the correct schema
if '.' in name:
schema, tname = name.split('.', 1)
else:
tname = name
schema = 'public'
# column typarray not available before PG 8.3
typarray = conn.server_version >= 80300 and "typarray" or "NULL"
# get the type oid and attributes
curs.execute("""\
SELECT t.oid, %s, attname, atttypid
FROM pg_type t
JOIN pg_namespace ns ON typnamespace = ns.oid
JOIN pg_attribute a ON attrelid = typrelid
WHERE typname = %%s AND nspname = %%s
AND attnum > 0 AND NOT attisdropped
ORDER BY attnum;
""" % typarray, (tname, schema))
recs = curs.fetchall()
# revert the status of the connection as before the command
if (conn_status != _ext.STATUS_IN_TRANSACTION
and not conn.autocommit):
conn.rollback()
if not recs:
raise psycopg2.ProgrammingError(
"PostgreSQL type '%s' not found" % name)
type_oid = recs[0][0]
array_oid = recs[0][1]
type_attrs = [ (r[2], r[3]) for r in recs ]
return self(tname, type_oid, type_attrs,
array_oid=array_oid, schema=schema)