Skip to content

Commit

Permalink
* moved classes and modules into PostgresPR module
Browse files Browse the repository at this point in the history
* moved main part from postgres.rb into postgres-pr/postgres-compat.rb.
  postgres.rb now only contains the LoadError dispatch.
* new version 0.2.2
  • Loading branch information
mneumann committed Nov 22, 2004
1 parent a10a89b commit fa1e440
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 52 deletions.
2 changes: 1 addition & 1 deletion README
Expand Up @@ -21,5 +21,5 @@ Then in the interactive Ruby interpreter type (replace DBNAME and DBUSER
accordingly):

require 'postgres-pr/connection'
c = Connection.new('DBNAME', 'DBUSER')
c = PostgresPR::Connection.new('DBNAME', 'DBUSER')
c.query('SELECT 1+2').rows # => [["3"]]
5 changes: 3 additions & 2 deletions examples/test_connection.rb
@@ -1,8 +1,9 @@
$LOAD_PATH.unshift '../lib'
require 'postgres-pr/connection'

conn = Connection.new('mneumann', 'mneumann')
p conn.query("DROP TABLE test; CREATE TABLE test (a VARCHAR(100))")
conn = PostgresPR::Connection.new('mneumann', 'mneumann')
p conn.query("DROP TABLE test") rescue nil
p conn.query("CREATE TABLE test (a VARCHAR(100))")
p conn.query("INSERT INTO test VALUES ('hallo')")
p conn.query("INSERT INTO test VALUES ('leute')")
conn.query("COMMIT")
Expand Down
4 changes: 4 additions & 0 deletions lib/postgres-pr/connection.rb
Expand Up @@ -8,6 +8,8 @@
require 'socket'
require 'thread'

module PostgresPR

PROTO_VERSION = 3 << 16 #196608

class Connection
Expand Down Expand Up @@ -140,3 +142,5 @@ def establish_connection(uri)
end
end
end

end # module PostgresPR
4 changes: 4 additions & 0 deletions lib/postgres-pr/message.rb
Expand Up @@ -6,6 +6,8 @@
require 'buffer'
require 'readbytes'

module PostgresPR

class ParseError < RuntimeError; end
class DumpError < RuntimeError; end

Expand Down Expand Up @@ -520,3 +522,5 @@ class Sync < Message
class Terminate < Message
register_message_type ?X
end

end # module PostgresPR
52 changes: 52 additions & 0 deletions lib/postgres-pr/postgres-compat.rb
@@ -0,0 +1,52 @@
# This is a compatibility layer for using the pure Ruby postgres-pr instead of
# the C interface of postgres.

require 'postgres-pr/connection'

class PGconn
class << self
alias connect new
end

def initialize(host, port, options, tty, database, user, auth)
uri =
if host.nil?
nil
elsif host[0] != ?/
"tcp://#{ host }:#{ port }"
else
"unix:#{ host }/.s.PGSQL.#{ port }"
end

@db = database
@conn = PostgresPR::Connection.new(database, user, auth, uri)
end

attr_reader :db

def query(sql)
PGresult.new(@conn.query(sql))
end

alias exec query
end

class PGresult
attr_reader :fields, :result

def initialize(res)
@res = res
@fields = @res.fields.map {|f| f.name}
@result = @res.rows
end

include Enumerable

def each(&block)
@result.each(&block)
end

def [](index)
@result[index]
end
end
49 changes: 1 addition & 48 deletions lib/postgres.rb
Expand Up @@ -4,52 +4,5 @@
begin
require 'postgres.so'
rescue LoadError
require 'postgres-pr/connection'
class PGconn
class << self
alias connect new
end

def initialize(host, port, options, tty, database, user, auth)
uri =
if host.nil?
nil
elsif host[0] != ?/
"tcp://#{ host }:#{ port }"
else
"unix:#{ host }/.s.PGSQL.#{ port }"
end

@db = database
@conn = Connection.new(database, user, auth, uri)
end

attr_reader :db

def query(sql)
PGresult.new(@conn.query(sql))
end

alias exec query
end

class PGresult
attr_reader :fields, :result

def initialize(res)
@res = res
@fields = @res.fields.map {|f| f.name}
@result = @res.rows
end

include Enumerable

def each(&block)
@result.each(&block)
end

def [](index)
@result[index]
end
end
require 'postgres-pr/postgres-compat'
end
2 changes: 1 addition & 1 deletion postgres-pr.gemspec
Expand Up @@ -2,7 +2,7 @@ require 'rubygems'

spec = Gem::Specification.new do |s|
s.name = 'postgres-pr'
s.version = '0.2.1'
s.version = '0.2.2'
s.summary = 'A pure Ruby interface to the PostgreSQL database'

s.files = (Dir['lib/**/*'] + Dir['test/**/*'] +
Expand Down
2 changes: 2 additions & 0 deletions test/TC_message.rb
Expand Up @@ -24,6 +24,8 @@ def #{a}=(v) @#{a}=v end

$LOAD_PATH.unshift '../lib'
require 'postgres-pr/message'
include PostgresPR

class Buffer
alias old_content content
def content
Expand Down

0 comments on commit fa1e440

Please sign in to comment.