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 "    SELECT\n    proname AS name,\n    pg_get_function_arguments(pr.oid) AS input_data_type,\n    aggtransfn AS sfunc,\n    aggfinalfn AS ffunc,\n    agginitval AS initial_condition,\n    op.oprname AS sort_operator,\n    proargtypes,\n    aggtranstype AS state_data_type , proacl,\n    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,\n    prorettype AS aggfinaltype,\n    --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,\n    description,\n    (SELECT array_agg(label) FROM pg_seclabels sl1 WHERE sl1.objoid=aggfnoid) AS labels,\n    (SELECT array_agg(provider) FROM pg_seclabels sl2 WHERE sl2.objoid=aggfnoid) AS providers, oprname, opn.nspname as oprnsp\n     FROM pg_aggregate ag\n     LEFT OUTER JOIN pg_operator op ON op.oid=aggsortop\n     LEFT OUTER JOIN pg_namespace opn ON opn.oid=op.oprnamespace\n     JOIN pg_proc pr ON pr.oid = ag.aggfnoid\n     JOIN pg_type tt on tt.oid=aggtranstype\n     JOIN pg_type tf on tf.oid=prorettype\n     LEFT OUTER JOIN pg_description des ON (des.objoid=aggfnoid::oid AND des.classoid='pg_aggregate'::regclass)\n    WHERE aggfnoid = \#{oid}\n  SQL\n\n  result.first.each do |k, v|\n    send(\"\#{k}=\", v) rescue nil\n  end\n\n  result.first\nend\n"

#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

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