Karsten Hilbert wrote:
> while evaluating whether to move to SA with our application
> (http://wiki.gnumed.de) I am wondering whether there is any
> way to access SQLSTATE information from within the SA
> framework. Some information on SQLSTATE (as applies to
> PostgreSQL) is here:
I find it useful to have access this - say I want to both enforce a
unique constraint and have user-friendly error messages.
So I wrap session flush and turn the PostgreSQL error into an exception.
==== flush.py ====
import sqlalchemy
class SQLUniqueConstraintViolation(Exception):
def __init__(self, constraint):
self.constraint = constraint
# Regexp to parse unique constraint violation error message
reUCV = re.compile('[^"]*"([^"]+)"[^"]*')
def flush(session):
try:
session.flush()
except sqlalchemy.exceptions.SQLError, e:
if e.orig.pgcode == '23505':
raise SQLUniqueConstraintViolation(
reUCV.match(e.orig.pgerror).group(1))
else:
raise
==== yourcode.py ====
import sqlalchemy
import flush
# ...
session = sqlalchemy.create_session()
# ...
try:
flush.flush(session)
except flush.SQLUniqueConstraintViolation, e:
if e.constraint == 'some_constraint_name':
# do something
else:
# do something else
except:
# some other exception!
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users