sequel-tablefunc 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 +12 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/README.md +60 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/sequel.rb +1 -0
- data/lib/sequel/extensions/tablefunc.rb +21 -0
- data/lib/sequel/extensions/tablefunc/version.rb +5 -0
- data/sequel-tablefunc.gemspec +38 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 77edaf93fe9fffe750170b59076b2f98de4b3fd4
|
4
|
+
data.tar.gz: 2395359646d67056d70ae57c6c473253cfeb6ac5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 956fa6a4ee7009ea9ad4465b79b268368fc093fc1beec287c27086032b84c58b4d4381dc8278225aa9b11b3add48f88376c103ae7bdee35a39065ca7c90a86a9
|
7
|
+
data.tar.gz: 277b1b860001dcefd7a33dd177ebe042c0a295178cc7823f9c9f2057fd5b6fdf05ab107bc60dcea867b4e614980f484d3db450cd5c795946054d7ced0f1c4ff1
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
# Sequel::Tablefunc
|
2
|
+
|
3
|
+
This is sequel extension that makes using crosstab function more convenient
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'sequel-tablefunc', github: 'fiscal-cliff/sequel-tablefunc'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
## Usage
|
19
|
+
|
20
|
+
Assuming you have table which already have 2 categories. This table can be represented as a pivot table
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
User.select_group(:type_id, :status_id)
|
24
|
+
.select_append(:count.sql_function('*'.lit))
|
25
|
+
.order(:type_id)
|
26
|
+
.crosstab(User.select(:status_id).distinct.order(:status_id))
|
27
|
+
.all
|
28
|
+
```
|
29
|
+
|
30
|
+
| row_name | status1 | status2 |
|
31
|
+
| :------------- | :------------- | :------------- |
|
32
|
+
| type1 | 5 | 10|
|
33
|
+
| type2 | 1 | 2|
|
34
|
+
|
35
|
+
It is easy, isn't it?
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
User.select_group(:date_trunc.sql_function('year', :created_at), :status_id)
|
39
|
+
.select_append(:count.sql_function('*'.lit))
|
40
|
+
.order(:date_trunc.sql_function('year', :created_at))
|
41
|
+
.crosstab(User.select(:status_id)
|
42
|
+
.distinct
|
43
|
+
.order(:status_id))
|
44
|
+
.all
|
45
|
+
```
|
46
|
+
|
47
|
+
| row_name | status1 | status2 |
|
48
|
+
| :------------- | :------------- | :------------- |
|
49
|
+
| 2012-01-01 00:00:00 | 6 | nil|
|
50
|
+
| 2013-01-01 00:00:00 | nil | 12|
|
51
|
+
|
52
|
+
## Development
|
53
|
+
|
54
|
+
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.
|
55
|
+
|
56
|
+
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).
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/fiscal-cliff/sequel-tablefunc.
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "sequel/tablefunc"
|
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(__FILE__)
|
data/bin/setup
ADDED
data/lib/sequel.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sequel/extensions/tablefunc'
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "sequel/extensions/tablefunc/version"
|
2
|
+
require 'sequel/model'
|
3
|
+
|
4
|
+
module Sequel
|
5
|
+
module Tablefunc
|
6
|
+
def crosstab(categories_dataset, type: 'text', cat_name: 'category')
|
7
|
+
data_sql = tablefunc_quote(self.sql)
|
8
|
+
categories_sql = tablefunc_quote(categories_dataset.sql)
|
9
|
+
categories_types = DB[categories_dataset].all.map(&:values).flatten.map{|i| "\"#{i}\" #{type}"}.join(', ')
|
10
|
+
DB.from(Sequel.lit("crosstab('#{data_sql}','#{categories_sql}') as ct(#{cat_name} text, #{categories_types})"))
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def tablefunc_quote(query)
|
16
|
+
return query.gsub("'","''")
|
17
|
+
end
|
18
|
+
|
19
|
+
Dataset.register_extension(:tablefunc, Tablefunc)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sequel/extensions/tablefunc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "sequel-tablefunc"
|
8
|
+
spec.version = Sequel::Tablefunc::VERSION
|
9
|
+
spec.authors = ["Fox"]
|
10
|
+
spec.email = ["why@not.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Sequel extension that facilitates using crosstab function in postgres}
|
13
|
+
spec.homepage = "https://github.com/fiscal-cliff/sequel-tablefunc"
|
14
|
+
|
15
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
16
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
17
|
+
if spec.respond_to?(:metadata)
|
18
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
19
|
+
else
|
20
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
21
|
+
"public gem pushes."
|
22
|
+
end
|
23
|
+
|
24
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
f.match(%r{^(test|spec|features)/})
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_dependency "sequel", ">= 4.0.0"
|
32
|
+
spec.add_dependency "pg", ">= 0.17.0"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
spec.add_development_dependency "pry", "~> 0.10"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel-tablefunc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Fox
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-05-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pg
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.17.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.17.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.14'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.14'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.10'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.10'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- why@not.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- Gemfile
|
108
|
+
- README.md
|
109
|
+
- Rakefile
|
110
|
+
- bin/console
|
111
|
+
- bin/setup
|
112
|
+
- lib/sequel.rb
|
113
|
+
- lib/sequel/extensions/tablefunc.rb
|
114
|
+
- lib/sequel/extensions/tablefunc/version.rb
|
115
|
+
- sequel-tablefunc.gemspec
|
116
|
+
homepage: https://github.com/fiscal-cliff/sequel-tablefunc
|
117
|
+
licenses: []
|
118
|
+
metadata:
|
119
|
+
allowed_push_host: https://rubygems.org
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubyforge_project:
|
136
|
+
rubygems_version: 2.5.2
|
137
|
+
signing_key:
|
138
|
+
specification_version: 4
|
139
|
+
summary: Sequel extension that facilitates using crosstab function in postgres
|
140
|
+
test_files: []
|