active_house 0.5.0 → 0.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 460006ab1e148781921648c680e33a79790e35cd
4
- data.tar.gz: eb9ee437906deca34b1c6bb3bb172dc4bfcdab7b
3
+ metadata.gz: 2d60547649c4e0311e6ccc92d5e6ea5c75e71d35
4
+ data.tar.gz: b6d43bcbdab693c3da2c1bd1eb1b12c7853bdbf6
5
5
  SHA512:
6
- metadata.gz: 190813fb7b150e48c7c75c1a53ef12e5dc589ab60b109cf65463644ba3e4c17dd0c94b6fd927e778811dd1e631c0163ec5e6ef7fa5b72425aa34fd68eaca89fc
7
- data.tar.gz: 16fdecaf5792d2641aca7cd2b3e777d195894cbabe593b2ef1719ad16f83313fc547b3863829bcf764896b75b6721e77f90a08a420574f6d5ca46eb8d7c757f7
6
+ metadata.gz: 7542b7fcd7e6ec3d77a9e28558c7d4949a422e9a519c85c714ed7cd8db82f518e42a78ece6edbf6ae56a033c388678503346f946f42e4bde65aede08d68beb3e
7
+ data.tar.gz: 62486d802d065a5ac9c087de59956c85f05eca80f01e217c2be2c81b817d9cf2cb82349b64999b51ac4fb6d1eaf5f050de47f2fe7ea8a9f5a6cbe32f44ee27cb
data/active_house.gemspec CHANGED
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.add_dependency 'activemodel'
26
26
  spec.add_dependency 'activesupport'
27
27
  spec.add_dependency 'clickhouse', '~> 0.1.10'
28
+ spec.add_dependency 'kaminari-core', '~> 1.1'
28
29
  spec.add_dependency 'plain_model', '~> 0.2'
29
30
 
30
31
  spec.add_development_dependency 'bundler', '~> 1.16'
@@ -11,19 +11,30 @@ require_relative 'logging'
11
11
  module ActiveHouse
12
12
  class Model
13
13
  extend PlainModel::Modeling::Queryable
14
- include PlainModel::Modeling::Base
14
+ include ActiveModel::Model
15
15
  include ActiveHouse::Modeling::Scope
16
16
  include ActiveHouse::Modeling::Query
17
17
  include ActiveHouse::Modeling::Attributes
18
18
  include ActiveHouse::Modeling::Connection
19
19
  include ActiveHouse::Logging
20
20
 
21
+ Column = Struct.new(:name)
21
22
  class_attribute :_table_name, instance_accessor: false
22
23
 
23
24
  class << self
24
25
  def table_name(name)
25
26
  self._table_name = name.to_s
26
27
  end
28
+
29
+ def columns
30
+ return @columns if defined?(@columns)
31
+
32
+ @columns = attribute_types.keys.map { |name| Column.new(name.to_sym) }
33
+ end
34
+
35
+ def column_names
36
+ columns.map(&:name)
37
+ end
27
38
  end
28
39
  end
29
40
  end
@@ -10,6 +10,7 @@ require_relative 'querying/having'
10
10
  require_relative 'querying/limit'
11
11
  require_relative 'querying/union'
12
12
  require_relative 'querying/array_join'
13
+ require_relative 'querying/page'
13
14
  require_relative 'querying/scope'
14
15
  require_relative 'querying/collect'
15
16
 
@@ -29,8 +30,10 @@ module ActiveHouse
29
30
  include ActiveHouse::Querying::Union
30
31
  include ActiveHouse::Querying::ArrayJoin
31
32
  include ActiveHouse::Querying::Scope
33
+ include ActiveHouse::Querying::Page
32
34
  include ActiveHouse::Querying::Collect
33
35
 
36
+ # allows using query without model_class
34
37
  def initialize(model_class = nil)
35
38
  super(model_class || ActiveHouse::Model)
36
39
  end
@@ -1,6 +1,8 @@
1
1
  require 'active_support/concern'
2
2
  require 'active_support/core_ext/string/filters'
3
3
  require 'active_support/core_ext/module/delegation'
4
+ require_relative '../search'
5
+ require_relative '../search_query'
4
6
 
5
7
  module ActiveHouse
6
8
  module Querying
@@ -9,6 +11,7 @@ module ActiveHouse
9
11
 
10
12
  included do
11
13
  private :collection, :fetch_collection, :query_parts, :build_query
14
+ instance_delegate [:connection] => :model_class
12
15
  instance_delegate [
13
16
  :each,
14
17
  :size,
@@ -75,6 +78,20 @@ module ActiveHouse
75
78
  def build_query
76
79
  query_parts.reject(&:nil?).join("\n")
77
80
  end
81
+
82
+ def klass
83
+ model_class
84
+ end
85
+
86
+ def group_values
87
+ values[:group_by].empty? ? nil : values[:group_by]
88
+ end
89
+
90
+ def count(value = 'COUNT() AS cnt')
91
+ return 0 if group_values
92
+ except(:select, :limit, :offset, :order).select(value).to_hashes.first.values.first
93
+ end
94
+
78
95
  end
79
96
  end
80
97
  end
@@ -9,26 +9,26 @@ module ActiveHouse
9
9
 
10
10
  def build_limit_query_part
11
11
  return if values[:limit].nil?
12
- if values[:offset]
13
- "LIMIT #{values[:limit]}, #{values[:offset]}"
14
- else
15
- "LIMIT #{values[:limit]}"
16
- end
12
+
13
+ parts = ["LIMIT #{values[:limit]}"]
14
+ parts << "OFFSET #{values[:offset]}" if values[:offset] && values[:offset] != 0
15
+ parts << "BY #{values[:limit_by]}" if values[:limit_by]
16
+ parts.join(' ')
17
17
  end
18
18
 
19
19
  def initial_values
20
- super.merge offset: nil, limit: nil
21
- super
20
+ super.merge offset: nil, limit: nil, limit_by: nil
22
21
  end
23
22
 
24
- def limit!(limit_value, offset_value = nil)
23
+ def limit!(limit_value, offset_value = nil, limit_by = nil)
25
24
  values[:limit] = limit_value
26
25
  values[:offset] = offset_value unless offset_value.nil?
26
+ values[:limit_by] = limit_by unless limit_by.nil?
27
27
  self
28
28
  end
29
29
 
30
- def limit(limit_value, offset_value = nil)
31
- dup.limit!(limit_value, offset_value)
30
+ def limit(limit_value, offset_value = nil, limit_by = nil)
31
+ dup.limit!(limit_value, offset_value, limit_by)
32
32
  end
33
33
  end
34
34
  end
@@ -0,0 +1,35 @@
1
+ module ActiveHouse
2
+ module Querying
3
+ module Page
4
+ extend ActiveSupport::Concern
5
+
6
+ def initial_values
7
+ super.merge page_number: nil
8
+ end
9
+
10
+ def page(page_number)
11
+ dup.page!(page_number)
12
+ end
13
+
14
+ def page!(page_number)
15
+ page_number ||= 1
16
+ raise ArgumentError, 'page_number must be >= 1' if page_number < 1
17
+
18
+ values[:page_number] = page_number
19
+ self
20
+ end
21
+
22
+ def per(page_size)
23
+ page_number = values[:page_number] || 1
24
+ raise ArgumentError, 'page_number must be >= 1' if page_number < 1
25
+ raise ArgumentError, 'page_size must be >= 0' if page_size < 0
26
+
27
+ offset = (page_number - 1) * page_size
28
+ records = limit(page_size, offset).to_a
29
+ total_count = count
30
+ Kaminari.paginate_array(records, total_count: total_count).page(page_number).per(page_size)
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module ActiveHouse
2
- VERSION = '0.5.0'.freeze
2
+ VERSION = '0.5.1'.freeze
3
3
  end
data/lib/active_house.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'active_house/version'
2
2
  require 'active_house/configuration'
3
- require 'active_house/query_builder'
4
3
  require 'active_house/model'
4
+ require 'active_house/query_builder'
5
5
 
6
6
  module ActiveHouse
7
7
  def self.configure
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_house
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Talakevich
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-08-04 00:00:00.000000000 Z
11
+ date: 2019-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.1.10
55
+ - !ruby/object:Gem::Dependency
56
+ name: kaminari-core
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.1'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: plain_model
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -145,6 +159,7 @@ files:
145
159
  - lib/active_house/querying/having.rb
146
160
  - lib/active_house/querying/limit.rb
147
161
  - lib/active_house/querying/order_by.rb
162
+ - lib/active_house/querying/page.rb
148
163
  - lib/active_house/querying/scope.rb
149
164
  - lib/active_house/querying/select.rb
150
165
  - lib/active_house/querying/union.rb