Class: Dumbo::Aggregate

Inherits:
PgObject show all
Defined in:
lib/dumbo/aggregate.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

#ffuncObject

Returns the value of attribute ffunc.



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

def ffunc
  @ffunc
end

#initial_conditionObject

Returns the value of attribute initial_condition.



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

def initial_condition
  @initial_condition
end

#input_data_typeObject

Returns the value of attribute input_data_type.



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

def input_data_type
  @input_data_type
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#sfuncObject

Returns the value of attribute sfunc.



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

def sfunc
  @sfunc
end

#sort_operatorObject

Returns the value of attribute sort_operator.



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

def sort_operator
  @sort_operator
end

#state_data_typeObject

Returns the value of attribute state_data_type.



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

def state_data_type
  @state_data_type
end

#transnameObject

Returns the value of attribute transname.



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

def transname
  @transname
end

Instance Method Details

#load_attributesObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/dumbo/aggregate.rb', line 6

def load_attributes
  result = execute <<-SQL
    SELECT
    proname AS name,
    pg_get_function_arguments(pr.oid) AS input_data_type,
    aggtransfn AS sfunc,
    aggfinalfn AS ffunc,
    agginitval AS initial_condition,
    op.oprname AS sort_operator,
    proargtypes,
    aggtranstype AS state_data_type , proacl,
    CASE WHEN (tt.typlen = -1 AND tt.typelem != 0) THEN (SELECT at.typname FROM pg_type at WHERE at.oid = tt.typelem) || '[]' ELSE tt.typname END as state_data_type,
    prorettype AS aggfinaltype,
    --CASE WHEN (tf.typlen = -1 AND tf.typelem != 0) THEN (SELECT at.typname FROM pg_type at WHERE at.oid = tf.typelem) || '[]' ELSE tf.typname END as ffunc,
    description,
    (SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=aggfnoid) AS labels,
    (SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=aggfnoid) AS providers, oprname, opn.nspname as oprnsp
     FROM pg_aggregate ag
     LEFT OUTER JOIN pg_operator op ON op.oid=aggsortop
     LEFT OUTER JOIN pg_namespace opn ON opn.oid=op.oprnamespace
     JOIN pg_proc pr ON pr.oid = ag.aggfnoid
     JOIN pg_type tt on tt.oid=aggtranstype
     JOIN pg_type tf on tf.oid=prorettype
     LEFT OUTER JOIN pg_description des ON (des.objoid=aggfnoid::oid AND des.classoid='pg_aggregate'::regclass)
    WHERE aggfnoid = #{oid}
  SQL

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

  result.first
end

#to_sqlObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dumbo/aggregate.rb', line 40

def to_sql
  attributes = []
  attributes << "SFUNC = #{sfunc}"
  attributes << "STYPE = #{state_data_type}"
  attributes << "FINALFUNC = #{ffunc}" if ffunc && ffunc != '-'
  attributes << "INITCOND = '#{initial_condition}'" if initial_condition
  attributes << "SORTOP = #{sort_operator}" if sort_operator

  <<-SQL.gsub(/^ {6}/, '')
  CREATE AGGREGATE #{name}(#{input_data_type}) (
    #{attributes.join(",\n  ")}
  );
  SQL
end