Skip to content

Hacker0x01/pg_result_init

Repository files navigation

PgResultInit

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.

Installation

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

Usage

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)  # => 23

Column Configuration

Each 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

Common PostgreSQL Type OIDs

  • 23 - INTEGER
  • 25 - TEXT/VARCHAR
  • 16 - BOOLEAN
  • 1114 - TIMESTAMP
  • 1082 - DATE
  • 701 - FLOAT8/DOUBLE PRECISION

Use Cases

  • 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

Development

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 compile

Run tests with:

bundle exec rake spec

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/Hacker0x01/pg_result_init.

License

The gem is available as open source under the terms of the MIT License.

About

PostgreSQL result initialization C extension for Ruby

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published