{"id":1010,"date":"2023-11-09T21:51:42","date_gmt":"2023-11-09T12:51:42","guid":{"rendered":"https:\/\/shinke1987.net\/?p=1010"},"modified":"2023-11-09T21:51:42","modified_gmt":"2023-11-09T12:51:42","slug":"faker%e3%82%92%e5%88%a9%e7%94%a8%e3%81%97%e3%81%a6%e3%83%90%e3%83%ab%e3%82%afinsert%e6%96%87%e3%82%92%e7%94%9f%e6%88%90","status":"publish","type":"post","link":"https:\/\/shinke1987.net\/?p=1010","title":{"rendered":"faker\u3092\u5229\u7528\u3057\u3066\u30d0\u30eb\u30afINSERT\u6587\u3092\u751f\u6210"},"content":{"rendered":"\n<h2 id=\"toc0\" class=\"wp-block-heading\">\u6982\u8981<\/h2>\n\n\n\n<p>faker\u3092\u5229\u7528\u3057\u3066\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u30d0\u30eb\u30afINSERT\u6587\u3092\u751f\u6210\u3057\u3066\u307f\u305f\u306e\u3067\u3001\u30ed\u30b0\u3068\u3057\u3066\u6b8b\u3059\u3002<br>\u5b9f\u969b\u306bDB\u3078\u633f\u5165\u3067\u304d\u305f\u306e\u306f\u78ba\u8a8d\u3057\u305f\u3002<br>\u3053\u308c\u3092\u53c2\u8003\u306bfaker\u306e\u30e1\u30bd\u30c3\u30c9\u78ba\u8a8d\u3059\u308c\u3070\u8272\u3005\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u633f\u5165\u7528\u306eINSERT\u6587\u751f\u6210\u3067\u304d\u308b\u306f\u305a\u3002<\/p>\n\n\n\n<h2 id=\"toc1\" class=\"wp-block-heading\">\u30b3\u30fc\u30c9<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: php; title: ; notranslate\" title=\"\">\n&lt;?php\n\nrequire_once &#039;vendor\/autoload.php&#039;;\n\n\/**\n * \u6307\u5b9a\u3057\u305f\u30c6\u30fc\u30d6\u30eb\u540d\u306b\u5bfe\u3059\u308b\u3001\n * \u6307\u5b9a\u3057\u305f\u5217\u540d\u3068\u5024\u306e\n * \u5b9f\u884c\u53ef\u80fd\u306a\u30d0\u30eb\u30afINSERT\u6587\u3092\u8fd4\u3059\u3002\n *\n * \u914d\u5217\u306f\u6b21\u306e\u5f62\u3092\u3068\u308b\u3002\n * \uff08flagString\u304ctrue\u306e\u5834\u5408\u3001INSERT\u6587\u3067\u306e\u5024\u306f\u30b7\u30f3\u30b0\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u308f\u308c\u308b\u3002\n *   false\u306a\u3089\u30b7\u30f3\u30b0\u30eb\u30af\u30a9\u30fc\u30c6\u30fc\u30b7\u30e7\u30f3\u3067\u56f2\u308f\u308c\u306a\u3044\u3002\uff09\n * $values = &#x5B;\n *  &#039;\u5217\u540d1&#039; =&gt; &#x5B;\n *      &#039;flagString&#039; =&gt; true,\n *      &#039;values&#039; =&gt; &#x5B;\n *          &#039;\u50241&#039;, &#039;\u50242&#039;,\n *      ],\n *  ],\n *   &#039;\u5217\u540d2&#039; =&gt; &#x5B;\n *       &#039;flagString&#039; =&gt; false,\n *       &#039;values&#039; =&gt; &#x5B;\n *           &#039;\u50241&#039;, &#039;\u50242&#039;,\n *       ],\n *   ],\n * ];\n *\n * @param string $tableName \u30c6\u30fc\u30d6\u30eb\u540d\n * @param array $values \u5217\u540d\u3068\u5024\u306e\u9023\u60f3\u914d\u5217\n * @return string|null \u5b9f\u884c\u53ef\u80fd\u306aINSERT\u6587 \u307e\u305f\u306f \u30a8\u30e9\u30fc\u6642\u306b\u306fnull\n *\/\nfunction getInsertQuery(string $tableName, array $values): string|null {\n    \/**********************************************\n     * \u5f15\u6570\u306e\u78ba\u8a8d\n     **********************************************\/\n    \/\/ \u30c6\u30fc\u30d6\u30eb\u540d\u304c\u7a7a\u3067\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3002\n    if ($tableName === &#039;&#039;) return null;\n\n    \/\/ \u7a7a\u914d\u5217\u3067\u306a\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3002\n    if (count($values) === 0) return null;\n\n    \/\/ \u5024\u306e\u6570\u304c\u5168\u3066\u306e\u5217\u540d\u3067\u7b49\u3057\u3044\u3053\u3068\u3092\u78ba\u8a8d\u3002\n    $valueCount = count(array_values($values)&#x5B;0]&#x5B;&#039;values&#039;]);\n    foreach($values as $key =&gt; $arrays) {\n        if($valueCount !== count($arrays&#x5B;&#039;values&#039;])) return null;\n    }\n\n    \/**********************************************\n     * INSERT\u6587\u306e\u5217\u540d\u307e\u3067\u751f\u6210\u3002\n     **********************************************\/\n    $strInsert = &#039;INSERT INTO &#039; . $tableName . &#039; &#039;;\n\n    # INSERT\u6587\u306e\u5217\u540d\u542b\u3080\u7b87\u6240\u307e\u3067\u4f5c\u6210\u3002\n    $keys = array_keys($values);\n    $strColumns = &#039;(&#039;;\n    foreach($keys as $key) {\n        $strColumns .= ($key . &#039;, &#039;);\n    }\n    $strColumns = mb_substr($strColumns, 0, mb_strlen($strColumns) - 2);\n    $strColumns .= &#039;) &#039;;\n    $strInsert .= $strColumns;\n    unset($strColumns);\n\n    \/**********************************************\n     * INSERT\u6587\u306e\u5024\u3092\u751f\u6210\u3002\n     **********************************************\/\n    $strValues = &#039;VALUES &#039;;\n    $strValuesElement = &#039;&#039;;\n    for ($i = 0; $i &lt; $valueCount; $i++) {\n        if ($i === 0) {\n            $strValuesElement .= &#039;(&#039;;\n        } else {\n            $strValuesElement .= &#039;, (&#039;;\n        }\n\n        foreach ($values as $key =&gt; $arrays) {\n            if ($arrays&#x5B;&#039;flagString&#039;]) {\n                $strValuesElement .= &quot;&#039;&quot; . $arrays&#x5B;&#039;values&#039;]&#x5B;$i] . &quot;&#039;&quot;;\n                $strValuesElement .= &#039;, &#039;;\n            } else {\n                $strValuesElement .= $arrays&#x5B;&#039;values&#039;]&#x5B;$i];\n                $strValuesElement .= &#039;, &#039;;\n            }\n        }\n        unset($key);\n        unset($arrays);\n        $strValuesElement = mb_substr($strValuesElement, 0, mb_strlen($strValuesElement) - 2);\n        $strValuesElement .= &#039;)&#039;;\n        $strValues .= $strValuesElement;\n        $strValuesElement = &#039;&#039;;\n    }\n    $strValues .= &#039;;&#039;;\n\n    return $strInsert . $strValues;\n}\n\n$faker = Faker\\Factory::create(&#039;ja_JP&#039;);\n\n$temp = &#x5B;\n    &#039;family_name&#039; =&gt; &#x5B;\n        &#039;flagString&#039; =&gt; true,\n        &#039;values&#039; =&gt; &#x5B;],\n    ],\n    &#039;personal_name&#039; =&gt; &#x5B;\n\/\/        &#039;flagString&#039; =&gt; false,\n        &#039;flagString&#039; =&gt; true,\n        &#039;values&#039; =&gt; &#x5B;],\n    ],\n];\n\nfor ($i = 0; $i &lt; 1000; $i++) {\n    $temp&#x5B;&#039;family_name&#039;]&#x5B;&#039;values&#039;]&#x5B;] = $faker-&gt;lastName();\n    $temp&#x5B;&#039;personal_name&#039;]&#x5B;&#039;values&#039;]&#x5B;] = $faker-&gt;firstName();\n}\n\n$result = getInsertQuery(&#039;test&#039;, $temp);\nprintf(&quot;%s&quot;, $result);\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u6982\u8981 faker\u3092\u5229\u7528\u3057\u3066\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u306e\u30d0\u30eb\u30afINSERT\u6587\u3092\u751f\u6210\u3057\u3066\u307f\u305f\u306e\u3067\u3001\u30ed\u30b0\u3068\u3057\u3066\u6b8b\u3059\u3002\u5b9f\u969b\u306bDB\u3078\u633f\u5165\u3067\u304d\u305f\u306e\u306f\u78ba\u8a8d\u3057\u305f\u3002\u3053\u308c\u3092\u53c2\u8003\u306bfaker\u306e\u30e1\u30bd\u30c3\u30c9\u78ba\u8a8d\u3059\u308c\u3070\u8272\u3005\u306a\u30c6\u30b9\u30c8\u30c7\u30fc\u30bf\u633f\u5165\u7528\u306eINSERT\u6587\u751f\u6210\u3067 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[80],"tags":[81],"class_list":["post-1010","post","type-post","status-publish","format-standard","hentry","category-php","tag-php"],"_links":{"self":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1010","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1010"}],"version-history":[{"count":5,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1010\/revisions"}],"predecessor-version":[{"id":1015,"href":"https:\/\/shinke1987.net\/index.php?rest_route=\/wp\/v2\/posts\/1010\/revisions\/1015"}],"wp:attachment":[{"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1010"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1010"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shinke1987.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}