From f0f0483af81ac2d1a1438fb3ff9ebd65652d2cc2 Mon Sep 17 00:00:00 2001 From: 0x1eef <0x1eef@protonmail.com> Date: Thu, 6 Oct 2022 17:07:05 -0300 Subject: [PATCH] binlib/sql: namespace SQL-related classes, and modules. --- bin/sql/create-sql-seed-file | 10 +++------- binlib/sql.rb | 9 +++++++++ binlib/sql/chapter.rb | 14 +++++++------- binlib/sql/{row_mapper.rb => join_row.rb} | 10 +++++++--- binlib/sql/language.rb | 12 +++--------- binlib/sql/{sql_utils.rb => utils.rb} | 2 +- binlib/sql/verse.rb | 12 +----------- src/sql/seed.sql.erb | 2 +- 8 files changed, 32 insertions(+), 39 deletions(-) create mode 100644 binlib/sql.rb rename binlib/sql/{row_mapper.rb => join_row.rb} (60%) rename binlib/sql/{sql_utils.rb => utils.rb} (88%) diff --git a/bin/sql/create-sql-seed-file b/bin/sql/create-sql-seed-file index 153bbb2..6f7dab3 100755 --- a/bin/sql/create-sql-seed-file +++ b/bin/sql/create-sql-seed-file @@ -11,16 +11,12 @@ # Dependencies require "json" require "erb" -require_relative "../../binlib/sql/language" -require_relative "../../binlib/sql/chapter" -require_relative "../../binlib/sql/verse" -require_relative "../../binlib/sql/sql_utils" -require_relative "../../binlib/sql/row_mapper" +require_relative "../../binlib/sql" -extend RowMapper +extend SQL::JoinRow base_dir = File.join("src", "sql") template = File.read File.join(base_dir, "seed.sql.erb") -languages = %w[ar en pt fa].map { Language.new(_1) } +languages = %w[ar en pt fa].map { SQL::Language.new(_1) } result = ERB.new(template) .result_with_hash(chapter_id: 1, languages:) .each_line.map(&:strip) diff --git a/binlib/sql.rb b/binlib/sql.rb new file mode 100644 index 0000000..20f545d --- /dev/null +++ b/binlib/sql.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +module SQL + require_relative "sql/language" + require_relative "sql/chapter" + require_relative "sql/verse" + require_relative "sql/utils" + require_relative "sql/join_row" +end diff --git a/binlib/sql/chapter.rb b/binlib/sql/chapter.rb index eec2853..50c7429 100644 --- a/binlib/sql/chapter.rb +++ b/binlib/sql/chapter.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -class Chapter +class SQL::Chapter def initialize(path) @contents = JSON.parse(File.read(path)) @metadata = @contents.shift @@ -13,9 +13,9 @@ class Chapter def name if @locale == "ar" - SQLUtils.escape(@metadata['codepoints'].pack('U*')) + @metadata["codepoints"].pack("U*") else - SQLUtils.escape(@metadata['translated_name']) + @metadata["translated_name"] end end @@ -23,19 +23,19 @@ class Chapter if @locale == "ar" name else - SQLUtils.escape(@metadata['transliterated_name']) + @metadata["transliterated_name"] end end def slug - SQLUtils.escape(@metadata['slug']) + @metadata["slug"] end def city - SQLUtils.escape(@metadata['place_of_revelation'].capitalize) + @metadata["place_of_revelation"].capitalize end def verses - @contents.map { Verse.new(*_1) } + @contents.map { SQL::Verse.new(*_1) } end end diff --git a/binlib/sql/row_mapper.rb b/binlib/sql/join_row.rb similarity index 60% rename from binlib/sql/row_mapper.rb rename to binlib/sql/join_row.rb index ec02b5f..c58312d 100644 --- a/binlib/sql/row_mapper.rb +++ b/binlib/sql/join_row.rb @@ -1,16 +1,20 @@ -module RowMapper +# frozen_string_literal: true + +module SQL::JoinRow def chapter_row(quran_id, chapter_id, chapter) [ chapter_id, chapter.number, chapter.name, chapter.tr_name, chapter.slug, chapter.city, quran_id - ].join(",") + ].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) } + .join(",") end def verse_row(verse, quran_id, chapter_id) [ verse.number, quran_id, chapter_id, verse.content - ].join(",") + ].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) } + .join(",") end end diff --git a/binlib/sql/language.rb b/binlib/sql/language.rb index e567904..b7453a8 100644 --- a/binlib/sql/language.rb +++ b/binlib/sql/language.rb @@ -1,15 +1,9 @@ # frozen_string_literal: true -class Language - attr_reader :locale - - def initialize(locale) - @locale = locale - end - +class SQL::Language < Struct.new(:locale) def chapters Dir.glob(File.join( - "src", "json", @locale, "*.json" - )).map { Chapter.new(_1) }.sort_by(&:number) + "src", "json", locale, "*.json" + )).map { SQL::Chapter.new(_1) }.sort_by(&:number) end end diff --git a/binlib/sql/sql_utils.rb b/binlib/sql/utils.rb similarity index 88% rename from binlib/sql/sql_utils.rb rename to binlib/sql/utils.rb index 234c05e..0f88a10 100644 --- a/binlib/sql/sql_utils.rb +++ b/binlib/sql/utils.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -module SQLUtils +module SQL::Utils module_function def escape(str) diff --git a/binlib/sql/verse.rb b/binlib/sql/verse.rb index 606e291..c33c783 100644 --- a/binlib/sql/verse.rb +++ b/binlib/sql/verse.rb @@ -1,14 +1,4 @@ # frozen_string_literal: true -class Verse - attr_reader :number, :content - - def initialize(number, content) - @number = number - @content = content - end - - def content - SQLUtils.escape(@content) - end +class SQL::Verse < Struct.new(:number, :content) end diff --git a/src/sql/seed.sql.erb b/src/sql/seed.sql.erb index 002a3a4..db4fb6e 100644 --- a/src/sql/seed.sql.erb +++ b/src/sql/seed.sql.erb @@ -1,5 +1,5 @@ <% languages.each.with_index(1) do |language, quran_id| %> - INSERT INTO qurans (locale) VALUES (<%= SQLUtils.escape(language.locale) %>); + INSERT INTO qurans (locale) VALUES (<%= SQL::Utils.escape(language.locale) %>); <% language.chapters.each do |chapter| %> INSERT INTO chapters ( id, number, name, tr_name, slug, city, quran_id