1010module DB
1111 module Postgres
1212 module Native
13+ # Helper class for managing FFI string arrays.
1314 class Strings
15+ # Initialize a string array for FFI.
16+ # @parameter values [Array] The array of values to convert to FFI strings.
1417 def initialize ( values )
1518 @array = FFI ::MemoryPointer . new ( :pointer , values . size + 1 )
1619 @pointers = values . map do |value |
@@ -19,6 +22,7 @@ def initialize(values)
1922 @array . write_array_of_pointer ( @pointers )
2023 end
2124
25+ # @attribute [FFI::MemoryPointer] The FFI array pointer.
2226 attr :array
2327 end
2428
@@ -81,7 +85,13 @@ def initialize(values)
8185 ffi_attach_function :PQescapeLiteral , [ :pointer , :string , :size_t ] , :pointer , as : :escape_literal
8286 ffi_attach_function :PQescapeIdentifier , [ :pointer , :string , :size_t ] , :pointer , as : :escape_identifier
8387
88+ # A native FFI connection to the PostgreSQL client library.
8489 class Connection < FFI ::Pointer
90+ # Establish a connection to the PostgreSQL server.
91+ # @parameter types [Hash] Type mapping configuration.
92+ # @parameter options [Hash] Connection options (database, username, password, host, port, etc.).
93+ # @returns [Connection] A new connected instance.
94+ # @raises [Error] If the connection fails.
8595 def self . connect ( types : DEFAULT_TYPES , **options )
8696 # Postgres expects "dbname" as the key name:
8797 if database = options . delete ( :database )
@@ -121,13 +131,18 @@ def self.connect(types: DEFAULT_TYPES, **options)
121131 return self . new ( pointer , io , types )
122132 end
123133
134+ # Initialize a native connection wrapper.
135+ # @parameter address [FFI::Pointer] The pointer to the native connection.
136+ # @parameter io [IO] The IO object for the socket.
137+ # @parameter types [Hash] Type mapping configuration.
124138 def initialize ( address , io , types )
125139 super ( address )
126140
127141 @io = io
128142 @types = types
129143 end
130144
145+ # @attribute [Hash] The type mapping configuration.
131146 attr :types
132147
133148 # Return the status of the connection.
@@ -152,6 +167,9 @@ def close
152167 @io . close
153168 end
154169
170+ # Escape a literal string value for safe inclusion in SQL queries.
171+ # @parameter value [String] The value to escape.
172+ # @returns [String] The escaped string with quotes.
155173 def escape_literal ( value )
156174 value = value . to_s
157175
@@ -164,6 +182,9 @@ def escape_literal(value)
164182 return string
165183 end
166184
185+ # Escape an identifier for safe inclusion in SQL queries.
186+ # @parameter value [String] The identifier to escape.
187+ # @returns [String] The escaped identifier with quotes.
167188 def escape_identifier ( value )
168189 value = value . to_s
169190
@@ -176,16 +197,23 @@ def escape_identifier(value)
176197 return string
177198 end
178199
200+ # Enable single row mode for streaming large result sets.
179201 def single_row_mode!
180202 Native . set_single_row_mode ( self )
181203 end
182204
205+ # Send a query to the server for execution.
206+ # @parameter statement [String] The SQL statement to execute.
183207 def send_query ( statement )
184208 check! Native . send_query ( self , statement )
185209
186210 flush
187211 end
188212
213+ # Get the next result set from a multi-result query.
214+ # @parameter types [Hash] Type mapping to use for this result.
215+ # @returns [Result | Nil] The next result set, or `nil` if no more results.
216+ # @raises [Error] If the query resulted in an error.
189217 def next_result ( types : @types )
190218 if result = self . get_result
191219 status = Native . result_status ( result )
0 commit comments