sequel_replace_select_with_alias 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +1 -0
- data/Gemfile +3 -0
- data/LICENSE +19 -0
- data/README.md +24 -0
- data/Rakefile +5 -0
- data/lib/sequel_replace_select_with_alias.rb +35 -0
- data/sequel_replace_select_with_alias.gemspec +22 -0
- data/spec/sequel_replace_select_with_alias_spec.rb +32 -0
- data/spec/spec_helper.rb +4 -0
- metadata +90 -0
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2014 Michael Nussbaum
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to
|
5
|
+
deal in the Software without restriction, including without limitation the
|
6
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
7
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
16
|
+
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
17
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
18
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
19
|
+
|
data/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This gem adds the `Sequel::Dataset#replace_select_with_alias` method. The
|
2
|
+
method replaces selected columns from a dataset with aliases for the same
|
3
|
+
column names. It preserves the order in which columns are already selected.
|
4
|
+
|
5
|
+
If the aliased column isn't already selected in the dataset then the method
|
6
|
+
returns the existing dataset unchanged.
|
7
|
+
|
8
|
+
This functionality is particularly useful when building up a `SELECT` statement
|
9
|
+
as the input for an `INSERT` when some columns need to be transformed (i.e.
|
10
|
+
replaced with an alias) before they are inserted.
|
11
|
+
|
12
|
+
Example usages:
|
13
|
+
|
14
|
+
DB[:items].select(:a, :b).replace_select_with_alias(Sequel.as("1", :a))
|
15
|
+
# SELECT '1' AS a, b FROM items
|
16
|
+
|
17
|
+
DB[:items].replace_select_with_alias(Sequel.as("1", :a))
|
18
|
+
# SELECT * FROM items
|
19
|
+
|
20
|
+
DB[:items].select(:a).replace_select_with_alias(Sequel.as("1", :b))
|
21
|
+
# SELECT a FROM items
|
22
|
+
|
23
|
+
DB[:items].select(:a, :b).replace_select_with_alias { |o| Sequel.as("1", o.a) }
|
24
|
+
# SELECT '1' AS a, b FROM items
|
data/Rakefile
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "sequel"
|
2
|
+
|
3
|
+
module Sequel
|
4
|
+
class Dataset
|
5
|
+
# Returns a copy of the dataset with the select statements
|
6
|
+
# for the given aliased columns replacing the original selects.
|
7
|
+
# If no aliases are given, it will return the existing selection.
|
8
|
+
# If no columns are currently selected, it will select *.
|
9
|
+
def replace_select_with_alias(*columns, &block)
|
10
|
+
virtual_row_columns(columns, block)
|
11
|
+
aliased_columns = _aliased_columns(columns)
|
12
|
+
return self if !@opts[:select] || (@opts[:select] & aliased_columns.keys).empty?
|
13
|
+
|
14
|
+
select(*_replace_aliases(@opts[:select], aliased_columns))
|
15
|
+
end
|
16
|
+
|
17
|
+
def _aliased_columns(columns)
|
18
|
+
columns.reduce({}) do |aliased_columns, column|
|
19
|
+
case column.alias
|
20
|
+
when nil
|
21
|
+
when Sequel::SQL::Identifier
|
22
|
+
aliased_columns[column.alias.value.to_sym] = column
|
23
|
+
else
|
24
|
+
aliased_columns[column.alias] = column
|
25
|
+
end
|
26
|
+
|
27
|
+
aliased_columns
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def _replace_aliases(selects, aliased_columns)
|
32
|
+
selects.map { |select| aliased_columns[select] || select }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "sequel_replace_select_with_alias"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["mnussbaum"]
|
9
|
+
s.email = ["michaelnussbaum08@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/mnussbaum/sequel_replace_select_with_alias"
|
11
|
+
s.summary = %q{Mutate sequel datasets to replace selected columns with aliases}
|
12
|
+
s.description = %q{This gem allows you to replace a column selection in a dataset with an alias for the same column name.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "sequel_replace_select_with_alias"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency "sequel", "~> 4.8.0"
|
21
|
+
s.add_development_dependency "rspec", "~> 2.14.1"
|
22
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "Dataset#replace_select_with_alias" do
|
4
|
+
before do
|
5
|
+
@dataset = Sequel.mock.from(:test)
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should do nothing if aliased columns are not present" do
|
9
|
+
replaced_sql = @dataset.select(:a).replace_select_with_alias(Sequel.as("5", :b)).sql
|
10
|
+
expect(replaced_sql).to eq("SELECT a FROM test")
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should select all if no select are present" do
|
14
|
+
replaced_sql = @dataset.replace_select_with_alias(Sequel.as("1", :a)).sql
|
15
|
+
expect(replaced_sql).to eq("SELECT * FROM test")
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should replace the currently selected columns with matching alias" do
|
19
|
+
replaced_sql = @dataset.select(:a, :b).replace_select_with_alias(Sequel.as("1", :a), Sequel.as("2", :b)).sql
|
20
|
+
expect(replaced_sql).to eq("SELECT '1' AS a, '2' AS b FROM test")
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should leave unaliased columns untouched" do
|
24
|
+
replaced_sql = @dataset.select(:a, :b).replace_select_with_alias(Sequel.as("1", :a)).sql
|
25
|
+
expect(replaced_sql).to eq("SELECT '1' AS a, b FROM test")
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should accept a block that yields a virtual row" do
|
29
|
+
replaced_sql = @dataset.select(:a, :b).replace_select_with_alias { |o| Sequel.as("1", o.a) }.sql
|
30
|
+
expect(replaced_sql).to eq("SELECT '1' AS a, b FROM test")
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_replace_select_with_alias
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- mnussbaum
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: sequel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.8.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.8.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.14.1
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.14.1
|
46
|
+
description: This gem allows you to replace a column selection in a dataset with an
|
47
|
+
alias for the same column name.
|
48
|
+
email:
|
49
|
+
- michaelnussbaum08@gmail.com
|
50
|
+
executables: []
|
51
|
+
extensions: []
|
52
|
+
extra_rdoc_files: []
|
53
|
+
files:
|
54
|
+
- .gitignore
|
55
|
+
- .rspec
|
56
|
+
- Gemfile
|
57
|
+
- LICENSE
|
58
|
+
- README.md
|
59
|
+
- Rakefile
|
60
|
+
- lib/sequel_replace_select_with_alias.rb
|
61
|
+
- sequel_replace_select_with_alias.gemspec
|
62
|
+
- spec/sequel_replace_select_with_alias_spec.rb
|
63
|
+
- spec/spec_helper.rb
|
64
|
+
homepage: http://github.com/mnussbaum/sequel_replace_select_with_alias
|
65
|
+
licenses: []
|
66
|
+
post_install_message:
|
67
|
+
rdoc_options: []
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ! '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ! '>='
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
requirements: []
|
83
|
+
rubyforge_project: sequel_replace_select_with_alias
|
84
|
+
rubygems_version: 1.8.23
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: Mutate sequel datasets to replace selected columns with aliases
|
88
|
+
test_files:
|
89
|
+
- spec/sequel_replace_select_with_alias_spec.rb
|
90
|
+
- spec/spec_helper.rb
|