clickhouse-activerecord 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 +55 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/clickhouse-activerecord.gemspec +29 -0
- data/lib/active_record/connection_adapters/clickhouse/oid/date.rb +21 -0
- data/lib/active_record/connection_adapters/clickhouse/oid/date_time.rb +21 -0
- data/lib/active_record/connection_adapters/clickhouse_adapter.rb +207 -0
- data/lib/clickhouse.rb +7 -0
- data/lib/clickhouse/railtie.rb +7 -0
- data/lib/tasks/clickhouse.rake +23 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 42e313d78ebe5e65c9d69dee42e516e2026c381f
|
4
|
+
data.tar.gz: abe79df94ae67c6ff8777a39f1e2a0fa1ddeb145
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 76a8b83b2905276ca6be59fe477a7e85ecb6a33ea56c7c9c81d72512f5240dc7c2785f978c494938db824e77381e4bb80e7fd121adb3ca0f2d0b19897b1c64dc
|
7
|
+
data.tar.gz: 7493d61f0f820585a8de6a6c1b42fb6f4b7c2fccaad85162225e7fec8c045658c72bc2ca7146c99f97a0b5b79bf1a14517e2e025bedbcf0f4bb4ef935a232a12
|
data/.gitignore
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
*.gem
|
11
|
+
|
12
|
+
### JetBrains template
|
13
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
14
|
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
15
|
+
|
16
|
+
# User-specific stuff:
|
17
|
+
.idea/
|
18
|
+
.idea/**/tasks.xml
|
19
|
+
.idea/dictionaries
|
20
|
+
|
21
|
+
# Sensitive or high-churn files:
|
22
|
+
.idea/**/dataSources/
|
23
|
+
.idea/**/dataSources.ids
|
24
|
+
.idea/**/dataSources.xml
|
25
|
+
.idea/**/dataSources.local.xml
|
26
|
+
.idea/**/sqlDataSources.xml
|
27
|
+
.idea/**/dynamic.xml
|
28
|
+
.idea/**/uiDesigner.xml
|
29
|
+
|
30
|
+
# Gradle:
|
31
|
+
.idea/**/gradle.xml
|
32
|
+
.idea/**/libraries
|
33
|
+
|
34
|
+
# Mongo Explorer plugin:
|
35
|
+
.idea/**/mongoSettings.xml
|
36
|
+
|
37
|
+
## File-based project format:
|
38
|
+
*.iws
|
39
|
+
|
40
|
+
## Plugin-specific files:
|
41
|
+
|
42
|
+
# IntelliJ
|
43
|
+
/out/
|
44
|
+
|
45
|
+
# mpeltonen/sbt-idea plugin
|
46
|
+
.idea_modules/
|
47
|
+
|
48
|
+
# JIRA plugin
|
49
|
+
atlassian-ide-plugin.xml
|
50
|
+
|
51
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
52
|
+
com_crashlytics_export_strings.xml
|
53
|
+
crashlytics.properties
|
54
|
+
crashlytics-build.properties
|
55
|
+
fabric.properties
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at nixx.dj@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 nixx
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# Clickhouse::Activerecord
|
2
|
+
|
3
|
+
A Ruby database ActiveRecord driver for ClickHouse.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'clickhouse-activerecord'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install clickhouse-activerecord
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Add your `database.yml` connection information with postfix `_clickhouse` for you environment:
|
24
|
+
|
25
|
+
```yml
|
26
|
+
development_clickhouse:
|
27
|
+
adapter: clickhouse
|
28
|
+
database: database
|
29
|
+
host: localhost
|
30
|
+
username: username
|
31
|
+
password: password
|
32
|
+
```
|
33
|
+
|
34
|
+
Add to your model:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class Action < ActiveRecord::Base
|
38
|
+
establish_connection "#{Rails.env}_clickhouse".to_sym
|
39
|
+
end
|
40
|
+
```
|
41
|
+
|
42
|
+
Or global connection, but schema dump don't works:
|
43
|
+
|
44
|
+
```yml
|
45
|
+
development:
|
46
|
+
adapter: clickhouse
|
47
|
+
database: database
|
48
|
+
host: localhost
|
49
|
+
username: username
|
50
|
+
password: password
|
51
|
+
```
|
52
|
+
|
53
|
+
Schema dump:
|
54
|
+
|
55
|
+
$ rake clickhouse:schema:dump
|
56
|
+
|
57
|
+
### Insert and select data
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
Action.where(url: 'http://example.com', date: Date.current).where.not(name: nil).order(created_at: :desc).limit(10)
|
61
|
+
=> #<ActiveRecord::Relation [#<Action *** >]>
|
62
|
+
|
63
|
+
Action.create(url: 'http://example.com', date: Date.yesterday)
|
64
|
+
=> true
|
65
|
+
```
|
66
|
+
|
67
|
+
NOTE: Creating tables in developing.
|
68
|
+
|
69
|
+
## Development
|
70
|
+
|
71
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
72
|
+
|
73
|
+
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).
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Bug reports and pull requests are welcome on GitHub at [https://github.com/pnixx/clickhouse-activerecord](https://github.com/pnixx/clickhouse-activerecord). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
78
|
+
|
79
|
+
## License
|
80
|
+
|
81
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "clickhouse/activerecord"
|
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
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'clickhouse-activerecord'
|
8
|
+
spec.version = '0.1.0'
|
9
|
+
spec.authors = ['Sergey Odintsov']
|
10
|
+
spec.email = ['nixx.dj@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'ClickHouse ActiveRecord'
|
13
|
+
spec.description = 'ActiveRecord adapter for ClickHouse'
|
14
|
+
spec.homepage = 'https://github.com/pnixx/clickhouse-activerecord'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = 'exe'
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency "bundler", ">= 1.13.4"
|
25
|
+
spec.add_dependency 'rails', '>= 4.1.8'
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
29
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Clickhouse
|
4
|
+
module OID # :nodoc:
|
5
|
+
class Date < Type::Date # :nodoc:
|
6
|
+
|
7
|
+
def type_cast_from_database(value)
|
8
|
+
value
|
9
|
+
end
|
10
|
+
|
11
|
+
# Type cast a value for schema dumping. This method is private, as we are
|
12
|
+
# hoping to remove it entirely.
|
13
|
+
def type_cast_for_schema(value) # :nodoc:
|
14
|
+
value.inspect
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ActiveRecord
|
2
|
+
module ConnectionAdapters
|
3
|
+
module Clickhouse
|
4
|
+
module OID # :nodoc:
|
5
|
+
class DateTime < Type::DateTime # :nodoc:
|
6
|
+
|
7
|
+
def type_cast_from_database(value)
|
8
|
+
value
|
9
|
+
end
|
10
|
+
|
11
|
+
# Type cast a value for schema dumping. This method is private, as we are
|
12
|
+
# hoping to remove it entirely.
|
13
|
+
def type_cast_for_schema(value) # :nodoc:
|
14
|
+
value.inspect
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,207 @@
|
|
1
|
+
require 'active_record/connection_adapters/abstract_adapter'
|
2
|
+
require 'active_record/connection_adapters/clickhouse/oid/date'
|
3
|
+
require 'active_record/connection_adapters/clickhouse/oid/date_time'
|
4
|
+
|
5
|
+
module ActiveRecord
|
6
|
+
class Base
|
7
|
+
class << self
|
8
|
+
# Establishes a connection to the database that's used by all Active Record objects
|
9
|
+
def clickhouse_connection(config)
|
10
|
+
config = config.symbolize_keys
|
11
|
+
host = config[:host]
|
12
|
+
port = config[:port] || 8123
|
13
|
+
|
14
|
+
if config.key?(:database)
|
15
|
+
database = config[:database]
|
16
|
+
else
|
17
|
+
raise ArgumentError, 'No database specified. Missing argument: database.'
|
18
|
+
end
|
19
|
+
|
20
|
+
ConnectionAdapters::ClickhouseAdapter.new(nil, logger, [host, port], { user: config[:username], password: config[:password], database: database }.compact)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
module ConnectionAdapters
|
26
|
+
|
27
|
+
class ClickhouseColumn < Column
|
28
|
+
def initialize(name, default, cast_type, sql_type = nil, null = true)
|
29
|
+
super(name, default, cast_type, sql_type, null)
|
30
|
+
@default_function = 'now'
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
# Extracts the value from a PostgreSQL column default definition.
|
36
|
+
def extract_value_from_default(default)
|
37
|
+
case default
|
38
|
+
# Quoted types
|
39
|
+
when /\A[\(B]?'(.*)'.*::"?([\w. ]+)"?(?:\[\])?\z/m
|
40
|
+
# The default 'now'::date is CURRENT_DATE
|
41
|
+
if $1 == "now".freeze && $2 == "date".freeze
|
42
|
+
nil
|
43
|
+
else
|
44
|
+
$1.gsub("''".freeze, "'".freeze)
|
45
|
+
end
|
46
|
+
# Boolean types
|
47
|
+
when "true".freeze, "false".freeze
|
48
|
+
default
|
49
|
+
# Numeric types
|
50
|
+
when /\A\(?(-?\d+(\.\d*)?)\)?(::bigint)?\z/
|
51
|
+
$1
|
52
|
+
# Object identifier types
|
53
|
+
when /\A-?\d+\z/
|
54
|
+
$1
|
55
|
+
else
|
56
|
+
# Anything else is blank, some user type, or some function
|
57
|
+
# and we can't know the value of that, so return nil.
|
58
|
+
nil
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def extract_default_function(default_value, default) # :nodoc:
|
63
|
+
default if has_default_function?(default_value, default)
|
64
|
+
end
|
65
|
+
|
66
|
+
def has_default_function?(default_value, default) # :nodoc:
|
67
|
+
!default_value && (%r{\w+\(.*\)} === default)
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
class ClickhouseAdapter < AbstractAdapter
|
73
|
+
ADAPTER_NAME = 'Clickhouse'.freeze
|
74
|
+
|
75
|
+
NATIVE_DATABASE_TYPES = {
|
76
|
+
string: { name: 'String', limit: 255 },
|
77
|
+
integer: { name: 'UInt32' },
|
78
|
+
float: { name: 'Float32' },
|
79
|
+
datetime: { name: 'DateTime' },
|
80
|
+
date: { name: 'Date' },
|
81
|
+
boolean: { name: 'UInt8' }
|
82
|
+
}.freeze
|
83
|
+
|
84
|
+
# Initializes and connects a Clickhouse adapter.
|
85
|
+
def initialize(connection, logger, connection_parameters, config)
|
86
|
+
super(connection, logger)
|
87
|
+
@connection_parameters = connection_parameters
|
88
|
+
@config = config
|
89
|
+
|
90
|
+
@visitor = Arel::Visitors::ToSql.new self
|
91
|
+
|
92
|
+
connect
|
93
|
+
end
|
94
|
+
|
95
|
+
def native_database_types #:nodoc:
|
96
|
+
NATIVE_DATABASE_TYPES
|
97
|
+
end
|
98
|
+
|
99
|
+
def valid_type?(type)
|
100
|
+
!native_database_types[type].nil?
|
101
|
+
end
|
102
|
+
|
103
|
+
def extract_limit(sql_type) # :nodoc:
|
104
|
+
case sql_type
|
105
|
+
when 'Nullable(String)'
|
106
|
+
255
|
107
|
+
else
|
108
|
+
super
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def initialize_type_map(m) # :nodoc:
|
113
|
+
super
|
114
|
+
register_class_with_limit m, 'String', Type::String
|
115
|
+
register_class_with_limit m, 'Nullable(String)', Type::String
|
116
|
+
register_class_with_limit m, 'Uint8', Type::UnsignedInteger
|
117
|
+
register_class_with_limit m, 'Date', Clickhouse::OID::Date
|
118
|
+
register_class_with_limit m, 'DateTime', Clickhouse::OID::DateTime
|
119
|
+
m.alias_type 'UInt16', 'uint8'
|
120
|
+
m.alias_type 'UInt32', 'uint8'
|
121
|
+
m.alias_type 'UInt64', 'uint8'
|
122
|
+
m.alias_type 'Int8', 'int8'
|
123
|
+
m.alias_type 'Int16', 'int8'
|
124
|
+
m.alias_type 'Int32', 'int8'
|
125
|
+
m.alias_type 'Int64', 'int8'
|
126
|
+
end
|
127
|
+
|
128
|
+
# Queries the database and returns the results in an Array-like object
|
129
|
+
def query(sql, _name = nil)
|
130
|
+
res = @connection.get("/?#{@config.merge(query: "#{sql} FORMAT JSONCompact").to_param}")
|
131
|
+
raise ActiveRecord::ActiveRecordError, "Response code: #{res.code}:\n#{res.body}" unless res.code.to_i == 200
|
132
|
+
JSON.parse res.body
|
133
|
+
end
|
134
|
+
|
135
|
+
def execute(sql, name = nil)
|
136
|
+
log(sql, "#{adapter_name} #{name}") do
|
137
|
+
query sql, name
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def exec_query(sql, name = nil, _binds = [])
|
142
|
+
result = execute(sql, name)
|
143
|
+
ActiveRecord::Result.new(result['meta'].map { |m| m['name'] }, result['data'])
|
144
|
+
end
|
145
|
+
|
146
|
+
# Executes insert +sql+ statement in the context of this connection using
|
147
|
+
# +binds+ as the bind substitutes. +name+ is logged along with
|
148
|
+
# the executed +sql+ statement.
|
149
|
+
def exec_insert(sql, name, _binds, _pk = nil, _sequence_name = nil)
|
150
|
+
log(sql, "#{adapter_name} #{name}") do
|
151
|
+
res = @connection.post("/?#{@config.to_param}", sql)
|
152
|
+
raise ActiveRecord::ActiveRecordError, "Response code: #{res.code}:\n#{res.body}" unless res.code.to_i == 200
|
153
|
+
true
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
def update(_arel, _name = nil, _binds = [])
|
158
|
+
raise ActiveRecord::ActiveRecordError, 'Clickhouse update is not supported'
|
159
|
+
end
|
160
|
+
|
161
|
+
def delete(_arel, _name = nil, _binds = [])
|
162
|
+
raise ActiveRecord::ActiveRecordError, 'Clickhouse delete is not supported'
|
163
|
+
end
|
164
|
+
|
165
|
+
# SCHEMA STATEMENTS ========================================
|
166
|
+
|
167
|
+
def tables(name = nil)
|
168
|
+
query('SHOW TABLES', name)['data'].flatten
|
169
|
+
end
|
170
|
+
|
171
|
+
def columns(table_name, _name = nil) #:nodoc:
|
172
|
+
table_structure(table_name).map do |field|
|
173
|
+
ClickhouseColumn.new(field[0], field[3].present? ? field[3] : nil, lookup_cast_type(field[1]), field[1], field[1].include?('Nullable'))
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
def indexes(_table_name, _name = nil) #:nodoc:
|
178
|
+
Rails.logger.warn 'Clickhouse indexes is not supported'
|
179
|
+
[]
|
180
|
+
end
|
181
|
+
|
182
|
+
def primary_key(table_name) #:nodoc:
|
183
|
+
pk = table_structure(table_name).first
|
184
|
+
return 'id' if pk.present? && pk[0] == 'id'
|
185
|
+
false
|
186
|
+
end
|
187
|
+
|
188
|
+
protected
|
189
|
+
|
190
|
+
def table_structure(table_name)
|
191
|
+
data = query("DESCRIBE TABLE #{table_name}", table_name)['data']
|
192
|
+
raise(ActiveRecord::StatementInvalid, "Could not find table '#{table_name}'") if data.empty?
|
193
|
+
data
|
194
|
+
end
|
195
|
+
|
196
|
+
def last_inserted_id(result)
|
197
|
+
result
|
198
|
+
end
|
199
|
+
|
200
|
+
private
|
201
|
+
|
202
|
+
def connect
|
203
|
+
@connection = Net::HTTP.start(@connection_parameters[0], @connection_parameters[1])
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
end
|
data/lib/clickhouse.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
namespace :clickhouse do
|
2
|
+
|
3
|
+
namespace :schema do
|
4
|
+
|
5
|
+
# todo not testing
|
6
|
+
# desc 'Load database schema'
|
7
|
+
# task load_schema: :environment do
|
8
|
+
# ActiveRecord::Base.establish_connection(:"#{Rails.env}_clickhouse")
|
9
|
+
# load("#{Rails.root}/db/clickhouse_schema.rb")
|
10
|
+
# end
|
11
|
+
|
12
|
+
desc 'Dump database schema'
|
13
|
+
task dump: :environment do
|
14
|
+
filename = "#{Rails.root}/db/clickhouse_schema.rb"
|
15
|
+
File.open(filename, 'w:utf-8') do |file|
|
16
|
+
ActiveRecord::Base.establish_connection(:"#{Rails.env}_clickhouse")
|
17
|
+
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: clickhouse-activerecord
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sergey Odintsov
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.13.4
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.13.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.1.8
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.1.8
|
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.15'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.15'
|
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
|
+
description: ActiveRecord adapter for ClickHouse
|
70
|
+
email:
|
71
|
+
- nixx.dj@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- CODE_OF_CONDUCT.md
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bin/console
|
83
|
+
- bin/setup
|
84
|
+
- clickhouse-activerecord.gemspec
|
85
|
+
- lib/active_record/connection_adapters/clickhouse/oid/date.rb
|
86
|
+
- lib/active_record/connection_adapters/clickhouse/oid/date_time.rb
|
87
|
+
- lib/active_record/connection_adapters/clickhouse_adapter.rb
|
88
|
+
- lib/clickhouse.rb
|
89
|
+
- lib/clickhouse/railtie.rb
|
90
|
+
- lib/tasks/clickhouse.rake
|
91
|
+
homepage: https://github.com/pnixx/clickhouse-activerecord
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.6.12
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: ClickHouse ActiveRecord
|
115
|
+
test_files: []
|
116
|
+
has_rdoc:
|