pg_sql_caller 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 14dd6a2499e57cd11eb961054e6afbdb48d86c8b61673918167e90189f812f75
4
+ data.tar.gz: df298464b9fc9c04b1da3e70f1bd715c3a3f17174b41ba7175c7c34aed7c4c2f
5
+ SHA512:
6
+ metadata.gz: 4cce2039fe18a4368d7b28af7fe241570f239364824db3f0307b87529810fc6517373ccfdd8c7c5c9df21627d20120e6ff728a9a278e9f8b2ff4f72093475e7d
7
+ data.tar.gz: 03e2c9a2d163f2b856662c6626a8a8aae7eecb32b604bd2db9c425cfa7df5da150ccd458e0ca28ceb04ac6f6777e4011dbaf1da3cf7609650f785ef4bdef9eed
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ /Gemfile.lock
14
+ /spec/config/database.yml
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,71 @@
1
+ AllCops:
2
+ DisplayCopNames: true
3
+ TargetRubyVersion: 2.5
4
+ Exclude:
5
+ - vendor/**/*
6
+ - tmp/**/*
7
+
8
+ Layout/LineLength:
9
+ Max: 180
10
+
11
+ Style/SymbolArray:
12
+ EnforcedStyle: brackets
13
+
14
+ Style/Lambda:
15
+ EnforcedStyle: literal
16
+
17
+ Naming/MethodParameterName:
18
+ AllowedNames:
19
+ - id
20
+
21
+ Style/HashEachMethods:
22
+ Enabled: true
23
+
24
+ Style/HashTransformKeys:
25
+ Enabled: true
26
+
27
+ Style/HashTransformValues:
28
+ Enabled: true
29
+
30
+ Style/Documentation:
31
+ Enabled: false
32
+
33
+ Metrics/PerceivedComplexity:
34
+ Enabled: false
35
+
36
+ Metrics/MethodLength:
37
+ Enabled: false
38
+
39
+ Metrics/AbcSize:
40
+ Enabled: false
41
+
42
+ Metrics/ModuleLength:
43
+ Enabled: false
44
+
45
+ Metrics/BlockLength:
46
+ Enabled: false
47
+
48
+ Metrics/ClassLength:
49
+ Enabled: false
50
+
51
+ Metrics/CyclomaticComplexity:
52
+ Enabled: false
53
+
54
+ Layout/MultilineOperationIndentation:
55
+ Enabled: false
56
+
57
+ Layout/FirstHashElementIndentation:
58
+ Enabled: false
59
+
60
+ Layout/FirstArrayElementIndentation:
61
+ Enabled: false
62
+
63
+ Layout/FirstArgumentIndentation:
64
+ Enabled: false
65
+
66
+ Layout/ClosingParenthesisIndentation:
67
+ Enabled: false
68
+
69
+ Layout/ArgumentAlignment:
70
+ Enabled: false
71
+
@@ -0,0 +1,12 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ services:
6
+ - postgresql
7
+ rvm:
8
+ - 2.5.7
9
+ before_install: gem install bundler -v 2.1.4
10
+ before_script:
11
+ - psql -c 'CREATE DATABASE pg_sql_caller_test;' -U postgres
12
+ - cp -v spec/config/database.travis.yml spec/config/database.yml
@@ -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 senid231@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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in pg_sql_caller.gemspec
6
+ gemspec
7
+
8
+ gem 'database_cleaner'
9
+ gem 'pg'
10
+ gem 'rake', '~> 12.0'
11
+ gem 'rspec', '~> 3.0'
12
+ gem 'rubocop', '~> 0.80.1'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Denis Talakevich
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.
@@ -0,0 +1,72 @@
1
+ # PgSqlCaller
2
+
3
+ Postgresql Sql Caller for ActiveRecord.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pg_sql_caller'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install pg_sql_caller
20
+
21
+ ## Usage
22
+
23
+ create subclass from `PgSqlCaller::Base` and define `model_class` for it
24
+
25
+ ```ruby
26
+ require 'pg_sql_caller'
27
+
28
+ class MySqlCaller < PgSqlCaller::Base
29
+ model_class 'ApplicationRecord'
30
+ end
31
+
32
+ MySqlCaller.select_values 'SELECT id from users WHERE parent_name = ?', 'John Doe' # => [1, 2, 3]
33
+ ```
34
+
35
+ or just define `model_class` for `PgSqlCaller::Base` itself
36
+
37
+ ```ruby
38
+ PgSqlCaller::Base.model_class 'ApplicationRecord'
39
+
40
+ PgSqlCaller::Base.select_values 'SELECT id from users WHERE parent_name = ?', 'John Doe' # => [1, 2, 3]
41
+ ```
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies.
46
+ Create `spec/config/database.yml` (look at `spec/config/database.travis.yml` for example).
47
+ You need to create test database, so run `psql -c 'CREATE DATABASE pg_sql_caller_test;'`.
48
+ Then, run `rake spec` to run the tests.
49
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
50
+
51
+ To install this gem onto your local machine, run `bundle exec rake install`.
52
+ 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).
53
+
54
+ ## TODO
55
+
56
+ * add more tests
57
+ * add more usage examples
58
+ * add documentation
59
+ * release 1.0 after all above
60
+
61
+ ## Contributing
62
+
63
+ Bug reports and pull requests are welcome on GitHub at https://github.com/senid231/pg_sql_caller. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sql_caller/blob/master/CODE_OF_CONDUCT.md).
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
69
+
70
+ ## Code of Conduct
71
+
72
+ Everyone interacting in the PGSqlCaller project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/senid231/pg_sql_caller/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RSpec::Core::RakeTask.new(:spec)
8
+ RuboCop::RakeTask.new(:rubocop)
9
+
10
+ task default: [:rubocop, :spec]
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'pg_sql_caller'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'pg_sql_caller/version'
4
+ require 'pg_sql_caller/base'
5
+
6
+ module PgSqlCaller
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,115 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'singleton'
4
+ require 'forwardable'
5
+ require 'active_support/core_ext/class/attribute'
6
+
7
+ module PgSqlCaller
8
+ class Base
9
+ include Singleton
10
+ extend Forwardable
11
+ extend SingleForwardable
12
+
13
+ CONNECTION_SQL_METHODS = [
14
+ :select_value,
15
+ :select_values,
16
+ :execute,
17
+ :select_all,
18
+ :select_rows,
19
+ :select_row
20
+ ].freeze
21
+
22
+ class_attribute :_model_class, instance_writer: false
23
+
24
+ class << self
25
+ # @names [Array] method names
26
+ def delegate(*names, **options)
27
+ raise ArgumentError, 'provide at least one method name' if names.empty?
28
+
29
+ target = options.fetch(:to)
30
+ type = options.fetch(:type, :instance)
31
+ raise ArgumentError, ':type can be :single or :instance' unless [:single, :instance].include?(type)
32
+
33
+ if type == :instance
34
+ instance_delegate names => target
35
+ else
36
+ single_delegate names => target
37
+ end
38
+ end
39
+
40
+ def define_sql_methods(*names)
41
+ names.each do |name|
42
+ define_method(name) do |sql, *bindings|
43
+ sql = sanitize_sql_array(sql, *bindings) if bindings.any?
44
+ connection.send(name, sql)
45
+ end
46
+ end
47
+ end
48
+
49
+ # @param klass [Class<ActiveRecord::Base>, String] class or class name
50
+ def model_class(klass)
51
+ self._model_class = klass
52
+ end
53
+ end
54
+
55
+ delegate(
56
+ *CONNECTION_SQL_METHODS,
57
+ :transaction_open?,
58
+ :select_all_serialized,
59
+ :transaction,
60
+ :explain_analyze,
61
+ :typecast_array,
62
+ :sanitize_sql_array,
63
+ :current_database,
64
+ *CONNECTION_SQL_METHODS,
65
+ to: :instance,
66
+ type: :single
67
+ )
68
+
69
+ define_sql_methods(*CONNECTION_SQL_METHODS)
70
+
71
+ delegate :transaction_open?, to: :connection
72
+
73
+ def select_all_serialized(sql, *bindings)
74
+ result = select_all(sql, *bindings)
75
+ result.map { |row| row.map { |k, v| [k.to_sym, result.column_types[k].deserialize(v)] }.to_h }
76
+ end
77
+
78
+ def transaction
79
+ raise ArgumentError, 'block must be given' unless block_given?
80
+
81
+ connection.transaction { yield }
82
+ end
83
+
84
+ def explain_analyze(sql)
85
+ result = select_values("EXPLAIN ANALYZE #{sql}")
86
+ ['QUERY_PLAN', *result].join("\n")
87
+ end
88
+
89
+ def typecast_array(values, type:)
90
+ type = ActiveRecord::Type.lookup(type, array: true)
91
+ data = type.serialize(values)
92
+ data.encoder.encode(data.values)
93
+ end
94
+
95
+ def sanitize_sql_array(sql, *bindings)
96
+ model_class.send :sanitize_sql_array, bindings.unshift(sql)
97
+ end
98
+
99
+ def current_database_name
100
+ select_value('SELECT current_database();')
101
+ end
102
+
103
+ private
104
+
105
+ delegate :connection, to: :model_class
106
+
107
+ def model_class
108
+ return @model_class if defined?(@model_class)
109
+
110
+ raise NotImplementedError, "define model_class in #{self.class}" if _model_class.nil?
111
+
112
+ @model_class = _model_class.is_a?(String) ? _model_class.constantize : _model_class
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PgSqlCaller
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/pg_sql_caller/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'pg_sql_caller'
7
+ spec.version = PgSqlCaller::VERSION
8
+ spec.authors = ['Denis Talakevich']
9
+ spec.email = ['senid231@gmail.com']
10
+
11
+ spec.summary = 'Postgresql Sql Caller for ActiveRecord'
12
+ spec.description = 'Postgresql Sql Caller for ActiveRecord.'
13
+ spec.homepage = 'https://github.com/senid231/pg_sql_caller'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = spec.homepage
19
+ spec.metadata['changelog_uri'] = spec.homepage
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
+ end
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'activerecord'
31
+ spec.add_dependency 'activesupport'
32
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg_sql_caller
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Talakevich
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-24 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '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'
41
+ description: Postgresql Sql Caller for ActiveRecord.
42
+ email:
43
+ - senid231@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - ".rubocop.yml"
51
+ - ".travis.yml"
52
+ - CODE_OF_CONDUCT.md
53
+ - Gemfile
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - bin/console
58
+ - bin/setup
59
+ - lib/pg_sql_caller.rb
60
+ - lib/pg_sql_caller/base.rb
61
+ - lib/pg_sql_caller/version.rb
62
+ - sql_caller.gemspec
63
+ homepage: https://github.com/senid231/pg_sql_caller
64
+ licenses:
65
+ - MIT
66
+ metadata:
67
+ homepage_uri: https://github.com/senid231/pg_sql_caller
68
+ source_code_uri: https://github.com/senid231/pg_sql_caller
69
+ changelog_uri: https://github.com/senid231/pg_sql_caller
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: 2.3.0
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubygems_version: 3.1.2
86
+ signing_key:
87
+ specification_version: 4
88
+ summary: Postgresql Sql Caller for ActiveRecord
89
+ test_files: []