1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.migrations.db
21 
22 import java.sql.Connection
23 import scala.collection.mutable.ArrayBuffer
24 import SqlTools.ImprovedResultSet
25 
26 @SuppressWarnings(Array("org.wartremover.warts.IterableOps"))
27 class V135_1__Add_language_to_crm_tpl extends Migration {
28 
29   private def getQuestionIds(implicit connection: Connection): ArrayBuffer[String] =
30     connection
31       .prepareStatement(s"SELECT question_id FROM crm_question_template")
32       .executeQuery
33       .toListOfRows(List("question_id"))
34       .map(_.head)
35 
36   private def getQuestionsLang(questionId: String)(implicit connection: Connection): String =
37     connection
38       .prepareStatement(s"SELECT default_language FROM question WHERE question_id = '$questionId'")
39       .executeQuery
40       .toListOfRows(List("default_language"))
41       .head
42       .head
43 
44   private def genUpdateLangQuery(questionId: String, lang: String)(implicit connection: Connection): Unit =
45     connection
46       .prepareStatement(s"UPDATE crm_question_template SET language = '$lang' WHERE question_id = '$questionId'")
47       .execute
48 
49   override def migrate(connection: Connection): Unit = {
50     implicit val C: Connection = connection
51     val questionIds = getQuestionIds
52     val addLanguageToTemplate: (String, String) => Unit = genUpdateLangQuery
53     questionIds
54       .zip(questionIds.map(getQuestionsLang))
55       .foreach(addLanguageToTemplate.tupled)
56   }
57 }
Line Stmt Id Pos Tree Symbol Tests Code
32 21458 1097 - 1201 Apply java.sql.PreparedStatement.executeQuery org.make.api.databasetest connection.prepareStatement(("SELECT question_id FROM crm_question_template": String)).executeQuery()
33 20537 1222 - 1241 Apply scala.collection.IterableFactory.apply org.make.api.databasetest scala.`package`.List.apply[String]("question_id")
34 19974 1254 - 1260 Select scala.collection.IterableOps.head x$1.head
34 21602 1097 - 1261 Apply scala.collection.StrictOptimizedIterableOps.map org.make.api.databasetest SqlTools.ImprovedResultSet(connection.prepareStatement(("SELECT question_id FROM crm_question_template": String)).executeQuery()).toListOfRows(scala.`package`.List.apply[String]("question_id")).map[String](((x$1: List[String]) => x$1.head))
39 20619 1361 - 1491 Apply java.sql.PreparedStatement.executeQuery connection.prepareStatement(("SELECT default_language FROM question WHERE question_id = \'".+(questionId).+("\'"): String)).executeQuery()
40 19739 1512 - 1536 Apply scala.collection.IterableFactory.apply scala.`package`.List.apply[String]("default_language")
42 21258 1361 - 1561 Select scala.collection.IterableOps.head SqlTools.ImprovedResultSet(connection.prepareStatement(("SELECT default_language FROM question WHERE question_id = \'".+(questionId).+("\'"): String)).executeQuery()).toListOfRows(scala.`package`.List.apply[String]("default_language")).head.head
47 19938 1807 - 1807 Literal <nosymbol> ()
47 20787 1675 - 1814 Apply java.sql.PreparedStatement.execute connection.prepareStatement(("UPDATE crm_question_template SET language = \'".+(lang).+("\' WHERE question_id = \'").+(questionId).+("\'"): String)).execute()
51 21419 1939 - 1953 ApplyToImplicitArgs org.make.api.migrations.db.V135_1__Add_language_to_crm_tpl.getQuestionIds org.make.api.databasetest V135_1__Add_language_to_crm_tpl.this.getQuestionIds(C)
52 20459 2012 - 2030 ApplyToImplicitArgs org.make.api.migrations.db.V135_1__Add_language_to_crm_tpl.genUpdateLangQuery V135_1__Add_language_to_crm_tpl.this.genUpdateLangQuery(questionId, lang)(C)
54 19955 2074 - 2090 ApplyToImplicitArgs org.make.api.migrations.db.V135_1__Add_language_to_crm_tpl.getQuestionsLang V135_1__Add_language_to_crm_tpl.this.getQuestionsLang(questionId)(C)
54 21534 2058 - 2091 Apply scala.collection.StrictOptimizedIterableOps.map org.make.api.databasetest questionIds.map[String](((questionId: String) => V135_1__Add_language_to_crm_tpl.this.getQuestionsLang(questionId)(C)))
55 19657 2035 - 2137 Apply scala.collection.IterableOnceOps.foreach org.make.api.databasetest questionIds.zip[String](questionIds.map[String](((questionId: String) => V135_1__Add_language_to_crm_tpl.this.getQuestionsLang(questionId)(C)))).foreach[Unit](addLanguageToTemplate.tupled)
55 20626 2108 - 2136 Select scala.Function2.tupled org.make.api.databasetest addLanguageToTemplate.tupled