forked from LocusEnergy/sqlalchemy-vertica-python
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbase.py
More file actions
345 lines (288 loc) · 11.7 KB
/
Copy pathbase.py
File metadata and controls
345 lines (288 loc) · 11.7 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
from __future__ import absolute_import, unicode_literals, print_function, division
import re
from sqlalchemy import exc
from sqlalchemy import sql
from textwrap import dedent
from sqlalchemy.dialects.postgresql import BYTEA, DOUBLE_PRECISION
from sqlalchemy.dialects.postgresql.base import PGDialect, PGDDLCompiler
from sqlalchemy.engine import reflection
from sqlalchemy.types import INTEGER, BIGINT, SMALLINT, VARCHAR, CHAR, \
NUMERIC, FLOAT, REAL, DATE, DATETIME, BOOLEAN, BLOB, TIMESTAMP, TIME
ischema_names = {
'INT': INTEGER,
'INTEGER': INTEGER,
'INT8': INTEGER,
'BIGINT': BIGINT,
'SMALLINT': SMALLINT,
'TINYINT': SMALLINT,
'CHAR': CHAR,
'VARCHAR': VARCHAR,
'VARCHAR2': VARCHAR,
'TEXT': VARCHAR,
'NUMERIC': NUMERIC,
'DECIMAL': NUMERIC,
'NUMBER': NUMERIC,
'MONEY': NUMERIC,
'FLOAT': FLOAT,
'FLOAT8': FLOAT,
'REAL': REAL,
'DOUBLE': DOUBLE_PRECISION,
'TIMESTAMP': TIMESTAMP,
'TIMESTAMPTZ': TIMESTAMP,
'TIMESTAMP WITH TIMEZONE': TIMESTAMP,
'TIME': TIME,
'TIME WITH TIMEZONE': TIME,
'DATE': DATE,
'DATETIME': DATETIME,
'SMALLDATETIME': DATETIME,
'BINARY': BLOB,
'VARBINARY': BLOB,
'RAW': BLOB,
'BYTEA': BYTEA,
'BOOLEAN': BOOLEAN,
'UUID' : VARCHAR
}
class VerticaDDLCompiler(PGDDLCompiler):
def get_column_specification(self, column, **kwargs):
colspec = self.preparer.format_column(column)
# noinspection PyUnusedLocal
impl_type = column.type.dialect_impl(self.dialect)
# noinspection PyProtectedMember
if column.primary_key and column is column.table._autoincrement_column:
colspec += " AUTO_INCREMENT"
else:
colspec += " " + self.dialect.type_compiler.process(column.type)
default = self.get_column_default_string(column)
if default is not None:
colspec += " DEFAULT " + default
if not column.nullable:
colspec += " NOT NULL"
return colspec
# noinspection PyArgumentList,PyAbstractClass
class VerticaDialect(PGDialect):
name = 'vertica'
ischema_names = ischema_names
ddl_compiler = VerticaDDLCompiler
def _get_default_schema_name(self, connection):
return connection.scalar("SELECT current_schema()")
def _get_server_version_info(self, connection):
v = connection.scalar("SELECT version()")
m = re.match(r".*Vertica Analytic Database v(\d+)\.(\d+)\.(\d)+.*", v)
if not m:
raise AssertionError("Could not determine version from string '%(ver)s'" % {'ver': v})
return tuple([int(x) for x in m.group(1, 2, 3) if x is not None])
# noinspection PyRedeclaration
def _get_default_schema_name(self, connection):
return connection.scalar("SELECT current_schema()")
def create_connect_args(self, url):
opts = url.translate_connect_args(username='user')
opts.update(url.query)
return [], opts
def has_schema(self, connection, schema):
has_schema_sql = sql.text(dedent("""
SELECT EXISTS (
SELECT schema_name
FROM v_catalog.schemata
WHERE lower(schema_name) = '%(schema)s')
""" % {'schema': schema.lower()}))
c = connection.execute(has_schema_sql)
return bool(c.scalar())
def has_table(self, connection, table_name, schema=None):
if schema is None:
schema = self._get_default_schema_name(connection)
has_table_sql = sql.text(dedent("""
SELECT EXISTS (
SELECT table_name
FROM v_catalog.all_tables
WHERE lower(table_name) = '%(table)s'
AND lower(schema_name) = '%(schema)s')
""" % {'schema': schema.lower(), 'table': table_name.lower()}))
c = connection.execute(has_table_sql)
return bool(c.scalar())
def has_sequence(self, connection, sequence_name, schema=None):
if schema is None:
schema = self._get_default_schema_name(connection)
has_seq_sql = sql.text(dedent("""
SELECT EXISTS (
SELECT sequence_name
FROM v_catalog.sequences
WHERE lower(sequence_name) = '%(sequence)s'
AND lower(sequence_schema) = '%(schema)s')
""" % {'schema': schema.lower(), 'sequence': sequence_name.lower()}))
c = connection.execute(has_seq_sql)
return bool(c.scalar())
def has_type(self, connection, type_name, schema=None):
has_type_sql = sql.text(dedent("""
SELECT EXISTS (
SELECT type_name
FROM v_catalog.types
WHERE lower(type_name) = '%(type)s')
""" % {'type': type_name.lower()}))
c = connection.execute(has_type_sql)
return bool(c.scalar())
@reflection.cache
def get_schema_names(self, connection, **kw):
get_schemas_sql = sql.text(dedent("""
SELECT schema_name
FROM v_catalog.schemata
"""))
c = connection.execute(get_schemas_sql)
return [row[0] for row in c if not row[0].startswith('v_')]
@reflection.cache
def get_table_oid(self, connection, table_name, schema=None, **kw):
if schema is None:
schema = self._get_default_schema_name(connection)
get_oid_sql = sql.text(dedent("""
SELECT table_id
FROM v_catalog.tables
WHERE lower(table_name) = '%(table)s'
AND lower(table_schema) = '%(schema)s'
""" % {'schema': schema.lower(), 'table': table_name.lower()}))
c = connection.execute(get_oid_sql)
table_oid = c.scalar()
if table_oid is None:
raise exc.NoSuchTableError(table_name)
return table_oid
@reflection.cache
def get_table_names(self, connection, schema=None, **kw):
if schema is not None:
schema_condition = "lower(table_schema) = '%(schema)s'" % {'schema': schema.lower()}
else:
schema_condition = "1"
get_tables_sql = sql.text(dedent("""
SELECT table_name
FROM v_catalog.tables
WHERE %(schema_condition)s
ORDER BY table_schema, table_name
""" % {'schema_condition': schema_condition}))
c = connection.execute(get_tables_sql)
return [row[0] for row in c]
@reflection.cache
def get_temp_table_names(self, connection, schema=None, **kw):
if schema is not None:
schema_condition = "lower(table_schema) = '%(schema)s'" % {'schema': schema.lower()}
else:
schema_condition = "1"
get_tables_sql = sql.text(dedent("""
SELECT table_name
FROM v_catalog.tables
WHERE %(schema_condition)s
AND IS_TEMP_TABLE
ORDER BY table_schema, table_name
""" % {'schema_condition': schema_condition}))
c = connection.execute(get_tables_sql)
return [row[0] for row in c]
@reflection.cache
def get_view_names(self, connection, schema=None, **kw):
if schema is not None:
schema_condition = "lower(table_schema) = '%(schema)s'" % {'schema': schema.lower()}
else:
schema_condition = "1"
get_views_sql = sql.text(dedent("""
SELECT table_name
FROM v_catalog.views
WHERE %(schema_condition)s
ORDER BY table_schema, table_name
""" % {'schema_condition': schema_condition}))
c = connection.execute(get_views_sql)
return [row[0] for row in c]
@reflection.cache
def get_temp_view_names(self, connection, schema=None, **kw):
return []
@reflection.cache
def get_columns(self, connection, table_name, schema=None, **kw):
if schema is not None:
schema_condition = "lower(table_schema) = '%(schema)s'" % {'schema': schema.lower()}
else:
schema_condition = "1"
s = sql.text(dedent("""
SELECT column_name, data_type, column_default, is_nullable
FROM v_catalog.columns
WHERE lower(table_name) = '%(table)s'
AND %(schema_condition)s
UNION ALL
SELECT column_name, data_type, '' as column_default, true as is_nullable
FROM v_catalog.view_columns
WHERE lower(table_name) = '%(table)s'
AND %(schema_condition)s
""" % {'table': table_name.lower(), 'schema_condition': schema_condition}))
spk = sql.text(dedent("""
SELECT column_name
FROM v_catalog.primary_keys
WHERE lower(table_name) = '%(table)s'
AND constraint_type = 'p'
AND %(schema_condition)s
""" % {'table': table_name.lower(), 'schema_condition': schema_condition}))
pk_columns = [x[0] for x in connection.execute(spk)]
columns = []
for row in connection.execute(s):
name = row.column_name
dtype = row.data_type.upper()
if '(' in dtype:
dtype = dtype.split('(')[0]
coltype = self.ischema_names[dtype]
primary_key = name in pk_columns
default = row.column_default
nullable = row.is_nullable
columns.append({
'name': name,
'type': coltype,
'nullable': nullable,
'default': default,
'primary_key': primary_key
})
return columns
@reflection.cache
def get_unique_constraints(self, connection, table_name, schema=None, **kw):
if schema is None:
schema = self._get_default_schema_name(connection)
get_constrains_sql = sql.text(dedent("""
SELECT constraint_name, column_name
FROM v_catalog.constraint_columns
WHERE lower(table_name) = '%(table)s'
-- AND constraint_type IN ('p', 'u')
AND lower(table_schema) = '%(schema)s'
""" % {'schema': schema.lower(), 'table': table_name.lower()}))
c = connection.execute(get_constrains_sql)
if c.rowcount <= 0:
return []
constraints, columns = zip(*c)
result_dict = {
unique_con: [col for con, col in zip(constraints, columns) if con == unique_con]
for unique_con in set(constraints)
}
return [{"name": name, "column_names": cols} for name, cols in result_dict.items()]
@reflection.cache
def get_check_constraints(
self, connection, table_name, schema=None, **kw):
table_oid = self.get_table_oid(connection, table_name, schema,
info_cache=kw.get('info_cache'))
constraints_sql = sql.text(dedent("""
SELECT constraint_name, column_name
FROM v_catalog.constraint_columns
WHERE table_id = %(oid)s
AND constraint_type = 'c'
""" % {'oid': table_oid}))
c = connection.execute(constraints_sql)
return [{'name': name, 'sqltext': col} for name, col in c.fetchall()]
def normalize_name(self, name):
name = name and name.rstrip()
if name is None:
return None
return name.lower()
def denormalize_name(self, name):
return name
# methods allows table introspection to work
@reflection.cache
def get_pk_constraint(self, bind, table_name, schema=None, **kw):
return {'constrained_columns': [], 'name': 'undefined'}
@reflection.cache
def get_foreign_keys(self, connection, table_name, schema=None, **kw):
return []
@reflection.cache
def get_indexes(self, connection, table_name, schema, **kw):
return []
# Disable index creation since that's not a thing in Vertica.
# noinspection PyUnusedLocal
def visit_create_index(self, create):
return None