{"id":4205,"date":"2020-11-02T15:32:54","date_gmt":"2020-11-02T14:32:54","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4205"},"modified":"2022-07-25T08:35:42","modified_gmt":"2022-07-25T07:35:42","slug":"javascript-build-sql-statements","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/javascript-build-sql-statements\/","title":{"rendered":"Javascript Build SQL statements"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">(() => {\n    function guid() { \/\/ Public Domain\/MIT\n        var d = new Date().getTime();\/\/Timestamp\n        var d2 = (performance &amp;&amp; performance.now &amp;&amp; (performance.now() * 1000)) || 0;\/\/Time in microseconds since page-load or 0 if unsupported\n        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(\/[xy]\/g, function (c) {\n            var r = Math.random() * 16;\/\/random number between 0 and 16\n            if (d > 0) {\/\/Use timestamp until depleted\n                r = (d + r) % 16 | 0;\n                d = Math.floor(d \/ 16);\n            } else {\/\/Use microseconds since page-load if supported\n                r = (d2 + r) % 16 | 0;\n                d2 = Math.floor(d2 \/ 16);\n            }\n            return (c === 'x' ? r : (r &amp; 0x3 | 0x8)).toString(16);\n        });\n    }\n\n    const questions = [\n        {\n            \"Text\": \"Laadruimte in goede staat? (J\/N)\",\n            \"QuestionType\": \"Meerkeuzevraag\",\n            \"Situation\": \"Vooraf\",\n            \"Level\": \"Aanmelding\",\n            \"IsForTransportDirectionOut\": \"Ja\",\n            \"IsForTransportDirectionIn\": \"Nee\",\n            \"Container\": \"Ja\",\n            \"Auto\": \"Ja\",\n            \"Bulk\": \"Ja\",\n            \"Bigbags\": \"Ja\",\n            \"Zakgoed \": \"Ja\",\n            \"Emballage\": \"Ja\"\n        }\n    ];\n\n    var sql = questions.map((q, i) => {\n        const getQuestionType = () => {\n            if (q.QuestionType == \"Invoer getal\") return 1;\n            if (q.QuestionType == \"Meerkeuzevraag\") return 2;\n            if (q.QuestionType == \"Foto\") return 3;\n            return 0;\n        }\n        const getSituation = () => {\n            if (q.Situation == \"Achteraf\") return 2;\n            return 1;\n        }\n\n        const getLevel = () => {\n            if (q.Level == \"Aanmelding\") return 1;\n            if (q.Level == \"Order\") return 2;\n            if (q.Level == \"Artikel\") return 3;\n            return 0;\n        }\n\n        const strToBool = (s) => s == \"Ja\" ? 1 : 0;\n\n        const columns = [\n            \"Id\",\n            \"SortOrder\",\n            \"Text\",\n            \"AS400Link\",\n            \"QuestionType\",\n            \"Level\",\n            \"Situation\",\n            \"IsForTransportDirectionIn\",\n            \"IsForTransportDirectionOut\",\n            \"IsForCargoTypeBulk\",\n            \"IsForCargoTypeBagged\",\n            \"IsForCargoTypePackaging\",\n            \"IsForBigBags\",\n            \"IsForTransporterTypeContainer\",\n            \"IsForTransporterTypeAuto\"\n        ]\n        const values = [\n            guid(), \/\/ Id\n            i + 1, \/\/ SortOrder\n            q.Text, \/\/ Text\n            '', \/\/AS400Link,\n            getQuestionType(), \/\/ QuestionType\n            getLevel(), \/\/ Level\n            getSituation(), \/\/Situation\n            strToBool(q.IsForTransportDirectionIn), \/\/IsForTransportDirectionIn\n            strToBool(q.IsForTransportDirectionOut), \/\/IsForTransportDirectionOut\n            strToBool(q.Bulk),\/\/ IsForCargoTypeBulk\n            strToBool(q.Zakgoed),\/\/ IsForCargoTypeBagged\n            strToBool(q.Emballage),\/\/ IsForCargoTypePackaging\n            strToBool(q.Bigbags), \/\/ IsForBigBags\n            strToBool(q.Container),\/\/ IsForTransporterTypeContainer\n            strToBool(q.Auto),\/\/ IsForTransporterTypeAuto\n        ];\n\n        const sqlColumns = columns.map(c => `[${c}]`).join(\", \");\n        const sqlValues = values.map(c => typeof c == \"number\" ? c : `'${c}'`).join(\", \");\n        return [\n            \"INSERT INTO [dbo].[Question]\",\n            `\\t(${sqlColumns})`,\n            \"\\tVALUES\",\n            `\\t(${sqlValues})`,\n        ].join(\"\\r\\n\");\n    });\n\n    console.log(sql.join(\"\\r\\n\"));\n})();<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[5,4,1],"tags":[],"class_list":["post-4205","post","type-post","status-publish","format-standard","hentry","category-javascript","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4205"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4205\/revisions"}],"predecessor-version":[{"id":4206,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4205\/revisions\/4206"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}