Skip to content

First step to Rails 3 compatibility #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/pg.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# This is a compatibility layer for using the pure Ruby postgres-pr instead of
# the C interface of postgres.

require 'postgres-pr/postgres-compat'
174 changes: 174 additions & 0 deletions lib/postgres-pr/pg-compat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
# This is a compatibility layer for using the pure Ruby postgres-pr instead of
# the C interface of postgres.

require 'rexml/syncenumerator'
require 'postgres-pr/connection'

class PGconn
PQTRANS_IDLE = 0 #(connection idle)
PQTRANS_INTRANS = 2 #(idle, within transaction block)
PQTRANS_INERROR = 3 #(idle, within failed transaction)
PQTRANS_UNKNOWN = 4 #(cannot determine status)

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
@host = host
@db = database
@user = user
@conn = PostgresPR::Connection.new(database, user, auth, uri)
end

def close
@conn.close
end

attr_reader :host, :db, :user

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

alias exec query

def transaction_status
@conn.transaction_status
end

def self.escape(str)
str.gsub("'","''").gsub("\\", "\\\\\\\\")
end

def escape(str)
self.class.escape(str)
end

def notice_processor
@conn.notice_processor
end

def notice_processor=(np)
@conn.notice_processor = np
end

def self.quote_ident(name)
%("#{name}")
end

end

class PGresult
include Enumerable

EMPTY_QUERY = 0
COMMAND_OK = 1
TUPLES_OK = 2
COPY_OUT = 3
COPY_IN = 4
BAD_RESPONSE = 5
NONFATAL_ERROR = 6
FATAL_ERROR = 7

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

def [](index)
@result[index]
end

def initialize(res)
@res = res
@fields = @res.fields.map {|f| f.name}
@result = []
@res.rows.each do |row|
@result << REXML::SyncEnumerator.new(fields, row).map {|name, value| [name, value]}
end
end

# TODO: status, getlength, cmdstatus

attr_reader :result, :fields

def num_tuples
@result.size
end

alias :ntuples :num_tuples

def num_fields
@fields.size
end

alias :nfields :num_fields

def fieldname(index)
@fields[index]
end

def fieldnum(name)
@fields.index(name)
end

def type(index)
# TODO: correct?
@res.fields[index].type_oid
end

alias :ftype :type

def size(index)
raise
# TODO: correct?
@res.fields[index].typlen
end

def getvalue(tup_num, field_num)
@result[tup_num][field_num]
end

def status
if num_tuples > 0
TUPLES_OK
else
COMMAND_OK
end
end

def cmdstatus
@res.cmd_tag || ''
end

# free the result set
def clear
@res = @fields = @result = nil
end

# Returns the number of rows affected by the SQL command
def cmdtuples
case @res.cmd_tag
when nil
return nil
when /^INSERT\s+(\d+)\s+(\d+)$/, /^(DELETE|UPDATE|MOVE|FETCH)\s+(\d+)$/
$2.to_i
else
nil
end
end

alias :cmd_tuples :cmdtuples

end

class PGError < Exception
end
17 changes: 17 additions & 0 deletions lib/postgres-pr/postgres-compat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
require 'postgres-pr/connection'

class PGconn
PQTRANS_IDLE = 0 #(connection idle)
PQTRANS_INTRANS = 2 #(idle, within transaction block)
PQTRANS_INERROR = 3 #(idle, within failed transaction)
PQTRANS_UNKNOWN = 4 #(cannot determine status)

class << self
alias connect new
end
Expand Down Expand Up @@ -43,6 +48,10 @@ def self.escape(str)
str.gsub("'","''").gsub("\\", "\\\\\\\\")
end

def escape(str)
self.class.escape(str)
end

def notice_processor
@conn.notice_processor
end
Expand Down Expand Up @@ -91,10 +100,14 @@ def num_tuples
@result.size
end

alias :ntuples :num_tuples

def num_fields
@fields.size
end

alias :nfields :num_fields

def fieldname(index)
@fields[index]
end
Expand All @@ -107,6 +120,8 @@ def type(index)
# TODO: correct?
@res.fields[index].type_oid
end

alias :ftype :type

def size(index)
raise
Expand Down Expand Up @@ -146,6 +161,8 @@ def cmdtuples
nil
end
end

alias :cmd_tuples :cmdtuples

end

Expand Down