1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 ============================
19 Tagged connection creation
20 ============================
21
22 Tagged connection creation.
23 """
24 __author__ = u"Andr\xe9 Malo"
25 __docformat__ = "restructuredtext en"
26
27 from wtf.ext.db import _connection
28 from wtf.ext.db import _decorators
29
30
31
32
33
34 _connection_tags = {}
35
36
38 """
39 Determine dbname from tag
40
41 :Parameters:
42 `tag` : ``str``
43 Connection tag
44
45 :Return: DB name
46 :Rtype: ``str``
47 """
48 return _connection_tags[tag]
49
50
52 """
53 Determine driver module
54
55 :Parameters:
56 `tag` : ``str``
57 Connection tag
58
59 :Return: Driver module
60 :Rtype: ``module``
61
62 :Exceptions:
63 - `DBConfigurationError` : DB not configured
64 - `KeyError` : DB name not found
65 - `ImportError` : Driver not found
66 """
67 return _connection.driver(dbname(tag))
68
69
71 """
72 Connect and create a connection
73
74 :Parameters:
75 `tag` : ``str``
76 Connection tag
77
78 :Return: connection
79 :Rtype: ``Connection``
80 """
81 return _connection.connect(dbname(tag), **kwargs)
82
83
84 -def connection(tag, arg=None, translate_exceptions=True):
85 """
86 Create decorator to inject connection into function
87
88 :Parameters:
89 `tag` : ``str``
90 Connection tag
91
92 `arg` : ``str``
93 Argument name. If omitted or ``None``, ``'db'`` is used.
94
95 `translate_exceptions` : ``bool``
96 Translate adapter exceptions to our ones?
97
98 :Return: Decorator function
99 :Rtype: ``callable``
100 """
101 return _decorators.connection(dbname(tag), arg, translate_exceptions)
102
103
105 """
106 Create decorator to wrap a connection into a transaction
107
108 :Parameters:
109 `arg` : ``str``
110 Argument name. If omitted or ``None``, ``'db'`` is used.
111
112 :Return: Decorator function
113 :Rtype: ``callable``
114 """
115 return _decorators.transaction(arg)
116
117
119 """
120 Register a connection tag
121
122 :Parameters:
123 `tag` : ``str``
124 Tag name
125
126 `dbname` : ``str``
127 DB name string
128
129 :Exceptions:
130 - `ValueError` : Tag already defined with a different value
131 """
132
133 if tag in _connection_tags and _connection_tags[tag] != dbname:
134 raise ValueError("Tag %r (=%r) redefined as %r" % (
135 tag, _connection_tags[tag], dbname
136 ))
137 _connection_tags[tag] = dbname
138