Javascript Build SQL statements

Date: 2020-11-02
(() => {
    function guid() { // Public Domain/MIT
        var d = new Date().getTime();//Timestamp
        var d2 = (performance && performance.now && (performance.now() * 1000)) || 0;//Time in microseconds since page-load or 0 if unsupported
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16;//random number between 0 and 16
            if (d > 0) {//Use timestamp until depleted
                r = (d + r) % 16 | 0;
                d = Math.floor(d / 16);
            } else {//Use microseconds since page-load if supported
                r = (d2 + r) % 16 | 0;
                d2 = Math.floor(d2 / 16);
            }
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
    }

    const questions = [
        {
            "Text": "Laadruimte in goede staat? (J/N)",
            "QuestionType": "Meerkeuzevraag",
            "Situation": "Vooraf",
            "Level": "Aanmelding",
            "IsForTransportDirectionOut": "Ja",
            "IsForTransportDirectionIn": "Nee",
            "Container": "Ja",
            "Auto": "Ja",
            "Bulk": "Ja",
            "Bigbags": "Ja",
            "Zakgoed ": "Ja",
            "Emballage": "Ja"
        }
    ];

    var sql = questions.map((q, i) => {
        const getQuestionType = () => {
            if (q.QuestionType == "Invoer getal") return 1;
            if (q.QuestionType == "Meerkeuzevraag") return 2;
            if (q.QuestionType == "Foto") return 3;
            return 0;
        }
        const getSituation = () => {
            if (q.Situation == "Achteraf") return 2;
            return 1;
        }

        const getLevel = () => {
            if (q.Level == "Aanmelding") return 1;
            if (q.Level == "Order") return 2;
            if (q.Level == "Artikel") return 3;
            return 0;
        }

        const strToBool = (s) => s == "Ja" ? 1 : 0;

        const columns = [
            "Id",
            "SortOrder",
            "Text",
            "AS400Link",
            "QuestionType",
            "Level",
            "Situation",
            "IsForTransportDirectionIn",
            "IsForTransportDirectionOut",
            "IsForCargoTypeBulk",
            "IsForCargoTypeBagged",
            "IsForCargoTypePackaging",
            "IsForBigBags",
            "IsForTransporterTypeContainer",
            "IsForTransporterTypeAuto"
        ]
        const values = [
            guid(), // Id
            i + 1, // SortOrder
            q.Text, // Text
            '', //AS400Link,
            getQuestionType(), // QuestionType
            getLevel(), // Level
            getSituation(), //Situation
            strToBool(q.IsForTransportDirectionIn), //IsForTransportDirectionIn
            strToBool(q.IsForTransportDirectionOut), //IsForTransportDirectionOut
            strToBool(q.Bulk),// IsForCargoTypeBulk
            strToBool(q.Zakgoed),// IsForCargoTypeBagged
            strToBool(q.Emballage),// IsForCargoTypePackaging
            strToBool(q.Bigbags), // IsForBigBags
            strToBool(q.Container),// IsForTransporterTypeContainer
            strToBool(q.Auto),// IsForTransporterTypeAuto
        ];

        const sqlColumns = columns.map(c => `[${c}]`).join(", ");
        const sqlValues = values.map(c => typeof c == "number" ? c : `'${c}'`).join(", ");
        return [
            "INSERT INTO [dbo].[Question]",
            `\t(${sqlColumns})`,
            "\tVALUES",
            `\t(${sqlValues})`,
        ].join("\r\n");
    });

    console.log(sql.join("\r\n"));
})();
42050cookie-checkJavascript Build SQL statements