db-postgres 0.4.0 → 0.6.0

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: 7d9080559ec253dbc5e79e48cba81f8dec73e6bb9abd2f2cd5ae0cdeaf882b97
4
- data.tar.gz: 8011d89fb8b19abbd805ca0068986da5763e189a7a0b046920e7d408cabdfa60
3
+ metadata.gz: 1f6e34604ab35a726644bb1783228a2d60f01f76b0d431110ae20c5974ec3fcb
4
+ data.tar.gz: e879e050b47d3975a29bc6b67e59b5837ad8b8cf067d96554eaf951a8d04198a
5
5
  SHA512:
6
- metadata.gz: 8e6e16336a47e226dcc2adaa548abd522a44af09dfe0751d09806fa991fed224dd254a34a39e2f8318da843a82936d988b27c854a2382b38f18a3e2b801c943d
7
- data.tar.gz: 183c5724efcc8025409d2e6856c3934a154aa10c59b568a9e20d8ae90769fd1595b584b9a6fe5b0c8cb72759e3068f0296327a36eb7904a01590ca186d52d07c
6
+ metadata.gz: 7f4842d9d1c6c3e70029b9a4df1fd4e27a2a10bb4920465dddb1f25616b01ef50c67029d1666c218af68521bb9803cba5bc9b9f478e0c409c7c376e31466af92
7
+ data.tar.gz: d9a04d2d4a541f44bc51493cb0509c0a2a0ad8e39a24e0803fa2e61532ab85cbbbe3c48045c01d60c76b9be88b464797626d966bfbe884dcdd2736c03ba7ee4e
data/lib/.DS_Store CHANGED
Binary file
@@ -86,7 +86,11 @@ module DB
86
86
 
87
87
  append_identifier(name, buffer)
88
88
 
89
- buffer << " BIGSERIAL"
89
+ if primary
90
+ buffer << " BIGSERIAL"
91
+ else
92
+ buffer << " BIGINT"
93
+ end
90
94
 
91
95
  if primary
92
96
  buffer << " PRIMARY KEY"
@@ -18,14 +18,38 @@
18
18
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  # THE SOFTWARE.
20
20
 
21
- require 'ffi'
21
+ require 'ffi/module'
22
+ require 'ffi/module/config_tool'
22
23
 
23
24
  module DB
24
25
  module Postgres
25
26
  module Native
26
- extend FFI::Library
27
+ extend FFI::Module::Library
28
+ extend FFI::Module::Loader
29
+ extend FFI::Module::ConfigTool
27
30
 
28
- ffi_lib 'pq'
31
+ ffi_load('pq') ||
32
+ ffi_load_using_config_tool(%w{pg_config --libdir}, names: ['pq']) ||
33
+ ffi_load_failure(<<~EOF)
34
+ Unable to load libpq!
35
+
36
+ ## Ubuntu
37
+
38
+ sudo apt-get install libpq-dev
39
+
40
+ ## Arch Linux
41
+
42
+ sudo pacman -S postgresql
43
+
44
+ ## MacPorts
45
+
46
+ sudo port install postgresql11
47
+
48
+ ## Homebrew
49
+
50
+ brew install postgresql
51
+
52
+ EOF
29
53
  end
30
54
  end
31
55
  end
@@ -37,23 +37,23 @@ module DB
37
37
  attr :array
38
38
  end
39
39
 
40
- attach_function :connect_start_params, :PQconnectStartParams, [:pointer, :pointer, :int], :pointer
40
+ ffi_attach_function :PQconnectStartParams, [:pointer, :pointer, :int], :pointer, as: :connect_start_params
41
41
 
42
- enum :polling_status, [
42
+ ffi_define_enumeration :polling_status, [
43
43
  :failed,
44
44
  :wait_readable,
45
45
  :wait_writable,
46
46
  :ok,
47
47
  ]
48
48
 
49
- attach_function :connect_poll, :PQconnectPoll, [:pointer], :polling_status
49
+ ffi_attach_function :PQconnectPoll, [:pointer], :polling_status, as: :connect_poll
50
50
 
51
51
  # Close the connection and release underlying resources.
52
- attach_function :finish, :PQfinish, [:pointer], :void
52
+ ffi_attach_function :PQfinish, [:pointer], :void, as: :finish
53
53
 
54
- attach_function :error_message, :PQerrorMessage, [:pointer], :string
54
+ ffi_attach_function :PQerrorMessage, [:pointer], :string, as: :error_message
55
55
 
56
- enum :status, [
56
+ ffi_define_enumeration :status, [
57
57
  # Normal mode:
58
58
  :ok,
59
59
  :bad,
@@ -70,31 +70,31 @@ module DB
70
70
  :consume, # Wait for any pending message and consume them.
71
71
  ]
72
72
 
73
- attach_function :status, :PQstatus, [:pointer], :status
73
+ ffi_attach_function :PQstatus, [:pointer], :status, as: :status
74
74
 
75
- attach_function :socket, :PQsocket, [:pointer], :int
75
+ ffi_attach_function :PQsocket, [:pointer], :int, as: :socket
76
76
 
77
- attach_function :set_nonblocking, :PQsetnonblocking, [:pointer, :int], :int
78
- attach_function :flush, :PQflush, [:pointer], :int
77
+ ffi_attach_function :PQsetnonblocking, [:pointer, :int], :int, as: :set_nonblocking
78
+ ffi_attach_function :PQflush, [:pointer], :int, as: :flush
79
79
 
80
80
  # Submits a command to the server without waiting for the result(s). 1 is returned if the command was successfully dispatched and 0 if not (in which case, use PQerrorMessage to get more information about the failure).
81
- attach_function :send_query, :PQsendQuery, [:pointer, :string], :int
81
+ ffi_attach_function :PQsendQuery, [:pointer, :string], :int, as: :send_query
82
82
 
83
83
  # int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
84
- attach_function :send_query_params, :PQsendQueryParams, [:pointer, :string, :int, :pointer, :pointer, :pointer, :pointer, :int], :int
84
+ ffi_attach_function :PQsendQueryParams, [:pointer, :string, :int, :pointer, :pointer, :pointer, :pointer, :int], :int, as: :send_query_params
85
85
 
86
- attach_function :set_single_row_mode, :PQsetSingleRowMode, [:pointer], :int
86
+ ffi_attach_function :PQsetSingleRowMode, [:pointer], :int, as: :set_single_row_mode
87
87
 
88
- attach_function :get_result, :PQgetResult, [:pointer], :pointer
88
+ ffi_attach_function :PQgetResult, [:pointer], :pointer, as: :get_result
89
89
 
90
90
  # If input is available from the server, consume it:
91
- attach_function :consume_input, :PQconsumeInput, [:pointer], :int
91
+ ffi_attach_function :PQconsumeInput, [:pointer], :int, as: :consume_input
92
92
 
93
93
  # Returns 1 if a command is busy, that is, PQgetResult would block waiting for input. A 0 return indicates that PQgetResult can be called with assurance of not blocking.
94
- attach_function :is_busy, :PQisBusy, [:pointer], :int
94
+ ffi_attach_function :PQisBusy, [:pointer], :int, as: :is_busy
95
95
 
96
- attach_function :escape_literal, :PQescapeLiteral, [:pointer, :string, :size_t], :pointer
97
- attach_function :escape_identifier, :PQescapeIdentifier, [:pointer, :string, :size_t], :pointer
96
+ ffi_attach_function :PQescapeLiteral, [:pointer, :string, :size_t], :pointer, as: :escape_literal
97
+ ffi_attach_function :PQescapeIdentifier, [:pointer, :string, :size_t], :pointer, as: :escape_identifier
98
98
 
99
99
  module IO
100
100
  def self.new(fd, mode)
@@ -214,7 +214,7 @@ module DB
214
214
 
215
215
  Native.clear(result)
216
216
 
217
- raise Error, "Could not get next result: #{message}"
217
+ raise Error, message
218
218
  end
219
219
 
220
220
  return Result.new(self, types, result)
@@ -20,9 +20,6 @@
20
20
 
21
21
  require_relative 'types'
22
22
 
23
- require 'json'
24
- require 'time'
25
-
26
23
  module DB
27
24
  module Postgres
28
25
  module Native
@@ -34,7 +31,7 @@ module DB
34
31
  21 => Types::Integer,
35
32
  23 => Types::Integer,
36
33
 
37
- 114 => JSON,
34
+ 114 => Types::JSON,
38
35
 
39
36
  700 => Types::Float,
40
37
  701 => Types::Float,
@@ -23,7 +23,7 @@ require_relative '../native'
23
23
  module DB
24
24
  module Postgres
25
25
  module Native
26
- enum :query_status, [
26
+ ffi_define_enumeration :query_status, [
27
27
  :empty_query, # empty query string was executed
28
28
  :command_ok, # a query command that doesn't return anything was executed properly by the backend
29
29
  :tuples_ok, # a query command that returns tuples was executed properly by the backend, PGresult contains the result tuples
@@ -36,26 +36,24 @@ module DB
36
36
  :single_tuple, # single tuple from larger resultset
37
37
  ]
38
38
 
39
- attach_function :result_status, :PQresultStatus, [:pointer], :query_status
40
- attach_function :result_error_message, :PQresultErrorMessage, [:pointer], :string
39
+ ffi_attach_function :PQresultStatus, [:pointer], :query_status, as: :result_status
40
+ ffi_attach_function :PQresultErrorMessage, [:pointer], :string, as: :result_error_message
41
41
 
42
- attach_function :row_count, :PQntuples, [:pointer], :int
43
- attach_function :field_count, :PQnfields, [:pointer], :int
44
- attach_function :field_name, :PQfname, [:pointer, :int], :string
45
- attach_function :field_type, :PQftype, [:pointer, :int], :int
42
+ ffi_attach_function :PQntuples, [:pointer], :int, as: :row_count
43
+ ffi_attach_function :PQnfields, [:pointer], :int, as: :field_count
44
+ ffi_attach_function :PQfname, [:pointer, :int], :string, as: :field_name
45
+ ffi_attach_function :PQftype, [:pointer, :int], :int, as: :field_type
46
46
 
47
- attach_function :get_value, :PQgetvalue, [:pointer, :int, :int], :string
48
- attach_function :get_is_null, :PQgetisnull, [:pointer, :int, :int], :int
47
+ ffi_attach_function :PQgetvalue, [:pointer, :int, :int], :string, as: :get_value
48
+ ffi_attach_function :PQgetisnull, [:pointer, :int, :int], :int, as: :get_is_null
49
49
 
50
- attach_function :clear, :PQclear, [:pointer], :void
50
+ ffi_attach_function :PQclear, [:pointer], :void, as: :clear
51
51
 
52
- attach_function :put_copy_end, :PQputCopyEnd, [:pointer, :string], :int
53
- attach_function :get_copy_data, :PQgetCopyData, [:pointer, :pointer, :int], :int
54
- attach_function :free_memory, :PQfreemem, [:pointer], :void
52
+ ffi_attach_function :PQputCopyEnd, [:pointer, :string], :int, as: :put_copy_end
53
+ ffi_attach_function :PQgetCopyData, [:pointer, :pointer, :int], :int, as: :get_copy_data
54
+ ffi_attach_function :PQfreemem, [:pointer], :void, as: :free_memory
55
55
 
56
56
  class Result < FFI::Pointer
57
- include Enumerable
58
-
59
57
  def initialize(connection, types = {}, address)
60
58
  super(address)
61
59
 
@@ -101,7 +99,28 @@ module DB
101
99
  Native.clear(self)
102
100
  end
103
101
 
102
+ def map(&block)
103
+ results = []
104
+
105
+ self.each do |row|
106
+ results << yield(row)
107
+ end
108
+
109
+ return results
110
+ end
111
+
112
+ def to_a
113
+ rows = []
114
+
115
+ self.each do |row|
116
+ rows << row
117
+ end
118
+
119
+ return rows
120
+ end
121
+
104
122
  protected
123
+
105
124
  def get_value(row, field)
106
125
  if Native.get_is_null(self, row, field) == 0
107
126
  Native.get_value(self, row, field)
@@ -22,6 +22,7 @@ require 'ffi'
22
22
 
23
23
  require 'json'
24
24
  require 'bigdecimal'
25
+ require 'time'
25
26
 
26
27
  module DB
27
28
  module Postgres
@@ -35,33 +36,41 @@ module DB
35
36
 
36
37
  module Integer
37
38
  def self.parse(string)
38
- Integer(string)
39
+ Integer(string) if string
39
40
  end
40
41
  end
41
42
 
42
43
  module Decimal
43
44
  def self.parse(string)
44
- BigDecimal(string)
45
+ BigDecimal(string) if string
45
46
  end
46
47
  end
47
48
 
48
49
  module Float
49
50
  def self.parse(string)
50
- Float(string)
51
+ Float(string) if string
51
52
  end
52
53
  end
53
54
 
54
55
  module Symbol
55
56
  def self.parse(string)
56
- string.to_sym
57
+ string&.to_sym
57
58
  end
58
59
  end
59
60
 
60
61
  module DateTime
61
62
  def self.parse(string)
62
- parts = string.split(/[\-\s:\.]/)
63
-
64
- return Time.utc(*parts)
63
+ if string
64
+ parts = string.split(/[\-\s:\.]/)
65
+
66
+ return Time.utc(*parts)
67
+ end
68
+ end
69
+ end
70
+
71
+ module JSON
72
+ def self.parse(string)
73
+ ::JSON.parse(string, symbolize_names: true) if string
65
74
  end
66
75
  end
67
76
  end
@@ -20,6 +20,6 @@
20
20
 
21
21
  module DB
22
22
  module Postgres
23
- VERSION = "0.4.0"
23
+ VERSION = "0.6.0"
24
24
  end
25
25
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db-postgres
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-04-01 00:00:00.000000000 Z
11
+ date: 2021-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ffi
14
+ name: ffi-module
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: 0.3.0
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: 0.3.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: async-io
29
29
  requirement: !ruby/object:Gem::Requirement