db-postgres 0.1.2 → 0.2.3

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: c30d669b19ff5cf7b41eb45ff896363060472f2de9231c0c3a25236f23560c82
4
- data.tar.gz: a3026d300addc7817bf9dbae7124f1c04140a77d8d9a6bd8854ed3741ae7af53
3
+ metadata.gz: 980a129963d8ce756170656f2e073d4b975f87e04e6aa569c1f1948366c589a9
4
+ data.tar.gz: fc22f9f7c50b5d7c10c1315dc2d23d626b0d289e4140ef0e1ffb7a2e6901a7e1
5
5
  SHA512:
6
- metadata.gz: 7b0314e22e03bb2579ead32274df8015064d9799f751e02f38e5b9aee1160c92eb20ba490f502a0e39672934d7099a2445584f19cda5c42f08524648c1bda799
7
- data.tar.gz: b951e9c36caed76f39f98342cae5eda8383c658eba45d2cab17cfe8aa0a0b6a73a7bbb7d3475746514b09229e68696d4690b475b89e7035d6eb662045a6e7613
6
+ metadata.gz: 4ba0a3ae436070fb5ab0f3f99214020673febac6ca812cbef5cdc7db4248124c1d1b33f4c1a27777ad7c7a49ded638b10bed3ddf5c5e7bb3318f943253169da3
7
+ data.tar.gz: b415829ef5e86f15d0a65ffcc467738d4ca8c1ea623292869636772a2c1bff686f0a9351f30392554ce560275b8d82bf5dd22d5b101d7e5ae8ecd11083db7355
Binary file
@@ -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)
@@ -25,16 +25,16 @@ require_relative '../error'
25
25
  module DB
26
26
  module Postgres
27
27
  module Native
28
- def self.array_of_strings(values)
29
- array = FFI::MemoryPointer.new(:pointer, values.size + 1)
30
-
31
- pointers = values.map do |value|
32
- FFI::MemoryPointer.from_string(value.to_s)
28
+ class Strings
29
+ def initialize(values)
30
+ @array = FFI::MemoryPointer.new(:pointer, values.size + 1)
31
+ @pointers = values.map do |value|
32
+ FFI::MemoryPointer.from_string(value.to_s)
33
+ end
34
+ @array.write_array_of_pointer(@pointers)
33
35
  end
34
36
 
35
- array.write_array_of_pointer(pointers)
36
-
37
- return array
37
+ attr :array
38
38
  end
39
39
 
40
40
  attach_function :connect_start_params, :PQconnectStartParams, [:pointer, :pointer, :int], :pointer
@@ -96,19 +96,26 @@ module DB
96
96
  attach_function :escape_literal, :PQescapeLiteral, [:pointer, :string, :size_t], :pointer
97
97
  attach_function :escape_identifier, :PQescapeIdentifier, [:pointer, :string, :size_t], :pointer
98
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
+
99
105
  class Connection < FFI::Pointer
100
- def self.connect(io: IO, types: DEFAULT_TYPES, **options)
106
+ def self.connect(wrapper: IO, types: DEFAULT_TYPES, **options)
101
107
  # Prefer 'database' key for 'dbname' parameter:
102
108
  if database = options.delete(:database)
103
109
  options[:dbname] = database
104
110
  end
105
111
 
106
- keys = Native.array_of_strings(options.keys)
107
- values = Native.array_of_strings(options.values)
112
+ keys = Strings.new(options.keys)
113
+ values = Strings.new(options.values)
108
114
 
109
- pointer = Native.connect_start_params(keys, values, 0)
115
+ pointer = Native.connect_start_params(keys.array, values.array, 0)
116
+ Native.set_nonblocking(pointer, 1)
110
117
 
111
- io = io.new(Native.socket(pointer), "r+")
118
+ io = wrapper.new(Native.socket(pointer), "r+")
112
119
 
113
120
  while status = Native.connect_poll(pointer)
114
121
  break if status == :ok || status == :failed
@@ -124,11 +131,9 @@ module DB
124
131
 
125
132
  Native.finish(pointer)
126
133
 
127
- raise "connect: #{error_message}"
134
+ raise Error, "Could not connect: #{error_message}"
128
135
  end
129
136
 
130
- Native.set_nonblocking(pointer, 1)
131
-
132
137
  return self.new(pointer, io, types)
133
138
  end
134
139
 
@@ -43,7 +43,9 @@ module DB
43
43
  attach_function :field_count, :PQnfields, [:pointer], :int
44
44
  attach_function :field_name, :PQfname, [:pointer, :int], :string
45
45
  attach_function :field_type, :PQftype, [:pointer, :int], :int
46
+
46
47
  attach_function :get_value, :PQgetvalue, [:pointer, :int, :int], :string
48
+ attach_function :get_is_null, :PQgetisnull, [:pointer, :int, :int], :int
47
49
 
48
50
  attach_function :clear, :PQclear, [:pointer], :void
49
51
 
@@ -109,7 +111,9 @@ module DB
109
111
 
110
112
  protected
111
113
  def get_value(row, field)
112
- Native.get_value(self, row, field)
114
+ if Native.get_is_null(self, row, field) == 0
115
+ Native.get_value(self, row, field)
116
+ end
113
117
  end
114
118
 
115
119
  def get_row(row)
@@ -20,6 +20,6 @@
20
20
 
21
21
  module DB
22
22
  module Postgres
23
- VERSION = "0.1.2"
23
+ VERSION = "0.2.3"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.3
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-05 00:00:00.000000000 Z
11
+ date: 2020-11-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ffi
@@ -114,6 +114,7 @@ executables: []
114
114
  extensions: []
115
115
  extra_rdoc_files: []
116
116
  files:
117
+ - lib/.DS_Store
117
118
  - lib/db/postgres.rb
118
119
  - lib/db/postgres/adapter.rb
119
120
  - lib/db/postgres/connection.rb