SHARE
TWEET

Untitled

a guest Nov 6th, 2018 150 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. val Connection.asSuspendable get(): SuspendableConnection = SuspendableConnectionImpl(this)
  2.  
  3. interface SuspendableConnection {
  4.  
  5.   /**
  6.    *
  7.    * Disconnects this object. You should discard this object after calling this method. No more queries
  8.    * will be accepted.
  9.    *
  10.    * @return
  11.    */
  12.  
  13.   suspend fun disconnect(): Connection
  14.  
  15.   /**
  16.    *
  17.    * Connects this object to the database. Connection objects are not necessarily created , a connection to the
  18.    * database so you might have to call this method to be able to run queries against it.
  19.    *
  20.    * @return
  21.    */
  22.  
  23.   suspend fun connect(): Connection
  24.  
  25.   /**
  26.    *
  27.    * Sends a statement to the database. The statement can be anything your database can execute. Not all statements
  28.    * will return a collection of rows, so check the returned object if there are rows available.
  29.    *
  30.    * @param query
  31.    * @return
  32.    */
  33.  
  34.   suspend fun sendQuery(query: String): QueryResult
  35.  
  36.   /**
  37.    *
  38.    * Sends a prepared statement to the database. Prepared statements are special statements that are pre-compiled
  39.    * by the database to run faster, they also allow you to avoid SQL injection attacks by not having to concatenate
  40.    * strings from possibly unsafe sources (like users) and sending them directly to the database.
  41.    *
  42.    * When sending a prepared statement, you can insert ? signs in your statement and then provide values at the method
  43.    * call 'values' parameter, as in:
  44.    *
  45.    * {{{
  46.    *  connection.sendPreparedStatement( "SELECT * FROM users WHERE users.login = ?", Array( "john-doe" ) )
  47.    * }}}
  48.    *
  49.    * As you are using the ? as the placeholder for the value, you don't have to perform any kind of manipulation
  50.    * to the value, just provide it as is and the database will clean it up. You must provide as many parameters
  51.    * as you have provided placeholders, so, if your query is as "INSERT INTO users (login,email) VALUES (?,?)" you
  52.    * have to provide an array , at least two values, as in:
  53.    *
  54.    * {{{
  55.    *   Array("john-doe", "doe@mail.com")
  56.    * }}}
  57.    *
  58.    * You can still use this method if your statement doesn't take any parameters, the default is an empty collection.
  59.    *
  60.    * @param query
  61.    * @param values
  62.    * @return
  63.    */
  64.   suspend fun sendPreparedStatement(query: String, values: List<Any?>): QueryResult
  65.  
  66.   /**
  67.    *
  68.    * Sends a prepared statement to the database. Prepared statements are special statements that are pre-compiled
  69.    * by the database to run faster, they also allow you to avoid SQL injection attacks by not having to concatenate
  70.    * strings from possibly unsafe sources (like users) and sending them directly to the database.
  71.    *
  72.    * When sending a prepared statement, you can insert ? signs in your statement and then provide values at the method
  73.    * call 'values' parameter, as in:
  74.    *
  75.    * {{{
  76.    *  connection.sendPreparedStatement( "SELECT * FROM users WHERE users.login = ?", Array( "john-doe" ) )
  77.    * }}}
  78.    *
  79.    * As you are using the ? as the placeholder for the value, you don't have to perform any kind of manipulation
  80.    * to the value, just provide it as is and the database will clean it up. You must provide as many parameters
  81.    * as you have provided placeholders, so, if your query is as "INSERT INTO users (login,email) VALUES (?,?)" you
  82.    * have to provide an array , at least two values, as in:
  83.    *
  84.    * {{{
  85.    *   Array("john-doe", "doe@mail.com")
  86.    * }}}
  87.    *
  88.    * You can still use this method if your statement doesn't take any parameters, the default is an empty collection.
  89.    *
  90.    * @param query
  91.    * @return
  92.    */
  93.   suspend fun sendPreparedStatement(query: String): QueryResult
  94.  
  95.  
  96.   /**
  97.    *
  98.    * Executes an (asynchronous) function ,in a transaction block.
  99.    * If the function completes successfully, the transaction is committed, otherwise it is aborted.
  100.    *
  101.    * @param f operation to execute on this connection
  102.    * @return result of f, conditional on transaction operations succeeding
  103.    */
  104.  
  105.   suspend fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>): A
  106.  
  107. }
  108.  
  109. class SuspendableConnectionImpl(val connection: Connection): SuspendableConnection {
  110.   /**
  111.    *
  112.    * Disconnects this object. You should discard this object after calling this method. No more queries
  113.    * will be accepted.
  114.    *
  115.    * @return
  116.    */
  117.  
  118.   override suspend fun disconnect(): Connection = connection.disconnect().await()
  119.  
  120.   /**
  121.    *
  122.    * Connects this object to the database. Connection objects are not necessarily created , a connection to the
  123.    * database so you might have to call this method to be able to run queries against it.
  124.    *
  125.    * @return
  126.    */
  127.  
  128.   override suspend fun connect(): Connection = connection.connect().await()
  129.  
  130.   /**
  131.    *
  132.    * Sends a statement to the database. The statement can be anything your database can execute. Not all statements
  133.    * will return a collection of rows, so check the returned object if there are rows available.
  134.    *
  135.    * @param query
  136.    * @return
  137.    */
  138.  
  139.   override suspend fun sendQuery(query: String): QueryResult = connection.sendQuery(query).await()
  140.  
  141.   /**
  142.    *
  143.    * Sends a prepared statement to the database. Prepared statements are special statements that are pre-compiled
  144.    * by the database to run faster, they also allow you to avoid SQL injection attacks by not having to concatenate
  145.    * strings from possibly unsafe sources (like users) and sending them directly to the database.
  146.    *
  147.    * When sending a prepared statement, you can insert ? signs in your statement and then provide values at the method
  148.    * call 'values' parameter, as in:
  149.    *
  150.    * {{{
  151.    *  connection.sendPreparedStatement( "SELECT * FROM users WHERE users.login = ?", Array( "john-doe" ) )
  152.    * }}}
  153.    *
  154.    * As you are using the ? as the placeholder for the value, you don't have to perform any kind of manipulation
  155.    * to the value, just provide it as is and the database will clean it up. You must provide as many parameters
  156.    * as you have provided placeholders, so, if your query is as "INSERT INTO users (login,email) VALUES (?,?)" you
  157.    * have to provide an array , at least two values, as in:
  158.    *
  159.    * {{{
  160.    *   Array("john-doe", "doe@mail.com")
  161.    * }}}
  162.    *
  163.    * You can still use this method if your statement doesn't take any parameters, the default is an empty collection.
  164.    *
  165.    * @param query
  166.    * @param values
  167.    * @return
  168.    */
  169.   override suspend fun sendPreparedStatement(query: String, values: List<Any?>): QueryResult = connection.sendPreparedStatement(query, values).await()
  170.  
  171.   /**
  172.    *
  173.    * Sends a prepared statement to the database. Prepared statements are special statements that are pre-compiled
  174.    * by the database to run faster, they also allow you to avoid SQL injection attacks by not having to concatenate
  175.    * strings from possibly unsafe sources (like users) and sending them directly to the database.
  176.    *
  177.    * When sending a prepared statement, you can insert ? signs in your statement and then provide values at the method
  178.    * call 'values' parameter, as in:
  179.    *
  180.    * {{{
  181.    *  connection.sendPreparedStatement( "SELECT * FROM users WHERE users.login = ?", Array( "john-doe" ) )
  182.    * }}}
  183.    *
  184.    * As you are using the ? as the placeholder for the value, you don't have to perform any kind of manipulation
  185.    * to the value, just provide it as is and the database will clean it up. You must provide as many parameters
  186.    * as you have provided placeholders, so, if your query is as "INSERT INTO users (login,email) VALUES (?,?)" you
  187.    * have to provide an array , at least two values, as in:
  188.    *
  189.    * {{{
  190.    *   Array("john-doe", "doe@mail.com")
  191.    * }}}
  192.    *
  193.    * You can still use this method if your statement doesn't take any parameters, the default is an empty collection.
  194.    *
  195.    * @param query
  196.    * @return
  197.    */
  198.   override suspend fun sendPreparedStatement(query: String): QueryResult = connection.sendPreparedStatement(query).await()
  199.  
  200.  
  201.   /**
  202.    *
  203.    * Executes an (asynchronous) function ,in a transaction block.
  204.    * If the function completes successfully, the transaction is committed, otherwise it is aborted.
  205.    *
  206.    * @param f operation to execute on this connection
  207.    * @return result of f, conditional on transaction operations succeeding
  208.    */
  209.  
  210.   override suspend fun <A> inTransaction(f: (Connection) -> CompletableFuture<A>): A = connection.inTransaction(f).await()
  211. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top