db-postgres 0.1.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e84a4443e578019ea67c117e4dfc499b7d9edc78a128dde8d0d2814fbbcaceb8
4
- data.tar.gz: ac6f0e2c8907fd254e46feb85c7642643b28831993726be3320ad54c49c6961f
3
+ metadata.gz: 759fb2800aa247e46b47e73bca2fbd781303c98f23468df27c3d1d89c179c98a
4
+ data.tar.gz: d724242a698b01c258fcd021c2838e216c962f186baf09c1c8096ee0812d0028
5
5
  SHA512:
6
- metadata.gz: b8c5429ad14b63e14363ae2a3974db20bea363f20090aab97d14ffe9cfc83af5b04628258fbddf7da07c22762e84b987d2af315229cf267dee6950d972e32c17
7
- data.tar.gz: fd56abfce51d284a682b343436e342fceb8f7811e395a18b6977fe9a01c0b85643bf3755b3a6a235b6bf75abbcc987c9731ec7513e317c8aeec9ee2221d7a2be
6
+ metadata.gz: 0dc83c9e30e8d05841498abae61bedd1968d4b1b03eb1f0ceaa609403b121d441cd71c393a511fc2b78aa1710d57c45b9fcbd174bf96a89924db80d207592ea4
7
+ data.tar.gz: b7822ce7cf2b0fe4f969e247e702422fecb42d073487c07bd0212e98b0476275894f4dadcd8473eb4d59fbc884687f7aea8eeb035c42f6b2f78a22e08d1e8838
@@ -24,17 +24,15 @@ require_relative 'connection'
24
24
 
25
25
  module DB
26
26
  module Postgres
27
- LOCAL = "postgres://localhost/postgres"
28
-
29
27
  class Adapter
30
- def initialize(connection_string = LOCAL)
31
- @connection_string = connection_string
28
+ def initialize(**options)
29
+ @options = options
32
30
  end
33
31
 
34
- attr :connection_string
32
+ attr :options
35
33
 
36
34
  def call
37
- Connection.new(self.connection_string)
35
+ Connection.new(**@options)
38
36
  end
39
37
  end
40
38
  end
@@ -21,18 +21,12 @@
21
21
  # THE SOFTWARE.
22
22
 
23
23
  require 'async/pool/resource'
24
- require_relative 'native/connection'
25
-
26
24
  require 'async/io/generic'
27
25
 
26
+ require_relative 'native/connection'
27
+
28
28
  module DB
29
29
  module Postgres
30
- module IO
31
- def self.new(fd, mode)
32
- Async::IO::Generic.new(::IO.new(fd, mode, autoclose: false))
33
- end
34
- end
35
-
36
30
  # This implements the interface between the underlying
37
31
  class Connection < Async::Pool::Resource
38
32
  def initialize(**options)
@@ -76,31 +70,33 @@ module DB
76
70
  return buffer
77
71
  end
78
72
 
73
+ def id_column(name = 'id', primary_key: true)
74
+ buffer = String.new
75
+
76
+ append_identifier(name, buffer)
77
+
78
+ buffer << " BIGSERIAL"
79
+
80
+ if primary_key
81
+ buffer << " PRIMARY KEY"
82
+ end
83
+
84
+ return buffer
85
+ end
86
+
79
87
  def status
80
88
  @native.status
81
89
  end
82
90
 
83
91
  def send_query(statement)
92
+ @native.discard_results
93
+
84
94
  @native.send_query(statement)
85
95
  end
86
96
 
87
97
  def next_result
88
98
  @native.next_result
89
99
  end
90
-
91
- def call(statement, streaming: false)
92
- @native.send_query(statement)
93
-
94
- @native.single_row_mode! if streaming
95
-
96
- last_result = nil
97
-
98
- while result = @native.next_result
99
- last_result = result
100
- end
101
-
102
- return last_result
103
- end
104
100
  end
105
101
  end
106
102
  end
@@ -0,0 +1,28 @@
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
+ module Postgres
25
+ class Error < StandardError
26
+ end
27
+ end
28
+ end
@@ -20,6 +20,7 @@
20
20
 
21
21
  require_relative 'result'
22
22
  require_relative 'field'
23
+ require_relative '../error'
23
24
 
24
25
  module DB
25
26
  module Postgres
@@ -95,8 +96,14 @@ module DB
95
96
  attach_function :escape_literal, :PQescapeLiteral, [:pointer, :string, :size_t], :pointer
96
97
  attach_function :escape_identifier, :PQescapeIdentifier, [:pointer, :string, :size_t], :pointer
97
98
 
99
+ module IO
100
+ def self.new(fd, mode)
101
+ Async::IO::Generic.new(::IO.new(fd, mode, autoclose: false))
102
+ end
103
+ end
104
+
98
105
  class Connection < FFI::Pointer
99
- def self.connect(io: IO, types: DEFAULT_TYPES, **options)
106
+ def self.connect(wrapper: IO, types: DEFAULT_TYPES, **options)
100
107
  # Prefer 'database' key for 'dbname' parameter:
101
108
  if database = options.delete(:database)
102
109
  options[:dbname] = database
@@ -107,7 +114,7 @@ module DB
107
114
 
108
115
  pointer = Native.connect_start_params(keys, values, 0)
109
116
 
110
- io = io.new(Native.socket(pointer), "r+")
117
+ io = wrapper.new(Native.socket(pointer), "r+")
111
118
 
112
119
  while status = Native.connect_poll(pointer)
113
120
  break if status == :ok || status == :failed
@@ -196,6 +203,16 @@ module DB
196
203
 
197
204
  def next_result(types: @types)
198
205
  if result = self.get_result
206
+ status = Native.result_status(result)
207
+
208
+ if status == :fatal_error
209
+ message = Native.result_error_message(result)
210
+
211
+ Native.clear(result)
212
+
213
+ raise Error, "Could not get next result: #{message}"
214
+ end
215
+
199
216
  return Result.new(self, types, result)
200
217
  end
201
218
  end
@@ -208,7 +225,7 @@ module DB
208
225
 
209
226
  case status
210
227
  when :copy_in
211
- self.put_copy_end("discard results")
228
+ self.put_copy_end("Discard results")
212
229
  when :copy_out
213
230
  self.flush_copy_out
214
231
  end
@@ -244,7 +261,7 @@ module DB
244
261
 
245
262
  if status == -1
246
263
  message = Native.error_message(self)
247
- raise Error.new(message)
264
+ raise Error, message
248
265
  elsif status == 0
249
266
  @io.wait_writable
250
267
  else
@@ -261,7 +278,7 @@ module DB
261
278
 
262
279
  if status == -2
263
280
  message = Native.error_message(self)
264
- raise Error.new(message)
281
+ raise Error, message
265
282
  elsif status == -1
266
283
  break
267
284
  elsif status == 0
@@ -289,7 +306,7 @@ module DB
289
306
  def check!(result)
290
307
  if result == 0
291
308
  message = Native.error_message(self)
292
- raise Error.new(message)
309
+ raise Error, message
293
310
  end
294
311
  end
295
312
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module DB
22
22
  module Postgres
23
- VERSION = "0.1.0"
23
+ VERSION = "0.2.1"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
@@ -117,6 +117,7 @@ files:
117
117
  - lib/db/postgres.rb
118
118
  - lib/db/postgres/adapter.rb
119
119
  - lib/db/postgres/connection.rb
120
+ - lib/db/postgres/error.rb
120
121
  - lib/db/postgres/native.rb
121
122
  - lib/db/postgres/native/connection.rb
122
123
  - lib/db/postgres/native/field.rb
@@ -142,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
142
143
  - !ruby/object:Gem::Version
143
144
  version: '0'
144
145
  requirements: []
145
- rubygems_version: 3.1.2
146
+ rubygems_version: 3.0.3
146
147
  signing_key:
147
148
  specification_version: 4
148
149
  summary: Ruby FFI bindings for libpq C interface.