db 0.5.1 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/db/client.rb +9 -9
- data/lib/db/context/{query.rb → session.rb} +17 -1
- data/lib/db/context/transaction.rb +2 -2
- data/lib/db/query.rb +112 -0
- data/lib/db/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: def9c55f80bfb45766ffd249a05f9c449432259bf15e57a8e00b59dabc75dc91
|
4
|
+
data.tar.gz: 8632f9630388c98ab4587c708acf8c1506edddcd8c29284a2b66446f4dbf799e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 11f1c66a99d6dec1346f890c798efa65b77a3750e581a67dbcbbf20d970b6e5c30b337a4aeafd3d8375d68151585b6d74edc6b6cafaee4c539f6e857748424c0
|
7
|
+
data.tar.gz: eb43e9b1077fc237acc6cde551aeaae5299ce8fa1f6b5580b5672ae1a3ecd9d5dcee6195344b56c6b5256af817c331af39fc79a0a3a5a60e0564c21ff4b0aa62
|
data/lib/db/client.rb
CHANGED
@@ -24,7 +24,7 @@ require 'async/io'
|
|
24
24
|
require 'async/io/stream'
|
25
25
|
require 'async/pool/controller'
|
26
26
|
|
27
|
-
require_relative 'context/
|
27
|
+
require_relative 'context/session'
|
28
28
|
require_relative 'context/transaction'
|
29
29
|
|
30
30
|
module DB
|
@@ -50,21 +50,21 @@ module DB
|
|
50
50
|
# Acquires a connection and sends the specified statement if given.
|
51
51
|
# @parameters statement [String | Nil] An optional statement to send.
|
52
52
|
# @yields {|session| ...} A connected session if a block is given. Implicitly closed.
|
53
|
-
# @parameter session [Context::
|
54
|
-
# @returns [Context::
|
55
|
-
def
|
56
|
-
|
53
|
+
# @parameter session [Context::Session]
|
54
|
+
# @returns [Context::Session] A connected session if no block is given.
|
55
|
+
def session(statement = nil, **options)
|
56
|
+
session = Context::Session.new(@pool, **options)
|
57
57
|
|
58
58
|
if statement
|
59
|
-
|
59
|
+
session.send_query(statement)
|
60
60
|
end
|
61
61
|
|
62
|
-
return
|
62
|
+
return session unless block_given?
|
63
63
|
|
64
64
|
begin
|
65
|
-
yield
|
65
|
+
yield session
|
66
66
|
ensure
|
67
|
-
|
67
|
+
session.close
|
68
68
|
end
|
69
69
|
end
|
70
70
|
|
@@ -20,10 +20,12 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
|
+
require_relative '../query'
|
24
|
+
|
23
25
|
module DB
|
24
26
|
module Context
|
25
27
|
# A connected context for sending queries and reading results.
|
26
|
-
class
|
28
|
+
class Session
|
27
29
|
# Iniitalize the query context attached to the given connection pool.
|
28
30
|
def initialize(pool, **options)
|
29
31
|
@pool = pool
|
@@ -44,9 +46,22 @@ module DB
|
|
44
46
|
end
|
45
47
|
end
|
46
48
|
|
49
|
+
def clause(*arguments)
|
50
|
+
Query.new(self).clause(*arguments)
|
51
|
+
end
|
52
|
+
|
53
|
+
def query(fragment, **parameters)
|
54
|
+
if parameters.empty?
|
55
|
+
Query.new(self).clause(fragment)
|
56
|
+
else
|
57
|
+
Query.new(self).interpolate(fragment, **parameters)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
47
61
|
# Send a query to the server.
|
48
62
|
# @parameter statement [String] The SQL query to send.
|
49
63
|
def send_query(statement, **options)
|
64
|
+
# Console.logger.info(self, statement)
|
50
65
|
@connection.send_query(statement, **options)
|
51
66
|
end
|
52
67
|
|
@@ -59,6 +74,7 @@ module DB
|
|
59
74
|
# Send a query to the server and read the next result.
|
60
75
|
# @returns [Enumerable] The resulting records.
|
61
76
|
def call(statement, **options)
|
77
|
+
# Console.logger.info(self, statement)
|
62
78
|
@connection.send_query(statement, **options)
|
63
79
|
|
64
80
|
return @connection.next_result
|
@@ -20,11 +20,11 @@
|
|
20
20
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
21
|
# THE SOFTWARE.
|
22
22
|
|
23
|
-
require_relative '
|
23
|
+
require_relative 'session'
|
24
24
|
|
25
25
|
module DB
|
26
26
|
module Context
|
27
|
-
class Transaction <
|
27
|
+
class Transaction < Session
|
28
28
|
# Commit the transaction and return the connection to the connection pool.
|
29
29
|
def commit
|
30
30
|
self.call("COMMIT")
|
data/lib/db/query.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
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.
|
22
|
+
|
23
|
+
module DB
|
24
|
+
# Represents one or more identifiers for databases, tables or columns.
|
25
|
+
class Identifier < Array
|
26
|
+
# Construct an identifier from an array of names.
|
27
|
+
# e.g. `DB::Identifier[:mytable, :mycolumn]`
|
28
|
+
#
|
29
|
+
# @parameter names [Array] The array of names.
|
30
|
+
def self.[](*names)
|
31
|
+
self.new(names)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# A mutable query builder.
|
36
|
+
class Query
|
37
|
+
# Create a new query builder attached to the specified session.
|
38
|
+
# @parameter session [Context::Session] the connected session which is used for escaping arguments.
|
39
|
+
def initialize(session)
|
40
|
+
@session = session
|
41
|
+
@connection = session.connection
|
42
|
+
@buffer = String.new
|
43
|
+
end
|
44
|
+
|
45
|
+
# Append a raw textual clause to the query buffer.
|
46
|
+
# @parameter value [String] A raw SQL string, e.g. `WHERE x > 10`.
|
47
|
+
# @returns [Query] The mutable query itself.
|
48
|
+
def clause(value)
|
49
|
+
@buffer << ' ' unless @buffer.end_with?(' ')
|
50
|
+
|
51
|
+
@buffer << value
|
52
|
+
|
53
|
+
return self
|
54
|
+
end
|
55
|
+
|
56
|
+
# Append a literal value to the query buffer.
|
57
|
+
# Escapes the field according to the requirements of the underlying connection.
|
58
|
+
# @parameter value [Object] Any kind of object, passed to the underlying database connection for conversion to a string representation.
|
59
|
+
# @returns [Query] The mutable query itself.
|
60
|
+
def literal(value)
|
61
|
+
@buffer << ' ' unless @buffer.end_with?(' ')
|
62
|
+
|
63
|
+
@connection.append_literal(value, @buffer)
|
64
|
+
|
65
|
+
return self
|
66
|
+
end
|
67
|
+
|
68
|
+
# Append an identifier value to the query buffer.
|
69
|
+
# Escapes the field according to the requirements of the underlying connection.
|
70
|
+
# @parameter value [String | Symbol | DB::Identifier] Passed to the underlying database connection for conversion to a string representation.
|
71
|
+
# @returns [Query] The mutable query itself.
|
72
|
+
def identifier(value)
|
73
|
+
@buffer << ' ' unless @buffer.end_with?(' ')
|
74
|
+
|
75
|
+
@connection.append_identifier(value, @buffer)
|
76
|
+
|
77
|
+
return self
|
78
|
+
end
|
79
|
+
|
80
|
+
# Interpolate a query fragment with the specified parameters.
|
81
|
+
# The parameters are escaped before being appended.
|
82
|
+
#
|
83
|
+
# @parameter fragment [String] A fragment of SQL including placeholders, e.g. `WHERE x > %{column}`.
|
84
|
+
# @parameter parameters [Hash] The substitution parameters.
|
85
|
+
# @returns [Query] The mutable query itself.
|
86
|
+
def interpolate(fragment, **parameters)
|
87
|
+
parameters.transform_values! do |value|
|
88
|
+
case value
|
89
|
+
when Symbol, Identifier
|
90
|
+
@connection.append_identifier(value)
|
91
|
+
else
|
92
|
+
@connection.append_literal(value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
@buffer << sprintf(fragment, parameters)
|
97
|
+
|
98
|
+
return self
|
99
|
+
end
|
100
|
+
|
101
|
+
# Send the query to the remote server to be executed. See {Context::Session#send_query} for more details.
|
102
|
+
def send
|
103
|
+
@session.send_query(@buffer)
|
104
|
+
end
|
105
|
+
|
106
|
+
# Send the query to the remote server to be executed. See {Context::Session#call} for more details.
|
107
|
+
# @returns [Enumerable] The resulting records.
|
108
|
+
def call
|
109
|
+
@session.call(@buffer)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
data/lib/db/version.rb
CHANGED
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.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
@@ -89,8 +89,9 @@ files:
|
|
89
89
|
- lib/db.rb
|
90
90
|
- lib/db/adapters.rb
|
91
91
|
- lib/db/client.rb
|
92
|
-
- lib/db/context/
|
92
|
+
- lib/db/context/session.rb
|
93
93
|
- lib/db/context/transaction.rb
|
94
|
+
- lib/db/query.rb
|
94
95
|
- lib/db/version.rb
|
95
96
|
homepage: https://github.com/socketry/db
|
96
97
|
licenses:
|