LoginSignup
3
3

More than 5 years have passed since last update.

Sequel ProでSQLをフォーマットするのにsqlformat.orgのAPIを利用する

Last updated at Posted at 2014-09-29

いつの間にかデフォルトで入ってた「Format SQL」が使えなくなったので。

OS X上でPHPを使ってリクエストを投げます。

(Sequel Proのバンドルエディタの使い方は省略)

#!/usr/bin/php
<?php
$sql = file_get_contents('php://stdin');

$url = 'http://sqlformat.org/api/v1/format';

$query = [
    'sql' => $sql,
    'reindent' => 1,
    'keyword_case' => 'upper'
];

$queryStr = http_build_query($query);

$headers = [
    "Content-Type: application/x-www-form-urlencoded",
    "Content-Length: " . strlen($queryStr)
];

$options = [
    'http' => [
        'method' => 'POST',
        'header' => implode("\r\n", $headers),
        'content' => $queryStr
    ]
];

$context = stream_context_create($options);

$contents = file_get_contents($url, false, $context);

$results = json_decode($contents);

echo $results->result;
3
3
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
3