Class: CreateProcedures

Inherits:
ActiveRecord::Migration
  • Object
show all
Defined in:
lib/generators/post_json/install/templates/create_procedures.rb

Overview

Instance Method Summary collapse

Instance Method Details

#changeObject



4
5
6
7
8
9
10
11
12
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 4

def change
  ActiveRecord::Base.connection.execute(json_numeric_procedure)
  ActiveRecord::Base.connection.execute(json_text_procedure)
  ActiveRecord::Base.connection.execute(js_filter_procedure)
  ActiveRecord::Base.connection.execute(json_selector_procedure)
  ActiveRecord::Base.connection.execute(json_selectors_procedure)
  ActiveRecord::Base.connection.execute(show_all_indexes_procedure)
  ActiveRecord::Base.connection.execute(show_indexes_procedure)
end

#js_filter_procedureObject



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 32

def js_filter_procedure
"create or replace function js_filter(js_function text, json_arguments text, data json) returns numeric as $$
if (data == null) {
  return null;
}
eval('var func = ' + js_function);
eval('var args = ' + (json_arguments == '' ? 'null' : json_arguments));
var final_args = [data].concat(args);
var result = func.apply(null, final_args);
return result == true || 0 < parseInt(result) ? 1 : 0;
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
end

#json_numeric_procedureObject



14
15
16
17
18
19
20
21
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 14

def json_numeric_procedure
"CREATE OR REPLACE FUNCTION json_numeric(key text, data json) RETURNS numeric AS $$
if (data == null) {
  return null;
}
return data[key];
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
end

#json_selector_procedureObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 45

def json_selector_procedure
"CREATE OR REPLACE FUNCTION json_selector(selector text, data json) RETURNS text AS $$
if (data == null || selector == null || selector == '') {
  return null;
}
var names = selector.split('.');
var result = names.reduce(function(previousValue, currentValue, index, array) {
  if (previousValue == null) {
    return null;
  } else {
    return previousValue[currentValue];
  }
}, data);
return result;
$$ LANGUAGE plv8 IMMUTABLE STRICT;"    
end

#json_selectors_procedureObject



62
63
64
65
66
67
68
69
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 62

def json_selectors_procedure
"CREATE OR REPLACE FUNCTION json_selectors(selectors text, data json) RETURNS json AS $$
var json_selector = plv8.find_function('json_selector');
var selectorArray = selectors.replace(/\s+/g, '').split(',');
var result = selectorArray.map(function(selector) { return json_selector(selector, data); });
return result;
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
end

#json_text_procedureObject



23
24
25
26
27
28
29
30
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 23

def json_text_procedure
"CREATE OR REPLACE FUNCTION json_text(key text, data json) RETURNS text AS $$
if (data == null) {
  return null;
}
return data[key];
$$ LANGUAGE plv8 IMMUTABLE STRICT;"    
end

#show_all_indexes_procedureObject



71
72
73
74
75
76
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 71

def show_all_indexes_procedure
  "CREATE OR REPLACE FUNCTION show_all_indexes() RETURNS json AS $$
    var sql = \"SELECT c3.relname AS table, c2.relname AS index FROM pg_class c2 LEFT JOIN pg_index i ON c2.oid = i.indexrelid LEFT JOIN pg_class c1 ON c1.oid = i.indrelid RIGHT OUTER JOIN pg_class c3 ON c3.oid = c1.oid LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c3.relnamespace WHERE c3.relkind IN ('r','') AND n.nspname NOT IN ('pg_catalog', 'pg_toast') AND pg_catalog.pg_table_is_visible(c3.oid) ORDER BY c3.relpages DESC;\"
    return plv8.execute( sql );
  $$ LANGUAGE plv8 IMMUTABLE STRICT;"
end

#show_indexes_procedureObject



78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/generators/post_json/install/templates/create_procedures.rb', line 78

def show_indexes_procedure
"CREATE OR REPLACE FUNCTION show_indexes(table_name text DEFAULT '', index_prefix text DEFAULT '') RETURNS json AS $$
var show_all_indexes = plv8.find_function('show_all_indexes');
var indexes = show_all_indexes();
if (0 < (table_name || '').length) {
  indexes = indexes.filter(function(row) { return row['table'] === table_name; });
}
if (0 < (index_prefix || '').length) {
  indexes = indexes.filter(function(row) { return row['index'].lastIndexOf(index_prefix, 0) === 0; });
}
return indexes;
$$ LANGUAGE plv8 IMMUTABLE STRICT;"
end