| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
"""SQLAlchemy ORM connectivity and query structures. |
| 17 |
""" |
| 18 |
|
| 19 |
from oslo_utils import timeutils |
| 20 |
import sqlalchemy.orm |
| 21 |
|
| 22 |
from oslo_db.sqlalchemy import update_match |
| 23 |
|
| 24 |
|
| 25 |
class Query(sqlalchemy.orm.query.Query): |
| 26 |
"""Subclass of sqlalchemy.query with soft_delete() method.""" |
| 27 |
def soft_delete(self, synchronize_session='evaluate'): |
| 28 |
entity = self.column_descriptions[0]['entity'] |
| 29 |
return self.update({'deleted': entity.id, |
| 30 |
'updated_at': entity.updated_at, |
| 31 |
'deleted_at': timeutils.utcnow()}, |
| 32 |
synchronize_session=synchronize_session) |
| 33 |
|
| 34 |
def update_returning_pk(self, values, surrogate_key): |
| 35 |
"""Perform an UPDATE, returning the primary key of the matched row. |
| 36 |
|
| 37 |
This is a method-version of |
| 38 |
oslo_db.sqlalchemy.update_match.update_returning_pk(); see that |
| 39 |
function for usage details. |
| 40 |
|
| 41 |
""" |
| 42 |
return update_match.update_returning_pk(self, values, surrogate_key) |
| 43 |
|
| 44 |
def update_on_match(self, specimen, surrogate_key, values, **kw): |
| 45 |
"""Emit an UPDATE statement matching the given specimen. |
| 46 |
|
| 47 |
This is a method-version of |
| 48 |
oslo_db.sqlalchemy.update_match.update_on_match(); see that function |
| 49 |
for usage details. |
| 50 |
|
| 51 |
""" |
| 52 |
return update_match.update_on_match( |
| 53 |
self, specimen, surrogate_key, values, **kw) |
| 54 |
|
| 55 |
|
| 56 |
class Session(sqlalchemy.orm.session.Session): |
| 57 |
"""oslo.db-specific Session subclass.""" |
| 58 |
|
| 59 |
|
| 60 |
def get_maker(engine, autocommit=False, expire_on_commit=False): |
| 61 |
"""Return a SQLAlchemy sessionmaker using the given engine.""" |
| 62 |
return sqlalchemy.orm.sessionmaker(bind=engine, |
| 63 |
class_=Session, |
| 64 |
autocommit=autocommit, |
| 65 |
expire_on_commit=expire_on_commit, |
| 66 |
query_cls=Query) |