Change sochild view to use type registration pattern.
[xtuple] / enyo-client / database / source / xt / trigger_functions / sordtype_did_change.sql
1 create or replace function xt.sordtype_did_change() returns trigger as $$
2 /* Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
3    See www.xm.ple.com/CPAL for the full text of the software license. */
4
5 return (function () {
6
7   var sql,
8     rows,
9     ary = [];
10
11   rows = plv8.execute("select * from xt.sordtype");
12
13   if (rows.length) {
14
15     rows.forEach(function (row) {
16       sql = "select " +
17         row.sordtype_col_sochild_id + " as sochild_id, " +
18         "''" + row.sordtype_code + "''::text" + " as sochild_type, " +
19         row.sordtype_col_sochild_uuid + " as sochild_uuid, " +
20         row.sordtype_col_sochild_key + " as sochild_key, " +
21         row.sordtype_col_sochild_number + " as sochild_number, " +
22         row.sordtype_col_sochild_status + " as sochild_status, " +
23         row.sordtype_col_sochild_duedate + " as sochild_duedate, " +
24         row.sordtype_col_sochild_qty + " as sochild_qty " +
25         "from " + row.sordtype_nsname + "." + row.sordtype_tblname + " ";
26
27       if (row.sordtype_joins) {
28         sql = sql + row.sordtype_joins;
29       }
30
31       plv8.elog(WARNING, "sql: ", sql);
32       ary.push(sql);
33     });
34
35     sql = "select xt.create_view('xt.sochild','" + ary.join(" union all ") + "', true)";
36     plv8.elog(WARNING, "sql2: ", sql);
37     plv8.execute(sql);
38
39   }
40
41   return NEW;
42
43 }());
44
45 $$ language plv8;