db-postgres 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e84a4443e578019ea67c117e4dfc499b7d9edc78a128dde8d0d2814fbbcaceb8
4
+ data.tar.gz: ac6f0e2c8907fd254e46feb85c7642643b28831993726be3320ad54c49c6961f
5
+ SHA512:
6
+ metadata.gz: b8c5429ad14b63e14363ae2a3974db20bea363f20090aab97d14ffe9cfc83af5b04628258fbddf7da07c22762e84b987d2af315229cf267dee6950d972e32c17
7
+ data.tar.gz: fd56abfce51d284a682b343436e342fceb8f7811e395a18b6977fe9a01c0b85643bf3755b3a6a235b6bf75abbcc987c9731ec7513e317c8aeec9ee2221d7a2be
@@ -0,0 +1,27 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'postgres/native'
22
+ require_relative 'postgres/connection'
23
+
24
+ require_relative 'postgres/adapter'
25
+
26
+ require 'db/adapters'
27
+ DB::Adapters.register(:postgres, DB::Postgres::Adapter)
@@ -0,0 +1,41 @@
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
+ require_relative 'connection'
24
+
25
+ module DB
26
+ module Postgres
27
+ LOCAL = "postgres://localhost/postgres"
28
+
29
+ class Adapter
30
+ def initialize(connection_string = LOCAL)
31
+ @connection_string = connection_string
32
+ end
33
+
34
+ attr :connection_string
35
+
36
+ def call
37
+ Connection.new(self.connection_string)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,106 @@
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
+ require 'async/pool/resource'
24
+ require_relative 'native/connection'
25
+
26
+ require 'async/io/generic'
27
+
28
+ module DB
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
+ # This implements the interface between the underlying
37
+ class Connection < Async::Pool::Resource
38
+ def initialize(**options)
39
+ @native = Native::Connection.connect(**options)
40
+
41
+ super()
42
+ end
43
+
44
+ def close
45
+ @native.close
46
+
47
+ super
48
+ end
49
+
50
+ def append_string(value, buffer = String.new)
51
+ buffer << @native.escape_literal(value)
52
+
53
+ return buffer
54
+ end
55
+
56
+ def append_literal(value, buffer = String.new)
57
+ case value
58
+ when Numeric
59
+ buffer << value.to_s
60
+ when TrueClass
61
+ buffer << 'TRUE'
62
+ when FalseClass
63
+ buffer << 'FALSE'
64
+ when nil
65
+ buffer << 'NULL'
66
+ else
67
+ append_string(value, buffer)
68
+ end
69
+
70
+ return buffer
71
+ end
72
+
73
+ def append_identifier(value, buffer = String.new)
74
+ buffer << @native.escape_identifier(value)
75
+
76
+ return buffer
77
+ end
78
+
79
+ def status
80
+ @native.status
81
+ end
82
+
83
+ def send_query(statement)
84
+ @native.send_query(statement)
85
+ end
86
+
87
+ def next_result
88
+ @native.next_result
89
+ 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
+ end
105
+ end
106
+ end
@@ -0,0 +1,31 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'ffi'
22
+
23
+ module DB
24
+ module Postgres
25
+ module Native
26
+ extend FFI::Library
27
+
28
+ ffi_lib 'pq'
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,298 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'result'
22
+ require_relative 'field'
23
+
24
+ module DB
25
+ module Postgres
26
+ module Native
27
+ def self.array_of_strings(values)
28
+ array = FFI::MemoryPointer.new(:pointer, values.size + 1)
29
+
30
+ pointers = values.map do |value|
31
+ FFI::MemoryPointer.from_string(value.to_s)
32
+ end
33
+
34
+ array.write_array_of_pointer(pointers)
35
+
36
+ return array
37
+ end
38
+
39
+ attach_function :connect_start_params, :PQconnectStartParams, [:pointer, :pointer, :int], :pointer
40
+
41
+ enum :polling_status, [
42
+ :failed,
43
+ :wait_readable,
44
+ :wait_writable,
45
+ :ok,
46
+ ]
47
+
48
+ attach_function :connect_poll, :PQconnectPoll, [:pointer], :polling_status
49
+
50
+ # Close the connection and release underlying resources.
51
+ attach_function :finish, :PQfinish, [:pointer], :void
52
+
53
+ attach_function :error_message, :PQerrorMessage, [:pointer], :string
54
+
55
+ enum :status, [
56
+ # Normal mode:
57
+ :ok,
58
+ :bad,
59
+
60
+ # Non-blocking mode:
61
+ :started, # Waiting for connection to be made.
62
+ :made, # Connection OK; waiting to send.
63
+ :awaiting_response, #Waiting for a response from the postmaster.
64
+ :auth_ok, # Received authentication; waiting for backend startup.
65
+ :setenv, # Negotiating environment.
66
+ :ssl_startup, # Negotiating SSL.
67
+ :needed, # Internal state: connect() needed
68
+ :check_writable, # Check if we could make a writable connection.
69
+ :consume, # Wait for any pending message and consume them.
70
+ ]
71
+
72
+ attach_function :status, :PQstatus, [:pointer], :status
73
+
74
+ attach_function :socket, :PQsocket, [:pointer], :int
75
+
76
+ attach_function :set_nonblocking, :PQsetnonblocking, [:pointer, :int], :int
77
+ attach_function :flush, :PQflush, [:pointer], :int
78
+
79
+ # 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).
80
+ attach_function :send_query, :PQsendQuery, [:pointer, :string], :int
81
+
82
+ # int PQsendQueryParams(PGconn *conn, const char *command, int nParams, const Oid *paramTypes, const char * const *paramValues, const int *paramLengths, const int *paramFormats, int resultFormat);
83
+ attach_function :send_query_params, :PQsendQueryParams, [:pointer, :string, :int, :pointer, :pointer, :pointer, :pointer, :int], :int
84
+
85
+ attach_function :set_single_row_mode, :PQsetSingleRowMode, [:pointer], :int
86
+
87
+ attach_function :get_result, :PQgetResult, [:pointer], :pointer
88
+
89
+ # If input is available from the server, consume it:
90
+ attach_function :consume_input, :PQconsumeInput, [:pointer], :int
91
+
92
+ # 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.
93
+ attach_function :is_busy, :PQisBusy, [:pointer], :int
94
+
95
+ attach_function :escape_literal, :PQescapeLiteral, [:pointer, :string, :size_t], :pointer
96
+ attach_function :escape_identifier, :PQescapeIdentifier, [:pointer, :string, :size_t], :pointer
97
+
98
+ class Connection < FFI::Pointer
99
+ def self.connect(io: IO, types: DEFAULT_TYPES, **options)
100
+ # Prefer 'database' key for 'dbname' parameter:
101
+ if database = options.delete(:database)
102
+ options[:dbname] = database
103
+ end
104
+
105
+ keys = Native.array_of_strings(options.keys)
106
+ values = Native.array_of_strings(options.values)
107
+
108
+ pointer = Native.connect_start_params(keys, values, 0)
109
+
110
+ io = io.new(Native.socket(pointer), "r+")
111
+
112
+ while status = Native.connect_poll(pointer)
113
+ break if status == :ok || status == :failed
114
+
115
+ # one of :wait_readable or :wait_writable
116
+ io.send(status)
117
+ end
118
+
119
+ if status == :failed
120
+ io.close
121
+
122
+ error_message = Native.error_message(pointer)
123
+
124
+ Native.finish(pointer)
125
+
126
+ raise "connect: #{error_message}"
127
+ end
128
+
129
+ Native.set_nonblocking(pointer, 1)
130
+
131
+ return self.new(pointer, io, types)
132
+ end
133
+
134
+ def initialize(address, io, types)
135
+ super(address)
136
+
137
+ @io = io
138
+ @types = types
139
+ end
140
+
141
+ # Return the status of the connection.
142
+ def status
143
+ Native.status(self)
144
+ end
145
+
146
+ # Return the last error message.
147
+ def error_message
148
+ Native.error_message(self)
149
+ end
150
+
151
+ # Return the underlying socket used for IO.
152
+ def socket
153
+ Native.socket(self)
154
+ end
155
+
156
+ # Close the connection.
157
+ def close
158
+ Native.finish(self)
159
+
160
+ @io.close
161
+ end
162
+
163
+ def escape_literal(value)
164
+ value = value.to_s
165
+
166
+ result = Native.escape_literal(self, value, value.bytesize)
167
+
168
+ string = result.read_string
169
+
170
+ Native.free_memory(result)
171
+
172
+ return string
173
+ end
174
+
175
+ def escape_identifier(value)
176
+ value = value.to_s
177
+
178
+ result = Native.escape_identifier(self, value, value.bytesize)
179
+
180
+ string = result.read_string
181
+
182
+ Native.free_memory(result)
183
+
184
+ return string
185
+ end
186
+
187
+ def single_row_mode!
188
+ Native.set_single_row_mode(self)
189
+ end
190
+
191
+ def send_query(statement)
192
+ check! Native.send_query(self, statement)
193
+
194
+ flush
195
+ end
196
+
197
+ def next_result(types: @types)
198
+ if result = self.get_result
199
+ return Result.new(self, types, result)
200
+ end
201
+ end
202
+
203
+ # Silently discard any results that application didn't read.
204
+ def discard_results
205
+ while result = self.get_result
206
+ status = Native.result_status(result)
207
+ Native.clear(result)
208
+
209
+ case status
210
+ when :copy_in
211
+ self.put_copy_end("discard results")
212
+ when :copy_out
213
+ self.flush_copy_out
214
+ end
215
+ end
216
+
217
+ return nil
218
+ end
219
+
220
+ protected
221
+
222
+ def get_result
223
+ while true
224
+ check! Native.consume_input(self)
225
+
226
+ while Native.is_busy(self) == 0
227
+ result = Native.get_result(self)
228
+
229
+ # Did we finish reading all results?
230
+ if result.null?
231
+ return nil
232
+ else
233
+ return result
234
+ end
235
+ end
236
+
237
+ @io.wait_readable
238
+ end
239
+ end
240
+
241
+ def put_copy_end(message = nil)
242
+ while true
243
+ status = Native.put_copy_end(self, message)
244
+
245
+ if status == -1
246
+ message = Native.error_message(self)
247
+ raise Error.new(message)
248
+ elsif status == 0
249
+ @io.wait_writable
250
+ else
251
+ break
252
+ end
253
+ end
254
+ end
255
+
256
+ def flush_copy_out
257
+ buffer = FFI::MemoryPointer.new(:pointer, 1)
258
+
259
+ while true
260
+ status = Native.get_copy_data(self, buffer, 1)
261
+
262
+ if status == -2
263
+ message = Native.error_message(self)
264
+ raise Error.new(message)
265
+ elsif status == -1
266
+ break
267
+ elsif status == 0
268
+ @io.wait_readable
269
+ else
270
+ Native.free_memory(buffer.read_pointer)
271
+ end
272
+ end
273
+ end
274
+
275
+ # After sending any command or data on a nonblocking connection, call PQflush. If it returns 1, wait for the socket to become read- or write-ready. If it becomes write-ready, call PQflush again. If it becomes read-ready, call PQconsumeInput, then call PQflush again. Repeat until PQflush returns 0. (It is necessary to check for read-ready and drain the input with PQconsumeInput, because the server can block trying to send us data, e.g. NOTICE messages, and won't read our data until we read its.) Once PQflush returns 0, wait for the socket to be read-ready and then read the response as described above.
276
+ def flush
277
+ while true
278
+ case Native.flush(self)
279
+ when 1
280
+ @io.wait_any
281
+
282
+ check! Native.consume_input(self)
283
+ when 0
284
+ return
285
+ end
286
+ end
287
+ end
288
+
289
+ def check!(result)
290
+ if result == 0
291
+ message = Native.error_message(self)
292
+ raise Error.new(message)
293
+ end
294
+ end
295
+ end
296
+ end
297
+ end
298
+ end
@@ -0,0 +1,52 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'types'
22
+
23
+ require 'json'
24
+ require 'time'
25
+
26
+ module DB
27
+ module Postgres
28
+ module Native
29
+ # These are hard coded OIDs.
30
+ DEFAULT_TYPES = {
31
+ 16 => Types::Boolean,
32
+
33
+ 20 => Types::Integer,
34
+ 21 => Types::Integer,
35
+ 23 => Types::Integer,
36
+
37
+ 114 => JSON,
38
+
39
+ 700 => Types::Float,
40
+ 701 => Types::Float,
41
+
42
+ 1082 => Date,
43
+ 1083 => Types::DateTime,
44
+ 1114 => Types::DateTime,
45
+
46
+ 1700 => Types::Decimal,
47
+
48
+ 3500 => Types::Symbol,
49
+ }
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,121 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative '../native'
22
+
23
+ module DB
24
+ module Postgres
25
+ module Native
26
+ enum :query_status, [
27
+ :empty_query, # empty query string was executed
28
+ :command_ok, # a query command that doesn't return anything was executed properly by the backend
29
+ :tuples_ok, # a query command that returns tuples was executed properly by the backend, PGresult contains the result tuples
30
+ :copy_out, # Copy Out data transfer in progress
31
+ :copy_in, # Copy In data transfer in progress
32
+ :bad_response, # an unexpected response was recv'd from the backend
33
+ :nonfatal_error, # notice or warning message
34
+ :fatal_error, # query failed
35
+ :copy_both, # Copy In/Out data transfer in progress
36
+ :single_tuple, # single tuple from larger resultset
37
+ ]
38
+
39
+ attach_function :result_status, :PQresultStatus, [:pointer], :query_status
40
+ attach_function :result_error_message, :PQresultErrorMessage, [:pointer], :string
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
46
+ attach_function :get_value, :PQgetvalue, [:pointer, :int, :int], :string
47
+
48
+ attach_function :clear, :PQclear, [:pointer], :void
49
+
50
+ attach_function :put_copy_end, :PQputCopyEnd, [:pointer, :string], :int
51
+ attach_function :get_copy_data, :PQgetCopyData, [:pointer, :pointer, :int], :int
52
+ attach_function :free_memory, :PQfreemem, [:pointer], :void
53
+
54
+ class Result < FFI::Pointer
55
+ def initialize(connection, types = {}, address)
56
+ super(address)
57
+
58
+ @connection = connection
59
+ @fields = nil
60
+ @types = types
61
+ @casts = nil
62
+ end
63
+
64
+ def field_count
65
+ Native.field_count(self)
66
+ end
67
+
68
+ def field_types
69
+ field_count.times.collect{|i| @types[Native.field_type(self, i)]}
70
+ end
71
+
72
+ def field_names
73
+ field_count.times.collect{|i| Native.field_name(self, i)}
74
+ end
75
+
76
+ def row_count
77
+ Native.row_count(self)
78
+ end
79
+
80
+ def cast!(row)
81
+ @casts ||= self.field_types
82
+
83
+ row.size.times do |index|
84
+ if cast = @casts[index]
85
+ row[index] = cast.parse(row[index])
86
+ end
87
+ end
88
+
89
+ return row
90
+ end
91
+
92
+ def each
93
+ row_count.times do |i|
94
+ yield cast!(get_row(i))
95
+ end
96
+
97
+ Native.clear(self)
98
+ end
99
+
100
+ def to_a
101
+ rows = []
102
+
103
+ self.each do |row|
104
+ rows << row
105
+ end
106
+
107
+ return rows
108
+ end
109
+
110
+ protected
111
+ def get_value(row, field)
112
+ Native.get_value(self, row, field)
113
+ end
114
+
115
+ def get_row(row)
116
+ field_count.times.collect{|j| get_value(row, j)}
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,70 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require 'ffi'
22
+
23
+ require 'json'
24
+ require 'bigdecimal'
25
+
26
+ module DB
27
+ module Postgres
28
+ module Native
29
+ module Types
30
+ module Boolean
31
+ def self.parse(string)
32
+ string == 't'
33
+ end
34
+ end
35
+
36
+ module Integer
37
+ def self.parse(string)
38
+ Integer(string)
39
+ end
40
+ end
41
+
42
+ module Decimal
43
+ def self.parse(string)
44
+ BigDecimal(string)
45
+ end
46
+ end
47
+
48
+ module Float
49
+ def self.parse(string)
50
+ Float(string)
51
+ end
52
+ end
53
+
54
+ module Symbol
55
+ def self.parse(string)
56
+ string.to_sym
57
+ end
58
+ end
59
+
60
+ module DateTime
61
+ def self.parse(string)
62
+ parts = string.split(/[\-\s:\.]/)
63
+
64
+ return Time.utc(*parts)
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module DB
22
+ module Postgres
23
+ VERSION = "0.1.0"
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db-postgres
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: async-io
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: async-rspec
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
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: covered
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ 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: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.6'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.6'
111
+ description:
112
+ email:
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - lib/db/postgres.rb
118
+ - lib/db/postgres/adapter.rb
119
+ - lib/db/postgres/connection.rb
120
+ - lib/db/postgres/native.rb
121
+ - lib/db/postgres/native/connection.rb
122
+ - lib/db/postgres/native/field.rb
123
+ - lib/db/postgres/native/result.rb
124
+ - lib/db/postgres/native/types.rb
125
+ - lib/db/postgres/version.rb
126
+ homepage:
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '2.5'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubygems_version: 3.1.2
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: Ruby FFI bindings for libpq C interface.
149
+ test_files: []