relationizer 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f126a29bb0808131eb6cee18ebc837d3c2476236
4
+ data.tar.gz: c6ac6d1f8734ea4562743b36c264c7be1d37acbb
5
+ SHA512:
6
+ metadata.gz: 4e6324a4f3279f93f78fc50c4d1695b4a22031a32f903210720944a080aa5ad09f3c9ec7def7f099a42c339c01004d99c6f8b0e3c23b222263d60285067fbb52
7
+ data.tar.gz: 4df6ac38f1541305d2f7c1269f7f0e60c0d800785da8ab7a87fb21ffaba45750256f2d6b1ffa18aaf95efeba42bcddfdd59b91c4b4e48104e89dd1465febe9d6
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /*.gem
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in relationizer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Shinta Koyanagi
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,41 @@
1
+ # Relationizer
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/relationizer`. 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 'relationizer'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install relationizer
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. 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 tags, 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]/relationizer.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "relationizer"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ module Relationizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,8 @@
1
+ require "relationizer/version"
2
+ require_relative "./relationizer/postgresql.rb"
3
+ require_relative "./relationizer/big_query/Legacy.rb"
4
+ require_relative "./relationizer/big_query/Standard.rb"
5
+
6
+ module Relationizer
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,8 @@
1
+ module Relationizer
2
+ module BigQuery
3
+ module Legacy
4
+ def create_relation_literal(rows, schema)
5
+ end
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,129 @@
1
+ require 'bigdecimal'
2
+ require 'date'
3
+
4
+ module Relationizer
5
+ module BigQuery
6
+ module Standard
7
+ class ReasonlessTypeError < StandardError; end
8
+
9
+ DEFAULT_TYPES = {
10
+ Integer => :INT64,
11
+ Fixnum => :INT64,
12
+ Bignum => :INT64,
13
+ BigDecimal => :FLOAT64,
14
+ Float => :FLOAT64,
15
+ String => :STRING,
16
+ TrueClass => :BOOL,
17
+ FalseClass => :BOOL,
18
+ Date => :DATE,
19
+ Time => :TIMESTAMP,
20
+ DateTime => :TIMESTAMP,
21
+ Array => :ARRAY
22
+ }
23
+
24
+ def create_relation_literal(schema, tuples)
25
+ types = fixed_types(schema.values, tuples)
26
+
27
+ _types_exp = types_exp(schema.keys, types)
28
+
29
+ tuples_exp = tuples.map { |tuple|
30
+ tuple.zip(types).
31
+ map { |(col, type)| to_literal(col, type) }.
32
+ join(", ").
33
+ tap { |t| break "(#{t}#{', NULL' if tuple.one?})" }
34
+ }.join(", ").tap { |t| break "[#{t}]"}
35
+
36
+ select_exp = if schema.one?
37
+ "#{schema.keys.first}"
38
+ else
39
+ '*'
40
+ end
41
+
42
+ "SELECT #{select_exp} FROM UNNEST(#{_types_exp}#{tuples_exp})"
43
+ end
44
+
45
+ private
46
+
47
+ def array_type(array)
48
+ classes = array.compact.map(&:class).uniq
49
+
50
+ unless classes.length == 1
51
+ raise ReasonlessTypeError.new("Ambiguous type of element in array: #{classes}")
52
+ end
53
+
54
+ DEFAULT_TYPES[classes.first] || :STRING
55
+ end
56
+
57
+ def types_exp(names, types)
58
+ case names.length
59
+ when 1
60
+ %Q{ARRAY<STRUCT<#{names.first} #{types.first}, ___dummy STRING>>}
61
+ else
62
+ %Q{ARRAY<STRUCT<#{names.zip(types).map { |(name, type)| "#{name} #{type}" }.join(", ")}>>}
63
+ end
64
+ end
65
+
66
+ def many_candidate_check(types)
67
+ raise ReasonlessTypeError.new("Many candidate: #{types.join(', ')}") unless types.one?
68
+ end
69
+
70
+ def empty_candidate_check(types)
71
+ raise ReasonlessTypeError.new("Candidate nothing") if types.empty?
72
+ end
73
+
74
+ def fixed_types(schema, tuples)
75
+ tuples.transpose.zip(schema.to_a).map { |(values, (_, type))|
76
+ next type if type
77
+
78
+ if values.map { |o| o.is_a?(Array) }.all?
79
+ types = values.
80
+ map(&method(:array_type)).uniq.
81
+ tap(&method(:many_candidate_check)).
82
+ tap(&method(:empty_candidate_check))
83
+
84
+ next "ARRAY<#{types.first}>".to_sym
85
+ end
86
+
87
+ values.map(&:class).uniq.
88
+ map(&DEFAULT_TYPES).compact.uniq.
89
+ tap(&method(:many_candidate_check)).
90
+ tap(&method(:empty_candidate_check)).
91
+ first || :STRING
92
+ }
93
+ end
94
+
95
+ def to_literal(obj, type)
96
+ return "NULL" if obj.nil?
97
+
98
+ case type
99
+ when :ARRAY
100
+ t = array_type(obj)
101
+ obj.map { |e| to_literal(e, t) }.join(', ').tap { |s| break "[#{s}]"}
102
+ when /^ARRAY\<.+\>$/
103
+ t = /^ARRAY\<(.+)\>$/.match(type).to_a&.dig(1).to_sym
104
+ raise "Unknown type: #{t}" unless DEFAULT_TYPES.values.include?(t)
105
+ obj.map { |e| to_literal(e, t) }.join(', ').tap { |s| break "[#{s}]"}
106
+ when :TIMESTAMP
107
+ %Q{'#{obj.strftime('%Y-%m-%d %H:%M:%S')}'}
108
+ when :STRING, :DATE, :BOOL
109
+ obj.to_s.gsub(/'/, "\'").tap do |s|
110
+ break "'#{s}'"
111
+ end
112
+ when :FLOAT64
113
+ case obj
114
+ when Float::INFINITY
115
+ "CAST('inf' AS FLOAT64)"
116
+ when -Float::INFINITY
117
+ "CAST('-inf' AS FLOAT64)"
118
+ else
119
+ obj
120
+ end
121
+ when :INT64
122
+ obj.to_s
123
+ else
124
+ raise "Unknown type: #{type}"
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,73 @@
1
+ require 'bigdecimal'
2
+ require 'date'
3
+
4
+ module Relationizer
5
+ module Postgresql
6
+ class ReasonlessTypeError < StandardError; end
7
+
8
+ DEFAULT_TYPES = {
9
+ Integer => :INT8,
10
+ Fixnum => :INT8,
11
+ Bignum => :DECIMAL,
12
+ BigDecimal => :DECIMAL,
13
+ Float => :FLOAT8,
14
+ String => :TEXT,
15
+ TrueClass => :BOOLEAN,
16
+ FalseClass => :BOOLEAN,
17
+ Date => :DATE,
18
+ Time => :TIMESTAMPTZ
19
+ }
20
+
21
+ def create_relation_literal(schema, tuples)
22
+ _select_exp = select_exp(schema, tuples)
23
+
24
+ tuples_exp = tuples.map { |tuple|
25
+ tuple.
26
+ map(&method(:to_text_literal)).
27
+ join(", ").
28
+ tap { |t| break "(#{t})" }
29
+ }.join(", ")
30
+
31
+ schema_exp = schema.keys.map(&method(:identifer_quote)).join(", ")
32
+
33
+ "SELECT #{_select_exp} FROM (VALUES#{tuples_exp}) AS t(#{schema_exp})"
34
+ end
35
+
36
+ private
37
+
38
+ def select_exp(schema, tuples)
39
+ tuples.transpose.zip(schema.to_a).map { |(values, (name, type))|
40
+ next "#{name}::#{type.to_s.upcase}" if type
41
+
42
+ values.
43
+ map(&:class).uniq.
44
+ map(&DEFAULT_TYPES).compact.uniq.
45
+ tap(&method(:many_candidate_check)).
46
+ tap(&method(:empty_candidate_check)).
47
+ first.
48
+ to_s.upcase.
49
+ tap { |fixed_type| break "#{name}::#{fixed_type}" }
50
+ }.join(", ")
51
+ end
52
+
53
+ def many_candidate_check(types)
54
+ raise ReasonlessTypeError.new("Many candidate: #{types.join(', ')}") unless types.one?
55
+ end
56
+
57
+ def empty_candidate_check(types)
58
+ raise ReasonlessTypeError.new("Candidate nothing") if types.empty?
59
+ end
60
+
61
+ def identifer_quote(w)
62
+ %Q{"#{w.to_s.gsub(/"/, '""')}"}
63
+ end
64
+
65
+ def to_text_literal(obj)
66
+ return "NULL" if obj.nil?
67
+
68
+ obj.to_s.gsub(/'/, "''").tap do |s|
69
+ break "'#{s}'"
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module Relationizer
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,7 @@
1
+ require "relationizer/version"
2
+ require_relative "relationizer/postgresql"
3
+ require_relative "relationizer/big_query/standard"
4
+
5
+ module Relationizer
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'relationizer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "relationizer"
8
+ spec.version = Relationizer::VERSION
9
+ spec.authors = ["yancya"]
10
+ spec.email = ["yancya@upec.jp"]
11
+
12
+ spec.summary = %q{Array<Array> to Relation ppoi String}
13
+ spec.homepage = "https://github.com/yancya/relationizer"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
20
+ else
21
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
22
+ end
23
+
24
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ spec.bindir = "exe"
26
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
27
+ spec.require_paths = ["lib"]
28
+
29
+ spec.add_development_dependency "bundler", "~> 1.12"
30
+ spec.add_development_dependency "rake", "~> 10.0"
31
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: relationizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - yancya
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - yancya@upec.jp
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - bin/console
54
+ - bin/setup
55
+ - lib/rel_lib/relationizer.rb
56
+ - lib/rel_lib/relationizer/version.rb
57
+ - lib/relationizer.rb
58
+ - lib/relationizer/big_query/legacy.rb
59
+ - lib/relationizer/big_query/standard.rb
60
+ - lib/relationizer/postgresql.rb
61
+ - lib/relationizer/version.rb
62
+ - relationizer.gemspec
63
+ homepage: https://github.com/yancya/relationizer
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ allowed_push_host: https://rubygems.org
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.5.1
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Array<Array> to Relation ppoi String
88
+ test_files: []