arel_columns_hash 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +24 -0
- data/Rakefile +11 -0
- data/arel_columns_hash.gemspec +25 -0
- data/lib/arel_columns_hash.rb +3 -0
- data/lib/arel_columns_hash/to_sql.rb +19 -0
- data/lib/arel_columns_hash/version.rb +3 -0
- data/test/fake_record.rb +146 -0
- data/test/helper.rb +12 -0
- data/test/test_to_sql.rb +30 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: de6073fd78f8cf9c3fb162ea8cd631dc4045b762
|
4
|
+
data.tar.gz: a3a73edb61f78ec7b8ed900a682091ec8b4f9d71
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 32f6a65b9b3604c506b219273031f36e647b6c787387461f52c07b220bf1b029f9e0ca0426820659eaabb32383dfcc1e1d357e67a6e38e91233a032b65bf5c8f
|
7
|
+
data.tar.gz: 72339dfcca03956772721857731ef65ae68489bc2a69b33dee56c09ae13b0c2c5a4d3e1c8b43a36079d8036141a42c0188b99e967d5561958c5e349b6b8a664b
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Genki Sugawara
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# ArelColumnsHash
|
2
|
+
|
3
|
+
Monkey patch for fix Arel column_cache. (use AR model columns_hash)
|
4
|
+
|
5
|
+
see https://github.com/rails/arel/pull/358
|
6
|
+
|
7
|
+
[![Gem Version](https://badge.fury.io/rb/arel_columns_hash.svg)](http://badge.fury.io/rb/arel_columns_hash)
|
8
|
+
[![Build Status](https://travis-ci.org/winebarrel/arel_columns_hash.svg?branch=master)](https://travis-ci.org/winebarrel/arel_columns_hash)
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'arel_columns_hash'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install arel_columns_hash
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'arel_columns_hash/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "arel_columns_hash"
|
8
|
+
spec.version = ArelColumnsHash::VERSION
|
9
|
+
spec.authors = ["Genki Sugawara"]
|
10
|
+
spec.email = ["sgwr_dts@yahoo.co.jp"]
|
11
|
+
spec.summary = %q{Monkey patch for fix Arel column_cache.}
|
12
|
+
spec.description = %q{Monkey patch for fix Arel column_cache. (use AR model columns_hash)}
|
13
|
+
spec.homepage = "https://github.com/winebarrel/arel_columns_hash"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency "arel", "~> 5.0"
|
22
|
+
spec.add_development_dependency "bundler"
|
23
|
+
spec.add_development_dependency "rake"
|
24
|
+
spec.add_development_dependency "minitest", "~> 5.4"
|
25
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
class Arel::Visitors::ToSql
|
2
|
+
def column_for attr
|
3
|
+
return unless attr
|
4
|
+
name = attr.name.to_s
|
5
|
+
relation = attr.relation
|
6
|
+
|
7
|
+
if has_columns_hash? relation
|
8
|
+
relation.engine.columns_hash[name]
|
9
|
+
elsif table_exists? relation.table_name
|
10
|
+
column_cache(relation.table_name)[name]
|
11
|
+
else
|
12
|
+
nil
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def has_columns_hash?(relation)
|
17
|
+
relation.respond_to?(:engine) && relation.engine.respond_to?(:columns_hash)
|
18
|
+
end
|
19
|
+
end
|
data/test/fake_record.rb
ADDED
@@ -0,0 +1,146 @@
|
|
1
|
+
# https://github.com/rails/arel/blob/7b823819f7b1d71a23f884184df7c83a0d40de1b/test/support/fake_record.rb
|
2
|
+
|
3
|
+
# Copyright (c) 2007-2010 Nick Kallen, Bryan Helmkamp, Emilio Tagua, Aaron Patterson
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
# a copy of this software and associated documentation files (the
|
7
|
+
# "Software"), to deal in the Software without restriction, including
|
8
|
+
# without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
# distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
# permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
# the following conditions:
|
12
|
+
#
|
13
|
+
# The above copyright notice and this permission notice shall be
|
14
|
+
# included in all copies or substantial portions of the Software.
|
15
|
+
#
|
16
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
module FakeRecord
|
25
|
+
class Column < Struct.new(:name, :type)
|
26
|
+
end
|
27
|
+
|
28
|
+
class Connection
|
29
|
+
attr_reader :tables
|
30
|
+
attr_accessor :visitor
|
31
|
+
|
32
|
+
def initialize(visitor = nil)
|
33
|
+
@tables = %w{ users photos developers products}
|
34
|
+
@columns = {
|
35
|
+
'users' => [
|
36
|
+
Column.new('id', :integer),
|
37
|
+
Column.new('name', :string),
|
38
|
+
Column.new('bool', :boolean),
|
39
|
+
Column.new('created_at', :date)
|
40
|
+
],
|
41
|
+
'products' => [
|
42
|
+
Column.new('id', :integer),
|
43
|
+
Column.new('price', :decimal)
|
44
|
+
]
|
45
|
+
}
|
46
|
+
@columns_hash = {
|
47
|
+
'users' => Hash[@columns['users'].map { |x| [x.name, x] }],
|
48
|
+
'products' => Hash[@columns['products'].map { |x| [x.name, x] }]
|
49
|
+
}
|
50
|
+
@primary_keys = {
|
51
|
+
'users' => 'id',
|
52
|
+
'products' => 'id'
|
53
|
+
}
|
54
|
+
@visitor = visitor
|
55
|
+
end
|
56
|
+
|
57
|
+
def columns_hash table_name
|
58
|
+
@columns_hash[table_name]
|
59
|
+
end
|
60
|
+
|
61
|
+
def primary_key name
|
62
|
+
@primary_keys[name.to_s]
|
63
|
+
end
|
64
|
+
|
65
|
+
def table_exists? name
|
66
|
+
@tables.include? name.to_s
|
67
|
+
end
|
68
|
+
|
69
|
+
def columns name, message = nil
|
70
|
+
@columns[name.to_s]
|
71
|
+
end
|
72
|
+
|
73
|
+
def quote_table_name name
|
74
|
+
"\"#{name.to_s}\""
|
75
|
+
end
|
76
|
+
|
77
|
+
def quote_column_name name
|
78
|
+
"\"#{name.to_s}\""
|
79
|
+
end
|
80
|
+
|
81
|
+
def schema_cache
|
82
|
+
self
|
83
|
+
end
|
84
|
+
|
85
|
+
def quote thing, column = nil
|
86
|
+
if column && column.type == :integer
|
87
|
+
return 'NULL' if thing.nil?
|
88
|
+
return thing.to_i
|
89
|
+
end
|
90
|
+
|
91
|
+
case thing
|
92
|
+
when true
|
93
|
+
"'t'"
|
94
|
+
when false
|
95
|
+
"'f'"
|
96
|
+
when nil
|
97
|
+
'NULL'
|
98
|
+
when Numeric
|
99
|
+
thing
|
100
|
+
else
|
101
|
+
"'#{thing}'"
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
class ConnectionPool
|
107
|
+
class Spec < Struct.new(:config)
|
108
|
+
end
|
109
|
+
|
110
|
+
attr_reader :spec, :connection
|
111
|
+
|
112
|
+
def initialize
|
113
|
+
@spec = Spec.new(:adapter => 'america')
|
114
|
+
@connection = Connection.new
|
115
|
+
@connection.visitor = Arel::Visitors::ToSql.new(connection)
|
116
|
+
end
|
117
|
+
|
118
|
+
def with_connection
|
119
|
+
yield connection
|
120
|
+
end
|
121
|
+
|
122
|
+
def table_exists? name
|
123
|
+
connection.tables.include? name.to_s
|
124
|
+
end
|
125
|
+
|
126
|
+
def columns_hash
|
127
|
+
connection.columns_hash
|
128
|
+
end
|
129
|
+
|
130
|
+
def schema_cache
|
131
|
+
connection
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
class Base
|
136
|
+
attr_accessor :connection_pool
|
137
|
+
|
138
|
+
def initialize
|
139
|
+
@connection_pool = ConnectionPool.new
|
140
|
+
end
|
141
|
+
|
142
|
+
def connection
|
143
|
+
connection_pool.connection
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
data/test/helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "arel"
|
3
|
+
require "arel_columns_hash"
|
4
|
+
require "fake_record"
|
5
|
+
|
6
|
+
Arel::Table.engine = Arel::Sql::Engine.new(FakeRecord::Base.new)
|
7
|
+
|
8
|
+
class Object
|
9
|
+
def must_be_like other
|
10
|
+
gsub(/\s+/, ' ').strip.must_equal other.gsub(/\s+/, ' ').strip
|
11
|
+
end
|
12
|
+
end
|
data/test/test_to_sql.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "helper"
|
2
|
+
|
3
|
+
module Arel
|
4
|
+
module Visitors
|
5
|
+
describe 'the to_sql visitor' do
|
6
|
+
before do
|
7
|
+
@visitor = ToSql.new Table.engine.connection
|
8
|
+
@table = Table.new(:users)
|
9
|
+
@attr = @table[:id]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "uses columns_hash of engine" do
|
13
|
+
table = Class.new(Table) {
|
14
|
+
def engine
|
15
|
+
Class.new {
|
16
|
+
def columns_hash
|
17
|
+
{ 'active' => FakeRecord::Column.new('active', :integer) }
|
18
|
+
end
|
19
|
+
}.new
|
20
|
+
end
|
21
|
+
}.new(:users)
|
22
|
+
|
23
|
+
node = Nodes::NotEqual.new(table[:active], "1")
|
24
|
+
@visitor.accept(node).must_be_like %{
|
25
|
+
"users"."active" != 1
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arel_columns_hash
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Genki Sugawara
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: arel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.4'
|
69
|
+
description: Monkey patch for fix Arel column_cache. (use AR model columns_hash)
|
70
|
+
email:
|
71
|
+
- sgwr_dts@yahoo.co.jp
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .travis.yml
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- arel_columns_hash.gemspec
|
83
|
+
- lib/arel_columns_hash.rb
|
84
|
+
- lib/arel_columns_hash/to_sql.rb
|
85
|
+
- lib/arel_columns_hash/version.rb
|
86
|
+
- test/fake_record.rb
|
87
|
+
- test/helper.rb
|
88
|
+
- test/test_to_sql.rb
|
89
|
+
homepage: https://github.com/winebarrel/arel_columns_hash
|
90
|
+
licenses:
|
91
|
+
- MIT
|
92
|
+
metadata: {}
|
93
|
+
post_install_message:
|
94
|
+
rdoc_options: []
|
95
|
+
require_paths:
|
96
|
+
- lib
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - '>='
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 2.0.14
|
110
|
+
signing_key:
|
111
|
+
specification_version: 4
|
112
|
+
summary: Monkey patch for fix Arel column_cache.
|
113
|
+
test_files:
|
114
|
+
- test/fake_record.rb
|
115
|
+
- test/helper.rb
|
116
|
+
- test/test_to_sql.rb
|