Trait

com.github.mauricio.async.db

Connection

Related Doc: package db

Permalink

trait Connection extends AnyRef

Base interface for all objects that behave like a connection. This trait will usually be implemented by the objects that connect to a database, either over the filesystem or sockets. Connection are not supposed to be thread-safe and clients should assume implementations **are not** thread safe and shouldn't try to perform more than one statement (either common or prepared) at the same time. They should wait for the previous statement to be executed to then be able to pick the next one.

You can, for instance, compose on top of the futures returned by this class to execute many statements at the same time:

val handler: Connection = ...
val result: Future[QueryResult] = handler.connect
  .map(parameters => handler)
  .flatMap(connection => connection.sendQuery("BEGIN TRANSACTION ISOLATION LEVEL REPEATABLE READ"))
  .flatMap(query => handler.sendQuery("SELECT 0"))
  .flatMap(query => handler.sendQuery("COMMIT").map(value => query))

val queryResult: QueryResult = Await.result(result, Duration(5, SECONDS))
Linear Supertypes
AnyRef, Any
Known Subclasses
Ordering
  1. Alphabetic
  2. By Inheritance
Inherited
  1. Connection
  2. AnyRef
  3. Any
  1. Hide All
  2. Show All
Visibility
  1. Public
  2. All

Abstract Value Members

  1. abstract def connect: Future[Connection]

    Permalink

    Connects this object to the database.

    Connects this object to the database. Connection objects are not necessarily created with a connection to the database so you might have to call this method to be able to run queries against it.

  2. abstract def disconnect: Future[Connection]

    Permalink

    Disconnects this object.

    Disconnects this object. You should discard this object after calling this method. No more queries will be accepted.

  3. abstract def isConnected: Boolean

    Permalink

    Checks whether we are still connected to the database.

  4. abstract def sendPreparedStatement(query: String, values: Seq[Any] = List()): Future[QueryResult]

    Permalink

    Sends a prepared statement to the database.

    Sends a prepared statement to the database. Prepared statements are special statements that are pre-compiled by the database to run faster, they also allow you to avoid SQL injection attacks by not having to concatenate strings from possibly unsafe sources (like users) and sending them directy to the database.

    When sending a prepared statement, you can insert ? signs in your statement and then provide values at the method call 'values' parameter, as in:

    connection.sendPreparedStatement( "SELECT * FROM users WHERE users.login = ?", Array( "john-doe" ) )

    As you are using the ? as the placeholder for the value, you don't have to perform any kind of manipulation to the value, just provide it as is and the database will clean it up. You must provide as many parameters as you have provided placeholders, so, if your query is as "INSERT INTO users (login,email) VALUES (?,?)" you have to provide an array with at least two values, as in:

    Array("john-doe", "[email protected]")

    You can still use this method if your statement doesn't take any parameters, the default is an empty collection.

  5. abstract def sendQuery(query: String): Future[QueryResult]

    Permalink

    Sends a statement to the database.

    Sends a statement to the database. The statement can be anything your database can execute. Not all statements will return a collection of rows, so check the returned object if there are rows available.

Concrete Value Members

  1. final def !=(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  2. final def ##(): Int

    Permalink
    Definition Classes
    AnyRef → Any
  3. final def ==(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  4. final def asInstanceOf[T0]: T0

    Permalink
    Definition Classes
    Any
  5. def clone(): AnyRef

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate() @throws( ... )
  6. final def eq(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  7. def equals(arg0: Any): Boolean

    Permalink
    Definition Classes
    AnyRef → Any
  8. final def getClass(): Class[_]

    Permalink
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate()
  9. def hashCode(): Int

    Permalink
    Definition Classes
    AnyRef → Any
    Annotations
    @HotSpotIntrinsicCandidate()
  10. def inTransaction[A](f: (Connection) ⇒ Future[A])(implicit executionContext: ExecutionContext): Future[A]

    Permalink

    Executes an (asynchronous) function within a transaction block.

    Executes an (asynchronous) function within a transaction block. If the function completes successfully, the transaction is committed, otherwise it is aborted.

    f

    operation to execute on this connection

    returns

    result of f, conditional on transaction operations succeeding

  11. final def isInstanceOf[T0]: Boolean

    Permalink
    Definition Classes
    Any
  12. final def ne(arg0: AnyRef): Boolean

    Permalink
    Definition Classes
    AnyRef
  13. final def notify(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  14. final def notifyAll(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @HotSpotIntrinsicCandidate()
  15. final def synchronized[T0](arg0: ⇒ T0): T0

    Permalink
    Definition Classes
    AnyRef
  16. def toString(): String

    Permalink
    Definition Classes
    AnyRef → Any
  17. final def wait(arg0: Long, arg1: Int): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  18. final def wait(arg0: Long): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )
  19. final def wait(): Unit

    Permalink
    Definition Classes
    AnyRef
    Annotations
    @throws( ... )

Deprecated Value Members

  1. def finalize(): Unit

    Permalink
    Attributes
    protected[java.lang]
    Definition Classes
    AnyRef
    Annotations
    @Deprecated @deprecated @throws( classOf[java.lang.Throwable] )
    Deprecated

    (Since version ) see corresponding Javadoc for more information.

Inherited from AnyRef

Inherited from Any

Ungrouped