From: Saitou Noboru Date: 2000-04-14T00:44:49+09:00 Subject: [ruby-list:21887] psql.rb の修正 ----Next_Part(Fri_Apr_14_00:44:37_2000_41)-- Content-Type: Text/Plain; charset=iso-2022-jp Content-Transfer-Encoding: 7bit 斉藤 登と申します。 PostgreSQL用拡張ライブラリの psql.rb をみて学んでたんですが、挙動が おかしい所があったので修正しました。 修正点は、 ・sql文にエラーがあった場合、その後の sql を実行してくれなくなるの を修正。 ・ファイルからの入力がうまくいかなかったのを修正。起動オプションで は -f 。コマンドでは \i。 ・\E がうまく動かなかったのを修正。 ・細かい点を少し修正。 以上です。 ---- 斉藤 登 Saitou Noboru http://webclub.kcom.ne.jp/mb/noborus/ ---- ----Next_Part(Fri_Apr_14_00:44:37_2000_41)-- Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="psql.diff" --- psql.rb.orig Tue Jul 13 13:13:33 1999 +++ psql.rb Thu Apr 13 21:57:47 2000 @@ -599,29 +599,27 @@ when 'E' if args[1] - lastfile = args[1] + begin + lastfile = args[1] + File.file?(lastfile) && (mt = File.mtime(lastfile)) + editFile(lastfile) + File.file?(lastfile) && (mt2 = File.mtime(lastfile)) + fd = File.new(lastfile, "r") + if mt != mt2 + MainLoop(settings, fd) + fd.close() + else + if !settings.quiet + printf(STDERR, "warning: %s not modified. query not executed\n", lastfile) + end + fd.close() + end + rescue + # + end else printf(STDERR, "\\r must be followed by a file name initially\n"); - break - end - - st = File.stat(lastfile) - editFile(lastfile) - if ((st2 = File.stat(lastfile) == nil) || - (fd = File.new(lastfile, "r") == nil)) - break - end - - if st2.mtime == st.mtime - if !settings.quiet - printf(STDERR, "warning: %s not modified. query not executed\n", lastfile) - end - fd.close() - break end - MainLoop(settings, fd) - fd.close() - when 'f' if args[1] settings.opt.fieldSep = args[1] @@ -645,17 +643,17 @@ do_help(args[1]) when 'i' # \i is include file - if !args[1] + if args[1] + begin + fd = File.open(args[1], "r") + MainLoop(settings, fd) + fd.close() + rescue Errno::ENOENT + printf(STDERR, "file named %s could not be opened\n", args[1]) + end + else printf(STDERR, "\\i must be followed by a file name\n") - break - end - if fd = File.new(args[1], "r") == nil - printf(STDERR, "file named %s could not be opened\n", args[1]) - break end - MainLoop(settings, fd) - fd.close() - when 'l' # \l is list database listAllDbs(settings) @@ -750,7 +748,7 @@ if settings.singleStep printf("\n**************************************"); - ptintf("*****************************************\n") + printf("*****************************************\n") STDOUT.flush printf("\npress return to continue ..\n"); gets("", STDIN); @@ -771,8 +769,8 @@ settings.queryFout = STDOUT end else - results.print(settings.queryFout, settings.opt) - settings.queryFout.flush + results.print(settings.queryFout, settings.opt) + settings.queryFout.flush end results.clear @@ -885,13 +883,12 @@ if line.length == 0 next end - p line ### begin slash command handling ### if line[0, 1] == "\\" query = line slashCmdStatus, query = HandleSlashCmds(settings, line, nil) if slashCmdStatus == 0 && query != nil - success = success && SendQuery(settings, query, FALSE, FALSE, 0) + success = SendQuery(settings, query, FALSE, FALSE, 0) && success querySent = TRUE elsif slashCmdStatus == 1 query = nil @@ -905,7 +902,7 @@ ### begin query command handling ### slashCmdStatus = -1 if settings.singleLineMode - success = success && SendQuery(settings, line, FALSE, FALSE, 0) + success = SendQuery(settings, line, FALSE, FALSE, 0) && success querySent = TRUE else @@ -926,7 +923,7 @@ query = queryWaiting queryWaiting = nil - success = success && SendQuery(settings, query, FALSE, FALSE, 0) + success = SendQuery(settings, query, FALSE, FALSE, 0) && success querySent = TRUE else querySent = FALSE @@ -1080,7 +1077,7 @@ if singleSlashCmd line = singleQuery else - sprintf(line, "\\i %s", qfilename) + line = sprintf("\\i %s", qfilename) end HandleSlashCmds(settings, line, "") else ----Next_Part(Fri_Apr_14_00:44:37_2000_41)----