summaryrefslogtreecommitdiff
path: root/doc/source/usage.rst
blob: 0e5a2f29d29575a0c8f4a92cb217cc4e5cd2ba70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
========
Usage
========

To use oslo.db in a project::

* Session Handling
  
  .. code:: python

    from oslo.config import cfg
    from oslo.db.sqlalchemy import session as db_session

    _FACADE = None

    def _create_facade_lazily():
        global _FACADE
        if _FACADE is None:
            _FACADE = db_session.EngineFacade.from_config(cfg.CONF)
        return _FACADE

    def get_engine():
        facade = _create_facade_lazily()
        return facade.get_engine()

    def get_session(**kwargs):
        facade = _create_facade_lazily()
        return facade.get_session(**kwargs)


* Base class for models usage

  .. code:: python

	from oslo.db import models


    class ProjectSomething(models.TimestampMixin,
                           models.ModelBase):
        id = Column(Integer, primary_key=True)
        ...


* DB API backend support 

  .. code:: python

    from oslo.config import cfg
    from oslo.db import api as db_api


    _BACKEND_MAPPING = {'sqlalchemy': 'project.db.sqlalchemy.api'}

    IMPL = db_api.DBAPI.from_config(cfg.CONF, backend_mapping=_BACKEND_MAPPING)

    def get_engine():
        return IMPL.get_engine()

    def get_session():
        return IMPL.get_session()

    # DB-API method
    def do_something(somethind_id):
        return IMPL.do_something(somethind_id)