sequel_fast_columns 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +27 -0
- data/Rakefile +45 -0
- data/VERSION +1 -0
- data/lib/sequel/fast_columns.rb +64 -0
- data/lib/sequel_fast_columns.rb +1 -0
- data/spec/sequel_fast_columns_spec.rb +70 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +25 -0
- metadata +87 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Roland Swingler
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
= Sequel fast columns
|
2
|
+
|
3
|
+
Sequel's Dataset#columns method runs the dataset's query method and uses the
|
4
|
+
keys from the first row as the column names. This can be very slow for some
|
5
|
+
queries. This extension gets the columns from those specified in the query
|
6
|
+
wherever possible, avoiding this performance hit.
|
7
|
+
|
8
|
+
To use just:
|
9
|
+
|
10
|
+
require 'sequel'
|
11
|
+
require 'sequel/fast_columns'
|
12
|
+
|
13
|
+
and carry on as normal.
|
14
|
+
|
15
|
+
== Note on Patches/Pull Requests
|
16
|
+
|
17
|
+
* Fork the project.
|
18
|
+
* Make your feature addition or bug fix.
|
19
|
+
* Add tests for it. This is important so I don't break it in a
|
20
|
+
future version unintentionally.
|
21
|
+
* Commit, do not mess with rakefile, version, or history.
|
22
|
+
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
|
23
|
+
* Send me a pull request. Bonus points for topic branches.
|
24
|
+
|
25
|
+
== Copyright
|
26
|
+
|
27
|
+
Copyright (c) 2010 Roland Swingler. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "sequel_fast_columns"
|
8
|
+
gem.summary = %Q{A non-db-query implementation of Dataset#columns}
|
9
|
+
gem.description = %Q{A non-db-query implementation of Dataset#columns}
|
10
|
+
gem.email = "roland.swingler@gmail.com"
|
11
|
+
gem.homepage = "http://github.com/knaveofdiamonds/sequel_fast_columns"
|
12
|
+
gem.authors = ["Roland Swingler"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
15
|
+
end
|
16
|
+
Jeweler::GemcutterTasks.new
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
19
|
+
end
|
20
|
+
|
21
|
+
require 'spec/rake/spectask'
|
22
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
23
|
+
spec.libs << 'lib' << 'spec'
|
24
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
25
|
+
end
|
26
|
+
|
27
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
28
|
+
spec.libs << 'lib' << 'spec'
|
29
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
30
|
+
spec.rcov = true
|
31
|
+
end
|
32
|
+
|
33
|
+
task :spec => :check_dependencies
|
34
|
+
|
35
|
+
task :default => :spec
|
36
|
+
|
37
|
+
require 'rake/rdoctask'
|
38
|
+
Rake::RDocTask.new do |rdoc|
|
39
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
40
|
+
|
41
|
+
rdoc.rdoc_dir = 'rdoc'
|
42
|
+
rdoc.title = "sequel_fast_columns #{version}"
|
43
|
+
rdoc.rdoc_files.include('README*')
|
44
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
45
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module Sequel
|
2
|
+
class Dataset
|
3
|
+
def columns
|
4
|
+
return @columns if @columns
|
5
|
+
@columns = if opts[:select]
|
6
|
+
if opts[:select].map(&:class).any? {|x| x == Sequel::LiteralString } ||
|
7
|
+
(opts[:joins] && opts[:select].any? {|x| x == :* })
|
8
|
+
query_db_for_columns
|
9
|
+
else
|
10
|
+
opts[:select].map do |column|
|
11
|
+
case column
|
12
|
+
when Sequel::SQL::AliasedExpression
|
13
|
+
column.aliaz.to_sym
|
14
|
+
when Sequel::SQL::QualifiedIdentifier
|
15
|
+
column_name_from_symbol column.column
|
16
|
+
when Sequel::SQL::ColumnAll
|
17
|
+
get_all_columns(column.table)
|
18
|
+
when Symbol
|
19
|
+
column_name_from_symbol column
|
20
|
+
when Sequel::SQL::Expression
|
21
|
+
column.to_s(db).to_sym
|
22
|
+
else
|
23
|
+
column.to_s.to_sym
|
24
|
+
end
|
25
|
+
end.flatten
|
26
|
+
end
|
27
|
+
else
|
28
|
+
if opts[:joins]
|
29
|
+
query_db_for_columns
|
30
|
+
else
|
31
|
+
get_all_columns opts[:from]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def get_all_columns(table)
|
39
|
+
table = table.first if table.kind_of? Array
|
40
|
+
if table.kind_of?(Symbol)
|
41
|
+
db.schema(table).map {|c| c.first }
|
42
|
+
else
|
43
|
+
query_db_for_columns
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def column_name_from_symbol(column)
|
48
|
+
if column == :*
|
49
|
+
get_all_columns opts[:from]
|
50
|
+
else
|
51
|
+
t, col, aliaz = split_symbol(column)
|
52
|
+
aliaz ? aliaz.to_sym : col.to_sym
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
# This is the original Sequel Method
|
57
|
+
def query_db_for_columns
|
58
|
+
ds = unfiltered.unordered.clone(:distinct => nil, :limit => 1)
|
59
|
+
ds.each{break}
|
60
|
+
@columns = ds.instance_variable_get(:@columns)
|
61
|
+
@columns || []
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'sequel/fast_columns'
|
@@ -0,0 +1,70 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe "Fast column queries" do
|
4
|
+
before :each do
|
5
|
+
@dataset = TEST_DB[:a]
|
6
|
+
@dataset.should_not_receive(:unfiltered)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should just return the selected column symbols" do
|
10
|
+
@dataset.select(:foo).columns.should == [:foo]
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should return the column name from symbols with __" do
|
14
|
+
@dataset.select(:a__foo).columns.should == [:foo]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should return the aliased column name from symbols with ___" do
|
18
|
+
@dataset.select(:a__foo___baz).columns.should == [:baz]
|
19
|
+
@dataset.select(:foo___baz).columns.should == [:baz]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should return the aliased column name from aliased expressions" do
|
23
|
+
@dataset.select(:foo.as(:baz)).columns.should == [:baz]
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should return the column name from qualified expressions" do
|
27
|
+
@dataset.select(:foo.qualify(:b)).columns.should == [:foo]
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return the column name from qualified aliased expressions" do
|
31
|
+
@dataset.select(:a__foo.as(:bar)).columns.should == [:bar]
|
32
|
+
@dataset.select(:foo.qualify(:a).as(:bar)).columns.should == [:bar]
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return the columns from the schema if no columns specified" do
|
36
|
+
@dataset.columns.should == [:id, :foo]
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return all columns from the schema if :* specified" do
|
40
|
+
@dataset.select(:*).columns.should == [:id, :foo]
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return all columns from the schema if qualfied :* specified" do
|
44
|
+
@dataset.select(:*.qualify(:a)).columns.should == [:id, :foo]
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return the value as the column" do
|
48
|
+
@dataset.select("hello", 1).columns.should == [:hello, :"1"]
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return the SQL for the column for unaliased calculated values" do
|
52
|
+
# doesn't work with Sqlite
|
53
|
+
# @dataset.select(:sum[:foo]).columns.should == [:"SUM(`foo`)"]
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe "Falling back to database query" do
|
58
|
+
it "should return all columns when select_append is used" do
|
59
|
+
TEST_DB[:a].select_append(:foo).columns.should == [:id, :foo, :foo]
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should fall back to querying the db if the dataset is derived" do
|
63
|
+
TEST_DB[TEST_DB[:a]].columns.should == [:id, :foo]
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should fall back if unqualfied * is used and there are joins" do
|
67
|
+
# Again can't get this working with sqlite.
|
68
|
+
# TEST_DB.join(:b, :id => :id).columns.should == [:id, :foo, :id, :bar]
|
69
|
+
end
|
70
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--backtrace --color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
|
3
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
4
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
5
|
+
|
6
|
+
require 'sequel'
|
7
|
+
require 'sequel_fast_columns'
|
8
|
+
|
9
|
+
require 'spec'
|
10
|
+
require 'spec/autorun'
|
11
|
+
|
12
|
+
TEST_DB = Sequel.sqlite
|
13
|
+
TEST_DB.create_table(:a) do
|
14
|
+
integer :id
|
15
|
+
integer :foo
|
16
|
+
end
|
17
|
+
|
18
|
+
TEST_DB.create_table(:b) do
|
19
|
+
integer :id
|
20
|
+
integer :bar
|
21
|
+
end
|
22
|
+
|
23
|
+
Spec::Runner.configure do |config|
|
24
|
+
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,87 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_fast_columns
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Roland Swingler
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-09-20 00:00:00 +01:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
description: A non-db-query implementation of Dataset#columns
|
35
|
+
email: roland.swingler@gmail.com
|
36
|
+
executables: []
|
37
|
+
|
38
|
+
extensions: []
|
39
|
+
|
40
|
+
extra_rdoc_files:
|
41
|
+
- LICENSE
|
42
|
+
- README.rdoc
|
43
|
+
files:
|
44
|
+
- .document
|
45
|
+
- .gitignore
|
46
|
+
- LICENSE
|
47
|
+
- README.rdoc
|
48
|
+
- Rakefile
|
49
|
+
- VERSION
|
50
|
+
- lib/sequel/fast_columns.rb
|
51
|
+
- lib/sequel_fast_columns.rb
|
52
|
+
- spec/sequel_fast_columns_spec.rb
|
53
|
+
- spec/spec.opts
|
54
|
+
- spec/spec_helper.rb
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: http://github.com/knaveofdiamonds/sequel_fast_columns
|
57
|
+
licenses: []
|
58
|
+
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options:
|
61
|
+
- --charset=UTF-8
|
62
|
+
require_paths:
|
63
|
+
- lib
|
64
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project:
|
81
|
+
rubygems_version: 1.3.6
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: A non-db-query implementation of Dataset#columns
|
85
|
+
test_files:
|
86
|
+
- spec/sequel_fast_columns_spec.rb
|
87
|
+
- spec/spec_helper.rb
|