def sess(self): """ return the existing session if it exists :return: :rtype: _Session """ if self._db_path != config.db_path: self._sub_init() if self._session_maker is None: self._session_maker = _scoped_session(_sessionmaker(bind=self.engine)) sess = self._session_maker() """ :type: _Session """ if not self._checked: from . import _models as models try: sess.query(models.Generation).first() except exc.OperationalError: models.create_db() self._checked = True return self._session_maker()
def create_session(autocommit=False, autoflush=False, pool_recycle=3600): ''' Create a new MySQL/PostgreSQL session. Parameters ---------- autocommit : Boolean (False), see sqlalchemy documentation. autoflush : Boolean (False), see sqlalchemy documentation. pool_recycle : Integer (7200), causes the pool to recycle connections after the given number of seconds has passed. Returns ------- session : Database session. ''' engine = _create_engine(conf.get('DB', 'URI'), convert_unicode=True, pool_recycle=pool_recycle) session = _scoped_session( _sessionmaker(autocommit=autocommit, autoflush=autoflush, bind=engine)) # The following dumb assignments on session are just to trick pylint # so that it doesn't report "missing attribute" every time the session is used session.add = session.add session.commit = session.commit session.delete = session.delete session.flush = session.flush session.get_bind = session.get_bind session.query = session.query session.refresh = session.refresh session.rollback = session.rollback return session
def scoped_sessionmaker(engine): class Session(_Session): def after_commit(self, func): event.listens_for(self, 'after_commit', once=True)(adapt(func)) def after_rollback(self, func): event.listens_for(self, 'after_soft_rollback', once=True)(adapt(func)) return scoped_session(_sessionmaker(class_=Session, bind=engine))
def sessionmaker(*args, **kwargs): if sqlalchemy.__version__ < "0.5": if 'autocommit' in kwargs: kwargs['transactional'] = not kwargs['autocommit'] del kwargs['autocommit'] else: if 'transactional' in kwargs: kwargs['autocommit'] = not kwargs['transactional'] del kwargs['transactional'] return _sessionmaker(*args, **kwargs)
def connect(path=_config.DB_PATH): global _engine, _session if _engine != None: return _engine _engine = _create_engine('sqlite:///%s' % path) _session = _sessionmaker() _session.configure(bind=_engine) return _engine
def update_user_ranking(q): """ Update user rankings. """ # This code is now officially absolutely terrible XXX log.log([], LVL_ALWAYS, PyLogger.INFO, 'Updating user ranks...') from sqlalchemy import create_engine as _create_engine from stats_credentials import dbu, dbpwd, dwh, dbp, dbname _engine = create_engine("postgresql+psycopg2://%s:%s@%s:%s/%s" % (dbu, dbpwd, dwh, dbp, dbname)) # del dbu # del dbpwd # del dbh # del dwp # del dbname from sqlalchemy.orm import sessionmaker as _sessionmaker _Session = _sessionmaker(bind=_engine) session = _Session() _ut = UserTool(session) users = _ut.top(0, 0) rank = 1 for x in users: x[0].rank = rank session.add(x[0]) rank += 1 session.commit() del _sessionmaker del _create_engine del _ut del session log.log([], LVL_ALWAYS, PyLogger.INFO, 'Done updating user ranks...') q.put("Done updating user rank")
def get_session(echo=False): """Return a session and engine, uses config file. Args: echo: Echo all SQL to the console. Returns: session, engine: A SQLAlchemy session and engine object corresponding to the grasp database for use in querying. """ db_type = _config['DEFAULT']['DatabaseType'] if db_type == 'sqlite': engine_string = ('sqlite:///{db_file}' .format(db_file=_config['sqlite']['DatabaseFile'])) else: engine_string = ('{type}://{user}:{passwd}@{host}/grasp' .format(type=db_type, user=_config['other']['DatabaseUser'], passwd=_config['other']['DatabasePass'], host=_config['other']['DatabaseHost'])) engine = _create_engine(engine_string, echo=echo) Session = _sessionmaker(bind=engine) return Session(), engine
def connect2db(uri, echo=False): """Connects to an existing DB or creates a new empty DB to connect too. The DB has to be mappable by the results package. Parameters ---------- URI : an URI used to connect to a DB. By default set to: 'sqlite:///$USER_HOME/foo.db'. name : the name of the existing, or newly created, DB. Default: 'foo.db' echo : verbosity of the DB. False by default. """ global Session, engine # create the engine that hooks to an existing or creates a new DB engine = _create_engine(uri, echo=echo) if uri[:5] == "sqlite": engine.execute("PRAGMA FOREIGN_KEYS=ON") # map to existing or create new tables in the DB Base.metadata.create_all(engine) # create a Session object so transactions can be made Session = _sessionmaker(bind=engine)
try: _engine = _create_engine(conn, echo=False, max_overflow=10, pool_size=5) print('Database engine created.') except Exception as e: print(str(e)) _sys.exit() # initialize Base from sqlalchemy.ext.declarative import declarative_base as _declarative_base Base = _declarative_base() # define session scope from contextlib import contextmanager as _contextmanager from sqlalchemy.orm import scoped_session as _scoped_session from sqlalchemy.orm import sessionmaker as _sessionmaker Session = _sessionmaker() Session.configure(bind=_engine) @_contextmanager def session_scope(): """Provide a transactional scope around a series of operations.""" session = Session() try: yield session session.commit() except Exception as e: session.rollback() print(str(e)) finally: session.close()
def init_sqlalchemy(base_address): global Session db_engine = _create_engine(base_address) Base.metadata.create_all(db_engine) Session = _sessionmaker(bind=db_engine)
LANGUOID_ORDER = 'path' DEFAULT_HASH = 'sha256' FILE_PATH_SEP = '/' _SQLALCHEMY_FUTURE = True ENGINE = _proxies.SQLiteEngineProxy(future=_SQLALCHEMY_FUTURE) ROOT = _proxies.PathProxy() REGISTRY = _registry() SESSION = _sessionmaker(bind=ENGINE, future=_SQLALCHEMY_FUTURE) assert PATH_LABEL < LANGUOID_LABEL PathType = typing.Tuple[str, ...] RecordValueType = typing.Union[str, typing.List[str]] RecordType = typing.Mapping[str, typing.Mapping[str, RecordValueType]] def filepath_tuple(file_path: str, *, sep=FILE_PATH_SEP) -> typing.Tuple[str]: path_parts = file_path.split(sep) return tuple(path_parts)
def create_session_cls(self, **kwargs): if 'bind' not in kwargs: kwargs['bind'] = self._get_appdata('engine') return _sessionmaker(**kwargs)
def _session_class(self) -> _sessionmaker: return _sessionmaker(bind=self.engine)
def get_session(self): """Return engine, session for this database.""" Session = _sessionmaker(bind=self.engine) return Session()
have many contours. For more information, see the model classes themselves. """ __version__ = 0.1 # Hidden stuff. import os as _os from sqlalchemy import create_engine as _create_engine from sqlalchemy.orm import sessionmaker as _sessionmaker from ._Configuration import _Configuration _module_path = _os.path.dirname(_os.path.abspath(__file__)) _path = _os.path.join(_module_path,'db','pylidc.sqlite') _engine = _create_engine('sqlite:///'+_path) _session = _sessionmaker(bind=_engine)() # Public stuff. from .Scan import Scan from .Annotation import Annotation from .Annotation import _all_characteristics_ from .Contour import Contour def query(*args): """ Wraps the sqlalchemy session object. Some example usage: >>> import pylidc as pl >>> qu = pl.query(pl.Scan).filter(pl.Scan.resolution_z <= 1.) >>> print qu.count() >>> # => 97
For more information, see the model classes themselves. """ from __future__ import print_function as _pf __version__ = '0.1.2' # Hidden stuff. import os as _os import pkg_resources as _pr from sqlalchemy import create_engine as _create_engine from sqlalchemy.orm import sessionmaker as _sessionmaker _dbpath = _pr.resource_filename('pylidc', 'pylidc.sqlite') _engine = _create_engine('sqlite:///' + _dbpath) _session = _sessionmaker(bind=_engine)() # Public stuff. from .Scan import Scan from .Scan import dicompath from .Annotation import Annotation from .Contour import Contour def query(*args): """ Wraps the sqlalchemy session object. Some example usage: >>> import pylidc as pl >>> qu = pl.query(pl.Scan).filter(pl.Scan.slice_thickness <= 1.) >>> print qu.count()
from sqlalchemy import create_engine from sqlalchemy.orm import sessionmaker as _sessionmaker from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, String, Date from sqlalchemy import Column, String, Date, desc DBNAME = 'geulhub' engine = create_engine('sqlite:///:memory:', echo=True) # engine = create_engine('mysql+pymysql://root:root@localhost/sys?charset=utf8mb4', echo=True) # # try: # print('try create db') # engine.execute(f"CREATE DATABASE {DBNAME} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;") # except: # print('db already exists') # # engine.dispose() # engine = create_engine(f'mysql+pymysql://root:root@localhost/{DBNAME}?charset=utf8mb4', echo=True) Base = declarative_base() Session = _sessionmaker(bind=engine)