binlib/sql: namespace SQL-related classes, and modules.
This commit is contained in:
parent
22a2791260
commit
f0f0483af8
8 changed files with 32 additions and 39 deletions
|
@ -11,16 +11,12 @@
|
||||||
# Dependencies
|
# Dependencies
|
||||||
require "json"
|
require "json"
|
||||||
require "erb"
|
require "erb"
|
||||||
require_relative "../../binlib/sql/language"
|
require_relative "../../binlib/sql"
|
||||||
require_relative "../../binlib/sql/chapter"
|
|
||||||
require_relative "../../binlib/sql/verse"
|
|
||||||
require_relative "../../binlib/sql/sql_utils"
|
|
||||||
require_relative "../../binlib/sql/row_mapper"
|
|
||||||
|
|
||||||
extend RowMapper
|
extend SQL::JoinRow
|
||||||
base_dir = File.join("src", "sql")
|
base_dir = File.join("src", "sql")
|
||||||
template = File.read File.join(base_dir, "seed.sql.erb")
|
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 = ERB.new(template)
|
||||||
.result_with_hash(chapter_id: 1, languages:)
|
.result_with_hash(chapter_id: 1, languages:)
|
||||||
.each_line.map(&:strip)
|
.each_line.map(&:strip)
|
||||||
|
|
9
binlib/sql.rb
Normal file
9
binlib/sql.rb
Normal file
|
@ -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
|
|
@ -1,6 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Chapter
|
class SQL::Chapter
|
||||||
def initialize(path)
|
def initialize(path)
|
||||||
@contents = JSON.parse(File.read(path))
|
@contents = JSON.parse(File.read(path))
|
||||||
@metadata = @contents.shift
|
@metadata = @contents.shift
|
||||||
|
@ -13,9 +13,9 @@ class Chapter
|
||||||
|
|
||||||
def name
|
def name
|
||||||
if @locale == "ar"
|
if @locale == "ar"
|
||||||
SQLUtils.escape(@metadata['codepoints'].pack('U*'))
|
@metadata["codepoints"].pack("U*")
|
||||||
else
|
else
|
||||||
SQLUtils.escape(@metadata['translated_name'])
|
@metadata["translated_name"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -23,19 +23,19 @@ class Chapter
|
||||||
if @locale == "ar"
|
if @locale == "ar"
|
||||||
name
|
name
|
||||||
else
|
else
|
||||||
SQLUtils.escape(@metadata['transliterated_name'])
|
@metadata["transliterated_name"]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def slug
|
def slug
|
||||||
SQLUtils.escape(@metadata['slug'])
|
@metadata["slug"]
|
||||||
end
|
end
|
||||||
|
|
||||||
def city
|
def city
|
||||||
SQLUtils.escape(@metadata['place_of_revelation'].capitalize)
|
@metadata["place_of_revelation"].capitalize
|
||||||
end
|
end
|
||||||
|
|
||||||
def verses
|
def verses
|
||||||
@contents.map { Verse.new(*_1) }
|
@contents.map { SQL::Verse.new(*_1) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,16 +1,20 @@
|
||||||
module RowMapper
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module SQL::JoinRow
|
||||||
def chapter_row(quran_id, chapter_id, chapter)
|
def chapter_row(quran_id, chapter_id, chapter)
|
||||||
[
|
[
|
||||||
chapter_id, chapter.number, chapter.name,
|
chapter_id, chapter.number, chapter.name,
|
||||||
chapter.tr_name, chapter.slug, chapter.city,
|
chapter.tr_name, chapter.slug, chapter.city,
|
||||||
quran_id
|
quran_id
|
||||||
].join(",")
|
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }
|
||||||
|
.join(",")
|
||||||
end
|
end
|
||||||
|
|
||||||
def verse_row(verse, quran_id, chapter_id)
|
def verse_row(verse, quran_id, chapter_id)
|
||||||
[
|
[
|
||||||
verse.number, quran_id,
|
verse.number, quran_id,
|
||||||
chapter_id, verse.content
|
chapter_id, verse.content
|
||||||
].join(",")
|
].map { Integer === _1 ? _1 : SQL::Utils.escape(_1) }
|
||||||
|
.join(",")
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -1,15 +1,9 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Language
|
class SQL::Language < Struct.new(:locale)
|
||||||
attr_reader :locale
|
|
||||||
|
|
||||||
def initialize(locale)
|
|
||||||
@locale = locale
|
|
||||||
end
|
|
||||||
|
|
||||||
def chapters
|
def chapters
|
||||||
Dir.glob(File.join(
|
Dir.glob(File.join(
|
||||||
"src", "json", @locale, "*.json"
|
"src", "json", locale, "*.json"
|
||||||
)).map { Chapter.new(_1) }.sort_by(&:number)
|
)).map { SQL::Chapter.new(_1) }.sort_by(&:number)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module SQLUtils
|
module SQL::Utils
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def escape(str)
|
def escape(str)
|
|
@ -1,14 +1,4 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Verse
|
class SQL::Verse < Struct.new(:number, :content)
|
||||||
attr_reader :number, :content
|
|
||||||
|
|
||||||
def initialize(number, content)
|
|
||||||
@number = number
|
|
||||||
@content = content
|
|
||||||
end
|
|
||||||
|
|
||||||
def content
|
|
||||||
SQLUtils.escape(@content)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
<% languages.each.with_index(1) do |language, quran_id| %>
|
<% 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| %>
|
<% language.chapters.each do |chapter| %>
|
||||||
INSERT INTO chapters (
|
INSERT INTO chapters (
|
||||||
id, number, name, tr_name, slug, city, quran_id
|
id, number, name, tr_name, slug, city, quran_id
|
||||||
|
|
Loading…
Reference in a new issue