db 0.5.0 → 0.5.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: 6969650d4cd9fde35de4541f64d10b67b78f0e55e42d4f4b6d6daff54022ed62
4
- data.tar.gz: f97c19216d6ccaa88fc5fb37c71e40c614e4e843c928df37edfb1723d4afbeb5
3
+ metadata.gz: 1149f3daa25b0832441ed9c08eeba087be727167ff5ee12b33e2d96a2ae54138
4
+ data.tar.gz: efc580a879bc9d91c46bc631a76b5761f276ce24e531daf20bc797e75ea06273
5
5
  SHA512:
6
- metadata.gz: 04fa032c0a0d79283fa5d51fff48ecac9d8dd5d04b1f0e7cfef9b5d279d63f7d1b947185ff8a257b1df0e031cdae7b7b548d75a4bbe6a7d85889764fb1c83e30
7
- data.tar.gz: e695f898cf1e6b37e130f3addd3d8631a3451f62cba9b49ec52584b018a2d8900ed5ef60c5175a10e654d9b034295e244ffafddfb8ba3be1505b633dc5cd2696
6
+ metadata.gz: 104d7bce64424a3666c1a699e8b06c655a37dc44f5b025530a422188d386c3b84ad00ccc314ba8d0a4f5b9c931375c21c0799e6d0868064d012220d5fc26e6cd
7
+ data.tar.gz: 856321b31c8894f5fe914100b5892cc160c812f56b6bf0cc8ec4f7bc159d9535195bf5c8d406e0ca1116b6fc4d73ecc2527b03d630bf336c6d768060c11918c2
@@ -1,12 +1,43 @@
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.
1
22
 
2
23
  module DB
24
+ # A global map of registered adapters.
25
+ # e.g. `DB::Adapters.register(:mariadb, DB::MariaDB::Adapter)`
3
26
  module Adapters
4
27
  @adapters = {}
5
28
 
29
+ # Register the adapter class to the specified name.
30
+ # @parameter name [Symbol] The adapter name.
31
+ # @parameter adapter [Class] The adapter class.
6
32
  def self.register(name, adapter)
7
33
  @adapters[name] = adapter
8
34
  end
9
35
 
36
+ # Enumerate all registered adapters.
37
+ # @yields {|name, adapter| ...} The adapters if a block is given.
38
+ # @parameter name [Symbol] The adapter name.
39
+ # @parameter adapter [Class] The adapter class
40
+ # @returns [Enumerator(Symbol, Class)] If no block is given.
10
41
  def self.each(&block)
11
42
  @adapters.each(&block)
12
43
  end
@@ -28,36 +28,30 @@ require_relative 'context/query'
28
28
  require_relative 'context/transaction'
29
29
 
30
30
  module DB
31
+ # Binds a connection pool to the specified adapter.
31
32
  class Client
33
+ # Initialize the client and internal connection pool using the specified adapter.
34
+ # @parameter adapter [Object] The adapter instance.
32
35
  def initialize(adapter, **options)
33
36
  @adapter = adapter
34
37
 
35
38
  @pool = connect(**options)
36
39
  end
37
40
 
38
- attr :endpoint
39
- attr :protocol
40
-
41
- # @return [client] if no block provided.
42
- # @yield [client, task] yield the client in an async task.
43
- def self.open(*arguments, &block)
44
- client = self.new(*arguments)
45
-
46
- return client unless block_given?
47
-
48
- Async do |task|
49
- begin
50
- yield client, task
51
- ensure
52
- client.close
53
- end
54
- end.wait
55
- end
41
+ # The adapter used for making connections.
42
+ # @attribute [Object]
43
+ attr :adapter
56
44
 
45
+ # Close all open connections in the connection pool.
57
46
  def close
58
47
  @pool.close
59
48
  end
60
49
 
50
+ # Acquires a connection and sends the specified statement if given.
51
+ # @parameters statement [String | Nil] An optional statement to send.
52
+ # @yields {|session| ...} A connected session if a block is given. Implicitly closed.
53
+ # @parameter session [Context::Query]
54
+ # @returns [Context::Query] A connected session if no block is given.
61
55
  def call(statement = nil, **options)
62
56
  query = Context::Query.new(@pool, **options)
63
57
 
@@ -74,6 +68,11 @@ module DB
74
68
  end
75
69
  end
76
70
 
71
+ # Acquires a connection and starts a transaction.
72
+ # @parameters statement [String | Nil] An optional statement to send. Defaults to `"BEGIN"`.
73
+ # @yields {|session| ...} A connected session if a block is given. Implicitly commits, or aborts the connnection if an exception is raised.
74
+ # @parameter session [Context::Transaction]
75
+ # @returns [Context::Transaction] A connected and started transaction if no block is given.
77
76
  def transaction(statement = "BEGIN", **options)
78
77
  transaction = Context::Transaction.new(@pool, **options)
79
78
 
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- # Copyright, 2018, by Huba Nagy.
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
7
6
  # of this software and associated documentation files (the "Software"), to deal
@@ -23,14 +22,18 @@
23
22
 
24
23
  module DB
25
24
  module Context
25
+ # A connected context for sending queries and reading results.
26
26
  class Query
27
+ # Iniitalize the query context attached to the given connection pool.
27
28
  def initialize(pool, **options)
28
29
  @pool = pool
29
30
  @connection = pool.acquire
30
31
  end
31
32
 
33
+ # The underlying connection.
32
34
  attr :connection
33
35
 
36
+ # Flush the connection and then return it to the connection pool.
34
37
  def close
35
38
  if @connection
36
39
  self.flush
@@ -41,20 +44,29 @@ module DB
41
44
  end
42
45
  end
43
46
 
47
+ # Send a query to the server.
48
+ # @parameter statement [String] The SQL query to send.
44
49
  def send_query(statement, **options)
45
50
  @connection.send_query(statement, **options)
46
51
  end
47
52
 
53
+ # Read the next result. Sending a query usually generates 1 or more results.
54
+ # @returns [Enumerable] The resulting records.
48
55
  def next_result
49
56
  @connection.next_result
50
57
  end
51
58
 
59
+ # Send a query to the server and read the next result.
60
+ # @returns [Enumerable] The resulting records.
52
61
  def call(statement, **options)
53
62
  @connection.send_query(statement, **options)
54
63
 
55
64
  return @connection.next_result
56
65
  end
57
66
 
67
+ # Enumerate all results.
68
+ # @yields {|result ...} The results if a block is given.
69
+ # @parameter result [Enumerable]
58
70
  def results
59
71
  while result = self.next_result
60
72
  yield result
@@ -63,6 +75,7 @@ module DB
63
75
  return nil
64
76
  end
65
77
 
78
+ # Flush all outstanding results.
66
79
  def flush
67
80
  until @connection.next_result.nil?
68
81
  end
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
- # Copyright, 2018, by Huba Nagy.
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
5
4
  #
6
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
7
6
  # of this software and associated documentation files (the "Software"), to deal
@@ -26,20 +25,24 @@ require_relative 'query'
26
25
  module DB
27
26
  module Context
28
27
  class Transaction < Query
28
+ # Commit the transaction and return the connection to the connection pool.
29
29
  def commit
30
30
  self.call("COMMIT")
31
31
  self.close
32
32
  end
33
33
 
34
+ # Abort the transaction and return the connection to the connection pool.
34
35
  def abort
35
36
  self.call("ROLLBACK")
36
37
  self.close
37
38
  end
38
39
 
40
+ # Mark a savepoint in the transaction.
39
41
  def savepoint(name)
40
42
  self.call("SAVEPOINT #{name}")
41
43
  end
42
44
 
45
+ # Return back to a previously registered savepoint.
43
46
  def rollback(name)
44
47
  self.call("ROLLBACK #{name}")
45
48
  end
@@ -21,5 +21,5 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  module DB
24
- VERSION = "0.5.0"
24
+ VERSION = "0.5.1"
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-06 00:00:00.000000000 Z
11
+ date: 2020-07-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: async-io
@@ -39,21 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bake
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: bake-bundler
42
+ name: bundler
57
43
  requirement: !ruby/object:Gem::Requirement
58
44
  requirements:
59
45
  - - ">="
@@ -80,20 +66,6 @@ dependencies:
80
66
  - - ">="
81
67
  - !ruby/object:Gem::Version
82
68
  version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: bundler
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '2.0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '2.0'
97
69
  - !ruby/object:Gem::Dependency
98
70
  name: rspec
99
71
  requirement: !ruby/object:Gem::Requirement
@@ -110,17 +82,10 @@ dependencies:
110
82
  version: '3.0'
111
83
  description:
112
84
  email:
113
- - samuel.williams@oriontransfer.co.nz
114
85
  executables: []
115
86
  extensions: []
116
87
  extra_rdoc_files: []
117
88
  files:
118
- - ".github/workflows/development.yml"
119
- - ".gitignore"
120
- - ".rspec"
121
- - README.md
122
- - db.gemspec
123
- - gems.rb
124
89
  - lib/db.rb
125
90
  - lib/db/adapters.rb
126
91
  - lib/db/client.rb
@@ -128,7 +93,8 @@ files:
128
93
  - lib/db/context/transaction.rb
129
94
  - lib/db/version.rb
130
95
  homepage: https://github.com/socketry/db
131
- licenses: []
96
+ licenses:
97
+ - MIT
132
98
  metadata: {}
133
99
  post_install_message:
134
100
  rdoc_options: []
@@ -138,7 +104,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
138
104
  requirements:
139
105
  - - ">="
140
106
  - !ruby/object:Gem::Version
141
- version: '0'
107
+ version: '2.5'
142
108
  required_rubygems_version: !ruby/object:Gem::Requirement
143
109
  requirements:
144
110
  - - ">="
@@ -1,65 +0,0 @@
1
- name: Development
2
-
3
- on: [push, pull_request]
4
-
5
- jobs:
6
- test:
7
- runs-on: ${{matrix.os}}-latest
8
- continue-on-error: ${{matrix.experimental}}
9
-
10
- strategy:
11
- matrix:
12
- os:
13
- - ubuntu
14
-
15
- ruby:
16
- - 2.5
17
- - 2.6
18
- - 2.7
19
-
20
- experimental: [false]
21
- env: [""]
22
-
23
- include:
24
- - os: ubuntu
25
- ruby: truffleruby
26
- experimental: true
27
- - os: ubuntu
28
- ruby: jruby
29
- experimental: true
30
- env: JRUBY_OPTS="--debug -X+O"
31
- - os: ubuntu
32
- ruby: head
33
- experimental: true
34
-
35
- steps:
36
- - uses: actions/checkout@v2
37
- - uses: ruby/setup-ruby@v1
38
- with:
39
- ruby-version: ${{matrix.ruby}}
40
-
41
- - name: Install dependencies
42
- run: ${{matrix.env}} bundle install
43
-
44
- - name: Preparing Postgres (ubuntu)
45
- if: matrix.os == 'ubuntu'
46
- run: |
47
- sudo apt-get install postgresql postgresql-contrib
48
- echo "local all all trust" | sudo tee -a /var/lib/postgresql/12/main/pg_hba.conf
49
- sudo systemctl restart postgresql
50
- sudo -u postgres createuser -s -d $(id -un)
51
- sudo -u postgres createdb test
52
-
53
- - name: Preparing MariaDB (ubuntu)
54
- if: matrix.os == 'ubuntu'
55
- run: |
56
- sudo apt-get install libmariadb-dev
57
- sudo systemctl start mysql
58
- mysql -uroot -proot -e "STATUS"
59
- mysql -uroot -proot -e "CREATE USER '$(id -un)'@'localhost'; GRANT ALL ON *.* TO '$(id -un)'@'localhost'; FLUSH PRIVILEGES; CREATE DATABASE test;"
60
-
61
- - name: Run tests
62
- timeout-minutes: 5
63
- run: |
64
- export MYSQL_UNIX_PORT=/var/run/mysqld/mysqld.sock
65
- ${{matrix.env}} bundle exec rspec
data/.gitignore DELETED
@@ -1,12 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
9
-
10
- # rspec failure tracking
11
- .rspec_status
12
- gems.locked
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --format documentation
2
- --color
3
- --require spec_helper
data/README.md DELETED
@@ -1,35 +0,0 @@
1
- # Db
2
-
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/db`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
6
-
7
- ## Installation
8
-
9
- Add this line to your application's Gemfile:
10
-
11
- ```ruby
12
- gem 'db'
13
- ```
14
-
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install db
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/db.
data/db.gemspec DELETED
@@ -1,29 +0,0 @@
1
-
2
- require_relative "lib/db/version"
3
-
4
- Gem::Specification.new do |spec|
5
- spec.name = "db"
6
- spec.version = DB::VERSION
7
- spec.authors = ["Samuel Williams"]
8
- spec.email = ["samuel.williams@oriontransfer.co.nz"]
9
-
10
- spec.summary = "A low level database access gem."
11
- spec.homepage = "https://github.com/socketry/db"
12
-
13
- spec.files = Dir.chdir(__dir__) do
14
- `git ls-files -z`.split("\x0").reject {|f| f.match(%r{^(test|spec|features)/})}
15
- end
16
-
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
- spec.require_paths = ["lib"]
19
-
20
- spec.add_dependency "async-io"
21
- spec.add_dependency "async-pool"
22
-
23
- spec.add_development_dependency "bake"
24
- spec.add_development_dependency "bake-bundler"
25
-
26
- spec.add_development_dependency "covered"
27
- spec.add_development_dependency "bundler", "~> 2.0"
28
- spec.add_development_dependency "rspec", "~> 3.0"
29
- end
data/gems.rb DELETED
@@ -1,15 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- # Specify your gem's dependencies in db.gemspec
4
- gemspec
5
-
6
- group :adapters do
7
- gem "db-postgres"#, path: "../db-postgres"
8
- gem "db-mariadb"#, path: "../db-mariadb"
9
- end
10
-
11
- group :benchmark do
12
- gem "benchmark-ips"
13
- gem "mysql2"
14
- gem "pg"
15
- end