All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.landoop.jdbc4.LsqlStatement.kt Maven / Gradle / Ivy

The newest version!
package com.landoop.jdbc4

import com.landoop.jdbc4.client.RestClient
import com.landoop.jdbc4.client.domain.StreamingSelectResult
import java.sql.Connection
import java.sql.ResultSet
import java.sql.SQLFeatureNotSupportedException
import java.sql.SQLWarning
import java.sql.Statement
import java.util.concurrent.TimeUnit

open class LsqlStatement(private val conn: Connection,
                         private val client: RestClient) : Statement, AutoCloseable, IWrapper {

  // the last resultset retrieved by this statement
  private var rs: ResultSet = RowResultSet.empty()

  override fun getResultSetType(): Int = ResultSet.TYPE_FORWARD_ONLY

  override fun isWrapperFor(iface: Class<*>?): Boolean = _isWrapperFor(iface)
  override fun  unwrap(iface: Class): T = _unwrap(iface)

  override fun getMaxRows(): Int = 0

  override fun executeQuery(sql: String): ResultSet {
    select(sql)
    return rs
  }

  override fun execute(sql: String): Boolean {
    return if (sql.toUpperCase().trim().startsWith("INSERT")) {
      insert(sql)
      true
    } else {
      // in this execute method we must block until we are completed
      // or we receive a record, otherwise we don't know if we can return true or false
      val result = select(sql)
      rs = StreamingRowResultSet(this, result)
      result.hasData(1, TimeUnit.DAYS)
    }
  }

  private fun insert(sql: String): Boolean {
    val withSetKey = """SET _ktype='STRING'; $sql"""
    client.insert(withSetKey)
    return true
  }

  private fun select(sql: String): StreamingSelectResult {
    val result = client.select(sql)
    rs = StreamingRowResultSet(this, result)
    return result
  }



  override fun getConnection(): Connection = conn

  override fun clearWarnings() {}
  override fun getWarnings(): SQLWarning? = null

  override fun getMaxFieldSize(): Int = 0
  override fun setMaxFieldSize(max: Int) {}

  override fun setMaxRows(max: Int) {}

  override fun getFetchSize(): Int = -1

  override fun setEscapeProcessing(enable: Boolean) {}

  override fun setCursorName(name: String?) = throw SQLFeatureNotSupportedException()

  override fun setFetchSize(rows: Int) {}

  override fun isPoolable(): Boolean = false

  override fun getResultSetConcurrency(): Int = ResultSet.CONCUR_READ_ONLY

  override fun getResultSet(): ResultSet = rs

  override fun getQueryTimeout(): Int = client.connectionRequestTimeout()
  override fun setQueryTimeout(seconds: Int) = throw UnsupportedOperationException()

  override fun getFetchDirection(): Int = ResultSet.FETCH_FORWARD
  override fun setFetchDirection(direction: Int) {
    if (direction != ResultSet.FETCH_FORWARD)
      throw SQLFeatureNotSupportedException("LSQL ResultSets can only be read FETCH_FORWARD")
  }

  override fun getResultSetHoldability(): Int = ResultSet.CLOSE_CURSORS_AT_COMMIT

  // we always fetch all results at once

  override fun getMoreResults(): Boolean = false
  override fun getMoreResults(current: Int): Boolean = false

  // == LsqlStatements are offline, and so there's nothing to close

  override fun isCloseOnCompletion(): Boolean = true
  override fun close() {} // lsql-statements have no resources associated
  override fun isClosed(): Boolean = true
  override fun closeOnCompletion() {}

  // == the following are methods that update and thus are not supported by this read only jdbc interface ==

  override fun execute(sql: String?, autoGeneratedKeys: Int): Boolean = throw SQLFeatureNotSupportedException("Auto generated keys are not supported by Lenses")
  override fun execute(sql: String?, columnIndexes: IntArray?): Boolean = throw SQLFeatureNotSupportedException("Auto generated keys are not supported by Lenses")
  override fun execute(sql: String?, columnNames: Array?): Boolean = throw SQLFeatureNotSupportedException("Auto generated keys are not supported by Lenses")
  override fun executeBatch(): IntArray = throw SQLFeatureNotSupportedException()
  override fun addBatch(sql: String?) = throw SQLFeatureNotSupportedException()
  override fun getGeneratedKeys(): ResultSet = throw SQLFeatureNotSupportedException()
  override fun clearBatch(): Unit = throw SQLFeatureNotSupportedException()
  override fun executeUpdate(sql: String?): Int = throw SQLFeatureNotSupportedException()
  override fun executeUpdate(sql: String?, autoGeneratedKeys: Int): Int = throw SQLFeatureNotSupportedException()
  override fun executeUpdate(sql: String?, columnIndexes: IntArray?): Int = throw SQLFeatureNotSupportedException()
  override fun executeUpdate(sql: String?, columnNames: Array?): Int = throw SQLFeatureNotSupportedException()
  override fun setPoolable(poolable: Boolean) = throw SQLFeatureNotSupportedException()

  override fun getUpdateCount(): Int = -1
  override fun cancel() = throw SQLFeatureNotSupportedException()

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy