1
2 r"""
3 ======================================
4 Column inspection and representation
5 ======================================
6
7 Column inspection and generation.
8
9 :Copyright:
10
11 Copyright 2010 - 2016
12 Andr\xe9 Malo or his licensors, as applicable
13
14 :License:
15
16 Licensed under the Apache License, Version 2.0 (the "License");
17 you may not use this file except in compliance with the License.
18 You may obtain a copy of the License at
19
20 http://www.apache.org/licenses/LICENSE-2.0
21
22 Unless required by applicable law or agreed to in writing, software
23 distributed under the License is distributed on an "AS IS" BASIS,
24 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 See the License for the specific language governing permissions and
26 limitations under the License.
27
28 """
29 if __doc__:
30
31 __doc__ = __doc__.encode('ascii').decode('unicode_escape')
32 __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
33 __docformat__ = "restructuredtext en"
34
35 from . import _type
36 from . import _util
40 """
41 Default clause container
42
43 :IVariables:
44 `_default` : Default clause
45 Default clause
46 """
47
49 """
50 Initialization
51
52 :Parameters:
53 `default` : Default clause
54 Default clause
55 """
56 self._default = default
57 self._symbols = symbols
58
60 """
61 Make string representation
62
63 :Return: The string representation
64 :Rtype: ``str``
65 """
66 if self._default.for_update:
67 for_update = ", for_update=%r" % (True,)
68 else:
69 for_update = ""
70 return "%s(%r%s)" % (
71 self._symbols['default'],
72 _util.unicode(self._default.arg),
73 for_update,
74 )
75
78 """
79 Column container
80
81 :IVariables:
82 `_name` : ``unicode``
83 Name
84
85 `_ctype` : SA type
86 Column type
87
88 `_nullable` : ``bool``
89 Nullable?
90
91 `_primary_key` : ``bool``
92 Part of a primary key?
93
94 `_autoincrement` : ``bool``
95 Possible autoincrement?
96
97 `_server_default` : Default clause
98 Default clause
99 """
100
101 - def __init__(self, name, ctype, nullable, primary_key, autoincrement,
102 server_default, symbols):
103 """
104 Initialization
105
106 :Parameters:
107 `name` : ``unicode``
108 Column name
109
110 `ctype` : SA type
111 Column type
112
113 `nullable` : ``bool``
114 Nullable?
115
116 `primary_key` : ``bool``
117 Part of a primary key?
118
119 `autoincrement` : ``bool``
120 Possible autoincrement?
121
122 `server_default` : Default clause
123 Default clause
124 """
125 self._name = name
126 self._ctype = ctype
127 self._nullable = nullable
128 self._primary_key = primary_key
129 self._autoincrement = autoincrement
130 self._server_default = server_default
131 self._symbols = symbols
132
133 @classmethod
134 - def from_sa(cls, column, symbols):
135 """
136 Construct from SA column
137
138 :Parameters:
139 `column` : SA column
140 SA column
141
142 :Return: New column instance
143 :Rtype: `Column`
144 """
145 return cls(
146 column.name,
147 _type.Type.by_column(column, symbols),
148 nullable=column.nullable,
149 primary_key=column.primary_key,
150 autoincrement=column.autoincrement,
151 server_default=column.server_default,
152 symbols=symbols,
153 )
154
156 """
157 Make string representation
158
159 :Return: The string representation
160 :Rtype: ``str``
161 """
162 params = list(map(repr, (self._name, self._ctype)))
163 if not self._nullable:
164 params.append('nullable=%r' % (False,))
165 if not self._autoincrement and self._primary_key:
166 params.append('autoincrement=%r' % (False,))
167 if self._server_default is not None:
168 params.append('server_default=%r' % (
169 ServerDefault(self._server_default, self._symbols),
170 ))
171 return "%s(%s)" % (
172 self._symbols['column'], ', '.join(params)
173 )
174