1 # Copyright (C) 2017 Nippon Telegraph and Telephone Corporation.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
16 from __future__ import absolute_import
21 from sqlalchemy.ext.declarative import declarative_base
24 LOG = logging.getLogger(__name__)
26 Base = declarative_base()
28 Base class for Zebra protocol database tables.
35 for k, v in self.__dict__.items() if not k.startswith('_')])
36 return "%s(%s)" % (self.__class__.__name__, m)
42 def sql_function(func):
44 Decorator for wrapping the given function in order to manipulate (CRUD)
47 For the adding/updating/deleting records function, this decorator
48 invokes "Session.commit()" after the given function.
49 If any exception while modifying records raised, this decorator invokes
50 "Session.rollbacks()".
52 @functools.wraps(func)
53 def _wrapper(session, *args, **kwargs):
56 ret = func(session, *args, **kwargs)
58 # If the given function has any update to records,
61 except Exception as e:
62 # If any exception raised, rollbacks the transaction.
63 LOG.error('Error in %s: %s', func.__name__, e)
65 LOG.error('Do rolling back %s table',
66 session.dirty[0].__tablename__)