github.com/jincm/wesharechain@v0.0.0-20210122032815-1537409ce26a/server/block/db/enginefacade.py (about)

     1  #!/usr/bin/python
     2  # -*- coding: utf-8 -*-
     3  
     4  from sqlalchemy import create_engine
     5  import sqlalchemy.orm
     6  from sqlalchemy.sql.expression import literal_column
     7  from datetime import datetime
     8  import threading
     9  import update_match
    10  
    11  class Query(sqlalchemy.orm.query.Query):
    12      """Subclass of sqlalchemy.query with soft_delete() method."""
    13      def soft_delete(self, synchronize_session='evaluate'):
    14          return self.update({'deleted': literal_column('id'),
    15                              'updated_at': literal_column('updated_at'),
    16                              'deleted_at': datetime.utcnow()},
    17                             synchronize_session=synchronize_session)
    18  
    19      def update_returning_pk(self, values, surrogate_key):
    20          """Perform an UPDATE, returning the primary key of the matched row.
    21  
    22          This is a method-version of
    23          oslo_db.sqlalchemy.update_match.update_returning_pk(); see that
    24          function for usage details.
    25  
    26          """
    27          return update_match.update_returning_pk(self, values, surrogate_key)
    28  
    29      def update_on_match(self, specimen, surrogate_key, values, **kw):
    30          """Emit an UPDATE statement matching the given specimen.
    31  
    32          This is a method-version of
    33          oslo_db.sqlalchemy.update_match.update_on_match(); see that function
    34          for usage details.
    35  
    36          """
    37          return update_match.update_on_match(
    38              self, specimen, surrogate_key, values, **kw)
    39  
    40  class Session(sqlalchemy.orm.session.Session):
    41      """oslo.db-specific Session subclass."""
    42  
    43  class EngineFacade():
    44      def __init__(self, connect, **kwargs):
    45          self.connect = connect
    46          self.kwargs = kwargs
    47          self._start_lock = threading.Lock()
    48          self._started = False
    49          self._start(self.connect)
    50  
    51      def get_engine(self, connect=""):
    52          connect = connect or self.connect
    53          engine = create_engine(connect, **self.kwargs)
    54          return engine
    55  
    56      def get_session(self,  **kwargs):
    57          return self._maker(**kwargs)
    58  
    59      def _setup_for_connection(self, connect="", autocommit=True):
    60          connect = connect or self.connect
    61          engine = create_engine(connect, **self.kwargs)
    62          sessionmaker = sqlalchemy.orm.sessionmaker(bind=engine,
    63                                                     class_=Session,
    64                                                     autocommit=autocommit,
    65                                                     query_cls=Query)
    66          return engine, sessionmaker
    67  
    68      def _start(self, connect="",):
    69          with self._start_lock:
    70              if self._started:
    71                  return
    72  
    73              self._engine, self._maker = \
    74                  self._setup_for_connection(connect=connect)
    75  
    76              self._started = True