db 0.11.0 → 0.12.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: '00818022e4f4c8f40dae41f5f7850b89021cbcb8165f2a1e4637d07520a2c748'
4
- data.tar.gz: 5d4ce254968d4f5187469537f2c1553970ba32e9125bea3a43de40ceb8d3bc40
3
+ metadata.gz: 190f7bf46c9e6f9e7af5b291eb71d07db32a6db3ac8997bc73dd50a058327744
4
+ data.tar.gz: fac1ca594b8f900b4ff7ac6e68cca1930c24fbc199a5187176f8d5e30e9006e7
5
5
  SHA512:
6
- metadata.gz: cb6f3c928363fbc5c9a12ebf6bc902636e93202ee3308edf28d4370f40c2c69c5b57d9c64f873e66642bba3afa025bcd1934366011a52040c9f882b931fafc40
7
- data.tar.gz: 0d0b154d9817cbc191c9a16203e5088538fe37375af00e060be6ae4c29379c0e03d61933420b15a36ab4efcbe003e1c279b38f0242b25b6afe156a3e10103980
6
+ metadata.gz: 1d0e7f56d1ea4f323e3c7d1525d3f4f2ef35cc8c9509b3d4f181ac3e992cd9c3ae954c696acc0b898ca1c196fd5638f46d0dac5ac36bdc99d9f989e081f24761
7
+ data.tar.gz: a5bca6583eeff6c0b1eb4debb7b9b8ff3d3011de914394742905bfad795e9f02ed6f65d452a36a9953db13016df67fffc546430df1f8269c60763fb7bfcfe0dc
checksums.yaml.gz.sig CHANGED
Binary file
data/lib/db/client.rb CHANGED
@@ -3,11 +3,10 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
- require 'async/pool/controller'
6
+ require "async/pool/controller"
7
7
 
8
- require_relative 'context/transient'
9
- require_relative 'context/session'
10
- require_relative 'context/transaction'
8
+ require_relative "context/session"
9
+ require_relative "context/transaction"
11
10
 
12
11
  module DB
13
12
  # Binds a connection pool to the specified adapter.
@@ -29,19 +28,6 @@ module DB
29
28
  @pool.close
30
29
  end
31
30
 
32
- # Acquire a generic context which will acquire a connection on demand.
33
- def context(**options)
34
- context = Context::Transient.new(@pool, **options)
35
-
36
- return context unless block_given?
37
-
38
- begin
39
- yield context
40
- ensure
41
- context.close
42
- end
43
- end
44
-
45
31
  # Acquires a connection and sends the specified statement if given.
46
32
  # @parameters statement [String | Nil] An optional statement to send.
47
33
  # @yields {|session| ...} A connected session if a block is given. Implicitly closed.
@@ -53,12 +39,16 @@ module DB
53
39
  return session unless block_given?
54
40
 
55
41
  begin
42
+ session.connect!
43
+
56
44
  yield session
57
45
  ensure
58
46
  session.close
59
47
  end
60
48
  end
61
49
 
50
+ alias context session
51
+
62
52
  # Acquires a connection and starts a transaction.
63
53
  # @parameters statement [String | Nil] An optional statement to send. Defaults to `"BEGIN"`.
64
54
  # @yields {|session| ...} A connected session if a block is given. Implicitly commits, or aborts the connnection if an exception is raised.
@@ -67,7 +57,7 @@ module DB
67
57
  def transaction(**options)
68
58
  transaction = Context::Transaction.new(@pool, **options)
69
59
 
70
- transaction.call("BEGIN")
60
+ transaction.begin
71
61
 
72
62
  return transaction unless block_given?
73
63
 
@@ -3,8 +3,8 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
- require_relative '../query'
7
- require_relative '../records'
6
+ require_relative "../query"
7
+ require_relative "../records"
8
8
 
9
9
  module DB
10
10
  module Context
@@ -16,12 +16,11 @@ module DB
16
16
  @connection = nil
17
17
  end
18
18
 
19
- def connection?
20
- @connection != nil
21
- end
19
+ attr :pool
20
+ attr :connection
22
21
 
23
- # Lazy initialize underlying connection.
24
- def connection
22
+ # Pin a connection to the current session.
23
+ def connect!
25
24
  @connection ||= @pool.acquire
26
25
  end
27
26
 
@@ -37,29 +36,47 @@ module DB
37
36
  @connection.nil?
38
37
  end
39
38
 
40
- def query(fragment = String.new, **parameters)
41
- if parameters.empty?
42
- Query.new(self, fragment)
39
+ def with_connection(&block)
40
+ if @connection
41
+ yield @connection
43
42
  else
44
- Query.new(self).interpolate(fragment, **parameters)
43
+ @pool.acquire do |connection|
44
+ @connection = connection
45
+
46
+ yield connection
47
+ ensure
48
+ @connection = nil
49
+ end
45
50
  end
46
51
  end
47
52
 
48
- def clause(fragment = String.new)
49
- Query.new(self, fragment)
50
- end
51
-
52
53
  # Send a query to the server.
53
54
  # @parameter statement [String] The SQL query to send.
54
55
  def call(statement, **options)
55
- connection = self.connection
56
-
57
- connection.send_query(statement, **options)
58
-
59
- if block_given?
60
- yield connection
61
- elsif result = connection.next_result
62
- return Records.wrap(result)
56
+ self.with_connection do |connection|
57
+ connection.send_query(statement, **options)
58
+
59
+ if block_given?
60
+ yield connection
61
+ elsif result = connection.next_result
62
+ return Records.wrap(result)
63
+ end
64
+ end
65
+ end
66
+
67
+ def query(fragment = String.new, **parameters)
68
+ with_connection do
69
+ if parameters.empty?
70
+ Query.new(self, fragment)
71
+ else
72
+ Query.new(self).interpolate(fragment, **parameters)
73
+ end
74
+ end
75
+ end
76
+
77
+ def clause(fragment = String.new)
78
+ with_connection do
79
+ Query.new(self, fragment)
63
80
  end
64
81
  end
65
82
  end
@@ -3,11 +3,17 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
- require_relative 'session'
6
+ require_relative "session"
7
7
 
8
8
  module DB
9
9
  module Context
10
10
  class Transaction < Session
11
+ # Begin a transaction.
12
+ def begin
13
+ self.connect!
14
+ self.call("BEGIN")
15
+ end
16
+
11
17
  # Commit the transaction and return the connection to the connection pool.
12
18
  def commit
13
19
  self.call("COMMIT")
data/lib/db/query.rb CHANGED
@@ -38,7 +38,7 @@ module DB
38
38
  # @parameter value [String] A raw SQL string, e.g. `WHERE x > 10`.
39
39
  # @returns [Query] The mutable query itself.
40
40
  def clause(value)
41
- @buffer << ' ' unless @buffer.end_with?(' ') || @buffer.empty?
41
+ @buffer << " " unless @buffer.end_with?(" ") || @buffer.empty?
42
42
 
43
43
  @buffer << value
44
44
 
@@ -50,7 +50,7 @@ module DB
50
50
  # @parameter value [Object] Any kind of object, passed to the underlying database connection for conversion to a string representation.
51
51
  # @returns [Query] The mutable query itself.
52
52
  def literal(value)
53
- @buffer << ' ' unless @buffer.end_with?(' ')
53
+ @buffer << " " unless @buffer.end_with?(" ")
54
54
 
55
55
  @connection.append_literal(value, @buffer)
56
56
 
@@ -62,7 +62,7 @@ module DB
62
62
  # @parameter value [String | Symbol | DB::Identifier] Passed to the underlying database connection for conversion to a string representation.
63
63
  # @returns [Query] The mutable query itself.
64
64
  def identifier(value)
65
- @buffer << ' ' unless @buffer.end_with?(' ')
65
+ @buffer << " " unless @buffer.end_with?(" ")
66
66
 
67
67
  @connection.append_identifier(value, @buffer)
68
68
 
data/lib/db/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
6
  module DB
7
- VERSION = "0.11.0"
7
+ VERSION = "0.12.0"
8
8
  end
data/lib/db.rb CHANGED
@@ -3,6 +3,6 @@
3
3
  # Released under the MIT License.
4
4
  # Copyright, 2020-2024, by Samuel Williams.
5
5
 
6
- require_relative 'db/version'
7
- require_relative 'db/adapters'
8
- require_relative 'db/client'
6
+ require_relative "db/version"
7
+ require_relative "db/adapters"
8
+ require_relative "db/client"
data/readme.md CHANGED
@@ -21,6 +21,14 @@ Please see the [project documentation](https://socketry.github.io/db/) for more
21
21
 
22
22
  - [Data Types](https://socketry.github.io/db/guides/datatypes/index) - This guide explains about SQL data types, and how they are used by the DB gem.
23
23
 
24
+ ## See Also
25
+
26
+ - [db-postgres](https://github.com/socketry/db-postgres) - Postgres adapter for the DB gem.
27
+ - [db-mariadb](https://github.com/socketry/db-mariadb) - MariaDB/MySQL adapter for the DB gem.
28
+ - [db-model](https://github.com/socketry/db-model) - A simple object relational mapper (ORM) for the DB gem.
29
+ - [db-migrate](https://github.com/socketry/db-migrate) - Database migration tooling for the DB gem.
30
+ - [db-active\_record](https://github.com/socketry/db-active_record) - An ActiveRecord adapter for the DB gem.
31
+
24
32
  ## Contributing
25
33
 
26
34
  We welcome contributions to this project.
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.0
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -37,7 +37,7 @@ cert_chain:
37
37
  Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
38
38
  voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
39
39
  -----END CERTIFICATE-----
40
- date: 2024-07-27 00:00:00.000000000 Z
40
+ date: 2024-09-21 00:00:00.000000000 Z
41
41
  dependencies:
42
42
  - !ruby/object:Gem::Dependency
43
43
  name: async-pool
@@ -64,7 +64,6 @@ files:
64
64
  - lib/db/client.rb
65
65
  - lib/db/context/session.rb
66
66
  - lib/db/context/transaction.rb
67
- - lib/db/context/transient.rb
68
67
  - lib/db/query.rb
69
68
  - lib/db/records.rb
70
69
  - lib/db/version.rb
metadata.gz.sig CHANGED
Binary file
@@ -1,19 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Released under the MIT License.
4
- # Copyright, 2020-2024, by Samuel Williams.
5
-
6
- require_relative 'session'
7
-
8
- module DB
9
- module Context
10
- # A connected context for sending queries and reading results.
11
- class Transient < Session
12
- def call(statement, **options, &block)
13
- super
14
- ensure
15
- self.close
16
- end
17
- end
18
- end
19
- end