Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/postgres/postgres-pr/postgres-compat.rb
Views: 11780
# -*- coding: binary -*-1# This is a compatibility layer for using the pure Ruby postgres-pr instead of2# the C interface of postgres.34require 'postgres_msf'5require 'postgres/postgres-pr/connection'67# Namespace for Metasploit branch.8module Msf9module Db1011class PGconn12class << self13alias connect new14end1516def initialize(host, port, options, tty, database, user, auth)17uri =18if host.nil?19nil20elsif host[0] != ?/21"tcp://#{ host }:#{ port }"22else23"unix:#{ host }/.s.PGSQL.#{ port }"24end25@host = host26@db = database27@user = user28@conn = PostgresPR::Connection.new(database, user, auth, uri)29end3031def close32@conn.close33end3435attr_reader :host, :db, :user3637def query(sql)38PGresult.new(@conn.query(sql))39end4041alias exec query4243def transaction_status44@conn.transaction_status45end4647def self.escape(str)48str.gsub("'","''").gsub("\\", "\\\\\\\\")49end5051def notice_processor52@conn.notice_processor53end5455def notice_processor=(np)56@conn.notice_processor = np57end5859def self.quote_ident(name)60%("#{name}")61end6263end6465class PGresult66include Enumerable6768EMPTY_QUERY = 069COMMAND_OK = 170TUPLES_OK = 271COPY_OUT = 372COPY_IN = 473BAD_RESPONSE = 574NONFATAL_ERROR = 675FATAL_ERROR = 77677def each(&block)78@result.each(&block)79end8081def [](index)82@result[index]83end8485def initialize(res)86@res = res87@fields = @res.fields.map {|f| f.name}88@result = @res.rows89end9091# TODO: status, getlength, cmdstatus9293attr_reader :result, :fields9495def num_tuples96@result.size97end9899def num_fields100@fields.size101end102103def fieldname(index)104@fields[index]105end106107def fieldnum(name)108@fields.index(name)109end110111def type(index)112# TODO: correct?113@res.fields[index].type_oid114end115116def size(index)117raise118# TODO: correct?119@res.fields[index].typlen120end121122def getvalue(tup_num, field_num)123@result[tup_num][field_num]124end125126def status127if num_tuples > 0128TUPLES_OK129else130COMMAND_OK131end132end133134def cmdstatus135@res.cmd_tag || ''136end137138# free the result set139def clear140@res = @fields = @result = nil141end142143# Returns the number of rows affected by the SQL command144def cmdtuples145case @res.cmd_tag146when nil147return nil148when /^INSERT\s+(\d+)\s+(\d+)$/, /^(DELETE|UPDATE|MOVE|FETCH)\s+(\d+)$/149$2.to_i150else151nil152end153end154155end156157class PGError < ::Exception158end159160end161end162163164