1 /*jshint node:true, indent:2, curly:false, eqeqeq:true, immed:true, latedef:true, newcap:true, noarg:true,
2 regexp:true, undef:true, strict:true, trailing:true, white:true */
8 var path = require('path');
10 var convertFromMetasql = function (content, filename, defaultSchema) {
11 var lines = content.split("\n"),
12 schema = defaultSchema ? "'" + defaultSchema + "'" : "NULL",
21 if (lines[0].indexOf("-- Group: ") !== 0 ||
22 lines[1].indexOf("-- Name: ") !== 0 ||
23 lines[2].indexOf("-- Notes:") !== 0) {
24 throw new Error("Improperly formatted metasql: " + filename);
26 group = lines[0].substring("-- Group: ".length).trim();
27 name = lines[1].substring("-- Name: ".length).trim();
28 while (lines[i].indexOf("--") === 0) {
29 notes = notes + lines[i].substring(2) + "\n";
32 notes = notes.substring(" Notes:".length);
33 if (notes.indexOf("must be grade 10") >= 0) {
37 insertSql = "select saveMetasql (" +
40 "$$" + notes + "$$," +
41 "$$" + content + "$$," +
42 "true, " + schema + ", " + grade + ");";
47 var convertFromReport = function (content, filename, defaultSchema) {
48 var lines = content.split("\n"),
51 tableName = defaultSchema ? defaultSchema + ".pkgreport" : "report",
55 if (lines[3].indexOf(" <name>") !== 0 ||
56 lines[4].indexOf(" <description>") !== 0) {
57 throw new Error("Improperly formatted report");
59 name = lines[3].substring(" <name>".length).trim();
60 name = name.substring(0, name.indexOf("<"));
61 description = lines[4].substring(" <description>".length).trim();
62 description = description.substring(0, description.indexOf("<"));
63 if (lines[5].indexOf("grade") >= 0) {
64 grade = lines[5].substring(" <grade>".length).trim();
65 grade = grade.substring(0, grade.indexOf("<"));
68 upsertSql = "do language plpgsql $do$" +
69 "declare _id integer := null;" +
71 " update " + tableName + " set " +
72 " report_descrip = $$" + description + "$$," +
73 " report_source = $$" + content + "$$," +
74 " report_loaddate = now() " +
75 " where report_name = '" + name + "'" +
76 " and report_grade = " + grade + " returning report_id into _id;" +
77 " if _id is null then " +
78 " insert into " + tableName + " (report_name, report_descrip," +
79 " report_source, report_loaddate, report_grade)" +
80 " select '" + name + "', $$" + description + "$$," +
81 " $$" + content + "$$, now(), min(sequence_value)" +
83 " where sequence_value >= " + grade + "" +
84 " and sequence_value not in (" +
85 " select c.report_grade from report c where report_name = '" + name + "'" +
92 var convertFromScript = function (content, filename, defaultSchema) {
93 var name = path.basename(filename, '.js'),
94 tableName = defaultSchema ? defaultSchema + ".pkgscript" : "unknown",
95 notes = "", //"xtMfg package",
99 insertSql = "insert into " + tableName + " (script_name, script_order, script_enabled, " +
100 "script_source, script_notes) select " +
101 "'" + name + "', 0, TRUE, " +
102 "$$" + content + "$$," +
104 " where not exists (select c.script_id from " + tableName + " c " +
105 "where script_name = '" + name + "');";
107 updateSql = "update " + tableName + " set " +
108 "script_name = '" + name + "', script_order = 0, script_enabled = TRUE, " +
109 "script_source = $$" + content +
110 "$$, script_notes = '" + notes + "' " +
111 "where script_name = '" + name + "';";
113 return insertSql + updateSql;
116 var convertFromUiform = function (content, filename, defaultSchema) {
117 var name = path.basename(filename, '.ui'),
118 tableName = defaultSchema ? defaultSchema + ".pkguiform" : "unknown",
119 notes = "", //"xtMfg package",
123 insertSql = "insert into " + tableName + " (uiform_name, uiform_order, uiform_enabled, " +
124 "uiform_source, uiform_notes) select " +
125 "'" + name + "', 0, TRUE, " +
126 "$$" + content + "$$," +
128 " where not exists (select c.uiform_id from " + tableName + " c " +
129 "where uiform_name = '" + name + "');";
131 updateSql = "update " + tableName + " set uiform_name = '" +
132 name + "', uiform_order = 0, uiform_enabled = TRUE, " +
133 "uiform_source = $$" + content + "$$, uiform_notes = '" + notes + "' " +
134 "where uiform_name = '" + name + "';";
136 return insertSql + updateSql;
139 exports.conversionMap = {
140 mql: convertFromMetasql,
141 xml: convertFromReport,
142 js: convertFromScript,
143 ui: convertFromUiform,
144 sql: function (content) {