activerecord6-redshift-adapter 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +54 -0
- data/README.md +41 -0
- data/lib/active_record/connection_adapters/redshift/array_parser.rb +93 -0
- data/lib/active_record/connection_adapters/redshift/column.rb +11 -0
- data/lib/active_record/connection_adapters/redshift/database_statements.rb +220 -0
- data/lib/active_record/connection_adapters/redshift/oid.rb +15 -0
- data/lib/active_record/connection_adapters/redshift/oid/date_time.rb +34 -0
- data/lib/active_record/connection_adapters/redshift/oid/decimal.rb +13 -0
- data/lib/active_record/connection_adapters/redshift/oid/json.rb +35 -0
- data/lib/active_record/connection_adapters/redshift/oid/jsonb.rb +23 -0
- data/lib/active_record/connection_adapters/redshift/oid/type_map_initializer.rb +64 -0
- data/lib/active_record/connection_adapters/redshift/quoting.rb +99 -0
- data/lib/active_record/connection_adapters/redshift/referential_integrity.rb +15 -0
- data/lib/active_record/connection_adapters/redshift/schema_definitions.rb +67 -0
- data/lib/active_record/connection_adapters/redshift/schema_dumper.rb +15 -0
- data/lib/active_record/connection_adapters/redshift/schema_statements.rb +419 -0
- data/lib/active_record/connection_adapters/redshift/type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/redshift/utils.rb +77 -0
- data/lib/active_record/connection_adapters/redshift_adapter.rb +635 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5830bb316b6a3021508d6542b09478a0b0c3ce7a
|
4
|
+
data.tar.gz: eb66565b485ab9464f93fe5f2e392a37a64d2805
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 69d0323991a44986cef4b87d9268eaeff017c46dfd83cd6785753353c18900cc0dedde17539ef4c99e476d10f4b61e262aea755aaea8acae9070763d61f8aa79
|
7
|
+
data.tar.gz: 819f6b941172c242926a3fc6229fe4b4d12742c89115178cfa4ae4d929e5eb2eea0cee773b852126074b2d26993db6e2faef0b99ecf6aafec75500bd0e49e41a
|
data/LICENSE
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
-----------------------------------------------------------------------------------
|
2
|
+
The MIT License (MIT)
|
3
|
+
|
4
|
+
Copyright (c) 2004-2013 David Heinemeier Hansson (original code author)
|
5
|
+
Copyright (c) 2013 Minero Aoki
|
6
|
+
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
8
|
+
this software and associated documentation files (the "Software"), to deal in
|
9
|
+
the Software without restriction, including without limitation the rights to
|
10
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
11
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
12
|
+
subject to the following conditions:
|
13
|
+
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
15
|
+
copies or substantial portions of the Software.
|
16
|
+
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
19
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
20
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
21
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
22
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
-----------------------------------------------------------------------------------
|
25
|
+
Copyright (c) 2010-2013, Fiksu, Inc.
|
26
|
+
All rights reserved.
|
27
|
+
|
28
|
+
Redistribution and use in source and binary forms, with or without
|
29
|
+
modification, are permitted provided that the following conditions are
|
30
|
+
met:
|
31
|
+
|
32
|
+
o Redistributions of source code must retain the above copyright
|
33
|
+
notice, this list of conditions and the following disclaimer.
|
34
|
+
|
35
|
+
o Redistributions in binary form must reproduce the above copyright
|
36
|
+
notice, this list of conditions and the following disclaimer in the
|
37
|
+
documentation and/or other materials provided with the
|
38
|
+
distribution.
|
39
|
+
|
40
|
+
o Fiksu, Inc. nor the names of its contributors may be used to
|
41
|
+
endorse or promote products derived from this software without
|
42
|
+
specific prior written permission.
|
43
|
+
|
44
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
45
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
46
|
+
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
47
|
+
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
48
|
+
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
49
|
+
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
50
|
+
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
51
|
+
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
52
|
+
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
53
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
54
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
activerecord6-redshift-adapter
|
2
|
+
==============================
|
3
|
+
|
4
|
+
Amazon Redshift adapter for ActiveRecord 6 (Rails 6).
|
5
|
+
I forked the project from https://github.com/ConsultingMD/activerecord5-redshift-adapter
|
6
|
+
|
7
|
+
Thanks for the auhors.
|
8
|
+
|
9
|
+
Usage
|
10
|
+
-------------------
|
11
|
+
|
12
|
+
For Rails 6, write following in Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'activerecord6-redshift-adapter'
|
16
|
+
```
|
17
|
+
|
18
|
+
In database.yml
|
19
|
+
|
20
|
+
```YAML
|
21
|
+
development:
|
22
|
+
adapter: redshift
|
23
|
+
host: host
|
24
|
+
port: port
|
25
|
+
database: db
|
26
|
+
username: user
|
27
|
+
password: password
|
28
|
+
encoding: utf8
|
29
|
+
```
|
30
|
+
|
31
|
+
OR your can use in URL
|
32
|
+
```ruby
|
33
|
+
class SomeModel < ApplicationRecord
|
34
|
+
establish_connection('redshift://username:password@host/database')
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
License
|
39
|
+
---------
|
40
|
+
|
41
|
+
MIT license (same as ActiveRecord)
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Redshift
|
4
|
+
module ArrayParser # :nodoc:
|
5
|
+
|
6
|
+
DOUBLE_QUOTE = '"'
|
7
|
+
BACKSLASH = "\\"
|
8
|
+
COMMA = ','
|
9
|
+
BRACKET_OPEN = '{'
|
10
|
+
BRACKET_CLOSE = '}'
|
11
|
+
|
12
|
+
def parse_pg_array(string) # :nodoc:
|
13
|
+
local_index = 0
|
14
|
+
array = []
|
15
|
+
while(local_index < string.length)
|
16
|
+
case string[local_index]
|
17
|
+
when BRACKET_OPEN
|
18
|
+
local_index,array = parse_array_contents(array, string, local_index + 1)
|
19
|
+
when BRACKET_CLOSE
|
20
|
+
return array
|
21
|
+
end
|
22
|
+
local_index += 1
|
23
|
+
end
|
24
|
+
|
25
|
+
array
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def parse_array_contents(array, string, index)
|
31
|
+
is_escaping = false
|
32
|
+
is_quoted = false
|
33
|
+
was_quoted = false
|
34
|
+
current_item = ''
|
35
|
+
|
36
|
+
local_index = index
|
37
|
+
while local_index
|
38
|
+
token = string[local_index]
|
39
|
+
if is_escaping
|
40
|
+
current_item << token
|
41
|
+
is_escaping = false
|
42
|
+
else
|
43
|
+
if is_quoted
|
44
|
+
case token
|
45
|
+
when DOUBLE_QUOTE
|
46
|
+
is_quoted = false
|
47
|
+
was_quoted = true
|
48
|
+
when BACKSLASH
|
49
|
+
is_escaping = true
|
50
|
+
else
|
51
|
+
current_item << token
|
52
|
+
end
|
53
|
+
else
|
54
|
+
case token
|
55
|
+
when BACKSLASH
|
56
|
+
is_escaping = true
|
57
|
+
when COMMA
|
58
|
+
add_item_to_array(array, current_item, was_quoted)
|
59
|
+
current_item = ''
|
60
|
+
was_quoted = false
|
61
|
+
when DOUBLE_QUOTE
|
62
|
+
is_quoted = true
|
63
|
+
when BRACKET_OPEN
|
64
|
+
internal_items = []
|
65
|
+
local_index,internal_items = parse_array_contents(internal_items, string, local_index + 1)
|
66
|
+
array.push(internal_items)
|
67
|
+
when BRACKET_CLOSE
|
68
|
+
add_item_to_array(array, current_item, was_quoted)
|
69
|
+
return local_index,array
|
70
|
+
else
|
71
|
+
current_item << token
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
local_index += 1
|
77
|
+
end
|
78
|
+
return local_index,array
|
79
|
+
end
|
80
|
+
|
81
|
+
def add_item_to_array(array, current_item, quoted)
|
82
|
+
return if !quoted && current_item.length == 0
|
83
|
+
|
84
|
+
if !quoted && current_item == 'NULL'
|
85
|
+
array.push nil
|
86
|
+
else
|
87
|
+
array.push current_item
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
class RedshiftColumn < Column #:nodoc:
|
4
|
+
delegate :oid, :fmod, to: :sql_type_metadata
|
5
|
+
|
6
|
+
def initialize(name, default, sql_type_metadata, null = true, default_function = nil)
|
7
|
+
super name, default, sql_type_metadata, null, default_function
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,220 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Redshift
|
4
|
+
module DatabaseStatements
|
5
|
+
def explain(arel, binds = [])
|
6
|
+
sql = "EXPLAIN #{to_sql(arel, binds)}"
|
7
|
+
ExplainPrettyPrinter.new.pp(exec_query(sql, 'EXPLAIN', binds))
|
8
|
+
end
|
9
|
+
|
10
|
+
class ExplainPrettyPrinter # :nodoc:
|
11
|
+
# Pretty prints the result of a EXPLAIN in a way that resembles the output of the
|
12
|
+
# PostgreSQL shell:
|
13
|
+
#
|
14
|
+
# QUERY PLAN
|
15
|
+
# ------------------------------------------------------------------------------
|
16
|
+
# Nested Loop Left Join (cost=0.00..37.24 rows=8 width=0)
|
17
|
+
# Join Filter: (posts.user_id = users.id)
|
18
|
+
# -> Index Scan using users_pkey on users (cost=0.00..8.27 rows=1 width=4)
|
19
|
+
# Index Cond: (id = 1)
|
20
|
+
# -> Seq Scan on posts (cost=0.00..28.88 rows=8 width=4)
|
21
|
+
# Filter: (posts.user_id = 1)
|
22
|
+
# (6 rows)
|
23
|
+
#
|
24
|
+
def pp(result)
|
25
|
+
header = result.columns.first
|
26
|
+
lines = result.rows.map(&:first)
|
27
|
+
|
28
|
+
# We add 2 because there's one char of padding at both sides, note
|
29
|
+
# the extra hyphens in the example above.
|
30
|
+
width = [header, *lines].map(&:length).max + 2
|
31
|
+
|
32
|
+
pp = []
|
33
|
+
|
34
|
+
pp << header.center(width).rstrip
|
35
|
+
pp << '-' * width
|
36
|
+
|
37
|
+
pp += lines.map {|line| " #{line}"}
|
38
|
+
|
39
|
+
nrows = result.rows.length
|
40
|
+
rows_label = nrows == 1 ? 'row' : 'rows'
|
41
|
+
pp << "(#{nrows} #{rows_label})"
|
42
|
+
|
43
|
+
pp.join("\n") + "\n"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def select_value(arel, name = nil, binds = [])
|
48
|
+
# In Rails 5.2, arel_from_relation replaced binds_from_relation,
|
49
|
+
# so we see which method exists to get the variables
|
50
|
+
if respond_to?(:arel_from_relation, true)
|
51
|
+
arel = arel_from_relation(arel)
|
52
|
+
sql, binds = to_sql(arel, binds)
|
53
|
+
else
|
54
|
+
arel, binds = binds_from_relation arel, binds
|
55
|
+
sql = to_sql(arel, binds)
|
56
|
+
end
|
57
|
+
execute_and_clear(sql, name, binds) do |result|
|
58
|
+
result.getvalue(0, 0) if result.ntuples > 0 && result.nfields > 0
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def select_values(arel, name = nil)
|
63
|
+
# In Rails 5.2, arel_from_relation replaced binds_from_relation,
|
64
|
+
# so we see which method exists to get the variables
|
65
|
+
if respond_to?(:arel_from_relation, true)
|
66
|
+
arel = arel_from_relation(arel)
|
67
|
+
sql, binds = to_sql(arel, [])
|
68
|
+
else
|
69
|
+
arel, binds = binds_from_relation arel, []
|
70
|
+
sql = to_sql(arel, binds)
|
71
|
+
end
|
72
|
+
|
73
|
+
execute_and_clear(sql, name, binds) do |result|
|
74
|
+
if result.nfields > 0
|
75
|
+
result.column_values(0)
|
76
|
+
else
|
77
|
+
[]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# Executes a SELECT query and returns an array of rows. Each row is an
|
83
|
+
# array of field values.
|
84
|
+
def select_rows(sql, name = nil, binds = [])
|
85
|
+
execute_and_clear(sql, name, binds) do |result|
|
86
|
+
result.values
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
# The internal PostgreSQL identifier of the money data type.
|
91
|
+
MONEY_COLUMN_TYPE_OID = 790 #:nodoc:
|
92
|
+
# The internal PostgreSQL identifier of the BYTEA data type.
|
93
|
+
BYTEA_COLUMN_TYPE_OID = 17 #:nodoc:
|
94
|
+
|
95
|
+
# create a 2D array representing the result set
|
96
|
+
def result_as_array(res) #:nodoc:
|
97
|
+
# check if we have any binary column and if they need escaping
|
98
|
+
ftypes = Array.new(res.nfields) do |i|
|
99
|
+
[i, res.ftype(i)]
|
100
|
+
end
|
101
|
+
|
102
|
+
rows = res.values
|
103
|
+
return rows unless ftypes.any? { |_, x|
|
104
|
+
x == BYTEA_COLUMN_TYPE_OID || x == MONEY_COLUMN_TYPE_OID
|
105
|
+
}
|
106
|
+
|
107
|
+
typehash = ftypes.group_by { |_, type| type }
|
108
|
+
binaries = typehash[BYTEA_COLUMN_TYPE_OID] || []
|
109
|
+
monies = typehash[MONEY_COLUMN_TYPE_OID] || []
|
110
|
+
|
111
|
+
rows.each do |row|
|
112
|
+
# unescape string passed BYTEA field (OID == 17)
|
113
|
+
binaries.each do |index, _|
|
114
|
+
row[index] = unescape_bytea(row[index])
|
115
|
+
end
|
116
|
+
|
117
|
+
# If this is a money type column and there are any currency symbols,
|
118
|
+
# then strip them off. Indeed it would be prettier to do this in
|
119
|
+
# PostgreSQLColumn.string_to_decimal but would break form input
|
120
|
+
# fields that call value_before_type_cast.
|
121
|
+
monies.each do |index, _|
|
122
|
+
data = row[index]
|
123
|
+
# Because money output is formatted according to the locale, there are two
|
124
|
+
# cases to consider (note the decimal separators):
|
125
|
+
# (1) $12,345,678.12
|
126
|
+
# (2) $12.345.678,12
|
127
|
+
case data
|
128
|
+
when /^-?\D+[\d,]+\.\d{2}$/ # (1)
|
129
|
+
data.gsub!(/[^-\d.]/, '')
|
130
|
+
when /^-?\D+[\d.]+,\d{2}$/ # (2)
|
131
|
+
data.gsub!(/[^-\d,]/, '').sub!(/,/, '.')
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
# Queries the database and returns the results in an Array-like object
|
138
|
+
def query(sql, name = nil) #:nodoc:
|
139
|
+
log(sql, name) do
|
140
|
+
result_as_array @connection.async_exec(sql)
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Executes an SQL statement, returning a PG::Result object on success
|
145
|
+
# or raising a PG::Error exception otherwise.
|
146
|
+
def execute(sql, name = nil)
|
147
|
+
log(sql, name) do
|
148
|
+
@connection.async_exec(sql)
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
def exec_query(sql, name = 'SQL', binds = [], prepare: false)
|
153
|
+
execute_and_clear(sql, name, binds, prepare: prepare) do |result|
|
154
|
+
types = {}
|
155
|
+
fields = result.fields
|
156
|
+
fields.each_with_index do |fname, i|
|
157
|
+
ftype = result.ftype i
|
158
|
+
fmod = result.fmod i
|
159
|
+
types[fname] = get_oid_type(ftype, fmod, fname)
|
160
|
+
end
|
161
|
+
ActiveRecord::Result.new(fields, result.values, types)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def exec_delete(sql, name = 'SQL', binds = [])
|
166
|
+
execute_and_clear(sql, name, binds) {|result| result.cmd_tuples }
|
167
|
+
end
|
168
|
+
alias :exec_update :exec_delete
|
169
|
+
|
170
|
+
def sql_for_insert(sql, pk, id_value, sequence_name, binds)
|
171
|
+
if pk.nil?
|
172
|
+
# Extract the table from the insert sql. Yuck.
|
173
|
+
table_ref = extract_table_ref_from_insert_sql(sql)
|
174
|
+
pk = primary_key(table_ref) if table_ref
|
175
|
+
end
|
176
|
+
|
177
|
+
if pk && use_insert_returning?
|
178
|
+
sql = "#{sql} RETURNING #{quote_column_name(pk)}"
|
179
|
+
end
|
180
|
+
|
181
|
+
super
|
182
|
+
end
|
183
|
+
|
184
|
+
def exec_insert(sql, name, binds, pk = nil, sequence_name = nil)
|
185
|
+
val = exec_query(sql, name, binds)
|
186
|
+
if !use_insert_returning? && pk
|
187
|
+
unless sequence_name
|
188
|
+
table_ref = extract_table_ref_from_insert_sql(sql)
|
189
|
+
sequence_name = default_sequence_name(table_ref, pk)
|
190
|
+
return val unless sequence_name
|
191
|
+
end
|
192
|
+
last_insert_id_result(sequence_name)
|
193
|
+
else
|
194
|
+
val
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
# Begins a transaction.
|
199
|
+
def begin_db_transaction
|
200
|
+
execute "BEGIN"
|
201
|
+
end
|
202
|
+
|
203
|
+
def begin_isolated_db_transaction(isolation)
|
204
|
+
begin_db_transaction
|
205
|
+
execute "SET TRANSACTION ISOLATION LEVEL #{transaction_isolation_levels.fetch(isolation)}"
|
206
|
+
end
|
207
|
+
|
208
|
+
# Commits a transaction.
|
209
|
+
def commit_db_transaction
|
210
|
+
execute "COMMIT"
|
211
|
+
end
|
212
|
+
|
213
|
+
# Aborts a transaction.
|
214
|
+
def exec_rollback_db_transaction
|
215
|
+
execute "ROLLBACK"
|
216
|
+
end
|
217
|
+
end
|
218
|
+
end
|
219
|
+
end
|
220
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'active_record/connection_adapters/redshift/oid/date_time'
|
2
|
+
require 'active_record/connection_adapters/redshift/oid/decimal'
|
3
|
+
require 'active_record/connection_adapters/redshift/oid/json'
|
4
|
+
require 'active_record/connection_adapters/redshift/oid/jsonb'
|
5
|
+
|
6
|
+
require 'active_record/connection_adapters/redshift/oid/type_map_initializer'
|
7
|
+
|
8
|
+
module ActiveRecord
|
9
|
+
module ConnectionAdapters
|
10
|
+
module Redshift
|
11
|
+
module OID # :nodoc:
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|