Last active
December 16, 2015 07:18
-
-
Save sonota88/5397309 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// JScript | |
function toWinPath(path){ | |
return path.replace(/\//g, "\\"); | |
} | |
var MYSQLADMIN = toWinPath("C:/path/to/mysql/bin/") + "mysqladmin -uXXXX -pXXXX"; | |
function print(value){ | |
WScript.StdOut.Write("" + value); | |
} | |
function puts(value){ | |
print("" + value + "\n"); | |
} | |
function copyFile(srcFile, destFile){ | |
var fso = new ActiveXObject( "Scripting.FileSystemObject" ); | |
fso.CopyFile(srcFile, destFile); | |
} | |
function deleteFile(path){ | |
var fso = new ActiveXObject( "Scripting.FileSystemObject" ); | |
fso.DeleteFile(path, true); | |
} | |
function getTimestamp(){ | |
function fmt(_n){ | |
var n = "" + _n; | |
return (n.length === 1) ? "0" + n : n; | |
} | |
var d = new Date(); | |
var time = ""; | |
time += d.getFullYear(); | |
time += ""; time += fmt(d.getMonth() + 1); | |
time += ""; time += fmt(d.getDate()); | |
time += "_"; time += fmt(d.getHours()); | |
time += ""; time += fmt(d.getMinutes()); | |
time += ""; time += fmt(d.getSeconds()); | |
return time; | |
} | |
function syncExec(cmd){ | |
var shell = new ActiveXObject("WScript.Shell"); | |
shell.Run(cmd, 7, true); | |
} | |
//////////////////////////////// | |
var isQuery = false; | |
function convertLine(line){ | |
var cmd, _cmd, rest, result; | |
if( line.match(/^(\t\t\s*(\d+ (.+?)))\t(.*)$/) ){ | |
_cmd = RegExp.$2; | |
cmd = RegExp.$3; | |
rest = RegExp.$4; | |
isQuery = (cmd === "Query"); | |
result = "\n-- -------- "+ _cmd +" --------\n\n"; | |
if(!isQuery){ result += "-- "; } | |
result += rest; | |
}else if( line.match(/^((.+?)\t\s*(\d+) (.+?))\t(.*)$/) ){ | |
_cmd = RegExp.$1; | |
cmd = RegExp.$4; | |
rest = RegExp.$5; | |
isQuery = (cmd === "Query"); | |
result = "\n-- -------- "+ _cmd +" --------\n\n"; | |
if(!isQuery){ result += "-- "; } | |
result += rest; | |
}else{ | |
if(isQuery){ | |
result = line; | |
}else{ | |
result = "-- " + line; | |
} | |
} | |
return result + "\n"; | |
} | |
function convertLines(srcFile, destFile){ | |
try{ | |
// reader | |
var sin = new ActiveXObject("ADODB.Stream"); | |
sin.type = 2; // text | |
sin.charset = "UTF-8"; | |
sin.lineSeparator = 10; // LF | |
sin.open(); | |
// writer | |
var sout = new ActiveXObject("ADODB.Stream"); | |
sout.type = 2; // text | |
sout.charset = "UTF-8"; | |
sout.open(); | |
sin.loadFromFile(srcFile); | |
var line; | |
while(!sin.EOS){ | |
line = sin.readText(-2); // 行ごと | |
sout.writeText(convertLine(line) , 0); | |
} | |
sout.saveToFile(destFile, 2); // 上書き | |
} finally { | |
sin.close(); | |
sout.close(); | |
} | |
} | |
function getEscapeFilePath(logPath){ | |
logPath.match(/^(.+)(\..+)$/); | |
var basename = RegExp.$1; | |
// var ext = RegExp.$2; | |
var ext = ".sql"; | |
return basename + "_" + getTimestamp() + ext; | |
} | |
//////////////////////////////// | |
function main(){ | |
syncExec(MYSQLADMIN + " flush-logs"); | |
var srcPath = toWinPath(WScript.arguments(0)); | |
puts("src path =" + srcPath); | |
var destPath = getEscapeFilePath(srcPath); | |
puts("dest path=" + destPath); | |
// ログファイルそのものが読み込めないため一時ファイルにコピーして使う | |
var tempPath = srcPath + "_TEMP"; | |
copyFile(srcPath, tempPath); | |
try{ | |
convertLines(tempPath , destPath); | |
deleteFile(srcPath); | |
} finally { | |
deleteFile(tempPath); | |
} | |
syncExec(MYSQLADMIN + " refresh"); | |
// puts("done"); | |
// WScript.Sleep(3000); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment