models/UB/DDLGenerator_reg.js

/**
 * @author xmax
 * @module DDLGenerator
 */

/**
 * Endpoint for generate DDL script
 * Expect POST request with JSON in BODY: `["entity1", "entity2", ...]` or  `{unsafe: Boolean, resultAsObject: Boolean, entities: ["entity1", "entity2", ...]}`
 *
 * In case `resultAsObject === true` return object notation of script, otherwise - text
 * @param {THTTPRequest} req
 * @param {THTTPResponse} resp
 */
function generateDDL(req, resp) {
    //todo - rewrire DDLGenerator to use modules, not Ext
    require('DDLGenerator');
    require('DDLMaker');
    require('DDLMakerFirebird');
    require('DDLMakerOracle');
    require('DDLMakerPostgre');
    require('DDLMakerSQLite3');
    require('DDLMakerSQLServer');

    function formatAsText(ddlResult) {
        var
            txtRes = [],
            connResult;

        function formatOutCode(arrOfString, comment) {
            if (typeof arrOfString === 'undefined'){
                txtRes.push('ERR: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ' + comment);
                return;
            }
            if (arrOfString.length) {
                arrOfString.push(' '); // for last delimiter
                txtRes.push(
                    '-- ' + comment,
                    '--#############################################################',
                    arrOfString.join(';' + '\r\n--\r\n')
                );
            }
        }

        Object.keys(ddlResult).forEach(function (connectionName) {
            "use strict";
            //txtRes.push('--##############     start script for conection "' + connectionName + '" #######');
            connResult = ddlResult[connectionName];

            if (connResult.warnings.length > 0) {
                txtRes.push('/*\r\n $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \r\n Attantion! Achtung! Vnimanie! ',
                    '\r\n', connResult.warnings.join('\r\n'),
                    '\r\n $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ \r\n*/\r\n');
            }

            formatOutCode(connResult.sysSQL, 'System objects');
            formatOutCode(connResult.dropFkSQL, 'Drop foreign keys');
            formatOutCode(connResult.dropIndexSQL, 'Drop indexes');
            formatOutCode(connResult.dropPkSQL, 'Drop primary keys');
            formatOutCode(connResult.dropDefaultSQL, 'Drop default constraint');
            formatOutCode(connResult.dropCheckCSQL, 'Drop check constraint');
            formatOutCode(connResult.dropTriggerSQL, 'Drop triggers');
            formatOutCode(connResult.dropSequnceSQL, 'Drop sequence');

            formatOutCode(connResult.createTableSQL, 'Create tables');
            formatOutCode(connResult.addColumnSQL, 'Add columns');

            formatOutCode(connResult.alterColumnSQL, 'Alter columns');
            formatOutCode(connResult.updateColumnSQL, '! update values for known changes or estimated changes  :-)');
            formatOutCode(connResult.renameSQL, 'Renamed objects');

            formatOutCode(connResult.setDefaultSQL, 'Set new default');
            formatOutCode(connResult.alterColumnNotNullSQL, 'Alter columns set not null where was null allowed');

            formatOutCode(connResult.createPkSQL, 'Create primary keys');
            formatOutCode(connResult.createIndexSQL, 'Create indexes');

            formatOutCode(connResult.createCheckCSQL, 'Create check constraint');
            formatOutCode(connResult.createFkSQL, 'Create foreign keys');
            formatOutCode(connResult.createSequenceSQL, 'Create sequence');
            formatOutCode(connResult.createTriggerSQL, 'Create triggers');

            formatOutCode(connResult.othersSQL, 'Create other object defined in dbExtensions section entity domain');

            formatOutCode(connResult.dropColumnSQL, 'drop columns');
            formatOutCode(connResult.captionSQL, 'caption');
        });

        if (txtRes.length){
            return txtRes.join('\r\n');
        } else {
            return '';
        }
    }
    var Ext = require('Ext');

    var fddlGenerator = Ext.create('UB.DDLgenerator');
    var requestObj = JSON.parse(req.read());
    var result = fddlGenerator.generateDDL(requestObj);
    if (requestObj.resultAsObject){
        resp.writeEnd(result);
    } else {
        resp.writeHead('Content-Type: text/plain');
        resp.writeEnd(formatAsText(result));
    }

    resp.statusCode = 200;
}
App.registerEndpoint('generateDDL', generateDDL);