title: How to insert batch and return inserted values + values from linked table?
body: <p>I have the next SQL-query in plain SQL for PostgreSQL using JDBC:</p>
  <pre><code>fun addMessages(newMessages: List&lt;NewMessage&gt;): List&lt;Message&gt; {
      val args = List(newMessages.size) { &quot;(?,?)&quot; }.joinToString(&quot;,&quot;)
      val sql = &quot;&quot;&quot;
          WITH msg_list AS (
              INSERT INTO messages(msg, created_by) VALUES $args RETURNING id, msg, created_by
          )
          SELECT m.id, m.msg, m.created_by, u.id, u.name, u.email
          FROM msg_list m
          INNER JOIN users u on u.id = m.created_by
          &quot;&quot;&quot;
      val statement = this.connection.prepareStatement(sql)
      var index = 1
      newMessages.forEach {
          statement.setString(index++, it.msg)
          statement.setInt(index++, it.createdBy)
      }
      val rs = statement.executeQuery()
      val messages = ArrayList&lt;Message&gt;(newMessages.size)
      while (rs.next()) {
          messages.add(Message(
              id = rs.getInt(1),
              msg = rs.getString(2),
              createdBy = User(
                  id = rs.getInt(3),
                  name = rs.getString(4),
                  email = rs.getString(5),
              ),
          ))
      }
      rs.close()
      statement.close()
      return messages
  }
  </code></pre>
  <p>How can I make it in <strong>Exposed DSL v0.37.3</strong>? At this moment I have code as below:</p>
  <pre><code>fun addMessages(newMessages: List&lt;NewMessage&gt;): List&lt;Message&gt; {
      return transaction {
          MessageTable.batchInsert(newMessages) {
              this[MessageTable.msg] = it.msg
              this[MessageTable.createdBy] = it.createdBy
          }.map {
              Message(
                  id = it[MessageTable.id],
                  msg = it[MessageTable.msg],
                  createdBy = User(
                      id = it[UserTable.id],
                      name = it[UserTable.name],
                      email = it[UserTable.email],
                  ),
              )
          }
      }
  }
  </code></pre>
  <p>However I don't know how to join <code>users</code> table or use <code>WITH</code> construction in <strong>Exposed DSL</strong>.</p>
  <p><strong>UPD:</strong>
  I rewrote it to working but not optimal version:</p>
  <pre><code>fun addMessages(newMessages: List&lt;NewMessage&gt;): List&lt;Message&gt; {
      return transaction {
          val msgResults = MessageTable.batchInsert(newMessages) {
              this[MessageTable.msg] = it.msg
              this[MessageTable.createdBy] = it.createdBy
          }
          val users = UserTable.select { UserTable.id inList msgResults.map { it[MessageTable.id] } }.map {
              it[UserTable.id] to User(
                  id = it[UserTable.id],
                  name = it[UserTable.name],
                  email = it[UserTable.email],
              )
          }.toMap()
          msgResults.map {
              Message(
                  id = it[MessageTable.id],
                  msg = it[MessageTable.msg],
                  createdBy = users[it[MessageTable.id]]!!,
              )
          }
      }
  }
  </code></pre>