doobie

How to execute list of string SQL statements against a PostgreSQL db in Scala using doobie?

回眸只為那壹抹淺笑 提交于 2020-06-11 03:14:32
问题 I am porting the following 10 lines of Python code to Scala: import psycopg2 def execute(user, password, database, host, port, *queries): connection = psycopg2.connect(user=user, password=password, host=host, port=port, database=database) cursor = connection.cursor() for sql in queries: print(sql) cursor.execute(sql) connection.commit() cursor.close() connection.close() I have the following equivalent Scala code: def execute(user: String, password: String, database: String, host: String, port

How to execute list of string SQL statements against a PostgreSQL db in Scala using doobie?

a 夏天 提交于 2020-06-11 03:13:06
问题 I am porting the following 10 lines of Python code to Scala: import psycopg2 def execute(user, password, database, host, port, *queries): connection = psycopg2.connect(user=user, password=password, host=host, port=port, database=database) cursor = connection.cursor() for sql in queries: print(sql) cursor.execute(sql) connection.commit() cursor.close() connection.close() I have the following equivalent Scala code: def execute(user: String, password: String, database: String, host: String, port

How to get optional result in insert statements with Doobie?

元气小坏坏 提交于 2019-12-10 15:35:37
问题 I have an optional insert query: val q = sql"insert into some_table (some_field) select 42 where ...(some condition)" Running this query with: q.update.withUniqueGeneratedKeys[Option[Long]]("id") fails with Result set exhausted: more rows expected then condition is false. How to get Optional[Long] result from insert statements with Doobie? UPD .withGeneratedKeys[Long]("id") gives just Long in for comprehension val q = sql"insert into some_table (some_field) select 42 where ...(some condition)

Doobie and DB access composition within 1 transaction

怎甘沉沦 提交于 2019-12-03 08:26:42
问题 Doobie book says that it's a good practice to return ConnectionIO from your repository layer. It gives an ability to chain calls and perform them in one transaction. Nice and clear. Now let's imagine we are working on REST API service and our scenario is: Find an object in database Perform some async manipulation (using cats.effect.IO or monix.eval.Task) with this object. Store the object in database. And we want to perform all these steps inside 1 transaction. The problem is that without

Doobie and DB access composition within 1 transaction

耗尽温柔 提交于 2019-12-02 23:51:16
Doobie book says that it's a good practice to return ConnectionIO from your repository layer. It gives an ability to chain calls and perform them in one transaction. Nice and clear. Now let's imagine we are working on REST API service and our scenario is: Find an object in database Perform some async manipulation (using cats.effect.IO or monix.eval.Task) with this object. Store the object in database. And we want to perform all these steps inside 1 transaction. The problem is that without natural transformation which is given for us by transactor.trans() we are working inside 2 monads - Task