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 import itertools as _it
36
37 from . import _type
38 from . import _util
42 """
43 Default clause container
44
45 :IVariables:
46 `_default` : Default clause
47 Default clause
48 """
49
51 """
52 Initialization
53
54 :Parameters:
55 `default` : Default clause
56 Default clause
57 """
58 self._default = default
59 self._symbols = symbols
60
62 """
63 Make string representation
64
65 :Return: The string representation
66 :Rtype: ``str``
67 """
68 if self._default.for_update:
69 for_update = ", for_update=%r" % (True,)
70 else:
71 for_update = ""
72 return "%s(%r%s)" % (
73 self._symbols['default'],
74 _util.unicode(self._default.arg),
75 for_update,
76 )
77
80 """
81 Column container
82
83 :IVariables:
84 `_name` : ``unicode``
85 Name
86
87 `_ctype` : SA type
88 Column type
89
90 `_nullable` : ``bool``
91 Nullable?
92
93 `_primary_key` : ``bool``
94 Part of a primary key?
95
96 `_autoincrement` : ``bool``
97 Possible autoincrement?
98
99 `_server_default` : Default clause
100 Default clause
101 """
102
103 - def __init__(self, name, ctype, nullable, primary_key, autoincrement,
104 server_default, symbols):
105 """
106 Initialization
107
108 :Parameters:
109 `name` : ``unicode``
110 Column name
111
112 `ctype` : SA type
113 Column type
114
115 `nullable` : ``bool``
116 Nullable?
117
118 `primary_key` : ``bool``
119 Part of a primary key?
120
121 `autoincrement` : ``bool``
122 Possible autoincrement?
123
124 `server_default` : Default clause
125 Default clause
126 """
127 self._name = name
128 self._ctype = ctype
129 self._nullable = nullable
130 self._primary_key = primary_key
131 self._autoincrement = autoincrement
132 self._server_default = server_default
133 self._symbols = symbols
134
135 @classmethod
136 - def from_sa(cls, column, symbols):
137 """
138 Construct from SA column
139
140 :Parameters:
141 `column` : SA column
142 SA column
143
144 :Return: New column instance
145 :Rtype: `Column`
146 """
147 return cls(
148 column.name,
149 _type.Type.by_column(column, symbols),
150 nullable=column.nullable,
151 primary_key=column.primary_key,
152 autoincrement=column.autoincrement,
153 server_default=column.server_default,
154 symbols=symbols,
155 )
156
158 """
159 Make string representation
160
161 :Return: The string representation
162 :Rtype: ``str``
163 """
164 params = list(_it.imap(repr, (self._name, self._ctype)))
165 if not self._nullable:
166 params.append('nullable=%r' % (False,))
167 if not self._autoincrement and self._primary_key:
168 params.append('autoincrement=%r' % (False,))
169 if self._server_default is not None:
170 params.append('server_default=%r' % (
171 ServerDefault(self._server_default, self._symbols),
172 ))
173 return "%s(%s)" % (
174 self._symbols['column'], ', '.join(params)
175 )
176