db 0.11.0 → 0.12.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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/db/client.rb +8 -18
- data/lib/db/context/session.rb +40 -23
- data/lib/db/context/transaction.rb +7 -1
- data/lib/db/query.rb +3 -3
- data/lib/db/version.rb +1 -1
- data/lib/db.rb +3 -3
- data/readme.md +8 -0
- data.tar.gz.sig +0 -0
- metadata +2 -3
- metadata.gz.sig +0 -0
- data/lib/db/context/transient.rb +0 -19
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 190f7bf46c9e6f9e7af5b291eb71d07db32a6db3ac8997bc73dd50a058327744
|
4
|
+
data.tar.gz: fac1ca594b8f900b4ff7ac6e68cca1930c24fbc199a5187176f8d5e30e9006e7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
6
|
+
require "async/pool/controller"
|
7
7
|
|
8
|
-
require_relative
|
9
|
-
require_relative
|
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.
|
60
|
+
transaction.begin
|
71
61
|
|
72
62
|
return transaction unless block_given?
|
73
63
|
|
data/lib/db/context/session.rb
CHANGED
@@ -3,8 +3,8 @@
|
|
3
3
|
# Released under the MIT License.
|
4
4
|
# Copyright, 2020-2024, by Samuel Williams.
|
5
5
|
|
6
|
-
require_relative
|
7
|
-
require_relative
|
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
|
-
|
20
|
-
|
21
|
-
end
|
19
|
+
attr :pool
|
20
|
+
attr :connection
|
22
21
|
|
23
|
-
#
|
24
|
-
def
|
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
|
41
|
-
if
|
42
|
-
|
39
|
+
def with_connection(&block)
|
40
|
+
if @connection
|
41
|
+
yield @connection
|
43
42
|
else
|
44
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
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
|
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 <<
|
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 <<
|
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 <<
|
65
|
+
@buffer << " " unless @buffer.end_with?(" ")
|
66
66
|
|
67
67
|
@connection.append_identifier(value, @buffer)
|
68
68
|
|
data/lib/db/version.rb
CHANGED
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
|
7
|
-
require_relative
|
8
|
-
require_relative
|
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.
|
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-
|
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
|
data/lib/db/context/transient.rb
DELETED
@@ -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
|