Debian Codegraph @

oslo_db/sqlalchemy/orm.py

raw symbols SCIP
1 # Copyright 2010 United States Government as represented by the
2 # Administrator of the National Aeronautics and Space Administration.
3 # All Rights Reserved.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License"); you may
6 # not use this file except in compliance with the License. You may obtain
7 # a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 # License for the specific language governing permissions and limitations
15 # under the License.
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)