Alex Spektr
Alex Spektr

Reputation: 63

How to generate SQL statements dynamically using Doobie (scala)

I have the query:

val sql = """select
                   id,
                   clientName
             from
                  partnerClients
             where
                  partnerName = ?
          """

I read partnerName from excel file and for each I perform the func:

case class Partner(name: String)
case class Client(id: Int, name: String)

def queryPartnerClients(partnerName: String) = Query[String, Client](sql, None).toQuery0(partnerName)

def getPartnerClients(partner: Partner): IO[Vector[Client]] =  partnerClients(partner.name)                                             
                                                              .to[Vector]
                                                              .transact(xa)

I used this FAQ (How do I turn an arbitrary SQL string into a Query/Query0)

The problem is getting empty results when I take partner name from excel, but it works if I specify the same partner name in the code like this for example:

def partnerClients(partnerName: String) = {
    val temp = "Partner Name"
    Query[String, Client](sql, None).toQuery0(temp)
}

I thought it was the problem with an encoding and I tried to fix it

def partnerClients(partnerName: String) = {
    val temp = new String(partner.getBytes("Windows-1251"), "UTF-8")
    Query[String, Client](sql, None).toQuery0(temp)
}

But the result is the same - the empty set.

Upvotes: 3

Views: 1014

Answers (1)

Alex Spektr
Alex Spektr

Reputation: 63

I use this answer to turn on logging and found out that parameters sent by me contain whitespaces. Trim function fixed it

Upvotes: 1

Related Questions