Пример #1
0
    def get_db_type(python_type):
        """
        Returns sqlalchemy type equivalent to given python type

        :type python_type: type
        :param python_type: python type

        :return: equivalent sqlalchemy type
        :rtype: sqlalchemy.sql.visitors.VisitableType
        """

        db_types = Types()

        if python_type == bool:
            return db_types.boolean
        elif python_type == int:
            return db_types.bigint
        elif python_type == float:
            return db_types.float
        elif python_type == datetime:
            return db_types.datetime
        elif python_type == date:
            return db_types.date
        else:
            return db_types.text
Пример #2
0
    def __init__(self,
                 url,
                 schema=None,
                 reflect_metadata=True,
                 engine_kwargs=None,
                 reflect_views=True,
                 ensure_schema=True,
                 row_type=row_type):
        """Configure and connect to the database."""
        if engine_kwargs is None:
            engine_kwargs = {}

        parsed_url = urlparse(url)
        # if parsed_url.scheme.lower() in 'sqlite':
        #     # ref: https://github.com/pudo/dataset/issues/163
        #     if 'poolclass' not in engine_kwargs:
        #         engine_kwargs['poolclass'] = StaticPool

        self.lock = threading.RLock()
        self.local = threading.local()

        if len(parsed_url.query):
            query = parse_qs(parsed_url.query)
            if schema is None:
                schema_qs = query.get('schema', query.get('searchpath', []))
                if len(schema_qs):
                    schema = schema_qs.pop()

        self.schema = schema
        self.engine = create_engine(url, **engine_kwargs)
        self.types = Types(self.engine.dialect.name)
        self.url = url
        self.row_type = row_type
        self.ensure_schema = ensure_schema
        self._tables = {}
Пример #3
0
    def __init__(
        self,
        url,
        schema=None,
        engine_kwargs=None,
        ensure_schema=True,
        row_type=row_type,
        sqlite_wal_mode=True,
        on_connect_statements=None,
    ):
        """Configure and connect to the database."""
        if engine_kwargs is None:
            engine_kwargs = {}

        parsed_url = urlparse(url)
        # if parsed_url.scheme.lower() in 'sqlite':
        #     # ref: https://github.com/pudo/dataset/issues/163
        #     if 'poolclass' not in engine_kwargs:
        #         engine_kwargs['poolclass'] = StaticPool

        self.lock = threading.RLock()
        self.local = threading.local()
        self.connections = {}

        if len(parsed_url.query):
            query = parse_qs(parsed_url.query)
            if schema is None:
                schema_qs = query.get("schema", query.get("searchpath", []))
                if len(schema_qs):
                    schema = schema_qs.pop()

        self.schema = schema
        self.engine = create_engine(url, **engine_kwargs)
        self.is_postgres = self.engine.dialect.name == "postgresql"
        self.is_sqlite = self.engine.dialect.name == "sqlite"
        if on_connect_statements is None:
            on_connect_statements = []

        def _run_on_connect(dbapi_con, con_record):
            # reference:
            # https://stackoverflow.com/questions/9671490/how-to-set-sqlite-pragma-statements-with-sqlalchemy
            # https://stackoverflow.com/a/7831210/1890086
            for statement in on_connect_statements:
                dbapi_con.execute(statement)

        if self.is_sqlite and parsed_url.path != "" and sqlite_wal_mode:
            # we only enable WAL mode for sqlite databases that are not in-memory
            on_connect_statements.append("PRAGMA journal_mode=WAL")

        if len(on_connect_statements):
            event.listen(self.engine, "connect", _run_on_connect)

        self.types = Types(is_postgres=self.is_postgres)
        self.url = url
        self.row_type = row_type
        self.ensure_schema = ensure_schema
        self._tables = {}