Created
March 7, 2013 12:11
-
-
Save hetima/5107613 to your computer and use it in GitHub Desktop.
Sequel Pro のプラグイン(bundles)サンプル(php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/php | |
<?php | |
/* | |
Sequel Pro のプラグイン(bundles)サンプル | |
needs PHP 5.3 or later because of str_getcsv() | |
copy field names to clipboard | |
*/ | |
$table = $_ENV['SP_SELECTED_TABLE']; | |
if(!$table){ | |
echo "<font color=red>Please select a table.</font>"; | |
exit(intval($_ENV['SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP'])); | |
} | |
//クエリを実行して結果を得る。下記参照 | |
$result=sp_query("DESCRIBE `".$table."`", TRUE, "csv"); | |
if(!$result){ | |
echo "<font color=red>can not DESCRIBE table:".$table.".</font>"; | |
exit(intval($_ENV['SP_BUNDLE_EXIT_SHOW_AS_HTML_TOOLTIP'])); | |
} | |
$ary=explode( "\n" , $result); | |
$head=0; | |
$output=""; | |
foreach ($ary as $line) { | |
if(!$head){ | |
$head=1; | |
}else{ | |
$data = str_getcsv($line); | |
if ($data[0]) { | |
$output.=$data[0]."\n"; | |
} | |
} | |
} | |
$cmd = 'echo '.escapeshellarg($output).' | pbcopy'; | |
shell_exec($cmd); | |
echo 'copied'; | |
// クエリを実行する | |
// $get_content=TRUE なら結果を返す | |
// FALSE なら成功したかどうかを返す。結果は $_ENV['SP_QUERY_RESULT_FILE'] に書き出されている | |
// タブ区切りで欲しいなら $format='tsv' | |
function sp_query($q, $get_content=FALSE, $format='csv') | |
{ | |
$query_file=$_ENV['SP_QUERY_FILE']; | |
$stat_file=$_ENV['SP_QUERY_RESULT_STATUS_FILE']; | |
$result_file=$_ENV['SP_QUERY_RESULT_FILE']; | |
file_put_contents($query_file, $q); | |
system('open "sequelpro://$SP_PROCESS_ID@passToDoc/ExecuteQuery/'.$format.'"'); | |
while(1){ | |
sleep(0.1); | |
if(file_exists($stat_file))break; | |
} | |
$stat=file_get_contents($stat_file); | |
if($stat=="0"){ | |
if($get_content)return file_get_contents($result_file); | |
else return TRUE; | |
} | |
return FALSE; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment