click_house 1.2.4 → 1.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7486a9c08726582c9824560d471e12260cc0349a80caa10f32ab19edca64cc06
4
- data.tar.gz: ada7929336b444323b135773e3e6678276e3a9d3433dc6e81b5e3db9238570f3
3
+ metadata.gz: 700d57a764315e709b8e78f10a5765801baa2b491aeb4aad6b6cc6c2016f8c74
4
+ data.tar.gz: 34f61608fd6b67fed90f9ec1d84db1677abdeda86f3c8751fb124457f25274e4
5
5
  SHA512:
6
- metadata.gz: d3a96167eaac87f3421d0ce146dc1c0f898f02330416e59b06483f02ed8c1acb5403d8f5bf19f1138018c02434736d7edd32a550b3a3ac70f110380e952f8086
7
- data.tar.gz: 8c6d0c99b0fbfa2e8d9a298d4d144202eabe564714e2821ee27bc4e436bad19b2d1c0060327be9216ee27a879fb75785af59ee260845fd995ce808faf8e1b71e
6
+ metadata.gz: bff6c7f9bda496860ee38038bde526f706f7f6a1c1be8be0800806c70c24de15764d090e579c4dafe0bf3e86eca18acfce4a3c96542b5e89756182b8711f8aaf
7
+ data.tar.gz: 5cd4d79cb422ea0feb9171b2e1ca5eb824d50fabdd718e2789c65e1ce75bcefedd60c479a0a46a9488a0c6ac8f352e25caa07e2c417c8ae79c351c2b121ffd3b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- click_house (1.2.4)
4
+ click_house (1.2.5)
5
5
  faraday
6
6
  faraday_middleware
7
7
 
data/README.md CHANGED
@@ -38,6 +38,9 @@ Yandex uses HTTP interface for working from Java and Perl, Python and Go as well
38
38
  * [Type casting](#type-casting)
39
39
  * [Using with a connection pool](#using-with-a-connection-pool)
40
40
  * [Using with Rails](#using-with-rails)
41
+ * [Using with ActiveRecord](#using-with-activerecord)
42
+ * [Using with RSpec](#using-with-rspec)
43
+ * [Development](#development)
41
44
 
42
45
  ## Configuration
43
46
 
@@ -459,18 +462,57 @@ class CreateAdvertVisits < ActiveRecord::Migration[6.0]
459
462
  end
460
463
  ```
461
464
 
465
+ ## Using with ActiveRecord
466
+
462
467
  if you use `ActiveRecord`, you can use the ORM query builder by using fake models
468
+ (empty tables must be present in the SQL database `create_table :visits`)
469
+
470
+ ```ruby
471
+ class ClickHouseRecord < ActiveRecord::Base
472
+ self.abstract_class = true
473
+
474
+ class << self
475
+ def agent
476
+ ClickHouse.connection
477
+ end
478
+
479
+ def insert(*argv, &block)
480
+ agent.insert(table_name, *argv, &block)
481
+ end
482
+
483
+ def select_one
484
+ agent.select_one(current_scope.to_sql)
485
+ end
486
+
487
+ def select_value
488
+ agent.select_value(current_scope.to_sql)
489
+ end
490
+
491
+ def select_all
492
+ agent.select_all(current_scope.to_sql)
493
+ end
494
+ end
495
+ end
496
+ ````
463
497
 
464
498
  ````ruby
465
499
  # FAKE MODEL FOR ClickHouse
466
- class Visit < ApplicationRecord
500
+ class Visit < ClickHouseRecord
467
501
  scope :with_os, -> { where.not(os_family_id: nil) }
468
502
  end
469
503
 
470
- scope = Visit.with_os.select('COUNT(*) as counter').group(:ipv4)
471
- ClickHouse.connection.select_all(scope.to_sql)
504
+ Visit.with_os.select('COUNT(*) as counter').group(:ipv4).select_all
505
+ #=> [{ 'ipv4' => 1455869, 'counter' => 104 }]
506
+
507
+ Visit.with_os.select('COUNT(*)').select_value
508
+ #=> 20_345_678
509
+
510
+ Visit.where(user_id: 1).select_one
511
+ #=> { 'ipv4' => 1455869, 'user_id' => 1 }
472
512
  ````
473
513
 
514
+ ## Using with RSpec
515
+
474
516
  You can clear the data table before each test with RSpec
475
517
 
476
518
  ```ruby
@@ -3,6 +3,9 @@
3
3
  module ClickHouse
4
4
  module Extend
5
5
  module ConnectionInserting
6
+ EMPTY_INSERT = true
7
+
8
+ # @return [Boolean]
6
9
  def insert(table, columns: [], values: [])
7
10
  yield(values) if block_given?
8
11
 
@@ -12,6 +15,8 @@ module ClickHouse
12
15
  values.map { |value_row| columns.zip(value_row).to_h.to_json }
13
16
  end
14
17
 
18
+ return EMPTY_INSERT if values.empty?
19
+
15
20
  execute("INSERT INTO #{table} FORMAT JSONEachRow", body.join("\n")).success?
16
21
  end
17
22
  end
@@ -3,11 +3,11 @@
3
3
  module ClickHouse
4
4
  module Type
5
5
  class BaseType
6
- def cast(_value)
6
+ def cast(_value, *)
7
7
  raise NotImplementedError, __method__
8
8
  end
9
9
 
10
- def serialize(_value)
10
+ def serialize(_value, *)
11
11
  raise NotImplementedError, __method__
12
12
  end
13
13
  end
@@ -13,8 +13,8 @@ module ClickHouse
13
13
  subtype.cast(*argv) unless argv.first.nil?
14
14
  end
15
15
 
16
- def serialize(value)
17
- value.nil ? 'NULL' : subtype.serialize(value)
16
+ def serialize(*argv)
17
+ subtype.serialize(*argv) unless argv.first.nil?
18
18
  end
19
19
  end
20
20
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module ClickHouse
4
4
  module Type
5
- class UndefinedType
5
+ class UndefinedType < BaseType
6
6
  def cast(value, *)
7
7
  value
8
8
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ClickHouse
4
- VERSION = '1.2.4'
4
+ VERSION = '1.2.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: click_house
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.4
4
+ version: 1.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aliaksandr Shylau
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-11-11 00:00:00.000000000 Z
11
+ date: 2019-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday