Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Conceptually:</p> <pre><code>def insertAll[T](items: Seq[T]): Seq[Either[(T, Exception), (T, Int)]] = items.map { i =&gt; try { // Perform an insert supposing returns and int representing the PK on the table val pk = … Right(i, pk) } catch { case e: Exception =&gt; Left(i, e) } } </code></pre> <p>You perform each insert operation and then, based on the result, you return a Left or Right object that keep tracks of the end result and give you a detailed context to interpret the operation.</p> <p><strong>EDIT</strong></p> <p>Let's suppose that your DAO object looks like:</p> <pre><code>object Dictionary extends Table[Word]("dictionary") { // ... } </code></pre> <p>where <code>Word</code> is your object model and moreover you have provided the nuts and bolts (as I can deduce from your pasted code) it should be (where <code>words</code> is a <code>Seq[Word]</code>):</p> <pre><code>words.map { w =&gt; try { Right(w, Dictionary.autoInc.insert(w)) } catch { case e: Exception =&gt; Left(w, e) } } </code></pre> <p>What you get is a sequence of <code>Either</code> that encapsulates the outcome for further processing.</p> <p><strong>Considerations</strong> The solution provided by me attempts optimistically to perform the operation against the DB without requiring to pre-filter the list based on the state of the DB. In general pre-filtering is problematic in an heavily multiuser application provided you can't assume that nobody added a word in your pre-filtered list after you've performed the filter. State more simply: uniqueness constraint is a robust feature provided by DBMS which is better to exploit than to reinvent. The solution you edited above is a no-solution because you still need to face possibly PK violation exception.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload