Skip to content
This repository was archived by the owner on Feb 21, 2020. It is now read-only.

Commit f97a196

Browse files
Merge pull request #24 from robin900/reflect-identity-and-sequences
reflect identity as autoincrement=True and primary_key=True
2 parents a8a8fa4 + af4e482 commit f97a196

1 file changed

Lines changed: 42 additions & 16 deletions

File tree

sqla_vertica_python/vertica_python.py

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ def get_columns(self, connection, table_name, schema=None, **kw):
192192
column_name,
193193
data_type,
194194
column_default,
195-
is_nullable
195+
is_nullable,
196+
is_identity
196197
FROM v_catalog.columns
197198
where table_name = '{table_name}'
198199
{schema_conditional}
@@ -201,25 +202,46 @@ def get_columns(self, connection, table_name, schema=None, **kw):
201202
column_name,
202203
data_type,
203204
'' as column_default,
204-
true as is_nullable
205+
true as is_nullable,
206+
false as is_identity
205207
FROM v_catalog.view_columns
206208
where table_name = '{table_name}'
207209
{schema_conditional}
208210
""".format(table_name=table_name, schema_conditional=schema_conditional)
209211
colobjs = []
210-
for row in connection.execute(column_select):
211-
colobj = self._get_column_info(
212-
row.column_name,
213-
row.data_type,
214-
row.is_nullable,
215-
row.column_default,
216-
(row.column_name in primary_key_columns)
217-
)
218-
if colobj:
219-
colobjs.append(colobj)
212+
column_select_results = list(connection.execute(column_select))
213+
for row in list(connection.execute(column_select)):
214+
sequence_info = connection.execute("""
215+
SELECT
216+
sequence_name as name,
217+
minimum as start,
218+
increment_by as increment
219+
FROM v_catalog.sequences
220+
WHERE identity_table_name = '{table_name}'
221+
{schema_conditional}
222+
""".format(
223+
table_name=table_name,
224+
schema_conditional=(
225+
"" if schema is None
226+
else "AND sequence_schema = '{schema}'".format(schema=schema)
227+
)
228+
)
229+
).first() if row.is_identity else None
230+
231+
colobj = self._get_column_info(
232+
row.column_name,
233+
row.data_type,
234+
row.is_nullable,
235+
row.column_default,
236+
row.is_identity,
237+
(row.column_name in primary_key_columns),
238+
sequence_info
239+
)
240+
if colobj:
241+
colobjs.append(colobj)
220242
return colobjs
221243

222-
def _get_column_info(self, name, data_type, is_nullable, default, is_primary_key):
244+
def _get_column_info(self, name, data_type, is_nullable, default, is_identity, is_primary_key, sequence):
223245
m = re.match(r'(\w+)(?:\((\d+)(?:,(\d+))?\))?', data_type)
224246
if not m:
225247
raise ValueError("data type string not parseable for type name and optional parameters: %s" % data_type)
@@ -241,14 +263,18 @@ def _get_column_info(self, name, data_type, is_nullable, default, is_primary_key
241263
if callable(typeobj):
242264
typeobj = typeobj(*typeargs, **typekwargs)
243265

244-
return {
266+
column_info = {
245267
'name': name,
246268
'type': typeobj,
247269
'nullable': is_nullable,
248270
'default': default,
249-
'primary_key': is_primary_key
271+
'primary_key': (is_primary_key or is_identity)
250272
}
251-
273+
if is_identity:
274+
column_info['autoincrement'] = True
275+
if sequence:
276+
column_info['sequence'] = dict(sequence)
277+
return column_info
252278

253279
@reflection.cache
254280
def get_unique_constraints(self, connection, table_name, schema=None, **kw):

0 commit comments

Comments
 (0)