Kouhei Sutou
null+****@clear*****
Sun May 17 18:18:24 JST 2015
Kouhei Sutou 2015-05-17 18:18:24 +0900 (Sun, 17 May 2015) New Revision: d4347d52e2ecf517801c624c95db223a471c050a https://github.com/droonga/wikipedia-search/commit/d4347d52e2ecf517801c624c95db223a471c050a Message: Use one INSERT for one page Modified files: lib/wikipedia-search/sql-converter.rb Modified: lib/wikipedia-search/sql-converter.rb (+38 -15) =================================================================== --- lib/wikipedia-search/sql-converter.rb 2015-05-17 18:04:45 +0900 (59ff70b) +++ lib/wikipedia-search/sql-converter.rb 2015-05-17 18:18:24 +0900 (d3e59fb) @@ -4,11 +4,48 @@ module WikipediaSearch class SQLConverter < Converter private def create_listener(output) - SQLListener.new(output, @options) + SQLOneShotListener.new(output, @options) + # SQLBulkListener.new(output, @options) end class SQLListener < Listener private + def escape_string(string) + escaped_content = string.gsub(/['\\\n]/) do |special_character| + case special_character + when "\n" + "\\n" + when "'" + "''" + else + "\\#{special_character}" + end + end + "'#{escaped_content}'" + end + end + + class SQLOneShotListener < SQLListener + private + def on_start + end + + def on_finish + end + + def on_page(page) + record_values = [ + @page.id, + escape_string(@page.title), + escape_string(shorten_text(@page.text)), + ] + @output.print("INSERT INTO wikipedia (id, title, text) VALUES ") + @output.print("(#{record_values.join(', ')});") + end + end + + class SQLBulkListener < SQLListener + private def on_start @output.puts("INSERT INTO wikipedia (id, title, text) VALUES") end @@ -27,20 +64,6 @@ module WikipediaSearch ] @output.print("(#{record_values.join(', ')})") end - - def escape_string(string) - escaped_content = string.gsub(/['\\\n]/) do |special_character| - case special_character - when "\n" - "\\n" - when "'" - "''" - else - "\\#{special_character}" - end - end - "'#{escaped_content}'" - end end end end -------------- next part -------------- HTML����������������������������...下载