sql_runner 0.3.0 → 0.4.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
  SHA256:
3
- metadata.gz: 89e2f8d25ad8e55c06aae24d62af47733867213d933cfcf7649f17c917d6e019
4
- data.tar.gz: 51ec63734cb7665e87e5a31c3c2ec02c943d6773d5ecfdd1a2d2762a761ac207
3
+ metadata.gz: 3224a627fbfdb75b22aff61e35fce3def752d203da72e6cfc89a80c539cfd413
4
+ data.tar.gz: 0b6102814d498c2fbf53b1b642f3d03b639af95514d5d1d9854d579deb79aee4
5
5
  SHA512:
6
- metadata.gz: 5e3f6c4efa3b0ebaa7fb53016fe35f4a1948d228491b52e0b7648e31298d29bb96fc267416d93f11973da6c2008ef80d199c07ea62ba2861649a002a110d0f33
7
- data.tar.gz: c62db307f7c62a358fd2c0113ace57b221cb800987290034d2b7c7c09aaef20454efdae92411c164a78ed2ab2560020a4cbf059a8b74fb9db945f454abfd196a
6
+ metadata.gz: 6a622ef1f472136f5298a3fa4c37ba3b46f3265e7538c126ad2718e950db0e24fcfd65b49dad8eee47cf7fa699ca0623cc970cebbaabc89f327092ef5998b910
7
+ data.tar.gz: 126036d892682c63236f01dec9152fdf8cbc66e58846e04ca2e4767a408409e1f154cf57104d8cc59b78f03310f359293856e987d1271ab4efdbd72867f97dc1
@@ -0,0 +1,81 @@
1
+ ---
2
+ name: ruby-tests
3
+
4
+ on:
5
+ pull_request_target:
6
+ push:
7
+ branches:
8
+ - main
9
+ workflow_dispatch:
10
+ inputs: {}
11
+
12
+ jobs:
13
+ build:
14
+ name: Tests with Ruby ${{ matrix.ruby }} and ${{ matrix.gemfile }}
15
+ runs-on: "ubuntu-latest"
16
+ if: |
17
+ github.actor == 'dependabot[bot]' && github.event_name == 'pull_request_target' ||
18
+ github.actor != 'dependabot[bot]'
19
+ strategy:
20
+ fail-fast: false
21
+ matrix:
22
+ ruby: ["3.0", "3.1", "3.2"]
23
+ gemfile:
24
+ - Gemfile
25
+
26
+ services:
27
+ postgres:
28
+ image: postgres
29
+ env:
30
+ POSTGRES_DB: test
31
+ POSTGRES_HOST_AUTH_METHOD: trust
32
+ options: >-
33
+ --health-cmd pg_isready --health-interval 10s --health-timeout 5s
34
+ --health-retries 5
35
+ ports:
36
+ - 5432:5432
37
+ mysql:
38
+ image: mysql:8.0
39
+ env:
40
+ MYSQL_ROOT_PASSWORD: test
41
+ MYSQL_PASSWORD: test
42
+ MYSQL_DATABASE: test
43
+ MYSQL_USER: test
44
+ ports:
45
+ - 3306:3306
46
+ options:
47
+ --health-cmd="mysqladmin ping" --health-interval=10s
48
+ --health-timeout=5s --health-retries=3
49
+
50
+ steps:
51
+ - uses: actions/checkout@v3
52
+
53
+ - uses: actions/cache@v3
54
+ with:
55
+ path: vendor/bundle
56
+ key: >
57
+ ${{ runner.os }}-${{ matrix.ruby }}-gems-${{
58
+ hashFiles(matrix.gemfile) }}
59
+
60
+ - name: Set up Ruby
61
+ uses: ruby/setup-ruby@v1
62
+ with:
63
+ ruby-version: ${{ matrix.ruby }}
64
+
65
+ - name: Install gem dependencies
66
+ env:
67
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
68
+ run: |
69
+ gem install bundler
70
+ bundle config path vendor/bundle
71
+ bundle update --jobs 4 --retry 3
72
+
73
+ - name: Run Tests
74
+ env:
75
+ PGHOST: localhost
76
+ PGUSER: postgres
77
+ PG_DATABASE_URL: "postgres://127.0.0.1/test?application_name=sql_runner"
78
+ MYSQL_DATABASE_URL: "mysql2://test:test@127.0.0.1/test?application_name=sql_runner"
79
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
80
+ run: |
81
+ bundle exec rake
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ Gemfile.lock
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /examples/**/*.html
11
+ *.db
data/.rubocop.yml CHANGED
@@ -3,7 +3,8 @@ inherit_gem:
3
3
  rubocop-fnando: .rubocop.yml
4
4
 
5
5
  AllCops:
6
- TargetRubyVersion: 2.6
6
+ NewCops: enable
7
+ TargetRubyVersion: 3.0
7
8
 
8
9
  Metrics/MethodLength:
9
10
  Enabled: false
data/CODE_OF_CONDUCT.md CHANGED
@@ -12,19 +12,19 @@ body size, race, ethnicity, age, religion, or nationality.
12
12
 
13
13
  Examples of unacceptable behavior by participants include:
14
14
 
15
- * The use of sexualized language or imagery
16
- * Personal attacks
17
- * Trolling or insulting/derogatory comments
18
- * Public or private harassment
19
- * Publishing other's private information, such as physical or electronic
15
+ - The use of sexualized language or imagery
16
+ - Personal attacks
17
+ - Trolling or insulting/derogatory comments
18
+ - Public or private harassment
19
+ - Publishing other's private information, such as physical or electronic
20
20
  addresses, without explicit permission
21
- * Other unethical or unprofessional conduct
21
+ - Other unethical or unprofessional conduct
22
22
 
23
- Project maintainers have the right and responsibility to remove, edit, or
24
- reject comments, commits, code, wiki edits, issues, and other contributions
25
- that are not aligned to this Code of Conduct, or to ban temporarily or
26
- permanently any contributor for other behaviors that they deem inappropriate,
27
- threatening, offensive, or harmful.
23
+ Project maintainers have the right and responsibility to remove, edit, or reject
24
+ comments, commits, code, wiki edits, issues, and other contributions that are
25
+ not aligned to this Code of Conduct, or to ban temporarily or permanently any
26
+ contributor for other behaviors that they deem inappropriate, threatening,
27
+ offensive, or harmful.
28
28
 
29
29
  By adopting this Code of Conduct, project maintainers commit themselves to
30
30
  fairly and consistently applying these principles to every aspect of managing
@@ -46,4 +46,4 @@ version 1.3.0, available at
46
46
  [http://contributor-covenant.org/version/1/3/0/][version]
47
47
 
48
48
  [homepage]: http://contributor-covenant.org
49
- [version]: http://contributor-covenant.org/version/1/3/0/
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/README.md CHANGED
@@ -1,8 +1,5 @@
1
1
  # SQLRunner
2
2
 
3
- [![Travis-CI](https://travis-ci.org/fnando/sql_runner.svg)](https://travis-ci.org/fnando/sql_runner)
4
- [![Code Climate](https://codeclimate.com/github/fnando/sql_runner/badges/gpa.svg)](https://codeclimate.com/github/fnando/sql_runner)
5
- [![Test Coverage](https://codeclimate.com/github/fnando/sql_runner/badges/coverage.svg)](https://codeclimate.com/github/fnando/sql_runner/coverage)
6
3
  [![Gem](https://img.shields.io/gem/v/sql_runner.svg)](https://rubygems.org/gems/sql_runner)
7
4
  [![Gem](https://img.shields.io/gem/dt/sql_runner.svg)](https://rubygems.org/gems/sql_runner)
8
5
 
@@ -48,7 +45,7 @@ Run SQL files:
48
45
  SQLRunner.root_dir = "#{__dir__}/sql"
49
46
 
50
47
  class GetMembers < SQLRunner::Query
51
- # by default will use root_dir/get_members.sql
48
+ # by default will use root_dir/get_members.{sql,psql}
52
49
  end
53
50
  ```
54
51
 
@@ -72,7 +69,7 @@ Specify other options:
72
69
 
73
70
  ```ruby
74
71
  class GetMembers < SQLRunner::Query
75
- query_name "users" #=> will load root_dir/users.sql
72
+ query_name "users" #=> will load root_dir/users.{psql,sql}
76
73
  root_dir "/some/path"
77
74
  end
78
75
  ```
@@ -27,6 +27,18 @@ module SQLRunner
27
27
  end
28
28
  end
29
29
 
30
+ class SQLite < SQLRunner::Adapters::SQLite
31
+ def initialize(connection) # rubocop:disable Lint/MissingSuper
32
+ @connection = connection
33
+ end
34
+
35
+ def connect(*)
36
+ end
37
+
38
+ def disconnect(*)
39
+ end
40
+ end
41
+
30
42
  class ConnectionPool
31
43
  def with
32
44
  ::ActiveRecord::Base.connection_pool.with_connection do |connection|
@@ -37,8 +49,12 @@ module SQLRunner
37
49
  PostgreSQL.new(connection)
38
50
  when "Mysql2::Client"
39
51
  MySQL.new(connection)
52
+ when "SQLite3::Database"
53
+ SQLite.new(connection)
40
54
  else
41
- raise UnsupportedDatabase
55
+ raise UnsupportedDatabase,
56
+ "#{connection.class.name} is not yet supported " \
57
+ "by the SQLRunner's ActiveRecord adapter"
42
58
  end
43
59
 
44
60
  yield(adapter)
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SQLRunner
4
+ module Adapters
5
+ class SQLite
6
+ InvalidPreparedStatement = Class.new(StandardError)
7
+
8
+ def self.load
9
+ require "sqlite3"
10
+ rescue LoadError
11
+ raise MissingDependency, "make sure the `sqlite3` gem is available"
12
+ end
13
+
14
+ def self.create_connection_pool(timeout:, size:, connection_string:)
15
+ ConnectionPool.new(timeout: timeout, size: size) do
16
+ new(connection_string)
17
+ end
18
+ end
19
+
20
+ def initialize(connection_string)
21
+ @connection_string = connection_string
22
+ @uri = URI(connection_string)
23
+ connect
24
+ end
25
+
26
+ def connect
27
+ @connection = SQLite3::Database.new(
28
+ @uri.hostname || @uri.opaque,
29
+ results_as_hash: true
30
+ )
31
+ end
32
+
33
+ def disconnect
34
+ @connection&.close && (@connection = nil)
35
+ end
36
+
37
+ def reconnect
38
+ disconnect
39
+ connect
40
+ end
41
+
42
+ def execute(query, **bind_vars)
43
+ _, _, names = parse(query, bind_vars)
44
+ validate_bindings(query, bind_vars, names)
45
+
46
+ @connection.execute(query, **bind_vars)
47
+ end
48
+
49
+ def active?
50
+ !@connection&.closed?
51
+ rescue SQLite3::Exception
52
+ false
53
+ end
54
+
55
+ def to_s
56
+ %[#<#{self.class.name} #{format('0x00%x', (object_id << 1))}>]
57
+ end
58
+
59
+ def inspect
60
+ to_s
61
+ end
62
+
63
+ def parse(query, bind_vars)
64
+ bindings = []
65
+ names = []
66
+
67
+ parsed_query = query.gsub(/(:?):([a-zA-Z]\w*)/) do |match|
68
+ next match if Regexp.last_match(1) == ":" # skip type casting
69
+
70
+ name = match[1..-1]
71
+ sym_name = name.to_sym
72
+ names << sym_name
73
+ bindings << bind_vars[sym_name]
74
+
75
+ "?"
76
+ end
77
+
78
+ [parsed_query, bindings, names]
79
+ end
80
+
81
+ private def validate_bindings(query, bind_vars, names)
82
+ names.each do |name|
83
+ next if bind_vars.key?(name)
84
+
85
+ raise InvalidPreparedStatement,
86
+ "missing value for :#{name} in #{query}"
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
@@ -11,6 +11,7 @@ module SQLRunner
11
11
  module Adapters
12
12
  require "sql_runner/adapters/postgresql"
13
13
  require "sql_runner/adapters/mysql"
14
+ require "sql_runner/adapters/sqlite"
14
15
  require "sql_runner/adapters/active_record"
15
16
 
16
17
  def self.register(name, adapter)
@@ -37,7 +37,14 @@ module SQLRunner
37
37
 
38
38
  def self.query(*value)
39
39
  @query = value.first if value.any?
40
- @query || (@query = File.read(File.join(root_dir, "#{query_name}.sql")))
40
+ @query || (@query = File.read(find_query_file))
41
+ end
42
+
43
+ def self.find_query_file
44
+ [
45
+ File.join(root_dir, "#{query_name}.psql"),
46
+ File.join(root_dir, "#{query_name}.sql")
47
+ ].find {|file| File.file?(file) }
41
48
  end
42
49
 
43
50
  def self.connection_pool
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SQLRunner
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.1"
5
5
  end
data/lib/sql_runner.rb CHANGED
@@ -22,6 +22,8 @@ module SQLRunner
22
22
  Adapters.register("postgresql", Adapters::PostgreSQL)
23
23
  Adapters.register("mysql", Adapters::MySQL)
24
24
  Adapters.register("mysql2", Adapters::MySQL)
25
+ Adapters.register("sqlite", Adapters::SQLite)
26
+ Adapters.register("sqlite3", Adapters::SQLite)
25
27
 
26
28
  Query.register_plugin :one, Query::One
27
29
  Query.register_plugin :many, Query::Many
data/sql_runner.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.version = SQLRunner::VERSION
8
8
  spec.authors = ["Nando Vieira"]
9
9
  spec.email = ["me@fnando.com"]
10
- spec.required_ruby_version = Gem::Requirement.new(">= 2.6.0")
10
+ spec.required_ruby_version = Gem::Requirement.new(">= 3.0.0")
11
11
  spec.metadata = {"rubygems_mfa_required" => "true"}
12
12
 
13
13
  spec.summary = <<~TEXT.tr("\n", " ")
@@ -39,4 +39,5 @@ Gem::Specification.new do |spec|
39
39
  spec.add_development_dependency "rubocop"
40
40
  spec.add_development_dependency "rubocop-fnando"
41
41
  spec.add_development_dependency "simplecov"
42
+ spec.add_development_dependency "sqlite3"
42
43
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sql_runner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-07-28 00:00:00.000000000 Z
11
+ date: 2023-01-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: connection_pool
@@ -178,6 +178,20 @@ dependencies:
178
178
  - - ">="
179
179
  - !ruby/object:Gem::Version
180
180
  version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: sqlite3
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
181
195
  description: SQLRunner allows you to load your queries out of SQL files, without using
182
196
  ORMs.
183
197
  email:
@@ -187,9 +201,9 @@ extensions: []
187
201
  extra_rdoc_files: []
188
202
  files:
189
203
  - ".github/FUNDING.yml"
204
+ - ".github/workflows/test.yml"
190
205
  - ".gitignore"
191
206
  - ".rubocop.yml"
192
- - ".travis.yml"
193
207
  - CODE_OF_CONDUCT.md
194
208
  - Gemfile
195
209
  - LICENSE.txt
@@ -211,6 +225,7 @@ files:
211
225
  - lib/sql_runner/adapters/active_record.rb
212
226
  - lib/sql_runner/adapters/mysql.rb
213
227
  - lib/sql_runner/adapters/postgresql.rb
228
+ - lib/sql_runner/adapters/sqlite.rb
214
229
  - lib/sql_runner/configuration.rb
215
230
  - lib/sql_runner/connection.rb
216
231
  - lib/sql_runner/query.rb
@@ -233,14 +248,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
233
248
  requirements:
234
249
  - - ">="
235
250
  - !ruby/object:Gem::Version
236
- version: 2.6.0
251
+ version: 3.0.0
237
252
  required_rubygems_version: !ruby/object:Gem::Requirement
238
253
  requirements:
239
254
  - - ">="
240
255
  - !ruby/object:Gem::Version
241
256
  version: '0'
242
257
  requirements: []
243
- rubygems_version: 3.3.7
258
+ rubygems_version: 3.4.1
244
259
  signing_key:
245
260
  specification_version: 4
246
261
  summary: SQLRunner allows you to load your queries out of SQL files, without using
data/.travis.yml DELETED
@@ -1,29 +0,0 @@
1
- ---
2
- language: ruby
3
- cache: bundler
4
- sudo: false
5
- rvm:
6
- - 2.7.0
7
- services:
8
- - mysql
9
- addons:
10
- postgresql: "10"
11
- apt:
12
- packages:
13
- - postgresql-10
14
- - postgresql-client-10
15
- before_script:
16
- - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
17
- - chmod +x ./cc-test-reporter
18
- - "./cc-test-reporter before-build"
19
- after_script:
20
- - "./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT"
21
- before_install:
22
- - gem install bundler
23
- - createdb test
24
- - mysql -e 'CREATE DATABASE IF NOT EXISTS test;'
25
- notifications:
26
- email: false
27
- env:
28
- global:
29
- secure: 4dDrl8nCItKPRKpoA2KJVWsDm3o7U0pIsdY8t19pJXbziteT3zw5pEmFh+/qWGB1VszzFFZzXcZCIoKv3NX/x24g+LU+qvL7vLYLyhUR8ZjR4rGzGkwBCgEYBWgVtqg9ncYTbYsdbAqryt6f+HvpFj8EFvGSjIWVqJyh59qHdwAVT+BUKjllEH+t5Dbjd2knIQw83af+0A5ljP2uMziNcuHD7MUBIKY1DfFnBYQ5YA50o/mZe8sG5h6bZU/sf0pdoa+z2xDIJOPO7qaCwANACetDtsfs1DN39DsubvlFLg0s2z0XCuYyWSsJQ4zhRipHgnqYn+Rmpql17t0WmhVPA1xvLyk0/4X8rjRcYyYHMM3ryga5bnrpvSiPsqG+JFNhDczpN394KGa7o+/jCuNA0MVFcCzsvW2AMkYpUbPtADY7/Tl4iUJWmFbl0nO1n1wh5dDJ7Wo7mdkPAmdV7hNmMUmHbPeh8mSjMnuS2gcMc11wDgmR56TEMu/B7LUM31og7L+JouEJAOWtzThtpdYLpqDQ3Xe1G5uMl1oZ0lNOqag5Bg+AQCAIGr1N4G2m8fI74M9lUQUfhu87L+zycYMHInOH+Czc+RamlkH3vLl9Fu6aOKoXA0+zh85sVDj3Er+X2NEHaoRe4PSkOHOBNYSkAUd36TYMt8J5MgAD0w7HqmY=