<commit_before>package dbrary

import scala.concurrent.{Future,ExecutionContext}
import scala.util.control.Exception.catching
import com.github.mauricio.async.db
import macros._
import macros.async._

class SQLArg[A](val value : A)(implicit val sqlType : SQLType[A]) {
  def put : Any = sqlType.put(value)
  def placeholder = "?::" + sqlType.name
}

object SQLArg {
  def apply[A](value : A)(implicit sqlType : SQLType[A]) = new SQLArg[A](value)(sqlType)
}

/** A list of arguments that may be passed to a query. */
class SQLArgs(val args : Seq[SQLArg[_]]) extends Iterable[SQLArg[_]] {
  def iterator = args.iterator
  def ++(other : Iterable[SQLArg[_]]) : SQLArgs = new SQLArgs(args ++ other)
  def :+(other : SQLArg[_]) : SQLArgs = new SQLArgs(args :+ other)
  def +:(other : SQLArg[_]) : SQLArgs = new SQLArgs(other +: args)
  def :+[A : SQLType](other : A) : SQLArgs = new SQLArgs(args :+ SQLArg(other))
  def +:[A : SQLType](other : A) : SQLArgs = new SQLArgs(SQLArg(other) +: args)
  def *(n : Int) : SQLArgs = new SQLArgs(0.until(n).flatMap(_ => args))
}

/** A special case of SQLArgs for running simple queries with no arguments. */
object SQLNoArgs extends SQLArgs(Nil)

/** Generic trait for anything which may accept SQLType args to produce a result. */
protected sealed abstract trait SQLArgsView[+R] extends RepeatedView[SQLType, SQLArg[_], R] {
  protected def arg[A : SQLType](a : A) = SQLArg(a)
  final def apply(a : SQLArgs) : R = result(a.args : _*)
}

object SQLArgs extends SQLArgsView[SQLArgs] {
  protected def result(args : SQLArg[_]*) : SQLArgs = new SQLArgs(args)
}

/** Exceptions that may occur during parsing query results, usually indicating type or arity mismatches. */
class SQLResultException(message : String) extends db.exceptions.DatabaseException(message)

/** A simple wrapper for Future[QueryResult] representing the result of a query. */
class SQLResult(val result : Future[db.QueryResult])(implicit context : ExecutionContext) {
  private[this] def fail(res : db.QueryResult, msg : String) = throw new SQLResultException(res.statusMessage + ": " + msg)
  private[this] def rows(res : db.QueryResult) = res.rows.getOrElse(fail(res, "no results"))
  private[this] def singleOpt(res : db.QueryResult) = rows(res) match {
    case Seq() => None
    case Seq(r) => Some(r)
    case l => fail(res, "got " + l.length + " rows, expected 1")
  }
  private[this] def single(res : db.QueryResult) = singleOpt(res).getOrElse(fail(res, "got no rows, expected 1"))

  def future(f : Future[db.QueryResult] => Future[db.QueryResult]) : SQLResult =
    new SQLResult(f(result))
  def map[A](f : db.QueryResult => A) : Future[A] = result.map(f)
  def flatMap[A](f : db.QueryResult => Future[A]) : Future[A] = result.flatMap(f)
  def rowsAffected : Future[Long] = map(_.rowsAffected)
  def execute : Future[Boolean] = map(_.rowsAffected > 0)

  def as[A](parse : SQLRow[A]) : SQLRows[A] = new SQLRows[A](result, parse)

  def list[A](parse : SQLRow[A]) : Future[IndexedSeq[A]] = map(rows(_).map(parse(_)))
  def singleOpt[A](parse : SQLRow[A]) : Future[Option[A]] = map(singleOpt(_).map(parse(_)))
  def single[A](parse : SQLRow[A]) : Future[A] = map(r => parse(single(r)))
}

object SQLResult {
  private final val emptyResult = new db.QueryResult(0, "")
  def empty(implicit context : ExecutionContext) = new SQLResult(emptyResult.async)(context)
}

/** SQLResult with an associated row parser. */
final class SQLRows[+A](result : Future[db.QueryResult], parse : SQLRow[A])(implicit context : ExecutionContext) extends SQLResult(result)(context) {
  override def future(f : Future[db.QueryResult] => Future[db.QueryResult]) : SQLRows[A] =
    new SQLRows[A](f(result), parse)
  def map[B](f : A => B) : SQLRows[B] = new SQLRows[B](result, parse.map[B](f))
  def list : Future[IndexedSeq[A]] = list(parse)
  def singleOpt : Future[Option[A]] = singleOpt(parse)
  def single : Future[A] = single(parse)
}

/** A generic row parser that can transform RowData query results. */
trait SQLRow[+A] extends (db.RowData => A) {
  parent =>

  def map[B](f : A => B) : SQLRow[B] = new SQLRow[B] {
    def apply(r : db.RowData) : B = f(parent.apply(r))
  }

  def flatMap[B](f : A => SQLRow[B]) : SQLRow[B] = new SQLRow[B] {
    def apply(r : db.RowData) : B = f(parent.apply(r)).apply(r)<commit_msg>Don't use implicit conversion for async<commit_after>package dbrary

import scala.concurrent.{Future,ExecutionContext}
import scala.util.control.Exception.catching
import com.github.mauricio.async.db
import macros._

class SQLArg[A](val value : A)(implicit val sqlType : SQLType[A]) {
  def put : Any = sqlType.put(value)
  def placeholder = "?::" + sqlType.name
}

object SQLArg {
  def apply[A](value : A)(implicit sqlType : SQLType[A]) = new SQLArg[A](value)(sqlType)
}

/** A list of arguments that may be passed to a query. */
class SQLArgs(val args : Seq[SQLArg[_]]) extends Iterable[SQLArg[_]] {
  def iterator = args.iterator
  def ++(other : Iterable[SQLArg[_]]) : SQLArgs = new SQLArgs(args ++ other)
  def :+(other : SQLArg[_]) : SQLArgs = new SQLArgs(args :+ other)
  def +:(other : SQLArg[_]) : SQLArgs = new SQLArgs(other +: args)
  def :+[A : SQLType](other : A) : SQLArgs = new SQLArgs(args :+ SQLArg(other))
  def +:[A : SQLType](other : A) : SQLArgs = new SQLArgs(SQLArg(other) +: args)
  def *(n : Int) : SQLArgs = new SQLArgs(0.until(n).flatMap(_ => args))
}

/** A special case of SQLArgs for running simple queries with no arguments. */
object SQLNoArgs extends SQLArgs(Nil)

/** Generic trait for anything which may accept SQLType args to produce a result. */
protected sealed abstract trait SQLArgsView[+R] extends RepeatedView[SQLType, SQLArg[_], R] {
  protected def arg[A : SQLType](a : A) = SQLArg(a)
  final def apply(a : SQLArgs) : R = result(a.args : _*)
}

object SQLArgs extends SQLArgsView[SQLArgs] {
  protected def result(args : SQLArg[_]*) : SQLArgs = new SQLArgs(args)
}

/** Exceptions that may occur during parsing query results, usually indicating type or arity mismatches. */
class SQLResultException(message : String) extends db.exceptions.DatabaseException(message)

/** A simple wrapper for Future[QueryResult] representing the result of a query. */
class SQLResult(val result : Future[db.QueryResult])(implicit context : ExecutionContext) {
  private[this] def fail(res : db.QueryResult, msg : String) = throw new SQLResultException(res.statusMessage + ": " + msg)
  private[this] def rows(res : db.QueryResult) = res.rows.getOrElse(fail(res, "no results"))
  private[this] def singleOpt(res : db.QueryResult) = rows(res) match {
    case Seq() => None
    case Seq(r) => Some(r)
    case l => fail(res, "got " + l.length + " rows, expected 1")
  }
  private[this] def single(res : db.QueryResult) = singleOpt(res).getOrElse(fail(res, "got no rows, expected 1"))

  def future(f : Future[db.QueryResult] => Future[db.QueryResult]) : SQLResult =
    new SQLResult(f(result))
  def map[A](f : db.QueryResult => A) : Future[A] = result.map(f)
  def flatMap[A](f : db.QueryResult => Future[A]) : Future[A] = result.flatMap(f)
  def rowsAffected : Future[Long] = map(_.rowsAffected)
  def execute : Future[Boolean] = map(_.rowsAffected > 0)

  def as[A](parse : SQLRow[A]) : SQLRows[A] = new SQLRows[A](result, parse)

  def list[A](parse : SQLRow[A]) : Future[IndexedSeq[A]] = map(rows(_).map(parse(_)))
  def singleOpt[A](parse : SQLRow[A]) : Future[Option[A]] = map(singleOpt(_).map(parse(_)))
  def single[A](parse : SQLRow[A]) : Future[A] = map(r => parse(single(r)))
}

object SQLResult {
  private final val emptyResult = new db.QueryResult(0, "")
  def empty(implicit context : ExecutionContext) = new SQLResult(async(emptyResult))(context)
}

/** SQLResult with an associated row parser. */
final class SQLRows[+A](result : Future[db.QueryResult], parse : SQLRow[A])(implicit context : ExecutionContext) extends SQLResult(result)(context) {
  override def future(f : Future[db.QueryResult] => Future[db.QueryResult]) : SQLRows[A] =
    new SQLRows[A](f(result), parse)
  def map[B](f : A => B) : SQLRows[B] = new SQLRows[B](result, parse.map[B](f))
  def list : Future[IndexedSeq[A]] = list(parse)
  def singleOpt : Future[Option[A]] = singleOpt(parse)
  def single : Future[A] = single(parse)
}

/** A generic row parser that can transform RowData query results. */
trait SQLRow[+A] extends (db.RowData => A) {
  parent =>

  def map[B](f : A => B) : SQLRow[B] = new SQLRow[B] {
    def apply(r : db.RowData) : B = f(parent.apply(r))
  }

  def flatMap[B](f : A => SQLRow[B]) : SQLRow[B] = new SQLRow[B] {
    def apply(r : db.RowData) : B = f(parent.apply(r)).apply(r)
{'fraction_non_alphanumeric': 0.15657209033005212, 'fraction_numerical': 0.0011580775911986102, 'mean_word_length': 4.662950819672131, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 33, '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': '2921349', 'n_tokens_mistral': 2938, 'n_tokens_neox': 2906, 'n_words': 788}