diff options
author | vlad <vlad@driver.xyz> | 2017-06-30 12:29:54 -0700 |
---|---|---|
committer | vlad <vlad@driver.xyz> | 2017-06-30 12:29:54 -0700 |
commit | a997aa6539d1f0af4ab4fc395ff2033335da312a (patch) | |
tree | 4f24529cd0beed94368caafdc0bdbb5677184851 /src/main/scala/xyz/driver/pdsuicommon/db/SqlContext.scala | |
parent | 5832f63b84d7388441d1200f2442dc1e9de0225c (diff) | |
download | rest-query-a997aa6539d1f0af4ab4fc395ff2033335da312a.tar.gz rest-query-a997aa6539d1f0af4ab4fc395ff2033335da312a.tar.bz2 rest-query-a997aa6539d1f0af4ab4fc395ff2033335da312a.zip |
Latest PDS UI utils
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/db/SqlContext.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuicommon/db/SqlContext.scala | 93 |
1 files changed, 0 insertions, 93 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/db/SqlContext.scala b/src/main/scala/xyz/driver/pdsuicommon/db/SqlContext.scala deleted file mode 100644 index c929eae..0000000 --- a/src/main/scala/xyz/driver/pdsuicommon/db/SqlContext.scala +++ /dev/null @@ -1,93 +0,0 @@ -package xyz.driver.pdsuicommon.db - -import java.io.Closeable -import java.time._ -import java.util.concurrent.Executors -import javax.sql.DataSource - -import com.typesafe.config.Config -import io.getquill._ -import xyz.driver.pdsuicommon.concurrent.MdcExecutionContext -import xyz.driver.pdsuicommon.db.SqlContext.Settings -import xyz.driver.pdsuicommon.error.IncorrectIdException -import xyz.driver.pdsuicommon.logging.{PhiLogging, Unsafe} - -import scala.concurrent.ExecutionContext -import scala.util.control.NonFatal -import scala.util.{Failure, Success, Try} - -object SqlContext extends PhiLogging { - - case class DbCredentials(user: String, - password: String, - host: String, - port: Int, - dbName: String, - dbCreateFlag: Boolean, - dbContext: String, - connectionParams: String, - url: String) - - case class Settings(credentials: DbCredentials, - connection: Config, - connectionAttemptsOnStartup: Int, - threadPoolSize: Int) - - def apply(settings: Settings): SqlContext = { - // Prevent leaking credentials to a log - Try(JdbcContextConfig(settings.connection).dataSource) match { - case Success(dataSource) => new SqlContext(dataSource, settings) - case Failure(NonFatal(e)) => - logger.error(phi"Can not load dataSource, error: ${Unsafe(e.getClass.getName)}") - throw new IllegalArgumentException("Can not load dataSource from config. Check your database and config") - } - } - -} - -class SqlContext(dataSource: DataSource with Closeable, settings: Settings) - extends MysqlJdbcContext[MysqlEscape](dataSource) with EntityExtractorDerivation[Literal] { - - private val tpe = Executors.newFixedThreadPool(settings.threadPoolSize) - - implicit val executionContext: ExecutionContext = { - val orig = ExecutionContext.fromExecutor(tpe) - MdcExecutionContext.from(orig) - } - - override def close(): Unit = { - super.close() - tpe.shutdownNow() - } - - /** - * Overrode, because Quill JDBC optionDecoder pass null inside decoders. - * If custom decoder don't have special null handler, it will failed. - * - * @see https://github.com/getquill/quill/issues/535 - */ - implicit override def optionDecoder[T](implicit d: Decoder[T]): Decoder[Option[T]] = - decoder( - sqlType = d.sqlType, - row => - index => { - try { - val res = d(index - 1, row) - if (row.wasNull) { - None - } else { - Some(res) - } - } catch { - case _: NullPointerException => None - case _: IncorrectIdException => None - } - } - ) - - final implicit class LocalDateTimeDbOps(val left: LocalDateTime) { - - // scalastyle:off - def <=(right: LocalDateTime): Quoted[Boolean] = quote(infix"$left <= $right".as[Boolean]) - } -} |