A Ruby C extension for creating PostgreSQL result sets programmatically. This gem provides the ability to construct PG::Result objects from raw data without requiring a database connection.
Add this line to your application's Gemfile:
gem 'pg_result_init', git: 'https://github.com/Hacker0x01/pg_result_init.git', branch: 'main'And then execute:
$ bundle install
The gem provides a single method PgResultInit.create that allows you to create a new PG::Result object:
require 'pg_result_init'
# You need a PostgreSQL connection and a base result
conn = PG.connect(dbname: 'your_database')
base_result = conn.exec('SELECT 1') # Any simple query to get a base result
# Define your columns
columns = [
  {
    name: 'user_id',
    typid: 23,        # PostgreSQL type OID (23 = integer)
    typlen: 4,        # Type length
    tableid: 0,       # Table OID (optional, defaults to 0)
    columnid: 0,      # Column number (optional, defaults to 0)
    format: 0,        # Format: 0 = text, 1 = binary (optional, defaults to 0)
    atttypmod: -1     # Type modifier (optional, defaults to -1)
  },
  {
    name: 'username',
    typid: 25,        # 25 = text
    typlen: -1        # Variable length
  }
]
# Define your row data
rows = [
  [1, 'alice'],
  [2, 'bob'],
  [3, 'charlie']
]
# Create the result
result = PgResultInit.create(conn, base_result, columns, rows)
# Use it like any PG::Result
puts result.to_a
# => [{"user_id"=>"1", "username"=>"alice"}, 
#     {"user_id"=>"2", "username"=>"bob"}, 
#     {"user_id"=>"3", "username"=>"charlie"}]
puts result.field_values('username')
# => ["alice", "bob", "charlie"]
puts result.fname(0)  # => "user_id"
puts result.ftype(0)  # => 23Each column hash supports the following keys:
- name (required): Column name as a string
 - typid (required): PostgreSQL type OID (integer)
 - typlen (required): Type length (integer)
 - tableid (optional): Table OID, defaults to 0
 - columnid (optional): Column number, defaults to 0
 - format (optional): Format (0 = text, 1 = binary), defaults to 0
 - atttypmod (optional): Type modifier, defaults to -1
 
23- INTEGER25- TEXT/VARCHAR16- BOOLEAN1114- TIMESTAMP1082- DATE701- FLOAT8/DOUBLE PRECISION
- Testing: Create mock PostgreSQL results for unit tests
 - Data Transformation: Convert external data into PostgreSQL-compatible result sets
 - Query Analysis Tools: Build development tools that simulate PostgreSQL results
 - Caching: Store and recreate query results without hitting the database
 
After checking out the repo, run bundle install to install dependencies. You'll need:
- Ruby 3.0+
 - PostgreSQL development headers
 - A running PostgreSQL instance for tests
 
Then, run the following to compile the C extension:
bundle exec rake compileRun tests with:
bundle exec rake specBug reports and pull requests are welcome on GitHub at https://github.com/Hacker0x01/pg_result_init.
The gem is available as open source under the terms of the MIT License.