AlexGruPerm / ora_ch

Data loader from oracle to clickhouse and back.
1 stars 0 forks source link

Use $ or # before parameter names in query #12

Open AlexGruPerm opened 9 months ago

AlexGruPerm commented 9 months ago

Example of SQL (calc) query

Name of parameters date_cache_1 datecalc_cache_1 curr_date_context analyt_datecalc

can be equal to field name or table name, etc.

It's better use parameter name with $, like $date_cache_1 $datecalc_cache_1 $curr_date_context $analyt_datecalc

Original query

               join (select * 
                       from msk_analytics_caches.cache_for_calc_12904_11487
                      where date_cache     = date_cache_1 and 
                            datecalc_cache = datecalc_cache_1
                    ) t3 on t3.id_contract = tt.contract_id
               join (select * 
                      from msk_arm_v2.eaist_v_contract t3_  
                     where 
                       t3_.date_start < parseDateTime(curr_date_context,'%Y-%m-%d') and 
                       t3_.date_end >= parseDateTime(curr_date_context,'%Y-%m-%d')  
                    ) t3_ on t3_.id = t3.id_contract
           where (
                   t3.type_info  <> 5 or 
                   (t3.type_info = 5 and 
                    t3.id_contract in (
                                       select a.id_contract
                                         from msk_arm_v2.v_ref_financeplan_data_depfi_v(datec=analyt_datecalc) a 
                                       where a.data_bo < parseDateTime(curr_date_context,'%Y-%m-%d') and 
                                             a.year_bo = year_bo_param and 
                                             a.payment_bo > 0 and 
                                             a.id_pbo_type = 1
                                      )
AlexGruPerm commented 9 months ago

Need change query: Option[String] in ViewQueryMeta to class like this

  case class optSqlQuery(query: Option[String], prefix: String = "$"){
    def replaceParam(what: String, to: String): optSqlQuery =
      optSqlQuery(query.map(s => s.replace(s"$prefix$what", s"$to")))
  }

Than we can call replaceParam in insertFromQuery liek this

       params.toList.sortBy(_.ord).foldLeft(strQuery) {
       case (r, c) =>
         c.chType match {
           case "Decimal(38,6)" => r.replaceParam(c.name,mapCalcParams.getOrElse(c.name, "*****"))
           case "String"        => r.replaceParam(c.name,s"'${mapCalcParams.getOrElse(c.name, "*****")}'")
           case "UInt32"        => r.replaceParam(c.name,mapCalcParams.getOrElse(c.name, "*****"))
         }
     }