Class: Conreality::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/conreality/client.rb

Overview

Client for accessing a Conreality master server.

Instance Attribute Summary collapse

Database transactions collapse

Database interface collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :dbname (String)

23
24
25
26
27
28
# File 'lib/conreality/client.rb', line 23

def initialize(options = {})
  @conn = PG.connect(options)
  @conn.type_map_for_results = PG::BasicTypeMapForResults.new(@conn)
  @session = Session.new(self)
  @session.start!
end

Instance Attribute Details

#connPG::Connection

The PostgreSQL socket connection.

Returns:

  • (PG::Connection)

12
13
14
# File 'lib/conreality/client.rb', line 12

def conn
  @conn
end

#sessionSession

The current session.

Returns:


18
19
20
# File 'lib/conreality/client.rb', line 18

def session
  @session
end

Instance Method Details

#call_proc_by_name(proc_name, args: [], cast: nil, &block) ⇒ any

Parameters:

  • proc_name (#to_s)
  • args (Array) (defaults to: [])
  • cast (#to_s, nil) (defaults to: nil)

Returns:

  • (any)

76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/conreality/client.rb', line 76

def call_proc_by_name(proc_name, args: [], cast: nil, &block)
  proc_sig = "#{q(Database::SCHEMA)}.#{q(proc_name)}("
  case args
    when Array
      proc_sig << 1.upto(args.size).map { |i| "$#{i}" }.join(', ') unless args.empty?
    when Hash
      raise TypeError # TODO
    else raise TypeError
  end
  proc_sig << ")"
  self.call_proc_by_signature(proc_sig, args: args, cast: cast, &block)
end

#call_proc_by_signature(proc_sig, args: [], cast: nil, &block) ⇒ any

Parameters:

  • proc_sig (#to_s)
  • args (Array) (defaults to: [])
  • cast (#to_s, nil) (defaults to: nil)

Returns:

  • (any)

94
95
96
97
# File 'lib/conreality/client.rb', line 94

def call_proc_by_signature(proc_sig, args: [], cast: nil, &block)
  query_body = cast ? "#{proc_sig}::#{cast}" : proc_sig.to_s
  self.exec_with_params("SELECT #{query_body}", *args, &block)
end

#call_proc_with_result(proc_name, args: [], cast: nil) ⇒ any

Parameters:

  • proc_name (#to_s)
  • args (Array) (defaults to: [])
  • cast (#to_s, nil) (defaults to: nil)

Returns:

  • (any)

65
66
67
68
69
# File 'lib/conreality/client.rb', line 65

def call_proc_with_result(proc_name, args: [], cast: nil)
  self.call_proc_by_name(proc_name, args: args, cast: cast) do |results|
    results.getvalue(0, 0)
  end
end

#exec_with_params(query, *args, &block) ⇒ PG::Result

Parameters:

  • query (String)
  • args (Array)

Returns:

  • (PG::Result)

103
104
105
# File 'lib/conreality/client.rb', line 103

def exec_with_params(query, *args, &block)
  @conn.exec_params(query, args, 0, &block)
end

#inspectString

Returns a developer-friendly representation of this client.

Returns:

  • (String)

34
35
36
# File 'lib/conreality/client.rb', line 34

def inspect
  sprintf("#<%s:%#0x>", self.class.name, self.__id__)
end

#open {|transaction| ... }

This method returns an undefined value.

Yields:

  • (transaction)

Yield Parameters:

Yield Returns:

  • (void)

45
46
47
48
49
50
51
52
53
54
# File 'lib/conreality/client.rb', line 45

def open(&block)
  tx = Database::Transaction.new(self)
  if @conn.transaction_status.zero?
    # not yet in transaction scope
    @conn.transaction { |_| block.call(tx) }
  else
    # already in transaction scope
    block.call(tx)
  end
end