sql_runner 0.3.0 → 0.4.0

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: d8ae856dccc72baadf453eab3f66fc6b375757e81d042e309de58b80a3794c22
4
+ data.tar.gz: bc6cdefb9735e36c972e0ae23fc062e746c6ca44f981444fb604fd9e3db388d5
5
5
  SHA512:
6
- metadata.gz: 5e3f6c4efa3b0ebaa7fb53016fe35f4a1948d228491b52e0b7648e31298d29bb96fc267416d93f11973da6c2008ef80d199c07ea62ba2861649a002a110d0f33
7
- data.tar.gz: c62db307f7c62a358fd2c0113ace57b221cb800987290034d2b7c7c09aaef20454efdae92411c164a78ed2ab2560020a4cbf059a8b74fb9db945f454abfd196a
6
+ metadata.gz: 1651fa64b28884dc534d878114bfab992f7d592b30c4b86cb518c24bfb63685795b61bb792db072ce07f7ea9495310b73196d5763b4250f3ea6d2fe7346efb3f
7
+ data.tar.gz: b660ace92f4e0446c8f583505a4d2da82f4854b53cdce460202d83086dd968e5a2a89af8953c484a12dfa023e12dfaa4ac68c38bb9ddb4e78f3144e27bf703bf
data/.gitignore CHANGED
@@ -8,3 +8,4 @@ Gemfile.lock
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  /examples/**/*.html
11
+ *.db
@@ -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)
@@ -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.0"
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
@@ -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,7 +1,7 @@
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.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
@@ -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:
@@ -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