Class: Dumbo::Operator

Inherits:
PgObject show all
Defined in:
lib/dumbo/operator.rb

Instance Attribute Summary collapse

Attributes inherited from PgObject

#oid

Instance Method Summary collapse

Methods inherited from PgObject

#downgrade, #execute, #get, identfied_by, #identify, #initialize, #upgrade

Constructor Details

This class inherits a constructor from Dumbo::PgObject

Instance Attribute Details

#commutatorObject

Returns the value of attribute commutator.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def commutator
  @commutator
end

#function_nameObject

Returns the value of attribute function_name.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def function_name
  @function_name
end

#hashesObject

Returns the value of attribute hashes.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def hashes
  @hashes
end

#joinObject

Returns the value of attribute join.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def join
  @join
end

#kindObject

Returns the value of attribute kind.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def kind
  @kind
end

#leftargObject

Returns the value of attribute leftarg.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def leftarg
  @leftarg
end

#mergesObject

Returns the value of attribute merges.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def merges
  @merges
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def name
  @name
end

#negatorObject

Returns the value of attribute negator.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def negator
  @negator
end

#restrictObject

Returns the value of attribute restrict.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def restrict
  @restrict
end

#result_typeObject

Returns the value of attribute result_type.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def result_type
  @result_type
end

#rightargObject

Returns the value of attribute rightarg.



3
4
5
# File 'lib/dumbo/operator.rb', line 3

def rightarg
  @rightarg
end

Instance Method Details

#dropObject



68
69
70
# File 'lib/dumbo/operator.rb', line 68

def drop
  "DROP OPERATOR #{name};"
end

#load_attributesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dumbo/operator.rb', line 18

def load_attributes
  result = execute <<-SQL
    SELECT
     op.oprname AS name,
     op.oprkind AS kind,
     op.oprcanhash AS hashes,
     op.oprcanmerge AS merges,
     lt.typname AS leftarg,
     rt.typname AS rightarg,
     et.typname AS result_type,
     co.oprname AS commutator,
     ne.oprname AS negator,
     op.oprcode AS function_name,
     op.oprjoin AS join,
     op.oprrest AS restrict,
     description
    FROM pg_operator op
    LEFT OUTER JOIN pg_type lt ON lt.oid=op.oprleft
    LEFT OUTER JOIN pg_type rt ON rt.oid=op.oprright
    JOIN pg_type et on et.oid=op.oprresult
    LEFT OUTER JOIN pg_operator co ON co.oid=op.oprcom
    LEFT OUTER JOIN pg_operator ne ON ne.oid=op.oprnegate
    LEFT OUTER JOIN pg_description des ON des.objoid=op.oid
    WHERE op.oid = #{oid}
  SQL

  result.first.each do |k, v|
    send("#{k}=", v) rescue nil
  end

  result.first
end

#to_sqlObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dumbo/operator.rb', line 51

def to_sql
  attrs = [:leftarg, :rightarg, :commutator, :negator, :restrict, :join].reduce([]) do |mem, attr|
    mem << "#{attr.to_s.upcase} = #{public_send(attr)}" if public_send(attr)
    mem
  end
  atttr_str = attrs.join(",\n  ")
  attrs << ",\n  HASHES" if hashes
  attrs << ",\n  MERGES" if merges

  <<-SQL.gsub(/^ {6}/, '')
  CREATE OPERATOR #{name} (
    PROCEDURE = #{function_name},
    #{atttr_str}
  );
  SQL
end