quill-sql 0.1.8 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/client_token.rb +43 -0
- data/lib/db/bigquery.rb +1346 -0
- data/lib/db/cached_connection.rb +187 -17
- data/lib/db/civil_time.rb +60 -0
- data/lib/db/clickhouse.rb +164 -48
- data/lib/db/databricks.rb +498 -61
- data/lib/db/db_helper.rb +112 -2
- data/lib/db/mssql.rb +917 -0
- data/lib/db/mysql.rb +934 -0
- data/lib/db/postgres.rb +222 -47
- data/lib/db/redshift.rb +164 -0
- data/lib/db/snowflake.rb +1167 -0
- data/lib/models/filters.rb +1 -1
- data/lib/quill-sql.rb +468 -100
- data/lib/utils/pivot_template.rb +457 -0
- data/lib/utils/report_error.rb +169 -0
- data/lib/utils/run_query_processes.rb +26 -4
- metadata +128 -12
data/lib/db/postgres.rb
CHANGED
|
@@ -5,11 +5,192 @@ require 'active_support/inflector'
|
|
|
5
5
|
require 'connection_pool'
|
|
6
6
|
|
|
7
7
|
module PostgresHelper
|
|
8
|
+
# postgres-interval 1.2 (the parser node-postgres loads): years, months, days,
|
|
9
|
+
# then an optional time sign and H:M:S.fff. Zeros are omitted, so "1 day" is
|
|
10
|
+
# {"days" => 1} and "00:00:00" is {}. The match is not end-anchored.
|
|
11
|
+
|
|
12
|
+
# pg 1.6 calls JSON.parse(string, quirks_mode: true). json 3 removed that
|
|
13
|
+
# keyword. Scalar JSON values still parse without it.
|
|
14
|
+
class JsonDecoder < PG::SimpleDecoder
|
|
15
|
+
def decode(string, _tuple = nil, _field = nil)
|
|
16
|
+
return nil if string.nil?
|
|
17
|
+
|
|
18
|
+
JSON.parse(string)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class IntervalDecoder < PG::SimpleDecoder
|
|
23
|
+
def decode(string, _tuple = nil, _field = nil)
|
|
24
|
+
PostgresHelper.parse_postgres_interval(string)
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# "5" is 500000 microseconds, then /1000 -> 500. Whole milliseconds stay
|
|
29
|
+
# integers so JSON is 500, matching JSON.stringify of the JS number.
|
|
30
|
+
def self.fraction_to_milliseconds(fraction)
|
|
31
|
+
micros = "#{fraction}000000"[0, 6].to_i
|
|
32
|
+
return micros / 1000 if (micros % 1000).zero?
|
|
33
|
+
|
|
34
|
+
micros / 1000.0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Postgres prints fields separated by spaces ("1 day 01:02:03.5").
|
|
38
|
+
# String#split is the scan; the rest only interprets each token.
|
|
39
|
+
def self.parse_postgres_interval(raw)
|
|
40
|
+
return {} if raw.nil? || raw.empty?
|
|
41
|
+
|
|
42
|
+
parsed = {}
|
|
43
|
+
tokens = raw.split
|
|
44
|
+
index = 0
|
|
45
|
+
length = tokens.length
|
|
46
|
+
while index < length
|
|
47
|
+
token = tokens[index]
|
|
48
|
+
if token.include?(':')
|
|
49
|
+
read_interval_clock(parsed, token)
|
|
50
|
+
break
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
index += 1
|
|
54
|
+
break if index >= length
|
|
55
|
+
|
|
56
|
+
unit = tokens[index]
|
|
57
|
+
number = token.to_i
|
|
58
|
+
if unit.start_with?('year')
|
|
59
|
+
parsed['years'] = number if number != 0
|
|
60
|
+
elsif unit.start_with?('mon')
|
|
61
|
+
parsed['months'] = number if number != 0
|
|
62
|
+
elsif unit.start_with?('day')
|
|
63
|
+
parsed['days'] = number if number != 0
|
|
64
|
+
else
|
|
65
|
+
break
|
|
66
|
+
end
|
|
67
|
+
index += 1
|
|
68
|
+
end
|
|
69
|
+
parsed
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def self.read_interval_clock(parsed, token)
|
|
73
|
+
index = 0
|
|
74
|
+
negative = false
|
|
75
|
+
byte = token.getbyte(0)
|
|
76
|
+
if byte == 45
|
|
77
|
+
negative = true
|
|
78
|
+
index = 1
|
|
79
|
+
elsif byte == 43
|
|
80
|
+
index = 1
|
|
81
|
+
end
|
|
82
|
+
hours = 0
|
|
83
|
+
saw = false
|
|
84
|
+
while index < token.bytesize
|
|
85
|
+
byte = token.getbyte(index)
|
|
86
|
+
break if byte < 48 || byte > 57
|
|
87
|
+
|
|
88
|
+
hours = hours * 10 + (byte - 48)
|
|
89
|
+
saw = true
|
|
90
|
+
index += 1
|
|
91
|
+
end
|
|
92
|
+
return unless saw && index < token.bytesize && token.getbyte(index) == 58
|
|
93
|
+
|
|
94
|
+
index += 1
|
|
95
|
+
return if index + 2 > token.bytesize
|
|
96
|
+
|
|
97
|
+
m1 = token.getbyte(index)
|
|
98
|
+
m2 = token.getbyte(index + 1)
|
|
99
|
+
return if m1 < 48 || m1 > 57 || m2 < 48 || m2 > 57
|
|
100
|
+
|
|
101
|
+
minutes = (m1 - 48) * 10 + (m2 - 48)
|
|
102
|
+
index += 2
|
|
103
|
+
return if index >= token.bytesize || token.getbyte(index) != 58
|
|
104
|
+
|
|
105
|
+
index += 1
|
|
106
|
+
return if index + 2 > token.bytesize
|
|
107
|
+
|
|
108
|
+
s1 = token.getbyte(index)
|
|
109
|
+
s2 = token.getbyte(index + 1)
|
|
110
|
+
return if s1 < 48 || s1 > 57 || s2 < 48 || s2 > 57
|
|
111
|
+
|
|
112
|
+
seconds = (s1 - 48) * 10 + (s2 - 48)
|
|
113
|
+
index += 2
|
|
114
|
+
sign = negative ? -1 : 1
|
|
115
|
+
parsed['hours'] = sign * hours if hours != 0
|
|
116
|
+
parsed['minutes'] = sign * minutes if minutes != 0
|
|
117
|
+
parsed['seconds'] = sign * seconds if seconds != 0
|
|
118
|
+
return unless index < token.bytesize && token.getbyte(index) == 46
|
|
119
|
+
|
|
120
|
+
index += 1
|
|
121
|
+
frac = 0
|
|
122
|
+
digits = 0
|
|
123
|
+
while digits < 6 && index < token.bytesize
|
|
124
|
+
byte = token.getbyte(index)
|
|
125
|
+
break if byte < 48 || byte > 57
|
|
126
|
+
|
|
127
|
+
frac = frac * 10 + (byte - 48)
|
|
128
|
+
digits += 1
|
|
129
|
+
index += 1
|
|
130
|
+
end
|
|
131
|
+
return unless digits.positive?
|
|
132
|
+
|
|
133
|
+
frac *= 10**(6 - digits)
|
|
134
|
+
millis = (frac % 1000).zero? ? frac / 1000 : frac / 1000.0
|
|
135
|
+
parsed['milliseconds'] = sign * millis if millis != 0
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Decode only the types node-postgres decodes by default; everything else (int8, numeric,
|
|
139
|
+
# money, timestamps, dates, ...) stays as the string Postgres emits.
|
|
140
|
+
# interval follows postgres-interval 1.2. numeric[] elements are floats (oid 1231).
|
|
141
|
+
# money[] elements stay strings (oid 791).
|
|
142
|
+
RESULT_TYPE_MAP = PG::TypeMapByOid.new.tap do |tm|
|
|
143
|
+
tm.add_coder(PG::TextDecoder::Boolean.new(oid: 16))
|
|
144
|
+
tm.add_coder(PG::TextDecoder::Integer.new(oid: 21))
|
|
145
|
+
tm.add_coder(PG::TextDecoder::Integer.new(oid: 23))
|
|
146
|
+
tm.add_coder(PG::TextDecoder::Float.new(oid: 700))
|
|
147
|
+
tm.add_coder(PG::TextDecoder::Float.new(oid: 701))
|
|
148
|
+
tm.add_coder(JsonDecoder.new(oid: 114))
|
|
149
|
+
tm.add_coder(JsonDecoder.new(oid: 3802))
|
|
150
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1000, elements_type: PG::TextDecoder::Boolean.new))
|
|
151
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1005, elements_type: PG::TextDecoder::Integer.new))
|
|
152
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1007, elements_type: PG::TextDecoder::Integer.new))
|
|
153
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1009))
|
|
154
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1015))
|
|
155
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1016))
|
|
156
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1021, elements_type: PG::TextDecoder::Float.new))
|
|
157
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1022, elements_type: PG::TextDecoder::Float.new))
|
|
158
|
+
tm.add_coder(IntervalDecoder.new(oid: 1186))
|
|
159
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1187, elements_type: IntervalDecoder.new))
|
|
160
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 791))
|
|
161
|
+
tm.add_coder(PG::TextDecoder::Array.new(oid: 1231, elements_type: PG::TextDecoder::Float.new))
|
|
162
|
+
end.freeze
|
|
163
|
+
|
|
164
|
+
# Pool defaults mirror node-postgres' pg.Pool: at most 10 connections, and callers queue
|
|
165
|
+
# (rather than raise ConnectionPool::TimeoutError after 5s) while the pool is saturated,
|
|
166
|
+
# so a dashboard fan-out degrades to slower responses instead of errors.
|
|
167
|
+
DEFAULT_POOL_MAX_SIZE = 10
|
|
168
|
+
DEFAULT_POOL_TIMEOUT_SECONDS = 600
|
|
169
|
+
# libpq waits forever when this is absent. connection_pool runs the connect
|
|
170
|
+
# while holding its mutex, so one unreachable host would stall every checkout
|
|
171
|
+
# for the operating system's TCP timeout. A connection string that sets
|
|
172
|
+
# connect_timeout keeps that value.
|
|
173
|
+
CONNECT_TIMEOUT_SECONDS = 10
|
|
174
|
+
|
|
8
175
|
class << self
|
|
176
|
+
def pool_env_number(name, default, minimum)
|
|
177
|
+
raw = ENV[name].to_s.strip
|
|
178
|
+
return default if raw.empty?
|
|
179
|
+
parsed = Float(raw, exception: false)
|
|
180
|
+
return default if parsed.nil?
|
|
181
|
+
[parsed, minimum].max
|
|
182
|
+
end
|
|
183
|
+
|
|
9
184
|
def connect_to_postgres(config)
|
|
10
|
-
|
|
11
|
-
|
|
185
|
+
size = pool_env_number('QUILL_PG_POOL_MAX_SIZE', DEFAULT_POOL_MAX_SIZE, 1).to_i
|
|
186
|
+
timeout = pool_env_number('QUILL_PG_POOL_TIMEOUT_SECONDS', DEFAULT_POOL_TIMEOUT_SECONDS, 0.1)
|
|
187
|
+
params = (config[:params] || {}).dup
|
|
188
|
+
unless params.key?(:connect_timeout) || params.key?('connect_timeout')
|
|
189
|
+
params[:connect_timeout] = CONNECT_TIMEOUT_SECONDS
|
|
190
|
+
end
|
|
191
|
+
ConnectionPool.new(size: size, timeout: timeout) do
|
|
12
192
|
PG.connect(
|
|
193
|
+
**params,
|
|
13
194
|
host: config[:host],
|
|
14
195
|
port: config[:port],
|
|
15
196
|
dbname: config[:database],
|
|
@@ -17,8 +198,7 @@ module PostgresHelper
|
|
|
17
198
|
password: config[:password],
|
|
18
199
|
sslmode: config[:sslmode] || 'prefer',
|
|
19
200
|
gssencmode: 'disable', # Disable GSSAPI encryption
|
|
20
|
-
krbsrvname: nil
|
|
21
|
-
target_session_attrs: 'read-write'
|
|
201
|
+
krbsrvname: nil # Disable Kerberos service name
|
|
22
202
|
)
|
|
23
203
|
end
|
|
24
204
|
end
|
|
@@ -26,52 +206,37 @@ module PostgresHelper
|
|
|
26
206
|
def disconnect_from_postgres(pool)
|
|
27
207
|
return unless pool.respond_to?(:shutdown)
|
|
28
208
|
pool.shutdown { |conn| conn.close if conn.respond_to?(:close) }
|
|
29
|
-
rescue PG::Error => e
|
|
30
|
-
puts "Error closing connection pool: #{e.message}"
|
|
31
209
|
end
|
|
32
210
|
|
|
33
211
|
def run_query_postgres(sql, pool)
|
|
34
212
|
pool.with do |client|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
213
|
+
begin
|
|
214
|
+
result = client.exec(sql)
|
|
215
|
+
result.type_map = RESULT_TYPE_MAP
|
|
216
|
+
|
|
217
|
+
fields = result.fields.map do |field|
|
|
218
|
+
{
|
|
219
|
+
name: field,
|
|
220
|
+
dataTypeID: result.ftype(result.fields.index(field))
|
|
221
|
+
}
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
rows = result.values.map do |row|
|
|
225
|
+
result.fields.zip(row).to_h
|
|
226
|
+
end
|
|
227
|
+
|
|
38
228
|
{
|
|
39
|
-
|
|
40
|
-
|
|
229
|
+
fields: fields,
|
|
230
|
+
rows: rows
|
|
41
231
|
}
|
|
232
|
+
rescue PG::ConnectionBad
|
|
233
|
+
# checkin would put this socket back. The next checkout would fail
|
|
234
|
+
# the same way until the process restarted.
|
|
235
|
+
pool.discard_current_connection { |conn| conn.close if conn.respond_to?(:close) }
|
|
236
|
+
raise
|
|
237
|
+
ensure
|
|
238
|
+
result&.clear if result.respond_to?(:clear)
|
|
42
239
|
end
|
|
43
|
-
|
|
44
|
-
# Create a map of field names to their types
|
|
45
|
-
field_types = result.fields.map.with_index { |field, i| [field, result.ftype(i)] }.to_h
|
|
46
|
-
json_types = [114, 3802] # JSON and JSONB OIDs
|
|
47
|
-
|
|
48
|
-
rows = result.values.map do |row|
|
|
49
|
-
result.fields.zip(row).map do |field_name, value|
|
|
50
|
-
# only parse JSON if the field is a JSON type
|
|
51
|
-
parsed_value = if json_types.include?(field_types[field_name])
|
|
52
|
-
begin
|
|
53
|
-
json_value = JSON.parse(value.to_s)
|
|
54
|
-
if json_value.is_a?(Array)
|
|
55
|
-
json_value.map(&:to_s)
|
|
56
|
-
else
|
|
57
|
-
json_value
|
|
58
|
-
end
|
|
59
|
-
rescue JSON::ParserError
|
|
60
|
-
value
|
|
61
|
-
end
|
|
62
|
-
else
|
|
63
|
-
value
|
|
64
|
-
end
|
|
65
|
-
[field_name, parsed_value]
|
|
66
|
-
end.to_h
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
{
|
|
70
|
-
fields: fields,
|
|
71
|
-
rows: rows
|
|
72
|
-
}
|
|
73
|
-
ensure
|
|
74
|
-
result&.clear if result.respond_to?(:clear)
|
|
75
240
|
end
|
|
76
241
|
end
|
|
77
242
|
|
|
@@ -93,7 +258,14 @@ module PostgresHelper
|
|
|
93
258
|
SELECT table_name, table_schema
|
|
94
259
|
FROM information_schema.tables
|
|
95
260
|
WHERE table_schema = '#{schema}'
|
|
96
|
-
|
|
261
|
+
|
|
262
|
+
UNION
|
|
263
|
+
|
|
264
|
+
SELECT c.relname as table_name, n.nspname as table_schema
|
|
265
|
+
FROM pg_class c
|
|
266
|
+
JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
267
|
+
WHERE n.nspname = '#{schema}'
|
|
268
|
+
AND c.relkind = 'm'
|
|
97
269
|
SQL
|
|
98
270
|
|
|
99
271
|
results = run_query_postgres(sql, client)
|
|
@@ -191,14 +363,17 @@ module PostgresHelper
|
|
|
191
363
|
|
|
192
364
|
def format_postgres_config(connection_string)
|
|
193
365
|
parsed = URI.parse(connection_string)
|
|
366
|
+
params = URI.decode_www_form(parsed.query.to_s).to_h.transform_keys(&:to_sym)
|
|
367
|
+
sslmode = params.delete(:sslmode)
|
|
194
368
|
|
|
195
369
|
{
|
|
196
370
|
host: parsed.host,
|
|
197
371
|
port: parsed.port || 5432,
|
|
198
372
|
database: parsed.path[1..-1],
|
|
199
|
-
username: parsed.user
|
|
200
|
-
password: parsed.password
|
|
201
|
-
sslmode:
|
|
373
|
+
username: parsed.user ? URI.decode_www_form_component(parsed.user) : 'postgres',
|
|
374
|
+
password: parsed.password ? URI.decode_www_form_component(parsed.password) : '',
|
|
375
|
+
sslmode: sslmode || 'prefer',
|
|
376
|
+
params: params
|
|
202
377
|
}
|
|
203
378
|
end
|
|
204
379
|
|
data/lib/db/redshift.rb
ADDED
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
require 'uri'
|
|
2
|
+
require_relative 'postgres'
|
|
3
|
+
|
|
4
|
+
# Redshift speaks the Postgres wire protocol, so connections and queries reuse
|
|
5
|
+
# PostgresHelper (and its node-postgres value decoders). Catalog lookups use the
|
|
6
|
+
# svv_* views, because information_schema hides external and late-binding objects,
|
|
7
|
+
# and column types are mapped from svv_columns.data_type onto Postgres OIDs.
|
|
8
|
+
module RedshiftHelper
|
|
9
|
+
REDSHIFT_TYPE_MAP = {
|
|
10
|
+
'bigint' => 20,
|
|
11
|
+
'int8' => 20,
|
|
12
|
+
'boolean' => 16,
|
|
13
|
+
'bool' => 16,
|
|
14
|
+
'character' => 1042,
|
|
15
|
+
'char' => 1042,
|
|
16
|
+
'bpchar' => 1042,
|
|
17
|
+
'character varying' => 1043,
|
|
18
|
+
'varchar' => 1043,
|
|
19
|
+
'date' => 1082,
|
|
20
|
+
'double precision' => 701,
|
|
21
|
+
'float8' => 701,
|
|
22
|
+
'real' => 700,
|
|
23
|
+
'float4' => 700,
|
|
24
|
+
'integer' => 23,
|
|
25
|
+
'int' => 23,
|
|
26
|
+
'int4' => 23,
|
|
27
|
+
'smallint' => 21,
|
|
28
|
+
'int2' => 21,
|
|
29
|
+
'numeric' => 1700,
|
|
30
|
+
'decimal' => 1700,
|
|
31
|
+
'timestamp without time zone' => 1114,
|
|
32
|
+
'timestamp' => 1114,
|
|
33
|
+
'timestamp with time zone' => 1184,
|
|
34
|
+
'timestamptz' => 1184,
|
|
35
|
+
'time without time zone' => 1083,
|
|
36
|
+
'time' => 1083,
|
|
37
|
+
'time with time zone' => 1266,
|
|
38
|
+
'timetz' => 1266,
|
|
39
|
+
'varbyte' => 17,
|
|
40
|
+
'binary' => 17,
|
|
41
|
+
'geometry' => 25,
|
|
42
|
+
'geography' => 25,
|
|
43
|
+
'super' => 114,
|
|
44
|
+
'hllsketch' => 17
|
|
45
|
+
}.freeze
|
|
46
|
+
|
|
47
|
+
class << self
|
|
48
|
+
def redshift_type_to_postgres_oid(data_type)
|
|
49
|
+
REDSHIFT_TYPE_MAP[data_type.to_s.downcase] || 1043
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Same parsed config as Postgres. Node always opens Redshift with SSL and
|
|
53
|
+
# does not verify the server certificate, so an omitted sslmode becomes
|
|
54
|
+
# `require` rather than Postgres' `prefer`.
|
|
55
|
+
def format_redshift_config(connection_string)
|
|
56
|
+
config = PostgresHelper.format_postgres_config(connection_string)
|
|
57
|
+
parsed = URI.parse(connection_string)
|
|
58
|
+
specified = URI.decode_www_form(parsed.query.to_s).to_h.key?('sslmode')
|
|
59
|
+
config[:sslmode] = 'require' unless specified
|
|
60
|
+
config
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def connect_to_redshift(config)
|
|
64
|
+
PostgresHelper.connect_to_postgres(config)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def disconnect_from_redshift(pool)
|
|
68
|
+
PostgresHelper.disconnect_from_postgres(pool)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def run_query_redshift(sql, pool)
|
|
72
|
+
PostgresHelper.run_query_postgres(sql, pool)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def get_schemas_redshift(client)
|
|
76
|
+
sql = <<~SQL
|
|
77
|
+
SELECT schema_name
|
|
78
|
+
FROM svv_all_schemas
|
|
79
|
+
WHERE database_name = current_database()
|
|
80
|
+
AND schema_name NOT LIKE 'pg\\_%'
|
|
81
|
+
AND schema_name <> 'information_schema'
|
|
82
|
+
ORDER BY schema_name;
|
|
83
|
+
SQL
|
|
84
|
+
|
|
85
|
+
results = run_query_redshift(sql, client)
|
|
86
|
+
results[:rows].map { |row| row['schema_name'] }
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def get_tables_by_schema_redshift(client, schema_names)
|
|
90
|
+
schema_names.flat_map do |schema_name|
|
|
91
|
+
sql = <<~SQL
|
|
92
|
+
SELECT
|
|
93
|
+
schema_name,
|
|
94
|
+
table_name
|
|
95
|
+
FROM
|
|
96
|
+
svv_all_tables
|
|
97
|
+
WHERE
|
|
98
|
+
schema_name = '#{schema_name}';
|
|
99
|
+
SQL
|
|
100
|
+
|
|
101
|
+
results = run_query_redshift(sql, client)
|
|
102
|
+
results[:rows].map do |row|
|
|
103
|
+
{
|
|
104
|
+
tableName: row['table_name'],
|
|
105
|
+
schemaName: row['schema_name']
|
|
106
|
+
}
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def get_columns_by_table_redshift(client, schema_name, table_name)
|
|
112
|
+
sql = <<~SQL
|
|
113
|
+
SELECT column_name
|
|
114
|
+
FROM svv_columns
|
|
115
|
+
WHERE table_schema = '#{schema_name}'
|
|
116
|
+
AND table_name = '#{table_name}'
|
|
117
|
+
ORDER BY ordinal_position;
|
|
118
|
+
SQL
|
|
119
|
+
|
|
120
|
+
results = run_query_redshift(sql, client)
|
|
121
|
+
results[:rows].map { |row| row['column_name'] }
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def get_foreign_keys_redshift(client, schema_name, table_name, primary_key)
|
|
125
|
+
PostgresHelper.get_foreign_keys_postgres(client, schema_name, table_name, primary_key)
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
# schema_name is unused: each table carries its own schema, matching Node.
|
|
129
|
+
def get_schema_column_info_redshift(client, _schema_name, table_names)
|
|
130
|
+
table_names.map do |table|
|
|
131
|
+
table_schema, table_name = table_identity(table)
|
|
132
|
+
sql = <<~SQL
|
|
133
|
+
SELECT column_name, data_type, ordinal_position
|
|
134
|
+
FROM svv_columns
|
|
135
|
+
WHERE table_schema = '#{table_schema}'
|
|
136
|
+
AND table_name = '#{table_name}'
|
|
137
|
+
ORDER BY ordinal_position;
|
|
138
|
+
SQL
|
|
139
|
+
|
|
140
|
+
results = run_query_redshift(sql, client)
|
|
141
|
+
{
|
|
142
|
+
tableName: "#{table_schema}.#{table_name}",
|
|
143
|
+
displayName: "#{table_schema}.#{table_name}",
|
|
144
|
+
columns: results[:rows].map do |row|
|
|
145
|
+
{
|
|
146
|
+
columnName: row['column_name'],
|
|
147
|
+
displayName: row['column_name'],
|
|
148
|
+
dataTypeID: redshift_type_to_postgres_oid(row['data_type']),
|
|
149
|
+
fieldType: row['data_type']
|
|
150
|
+
}
|
|
151
|
+
end
|
|
152
|
+
}
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
private
|
|
157
|
+
|
|
158
|
+
def table_identity(table)
|
|
159
|
+
schema_name = table[:schemaName] || table['schemaName'] || table[:schema_name] || table['schema_name']
|
|
160
|
+
table_name = table[:tableName] || table['tableName'] || table[:table_name] || table['table_name']
|
|
161
|
+
[schema_name, table_name]
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
end
|