<commit_before>package com.ephox.vault

import scalaz._, Scalaz._
import SqlValue._

sealed trait RowValue[A] extends NewType[Option[Either[SqlException, A]]] {
  import PossiblyNull._

  import RowValue._

  def fold[X](sqlErr: SqlException => X, sqlValue: A => X, nul: => X): X =
    value match {
      case None           => nul
      case Some(Left(e))  => sqlErr(e)
      case Some(Right(a)) => sqlValue(a)
    }

  def isNull: Boolean = fold(_ => false, _ => false, true)
  def isNotNull: Boolean = !isNull
  def isError: Boolean = fold(_ => true, _ => false, false)
  def isNotError: Boolean = !isError
  def isValue: Boolean = fold(_ => false, _ => true, false)
  def isNotValue: Boolean = !isValue

  def getError: Option[SqlException] =
    fold(Some(_), _ => None, None)

  def getErrorOr(e: => SqlException): SqlException =
    getError getOrElse e

  def getValue: Option[A] =
    fold(_ => None, Some(_), None)

  def getValueOr(v: => A): A =
    getValue getOrElse v

  def getOrDie: A =
    fold(e => throw new VaultException(e), x => x, throw new VaultException("Unexpected database null."))

  def getSqlValue: Option[SqlValue[A]] =
    fold(e => Some(sqlError(e)), a => Some(sqlValue(a)), None)

  def getSqlValueOr(v: => SqlValue[A]): SqlValue[A] =
    getSqlValue getOrElse v

  def printStackTraceOr(v: A => Unit, nul: => Unit): Unit =
    fold(_ => (), v, nul)

  def map[B](f: A => B): RowValue[B] =
    fold(rowError(_), a => rowValue(f(a)), rowNull)

  def flatMap[B](f: A => RowValue[B]): RowValue[B] =
    fold(rowError(_), f, rowNull)

  def unifyNullWithMessage(message: String): SqlValue[A] =
    fold(sqlError(_), sqlValue(_), sqlError(new SqlException(message)))

  def unifyNull: SqlValue[A] =
    unifyNullWithMessage("unify null")

  def possiblyNull: SqlValue[PossiblyNull[A]] =
    optionPossiblyNull(getSqlValue).sequence[SqlValue, A]

  def possiblyNullOr(d: => A): SqlValue[A] =
    possiblyNull map (_ | d)

  def toList: SqlValue[List[A]] =
    possiblyNull map (_.toList)

  // alias for possiblyNullOr
  def |?(d: => A) = possiblyNullOr(d)

  /**
   * Lifts this value into a possibly null value. The following holds:
   *
   * forall r | r in RowValue[A]. r.liftPossiblyNull.isNotNull
   */
  def liftPossiblyNull: RowValue[PossiblyNull[A]] =
    possiblyNull.toRowValue
}

object RowValue extends RowValues

trait RowValues {
  def rowError[A](e: SqlException): RowValue[A] = new RowValue[A] {
    val value = (Some(Left(e)): Option[Either[SqlException, A]])
  }<commit_msg>added short-hand for lifting into possibly-null<commit_after>package com.ephox.vault

import scalaz._, Scalaz._
import SqlValue._

sealed trait RowValue[A] extends NewType[Option[Either[SqlException, A]]] {
  import PossiblyNull._

  import RowValue._

  def fold[X](sqlErr: SqlException => X, sqlValue: A => X, nul: => X): X =
    value match {
      case None           => nul
      case Some(Left(e))  => sqlErr(e)
      case Some(Right(a)) => sqlValue(a)
    }

  def isNull: Boolean = fold(_ => false, _ => false, true)
  def isNotNull: Boolean = !isNull
  def isError: Boolean = fold(_ => true, _ => false, false)
  def isNotError: Boolean = !isError
  def isValue: Boolean = fold(_ => false, _ => true, false)
  def isNotValue: Boolean = !isValue

  def getError: Option[SqlException] =
    fold(Some(_), _ => None, None)

  def getErrorOr(e: => SqlException): SqlException =
    getError getOrElse e

  def getValue: Option[A] =
    fold(_ => None, Some(_), None)

  def getValueOr(v: => A): A =
    getValue getOrElse v

  def getOrDie: A =
    fold(e => throw new VaultException(e), x => x, throw new VaultException("Unexpected database null."))

  def getSqlValue: Option[SqlValue[A]] =
    fold(e => Some(sqlError(e)), a => Some(sqlValue(a)), None)

  def getSqlValueOr(v: => SqlValue[A]): SqlValue[A] =
    getSqlValue getOrElse v

  def printStackTraceOr(v: A => Unit, nul: => Unit): Unit =
    fold(_ => (), v, nul)

  def map[B](f: A => B): RowValue[B] =
    fold(rowError(_), a => rowValue(f(a)), rowNull)

  def flatMap[B](f: A => RowValue[B]): RowValue[B] =
    fold(rowError(_), f, rowNull)

  def unifyNullWithMessage(message: String): SqlValue[A] =
    fold(sqlError(_), sqlValue(_), sqlError(new SqlException(message)))

  def unifyNull: SqlValue[A] =
    unifyNullWithMessage("unify null")

  def possiblyNull: SqlValue[PossiblyNull[A]] =
    optionPossiblyNull(getSqlValue).sequence[SqlValue, A]

  def possiblyNullOr(d: => A): SqlValue[A] =
    possiblyNull map (_ | d)

  def toList: SqlValue[List[A]] =
    possiblyNull map (_.toList)

  // alias for possiblyNullOr
  def |?(d: => A) = possiblyNullOr(d)

  /**
   * Lifts this value into a possibly null value. The following holds:
   *
   * forall r. r.liftPossiblyNull.isNotNull
   */
  def liftPossiblyNull: RowValue[PossiblyNull[A]] =
    possiblyNull.toRowValue
}

object RowValue extends RowValues

trait RowValues {
  def rowError[A](e: SqlException): RowValue[A] = new RowValue[A] {
    val value = (Some(Left(e)): Option[Either[SqlException, A]])
  }
{'fraction_non_alphanumeric': 0.14568096461751334, 'fraction_numerical': 0.0, 'mean_word_length': 3.8007590132827325, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4809985', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1733, 'n_words': 462}