Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Short answer first. Suppose say you have a table in database containing 3 columns: <code>name</code>, <code>email</code>, <code>pass</code>. But from the user, you only got <code>name</code> and <code>password</code> but not <code>email</code>. So make Options of all 3</p> <pre><code>val name:Option[String] = Some("alice") val email:Option[String] = None val pass:Option[String] = Some("password") //For db insertion have this: DB.withConnection { implicit conn =&gt; SQL("INSERT INTO USERS (name,email,pass) VALUES ({n},{e},{p})").on( 'n -&gt; name, 'e -&gt; email,'p -&gt; pass).executeInsert() } </code></pre> <p>Doing the above, as <code>email</code> is <code>None</code>, it will insert <code>null</code> in your database. So in your case, for all your 10 columns, you can define them in the <code>SQL</code> statement above and pass <code>Option</code> in <code>on()</code>. If any of them is <code>None</code>, then it will take it as <code>null</code> in database. </p> <p>Though there can be a issue if there is a constraint on a column in your schema as <code>NOT NULL</code>. In which case you can use <code>getOrElse</code> for such columns asbelow:</p> <pre><code>DB.withConnection { implicit conn =&gt; SQL("INSERT INTO USERS (name,email,pass) VALUES ({n},{e},{p})").on( 'n -&gt; name, 'e -&gt; email.getOrElse("Default Email"),'p -&gt; pass).executeInsert() </code></pre> <p>The below is a comprehend list on how play converts the type to database type. It can be found in object <code>anorm.ToStatement</code>:</p> <pre><code> case Some(bd: java.math.BigDecimal) =&gt; stmt.setBigDecimal(index, bd) case Some(o) =&gt; stmt.setObject(index, o) case None =&gt; stmt.setObject(index, null) case bd: java.math.BigDecimal =&gt; stmt.setBigDecimal(index, bd) case date: java.util.Date =&gt; stmt.setTimestamp(index, new java.sql.Timestamp(date.getTime())) case o =&gt; stmt.setObject(index, o) </code></pre> <p>Above you see, for <code>None</code> it takes it as null.</p> <hr> <p>In case of <code>SELECT</code> hmm, I am not aware of any anorm feature which helps here, but I guess simple String manipulation might suffice:</p> <pre><code>def getColumns(xs:List[Option[_]]):String = { val notNone = xs.collect{ case Some(x) =&gt; x.toString } notNone.mkString(",") } </code></pre> <p>And then <code>SQL("SELECT %s from table".format(getColumns(List(nameColumn,emailColumn,passColumn)))</code>.</p> <p>Though this is not what you want. Anorm is just a sql building library. To do what you want, it will also have to remember your table schema (i.e. atleast column names..). I do not think anorm is made to do all that</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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