activerecord-bigquery-adapter 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +95 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/activerecord_bigquery_adapter.gemspec +29 -0
- data/lib/active_record/connection_adapters/bigquery/database_statements.rb +46 -0
- data/lib/active_record/connection_adapters/bigquery/quoting.rb +30 -0
- data/lib/active_record/connection_adapters/bigquery/schema_statements.rb +41 -0
- data/lib/active_record/connection_adapters/bigquery_adapter.rb +154 -0
- data/lib/active_record/type/bigquery/array.rb +40 -0
- data/lib/active_record/type/bigquery/bigquery_active_record_converter.rb +23 -0
- data/lib/active_record/type/bigquery/bytes.rb +18 -0
- data/lib/active_record/type/bigquery/timestamp.rb +22 -0
- data/lib/activerecord_bigquery_adapter/base.rb +50 -0
- data/lib/activerecord_bigquery_adapter/connection.rb +53 -0
- data/lib/activerecord_bigquery_adapter/information_schema.rb +21 -0
- data/lib/activerecord_bigquery_adapter/version.rb +3 -0
- data/lib/activerecord_bigquery_adapter.rb +13 -0
- data/lib/arel/visitors/bigquery.rb +14 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 49da0eef052b651f2e6653de97962c1cbeb21144a47495646371efae29d8613e
|
4
|
+
data.tar.gz: eeda61573a0362e9d001736249c36c16634d235a64e0b21af6e10a82dc4852d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6f871d4778e512b7e8dc81f72c1de74592f5dd2d4c70d8ee0edd61a0f12ef5ea7357031f34c658baa7d552df550a79e10a9e641b8dc27a2bfe1d560fa90866f3
|
7
|
+
data.tar.gz: f164fc9f9f608c382b566bac9e270273815c069e3303bd5eb6f9e2d3aac9f1dd9ad0978cb6a6e1aae2e1ede74aef1c7dbd1dd159168e5806df0fc27929936282
|
data/.DS_Store
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at ollimacgja@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
activerecord-bigquery-adapter (1.1.1)
|
5
|
+
activerecord (>= 6.0.0, < 7.1)
|
6
|
+
google-cloud-bigquery (~> 1.38)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (7.0.3)
|
12
|
+
activesupport (= 7.0.3)
|
13
|
+
activerecord (7.0.3)
|
14
|
+
activemodel (= 7.0.3)
|
15
|
+
activesupport (= 7.0.3)
|
16
|
+
activesupport (7.0.3)
|
17
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
18
|
+
i18n (>= 1.6, < 2)
|
19
|
+
minitest (>= 5.1)
|
20
|
+
tzinfo (~> 2.0)
|
21
|
+
addressable (2.8.0)
|
22
|
+
public_suffix (>= 2.0.2, < 5.0)
|
23
|
+
concurrent-ruby (1.1.10)
|
24
|
+
declarative (0.0.20)
|
25
|
+
faraday (2.3.0)
|
26
|
+
faraday-net_http (~> 2.0)
|
27
|
+
ruby2_keywords (>= 0.0.4)
|
28
|
+
faraday-net_http (2.0.3)
|
29
|
+
google-apis-bigquery_v2 (0.33.0)
|
30
|
+
google-apis-core (>= 0.4, < 2.a)
|
31
|
+
google-apis-core (0.5.0)
|
32
|
+
addressable (~> 2.5, >= 2.5.1)
|
33
|
+
googleauth (>= 0.16.2, < 2.a)
|
34
|
+
httpclient (>= 2.8.1, < 3.a)
|
35
|
+
mini_mime (~> 1.0)
|
36
|
+
representable (~> 3.0)
|
37
|
+
retriable (>= 2.0, < 4.a)
|
38
|
+
rexml
|
39
|
+
webrick
|
40
|
+
google-cloud-bigquery (1.38.1)
|
41
|
+
concurrent-ruby (~> 1.0)
|
42
|
+
google-apis-bigquery_v2 (~> 0.1)
|
43
|
+
google-cloud-core (~> 1.6)
|
44
|
+
googleauth (>= 0.16.2, < 2.a)
|
45
|
+
mini_mime (~> 1.0)
|
46
|
+
google-cloud-core (1.6.0)
|
47
|
+
google-cloud-env (~> 1.0)
|
48
|
+
google-cloud-errors (~> 1.0)
|
49
|
+
google-cloud-env (1.6.0)
|
50
|
+
faraday (>= 0.17.3, < 3.0)
|
51
|
+
google-cloud-errors (1.2.0)
|
52
|
+
googleauth (1.1.3)
|
53
|
+
faraday (>= 0.17.3, < 3.a)
|
54
|
+
jwt (>= 1.4, < 3.0)
|
55
|
+
memoist (~> 0.16)
|
56
|
+
multi_json (~> 1.11)
|
57
|
+
os (>= 0.9, < 2.0)
|
58
|
+
signet (>= 0.16, < 2.a)
|
59
|
+
httpclient (2.8.3)
|
60
|
+
i18n (1.10.0)
|
61
|
+
concurrent-ruby (~> 1.0)
|
62
|
+
jwt (2.3.0)
|
63
|
+
memoist (0.16.2)
|
64
|
+
mini_mime (1.1.2)
|
65
|
+
minitest (5.15.0)
|
66
|
+
multi_json (1.15.0)
|
67
|
+
os (1.1.4)
|
68
|
+
public_suffix (4.0.7)
|
69
|
+
representable (3.2.0)
|
70
|
+
declarative (< 0.1.0)
|
71
|
+
trailblazer-option (>= 0.1.1, < 0.2.0)
|
72
|
+
uber (< 0.2.0)
|
73
|
+
retriable (3.1.2)
|
74
|
+
rexml (3.2.5)
|
75
|
+
ruby2_keywords (0.0.5)
|
76
|
+
signet (0.16.1)
|
77
|
+
addressable (~> 2.8)
|
78
|
+
faraday (>= 0.17.5, < 3.0)
|
79
|
+
jwt (>= 1.5, < 3.0)
|
80
|
+
multi_json (~> 1.10)
|
81
|
+
trailblazer-option (0.1.2)
|
82
|
+
tzinfo (2.0.4)
|
83
|
+
concurrent-ruby (~> 1.0)
|
84
|
+
uber (0.1.0)
|
85
|
+
webrick (1.7.0)
|
86
|
+
|
87
|
+
PLATFORMS
|
88
|
+
arm64-darwin-21
|
89
|
+
x86_64-darwin-21
|
90
|
+
|
91
|
+
DEPENDENCIES
|
92
|
+
activerecord-bigquery-adapter!
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
2.3.5
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2022 Ventrata
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
# ActiveRecordBigqueryAdapter
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/activerecord_bigquery_adapter`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'activerecord_bigquery_adapter'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install activerecord_bigquery_adapter
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/activerecord_bigquery_adapter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/activerecord_bigquery_adapter/blob/master/CODE_OF_CONDUCT.md).
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the ActiveRecordBigqueryAdapter project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/activerecord_bigquery_adapter/blob/master/CODE_OF_CONDUCT.md).
|
@@ -0,0 +1,29 @@
|
|
1
|
+
lib = File.expand_path 'lib', __dir__
|
2
|
+
$LOAD_PATH.unshift lib unless $LOAD_PATH.include? lib
|
3
|
+
require 'activerecord_bigquery_adapter/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'activerecord-bigquery-adapter'
|
7
|
+
spec.version = ActiveRecordBigqueryAdapter::VERSION
|
8
|
+
spec.authors = ['Ventrata']
|
9
|
+
spec.email = ['dev@ventrata.com']
|
10
|
+
|
11
|
+
spec.summary = %q{Rails ActiveRecord connector for BigQuery}
|
12
|
+
spec.description = %q{Rails ActiveRecord connector for BigQuery}
|
13
|
+
spec.homepage = 'https://github.com/ventrata/activerecord-bigquery-adapter'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
# Specify which files should be added to the gem when it is released.
|
17
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
18
|
+
spec.files = Dir.chdir File.expand_path(__dir__) do
|
19
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match %r{^(test|spec|features)/} }
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename f }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.required_ruby_version = '>= 1.38'
|
26
|
+
|
27
|
+
spec.add_dependency 'google-cloud-bigquery', '~> 1.38'
|
28
|
+
spec.add_runtime_dependency 'activerecord', ['>= 6.0.0', '< 7.1']
|
29
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Bigquery
|
4
|
+
module DatabaseStatements
|
5
|
+
|
6
|
+
def execute(sql, name = nil, binds = [])
|
7
|
+
log(sql, name) do
|
8
|
+
types, params = to_types_and_params(binds)
|
9
|
+
@connection.query(sql, params: params, types: types)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def exec_query(sql, name = 'SQL', binds = [], prepare: false)
|
14
|
+
result = execute(sql, name, binds)
|
15
|
+
column_types = result.param_types.map { |key, sql_type| [key.to_s, lookup_cast_type(sql_type.to_s)] }.to_h
|
16
|
+
rows = result.all.collect { |row| row.values_at(*result.headers) }
|
17
|
+
columns = result.headers.map(&:to_s)
|
18
|
+
ActiveRecord::Result.new(columns, rows, column_types)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# Translates binds to BigQuery types and params.
|
24
|
+
def to_types_and_params(binds)
|
25
|
+
types = binds.enum_for(:each_with_index).map do |bind, i|
|
26
|
+
type = :INT64
|
27
|
+
if bind.respond_to?(:type)
|
28
|
+
type = ActiveRecord::Type::Bigquery::BigqueryActiveRecordConverter
|
29
|
+
.convert_active_model_type_to_bigquery(bind.type)
|
30
|
+
end
|
31
|
+
["p#{i + 1}", type]
|
32
|
+
end.to_h
|
33
|
+
params = binds.enum_for(:each_with_index).map do |bind, i|
|
34
|
+
type = bind.respond_to?(:type) ? bind.type : ActiveModel::Type::Integer
|
35
|
+
value = bind
|
36
|
+
value = type.serialize bind.value, :dml if type.respond_to?(:serialize) && type.method(:serialize).arity < 0
|
37
|
+
value = type.serialize bind.value if type.respond_to?(:serialize) && type.method(:serialize).arity >= 0
|
38
|
+
|
39
|
+
["p#{i + 1}", value]
|
40
|
+
end.to_h
|
41
|
+
[types, params]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Bigquery
|
4
|
+
module Quoting
|
5
|
+
def quote_column_name(name)
|
6
|
+
self.class.quoted_column_names[name] ||= "`#{super.gsub '`', '``'}`".freeze
|
7
|
+
end
|
8
|
+
|
9
|
+
def quote_table_name(name)
|
10
|
+
self.class.quoted_table_names[name] ||= super.gsub(".", "`.`").freeze
|
11
|
+
end
|
12
|
+
|
13
|
+
STR_ESCAPE_REGX = /[\n\r'\\]/.freeze
|
14
|
+
STR_ESCAPE_VALUES = {
|
15
|
+
"\n" => "\\n", "\r" => "\\r", "'" => "\\'", "\\" => "\\\\"
|
16
|
+
}.freeze
|
17
|
+
|
18
|
+
private_constant :STR_ESCAPE_REGX, :STR_ESCAPE_VALUES
|
19
|
+
|
20
|
+
def quote_string(s)
|
21
|
+
s.gsub(STR_ESCAPE_REGX, STR_ESCAPE_VALUES)
|
22
|
+
end
|
23
|
+
|
24
|
+
def quoted_binary(value)
|
25
|
+
"b'#{value}'"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Bigquery
|
4
|
+
module SchemaStatements
|
5
|
+
def tables
|
6
|
+
information_schema.table_names
|
7
|
+
end
|
8
|
+
|
9
|
+
def views
|
10
|
+
information_schema.view_names
|
11
|
+
end
|
12
|
+
|
13
|
+
def indexes(table_name)
|
14
|
+
[]
|
15
|
+
end
|
16
|
+
|
17
|
+
def column_definitions(table_name)
|
18
|
+
information_schema.table_fields(table_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
def new_column_from_field(table_name, field)
|
22
|
+
ConnectionAdapters::Column.new(field.name, nil, fetch_type_metadata(field.type), field.mode == 'NULLABLE')
|
23
|
+
end
|
24
|
+
|
25
|
+
def primary_keys(table_name)
|
26
|
+
[]
|
27
|
+
end
|
28
|
+
|
29
|
+
def internal_string_options_for_primary_key
|
30
|
+
{}
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def information_schema
|
36
|
+
ActiveRecordBigqueryAdapter::Connection.information_schema(@config)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,154 @@
|
|
1
|
+
require 'google/cloud/bigquery'
|
2
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
3
|
+
require 'active_record/connection_adapters/abstract/connection_pool'
|
4
|
+
require 'active_record/connection_adapters/bigquery/database_statements'
|
5
|
+
require 'active_record/connection_adapters/bigquery/schema_statements'
|
6
|
+
require 'active_record/connection_adapters/bigquery/quoting'
|
7
|
+
require 'active_record/type/bigquery/array'
|
8
|
+
require 'active_record/type/bigquery/bytes'
|
9
|
+
require 'active_record/type/bigquery/bigquery_active_record_converter'
|
10
|
+
require 'active_record/type/bigquery/timestamp'
|
11
|
+
require 'activerecord_bigquery_adapter/connection'
|
12
|
+
require 'activerecord_bigquery_adapter/information_schema'
|
13
|
+
require 'activerecord_bigquery_adapter/base'
|
14
|
+
|
15
|
+
module ActiveRecord
|
16
|
+
module ConnectionHandling # :nodoc:
|
17
|
+
def bigquery_connection(config)
|
18
|
+
connection = ActiveRecordBigqueryAdapter::Connection.new(config)
|
19
|
+
ConnectionAdapters::BigqueryAdapter.new(connection, logger, config)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module ConnectionAdapters
|
24
|
+
module AbstractPool
|
25
|
+
def get_schema_cache(connection)
|
26
|
+
self.schema_cache ||= SchemaCache.new(connection)
|
27
|
+
schema_cache.connection = connection
|
28
|
+
schema_cache
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class BigqueryAdapter < AbstractAdapter
|
33
|
+
ADAPTER_NAME = 'bigquery'.freeze
|
34
|
+
NATIVE_DATABASE_TYPES = {
|
35
|
+
primary_key: 'INT64',
|
36
|
+
parent_key: 'INT64',
|
37
|
+
string: { name: 'STRING' },
|
38
|
+
text: { name: 'STRING' },
|
39
|
+
integer: { name: 'INT64' },
|
40
|
+
bigint: { name: 'INT64' },
|
41
|
+
float: { name: 'FLOAT64' },
|
42
|
+
decimal: { name: 'NUMERIC' },
|
43
|
+
numeric: { name: 'NUMERIC' },
|
44
|
+
datetime: { name: 'TIMESTAMP' },
|
45
|
+
time: { name: 'TIME' },
|
46
|
+
date: { name: 'DATE' },
|
47
|
+
binary: { name: 'BYTES' },
|
48
|
+
boolean: { name: 'BOOL' },
|
49
|
+
json: { name: 'JSON' }
|
50
|
+
}.freeze
|
51
|
+
|
52
|
+
include Bigquery::Quoting
|
53
|
+
include Bigquery::DatabaseStatements
|
54
|
+
include Bigquery::SchemaStatements
|
55
|
+
|
56
|
+
def max_identifier_length
|
57
|
+
128
|
58
|
+
end
|
59
|
+
|
60
|
+
def native_database_types
|
61
|
+
NATIVE_DATABASE_TYPES
|
62
|
+
end
|
63
|
+
|
64
|
+
def active?
|
65
|
+
true
|
66
|
+
end
|
67
|
+
|
68
|
+
def reconnect!
|
69
|
+
@connection = Base.bigquery_connection(@config)
|
70
|
+
super
|
71
|
+
end
|
72
|
+
alias reset! reconnect!
|
73
|
+
|
74
|
+
def disconnect!
|
75
|
+
false
|
76
|
+
end
|
77
|
+
|
78
|
+
def arel_visitor
|
79
|
+
Arel::Visitors::Bigquery.new(self)
|
80
|
+
end
|
81
|
+
|
82
|
+
def prepared_statements
|
83
|
+
false
|
84
|
+
end
|
85
|
+
alias prepared_statements? prepared_statements
|
86
|
+
|
87
|
+
def insert_async(table_name, values)
|
88
|
+
@connection.insert_async(table_name, values)
|
89
|
+
end
|
90
|
+
|
91
|
+
module TypeMapBuilder
|
92
|
+
private
|
93
|
+
|
94
|
+
def initialize_type_map(m = type_map)
|
95
|
+
m.register_type 'BOOL', Type::Boolean.new
|
96
|
+
m.alias_type 'BOOLEAN', 'BOOL'
|
97
|
+
register_class_with_limit(
|
98
|
+
m, %r{^BYTES}i, ActiveRecord::Type::Bigquery::Bytes
|
99
|
+
)
|
100
|
+
m.register_type 'DATE', Type::Date.new
|
101
|
+
m.register_type 'FLOAT64', Type::Float.new
|
102
|
+
m.alias_type 'FLOAT', 'FLOAT64'
|
103
|
+
m.register_type 'NUMERIC', Type::Decimal.new
|
104
|
+
m.register_type 'INT64', Type::Integer.new(limit: 8)
|
105
|
+
m.alias_type 'INTEGER', 'INT64'
|
106
|
+
register_class_with_limit m, %r{^STRING}i, Type::String
|
107
|
+
m.register_type 'TIMESTAMP', ActiveRecord::Type::Bigquery::Timestamp.new
|
108
|
+
m.register_type 'TIME', ActiveRecord::Type::Time.new
|
109
|
+
m.register_type 'DATE', ActiveRecord::Type::Date.new
|
110
|
+
m.register_type 'JSON', ActiveRecord::Type::Json.new
|
111
|
+
|
112
|
+
register_array_types m
|
113
|
+
end
|
114
|
+
|
115
|
+
def register_array_types(m)
|
116
|
+
m.register_type %r{^ARRAY<BOOL>}i, Type::Bigquery::Array.new(Type::Boolean.new)
|
117
|
+
m.register_type %r{^ARRAY<BYTES\(\d+\)>}i,
|
118
|
+
Type::Bigquery::Array.new(ActiveRecord::Type::Bigquery::Bytes.new)
|
119
|
+
m.register_type %r{^ARRAY<DATE>}i, Type::Bigquery::Array.new(Type::Date.new)
|
120
|
+
m.register_type %r{^ARRAY<FLOAT64>}i, Type::Bigquery::Array.new(Type::Float.new)
|
121
|
+
m.register_type %r{^ARRAY<NUMERIC>}i, Type::Bigquery::Array.new(Type::Decimal.new)
|
122
|
+
m.register_type %r{^ARRAY<INT64>}i, Type::Bigquery::Array.new(Type::Integer.new(limit: 8))
|
123
|
+
m.register_type %r{^ARRAY<STRING\(\d+\)>}i, Type::Bigquery::Array.new(Type::String.new)
|
124
|
+
m.register_type %r{^ARRAY<TIMESTAMP>}i, Type::Bigquery::Array.new(ActiveRecord::Type::Bigquery::Timestamp.new)
|
125
|
+
m.register_type %r{^ARRAY<TIME>}i, Type::Bigquery::Array.new(ActiveRecord::Type::Time.new)
|
126
|
+
m.register_type %r{^ARRAY<DATE>}i, Type::Bigquery::Array.new(ActiveRecord::Type::Date.new)
|
127
|
+
m.register_type %r{^ARRAY<JSON>}i, Type::Bigquery::Array.new(ActiveRecord::Type::Json.new)
|
128
|
+
end
|
129
|
+
|
130
|
+
def extract_limit(sql_type)
|
131
|
+
value = /\(\d+\)/.match(sql_type)
|
132
|
+
return unless value
|
133
|
+
value[1].to_i
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
if ActiveRecord::VERSION::MAJOR >= 7
|
138
|
+
class << self
|
139
|
+
include TypeMapBuilder
|
140
|
+
end
|
141
|
+
|
142
|
+
TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map m }
|
143
|
+
|
144
|
+
private
|
145
|
+
|
146
|
+
def type_map
|
147
|
+
TYPE_MAP
|
148
|
+
end
|
149
|
+
else
|
150
|
+
include TypeMapBuilder
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
module Bigquery
|
4
|
+
class Array < Type::Value
|
5
|
+
attr_reader :element_type
|
6
|
+
delegate :type, :user_input_in_time_zone, :limit, :precision, :scale, to: :element_type
|
7
|
+
|
8
|
+
def initialize element_type
|
9
|
+
@element_type = element_type
|
10
|
+
end
|
11
|
+
|
12
|
+
def cast value
|
13
|
+
return super if value.nil?
|
14
|
+
return super unless value.respond_to? :map
|
15
|
+
|
16
|
+
value.map do |v|
|
17
|
+
@element_type.cast v
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def serialize value
|
22
|
+
return super if value.nil?
|
23
|
+
return super unless value.respond_to? :map
|
24
|
+
|
25
|
+
if @element_type.is_a? ActiveRecord::Type::Decimal
|
26
|
+
# Convert a decimal (NUMERIC) array to a String array to prevent it from being encoded as a FLOAT64 array.
|
27
|
+
value.map do |v|
|
28
|
+
next if v.nil?
|
29
|
+
v.to_s
|
30
|
+
end
|
31
|
+
else
|
32
|
+
value.map do |v|
|
33
|
+
@element_type.serialize v
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
module Bigquery
|
4
|
+
class BigqueryActiveRecordConverter
|
5
|
+
def self.convert_active_model_type_to_bigquery(type)
|
6
|
+
case type
|
7
|
+
when NilClass then nil
|
8
|
+
when ActiveModel::Type::Integer, ActiveModel::Type::BigInteger then :INT64
|
9
|
+
when ActiveModel::Type::Boolean then :BOOL
|
10
|
+
when ActiveModel::Type::String, ActiveModel::Type::ImmutableString then :STRING
|
11
|
+
when ActiveModel::Type::Binary, ActiveRecord::Type::Bigquery::Bytes then :BYTES
|
12
|
+
when ActiveModel::Type::Float then :FLOAT64
|
13
|
+
when ActiveModel::Type::Decimal then :NUMERIC
|
14
|
+
when ActiveModel::Type::DateTime, ActiveModel::Type::Time, ActiveRecord::Type::Bigquery::Timestamp then :TIMESTAMP
|
15
|
+
when ActiveModel::Type::Date then :DATE
|
16
|
+
when ActiveRecord::Type::Json then :JSON
|
17
|
+
when ActiveRecord::Type::Bigquery::Array then [convert_active_model_type_to_bigquery(type.element_type)]
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
module Bigquery
|
4
|
+
class Bytes < ActiveRecord::Type::Binary
|
5
|
+
def serialize(value)
|
6
|
+
return super value if value.nil?
|
7
|
+
|
8
|
+
if value.respond_to?(:read) && value.respond_to?(:rewind)
|
9
|
+
value.rewind
|
10
|
+
value = value.read
|
11
|
+
end
|
12
|
+
|
13
|
+
Base64.strict_encode64 value.force_encoding('ASCII-8BIT')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module Type
|
3
|
+
module Bigquery
|
4
|
+
class Timestamp < ActiveRecord::Type::DateTime
|
5
|
+
def serialize(value)
|
6
|
+
val = super(value)
|
7
|
+
val.acts_like?(:time) ? val.utc.rfc3339(9) : val
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def cast_value(value)
|
13
|
+
if value.is_a?(::String)
|
14
|
+
value = value.empty? ? nil : ::Time.parse(value)
|
15
|
+
end
|
16
|
+
|
17
|
+
value
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
class TableMetadata # :nodoc:
|
3
|
+
# This attr_reader is private in ActiveRecord 6.0.x and public in 6.1.x. This makes sure it is always available in
|
4
|
+
# the Spanner adapter.
|
5
|
+
attr_reader :arel_table
|
6
|
+
end
|
7
|
+
|
8
|
+
module ActiveRecordBigqueryAdapterBaseExtension
|
9
|
+
extend ActiveSupport::Concern
|
10
|
+
|
11
|
+
class_methods do
|
12
|
+
def bigquery_adapter?
|
13
|
+
connection.adapter_name == 'bigquery'
|
14
|
+
end
|
15
|
+
|
16
|
+
def _insert_record(values)
|
17
|
+
return super unless bigquery_adapter?
|
18
|
+
|
19
|
+
primary_key = self.primary_key
|
20
|
+
primary_key_value = nil
|
21
|
+
|
22
|
+
if primary_key && values.is_a?(Hash)
|
23
|
+
primary_key_value = values[primary_key]
|
24
|
+
end
|
25
|
+
|
26
|
+
metadata = TableMetadata.new(self, arel_table)
|
27
|
+
connection.insert_async(arel_table.name, values_for_insert(metadata, values))
|
28
|
+
primary_key_value
|
29
|
+
end
|
30
|
+
|
31
|
+
def values_for_insert(metadata, values)
|
32
|
+
values.collect do |key, value|
|
33
|
+
unwrapped_value = unwrap_attribute(value)
|
34
|
+
type = metadata.type(key)
|
35
|
+
[metadata.arel_table[key].name, type.serialize(unwrapped_value)]
|
36
|
+
end.to_h
|
37
|
+
end
|
38
|
+
|
39
|
+
def unwrap_attribute(attr_or_value)
|
40
|
+
if attr_or_value.is_a?(ActiveModel::Attribute)
|
41
|
+
attr_or_value.value
|
42
|
+
else
|
43
|
+
attr_or_value
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
Base.prepend(ActiveRecordBigqueryAdapterBaseExtension)
|
50
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'google/cloud/bigquery'
|
2
|
+
require 'arel/visitors/bigquery'
|
3
|
+
require 'activerecord_bigquery_adapter/information_schema'
|
4
|
+
|
5
|
+
module ActiveRecordBigqueryAdapter
|
6
|
+
class Connection
|
7
|
+
attr_reader :dataset, :config
|
8
|
+
|
9
|
+
class << self
|
10
|
+
def datasets(config)
|
11
|
+
config = config.symbolize_keys
|
12
|
+
@datasets ||= {}
|
13
|
+
@mutex ||= Mutex.new
|
14
|
+
@mutex.synchronize do
|
15
|
+
@datasets[database_path(config)] ||= Google::Cloud::Bigquery.new(project_id: config[:project]).dataset(config[:dataset])
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def async_inserter(config, table_name)
|
20
|
+
@async_inserter ||= {}
|
21
|
+
@async_inserter["#{database_path(config)}/#{table_name}"] ||= datasets(config).table(table_name).insert_async do |result|
|
22
|
+
if result.error?
|
23
|
+
Google::Apis.logger.error(result.error)
|
24
|
+
else
|
25
|
+
Google::Apis.logger.info("inserted #{result.insert_count} rows with #{result.error_count} errors")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def information_schema(config)
|
31
|
+
@information_schemas ||= {}
|
32
|
+
@information_schemas[database_path(config)] ||= ActiveRecordBigqueryAdapter::InformationSchema.new(datasets(config))
|
33
|
+
end
|
34
|
+
|
35
|
+
def database_path(config)
|
36
|
+
"#{config[:project]}/#{config[:dataset]}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize(config)
|
41
|
+
@dataset = self.class.datasets(config)
|
42
|
+
@config = config
|
43
|
+
end
|
44
|
+
|
45
|
+
def insert_async(table_name, values)
|
46
|
+
self.class.async_inserter(config, table_name).insert(Array.wrap(values))
|
47
|
+
end
|
48
|
+
|
49
|
+
def query(sql, params: nil, types: nil)
|
50
|
+
dataset.query(sql, params: params, types: types)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveRecordBigqueryAdapter
|
2
|
+
class InformationSchema
|
3
|
+
attr_reader :dataset
|
4
|
+
|
5
|
+
def initialize(dataset)
|
6
|
+
@dataset = dataset
|
7
|
+
end
|
8
|
+
|
9
|
+
def table_names
|
10
|
+
dataset.tables.select(&:table?).map(&:table_id)
|
11
|
+
end
|
12
|
+
|
13
|
+
def view_names
|
14
|
+
dataset.tables.select(&:view?).map(&:table_id)
|
15
|
+
end
|
16
|
+
|
17
|
+
def table_fields(table_name)
|
18
|
+
dataset.table(table_name).fields
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'activerecord_bigquery_adapter/version'
|
2
|
+
|
3
|
+
if defined?(Rails)
|
4
|
+
module ActiveRecord
|
5
|
+
module ConnectionAdapters
|
6
|
+
class BigqueryRailtie < ::Rails::Railtie
|
7
|
+
ActiveSupport.on_load :active_record do
|
8
|
+
require 'active_record/connection_adapters/bigquery_adapter'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: activerecord-bigquery-adapter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ventrata
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-05-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: google-cloud-bigquery
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.38'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.38'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activerecord
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 6.0.0
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '7.1'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 6.0.0
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '7.1'
|
47
|
+
description: Rails ActiveRecord connector for BigQuery
|
48
|
+
email:
|
49
|
+
- dev@ventrata.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- ".DS_Store"
|
55
|
+
- ".gitignore"
|
56
|
+
- ".rspec"
|
57
|
+
- ".rubocop.yml"
|
58
|
+
- CHANGELOG.md
|
59
|
+
- CODE_OF_CONDUCT.md
|
60
|
+
- Gemfile
|
61
|
+
- Gemfile.lock
|
62
|
+
- LICENSE.txt
|
63
|
+
- README.md
|
64
|
+
- activerecord_bigquery_adapter.gemspec
|
65
|
+
- lib/active_record/connection_adapters/bigquery/database_statements.rb
|
66
|
+
- lib/active_record/connection_adapters/bigquery/quoting.rb
|
67
|
+
- lib/active_record/connection_adapters/bigquery/schema_statements.rb
|
68
|
+
- lib/active_record/connection_adapters/bigquery_adapter.rb
|
69
|
+
- lib/active_record/type/bigquery/array.rb
|
70
|
+
- lib/active_record/type/bigquery/bigquery_active_record_converter.rb
|
71
|
+
- lib/active_record/type/bigquery/bytes.rb
|
72
|
+
- lib/active_record/type/bigquery/timestamp.rb
|
73
|
+
- lib/activerecord_bigquery_adapter.rb
|
74
|
+
- lib/activerecord_bigquery_adapter/base.rb
|
75
|
+
- lib/activerecord_bigquery_adapter/connection.rb
|
76
|
+
- lib/activerecord_bigquery_adapter/information_schema.rb
|
77
|
+
- lib/activerecord_bigquery_adapter/version.rb
|
78
|
+
- lib/arel/visitors/bigquery.rb
|
79
|
+
homepage: https://github.com/ventrata/activerecord-bigquery-adapter
|
80
|
+
licenses:
|
81
|
+
- MIT
|
82
|
+
metadata: {}
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '1.38'
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
requirements: []
|
98
|
+
rubygems_version: 3.2.15
|
99
|
+
signing_key:
|
100
|
+
specification_version: 4
|
101
|
+
summary: Rails ActiveRecord connector for BigQuery
|
102
|
+
test_files: []
|