Hash-xaritalaridan qadriyatlarga qanday qo'shilish mumkin?

JDBC orqali ba'zi SQL jadvallarini olib bo'ldim va men yangi jadval yaratish va qiymatlarni qo'shishdan oldin yana bir transformatsiyani qilishim kerak.

Ushbu ikkita xash-xaritalarni oldim:

Bo'limlar:

(
{:chapter_uuid "b7984dde-50a1-4147-bfee-95bbd068e031", :l1_chapter_text 
120876M, :l2_chapter_text nil, :l3_chapter_text nil, :l4_chapter_text nil} 

{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text 
120880M, :l2_chapter_text 120876M, :l3_chapter_text nil, :l4_chapter_text 
121621M}
)

Tarjima:

(
{:translation_id 3258779M, :translation_text 120876M, :translation_language "cs", 
:translation_name "Vnější fasáda"} 

{:translation_id 3258780M, :translation_text 120876M, :translation_language "en", 
:translation_name "Exterior Signage"} 

{:translation_id 3258782M, :translation_text 120880M, :translation_language "cs", 
:translation_name "Čistá výloha"} 

{:translation_id 3258783M, :translation_text 121621M, :translation_language "cs", 
:translation_name "Vnější signalizace"} 

{:translation_id 3258784M, :translation_text 121621M, :translation_language "en", 
:translation_name "Pre-signalization"}
)

Buni olishim kerak:

Where Chapters.lx_chapter_text = Translation.translation_text -> swap Chapters.lx_chapter_text with translation_name and insert :language value -> Make sure that every language has its own hash-map!

(
{:chapter_uuid "b7984dde-50a1-4147-bfee-95bbd068e031", :l1_chapter_text 
"Vnější fasáda", :l2_chapter_text nil, :l3_chapter_text nil, :l4_chapter_text nil 
:language "cs"}

{:chapter_uuid "b7984dde-50a1-4147-bfee-95bbd068e031", :l1_chapter_text 
"Exterior Signage", :l2_chapter_text nil, :l3_chapter_text nil, :l4_chapter_text nil 
:language "en"}

{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text
"Čistá výloha", :l2_chapter_text "Vnější fasáda", :l3_chapter_text nil, :l4_chapter_text
"Vnější signalizace" :language "cs"}

{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text
120880M, :l2_chapter_text "Exterior Signage", :l3_chapter_text nil, :l4_chapter_text
"Pre-signalization" :language "en"}
)

Bu qanchalik uzoq bo'ldi:

, lekin siz buni to'g'ri (yomon ichki) deb bilasiz

Qanday qilib buni to'g'ri qilishimni ayta olasizmi? Rahmat!

(defn test_table []
    (for [language (distinct(map #(:translation_language %) translation))]
        (for [chapter chapters]
            (for [text translation]
            (cond
            (and (= (:l1_chapter_text chapter) (:translation_text text)) (= (:translation_language text) language))
              (assoc chapter :l1_chapter_text (:translation_name text) :language (:translation_language text))
            (and (= (:l2_chapter_text chapter) (:translation_text text)) (= (:translation_language text) language))
              (assoc chapter :l2_chapter_text (:translation_name text) :language (:translation_language text))
            (and (= (:l3_chapter_text chapter) (:translation_text text)) (= (:translation_language text) language))
              (assoc chapter :l3_chapter_text (:translation_name text) :language (:translation_language text))
            (and (= (:l4_chapter_text chapter) (:translation_text text)) (= (:translation_language text) language))
              (assoc chapter :l4_chapter_text (:translation_name text) :language (:translation_language text))))))

(test-table)

->
((({:chapter_uuid "b7984dde-50a1-4147-bfee-95bbd068e031", :l1_chapter_text "Vnější fasáda", 
:l2_chapter_text nil, :l3_chapter_text nil, :l4_chapter_text nil, :language "cs"} 
nil nil nil nil) 
({:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text 120880M, 
:l2_chapter_text "Vnější fasáda", :l3_chapter_text nil, :l4_chapter_text 121621M, :language "cs"} 
nil 
{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text "Čistá výloha", 
:l2_chapter_text 120876M, :l3_chapter_text nil, :l4_chapter_text 121621M, :language "cs"} 
{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text 120880M, 
:l2_chapter_text 120876M, :l3_chapter_text nil, :l4_chapter_text "Vnější signalizace", :language "cs"} 
nil)) 
((nil 
{:chapter_uuid "b7984dde-50a1-4147-bfee-95bbd068e031", :l1_chapter_text "Exterior Signage", 
:l2_chapter_text nil, :l3_chapter_text nil, :l4_chapter_text nil, :language "en"} 
nil nil nil) 
(nil 
{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text 120880M, 
:l2_chapter_text "Exterior Signage", :l3_chapter_text nil, :l4_chapter_text 121621M, :language "en"} 
nil nil 
{:chapter_uuid "23df4f27-534b-4cdb-81ed-dbdc8b9b140c", :l1_chapter_text 120880M, 
:l2_chapter_text 120876M, :l3_chapter_text nil, :l4_chapter_text "Pre-signalization", :language "en"})))
0
Qanday qadriyatlarga qo'shilishni xohlaysiz? nima bo'yicha? Agar sizda {: a 1} va {: a 2} bo'lsa, a nima bo'ladi?
qo'shib qo'ydi muallif Ertuğrul Çetin, manba
Nima uchun jb/sql-ga darhol qo'shilasiz?
qo'shib qo'ydi muallif cfrick, manba
Kechirasiz, sizning savolingizni tushunmayapman. Bu erda: l * _chapter_text tayinlanishi mumkin: translation_text har bir til uchun bir marta ...
qo'shib qo'ydi muallif Akiz, manba
SQLda qanday yozishni bilmayman :-). Chapter hash map allaqachon SQL hiyerarşisini kengaytirish natijasidir (asl jadval faqat UUID, BOShQA, PARENT, TEXT maydonlari bor) va siz ushbu SQL ni siz taklif qilganingiz bilan qanday qilib birlashtirishni bilmayman ...
qo'shib qo'ydi muallif Akiz, manba

5 javoblar

Birinchidan, sizda tarjimalarda ishlatiladigan barcha tillar ro'yxati mavjudmi? Aks holda, uni tarjimalari to'plamidan olishimiz mumkin:

(def langs (distinct (map :translation_language translations))) ;; => ("cs" "en")

Ikkinchidan, siz qidirish kodi sifatida Translations to'plamini ishlatmoqchi bo'lganingizda, uning xaritasini yaratish mantiqan. Buni amalga oshirishning eng oson yo'li (eng samarali bo'lmasa-da) group-by funktsiyasidan foydalanishdir:

(def translations-map
  (group-by (juxt :translation_text :translation_language) translations))

Looks like what you need to do is to convert each :l

3
qo'shib qo'ydi

Birinchidan, sizda tarjimalarda ishlatiladigan barcha tillar ro'yxati mavjudmi? Aks holda, uni tarjimalari to'plamidan olishimiz mumkin:

(def langs (distinct (map :translation_language translations))) ;; => ("cs" "en")

Ikkinchidan, siz qidirish kodi sifatida Translations to'plamini ishlatmoqchi bo'lganingizda, uning xaritasini yaratish mantiqan. Buni amalga oshirishning eng oson yo'li (eng samarali bo'lmasa-da) group-by funktsiyasidan foydalanishdir:

(def translations-map
  (group-by (juxt :translation_text :translation_language) translations))

Looks like what you need to do is to convert each :l

3
qo'shib qo'ydi
(let [a ...
      b ...]
  (as-> [:l1_chapter_text :l2_chapter_text :l3_chapter_text :l4_chapter_text] 
        relations
        (interleave relations (repeat :translation_text))
        (apply hash-map relations)
        (mapcat #(clojure.set/join a b (into {} [%])) relations)
        (group-by (fn [a] (apply str ((juxt :chapter_uuid :l1_chapter_text :l2_chapter_text :l3_chapter_text :l4_chapter_text :translation_language) a))) relations)
        (vals relations)))
0
qo'shib qo'ydi
(let [a ...
      b ...]
  (as-> [:l1_chapter_text :l2_chapter_text :l3_chapter_text :l4_chapter_text] 
        relations
        (interleave relations (repeat :translation_text))
        (apply hash-map relations)
        (mapcat #(clojure.set/join a b (into {} [%])) relations)
        (group-by (fn [a] (apply str ((juxt :chapter_uuid :l1_chapter_text :l2_chapter_text :l3_chapter_text :l4_chapter_text :translation_language) a))) relations)
        (vals relations)))
0
qo'shib qo'ydi
(let [a ...
      b ...]
  (as-> [:l1_chapter_text :l2_chapter_text :l3_chapter_text :l4_chapter_text] 
        relations
        (interleave relations (repeat :translation_text))
        (apply hash-map relations)
        (mapcat #(clojure.set/join a b (into {} [%])) relations)
        (group-by (fn [a] (apply str ((juxt :chapter_uuid :l1_chapter_text :l2_chapter_text :l3_chapter_text :l4_chapter_text :translation_language) a))) relations)
        (vals relations)))
0
qo'shib qo'ydi