3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

表題のとおりの小品です。

ソース

<!DOCTYPE html>
<html lang="ja">
<head>
</head>
<body>
<div class="container">
  <input type="button" value="Format" onclick="format()" />
  <textarea rows="50" cols="100" id="sql_text"></textarea>
</div>

<script type="text/javascript">

function formatSql(text){
  // 改行するキーワード
  var array=["AND","FROM","FULL","GROUP","INNER","LEFT","ON","ORDER","RIGHT","SET","WHERE","VALUES"];

  // キーワード共通処理
  for(i=0; i<array.length; i++){
    var keyword = array[i];
    var after = " " + keyword + " ";

    // キーワード前後の半角スペースを1つだけにする
    // 例: キーワード"   AND  "は" AND "になる
    text = text.replace(new RegExp(" +" + keyword + " +", "gi"), after);

    // 改行
    var afterRn = "\r\n" + after;
    text = text.replace(new RegExp(after, "gi"), afterRn);
  }

  // SELECT:後ろ改行
  text = text.replace(/SELECT/gi, "SELECT\r\n");

  // カンマ:前後のスペース削除
  text = text.replace(/( +,|, +| +, +)/gi, ",");
  // カンマ:改行(前カンマ)
  text = text.replace(/,/g, "\r\n , ");

  // かっこ:改行
  text = text.replace(/\(/g, "(\r\n");
  text = text.replace(/\)/g, "\r\n)\r\n");

  return text;
}

function format() {
  const sql = document.getElementById("sql_text").value;
  document.getElementById("sql_text").value = formatSql(sql);
}

</script>
</body>
</html>

sql.gif

参考

以下を少々改善したもの。

また使いながら改善するかも。
以上です~

3
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?