Module: PgSaurus::ConnectionAdapters::PostgreSQLAdapter::FunctionMethods
- Included in:
- PgSaurus::ConnectionAdapters::PostgreSQLAdapter
- Defined in:
- lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb
Overview
Methods to extend ActiveRecord::ConnectionAdapters::PostgreSQLAdapter to support database functions.
Instance Method Summary collapse
-
#create_function(function_name, returning, definition, options = {}) ⇒ Object
Create a new database function.
-
#drop_function(function_name, options = {}) ⇒ Object
Drop the given database function.
-
#functions ⇒ Object
Return a list of defined DB functions.
-
#supports_functions? ⇒ Boolean
Return
true.
Instance Method Details
#create_function(function_name, returning, definition, options = {}) ⇒ Object
Create a new database function.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 55 def create_function(function_name, returning, definition, = {}) function_name = full_function_name(function_name, ) language = [:language] || 'plpgsql' replace = if [:replace] == false '' else 'OR REPLACE ' end sql = <<-SQL.gsub(/^[ ]{6}/, "") CREATE #{replace}FUNCTION #{function_name} RETURNS #{returning} LANGUAGE #{language} AS $function$ #{definition.strip} $function$ SQL execute(sql) end |
#drop_function(function_name, options = {}) ⇒ Object
Drop the given database function.
78 79 80 81 82 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 78 def drop_function(function_name, = {}) function_name = full_function_name(function_name, ) execute "DROP FUNCTION #{function_name}" end |
#functions ⇒ Object
Return a list of defined DB functions. Ignore function definitions that can’t be parsed.
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 11 def functions res = select_all <<-SQL SELECT n.nspname AS "Schema", p.proname AS "Name", pg_catalog.pg_get_function_result(p.oid) AS "Returning", CASE WHEN p.proisagg THEN 'agg' WHEN p.proiswindow THEN 'window' WHEN p.prorettype = 'pg_catalog.trigger'::pg_catalog.regtype THEN 'trigger' ELSE 'normal' END AS "Type", p.oid AS "Oid" FROM pg_catalog.pg_proc p LEFT JOIN pg_catalog.pg_namespace n ON n.oid = p.pronamespace WHERE pg_catalog.pg_function_is_visible(p.oid) AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' ORDER BY 1, 2, 3, 4; SQL res.inject([]) do |buffer, row| returning = row['Returning'] function_type = row['Type'] oid = row['Oid'] function_str = select_value("SELECT pg_get_functiondef(#{oid});") name = parse_function_name(function_str) language = parse_function_language(function_str) definition = parse_function_definition(function_str) if definition buffer << ::PgSaurus::ConnectionAdapters::FunctionDefinition.new(name, returning, definition.strip, function_type, language, oid) end buffer end end |
#supports_functions? ⇒ Boolean
Return true.
6 7 8 |
# File 'lib/pg_saurus/connection_adapters/postgresql_adapter/function_methods.rb', line 6 def supports_functions? true end |