-
Notifications
You must be signed in to change notification settings - Fork 902
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KYUUBI #3222][FOLLOWUP] Introdude JdbcUtils to simplify code
### _Why are the changes needed?_ This is the followup of #3235, the main change is introdude `JdbcUtils` to simplify code, and allow empty password for Jdbc auth. Jdbc connection pool has been removed because `JdbcAuthenticationProviderImpl` will be created on each connection, we can improve to use singleton in the future ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.apache.org/docs/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #3278 from pan3793/jdbc-followup. Closes #3222 2863cae [Cheng Pan] Update kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/JdbcAuthenticationProviderImplSuite.scala 51a9c45 [Cheng Pan] Update kyuubi-common/src/test/scala/org/apache/kyuubi/service/authentication/JdbcAuthenticationProviderImplSuite.scala eee3c55 [Cheng Pan] Update kyuubi-common/src/test/scala/org/apache/kyuubi/util/JdbcUtilsSuite.scala d02bb99 [Cheng Pan] nit e001b5b [Cheng Pan] nit 8cf5cd6 [Cheng Pan] nit 032f2df [Cheng Pan] nit 8a42f18 [Cheng Pan] nit c7893fd [Cheng Pan] JdbcUtilsSuite f97f2d9 [Cheng Pan] remove pool a8812d0 [Cheng Pan] move render result set to test 83d7d4c [Cheng Pan] fix ut db787a4 [Cheng Pan] nit 864f9dd [Cheng Pan] nit b60decf [Cheng Pan] nit 8c66e0b [Cheng Pan] nit 2063c43 [Cheng Pan] [KYUUBI #3222][FOLLOWUP] Introdude JdbcUtils to simplify code Authored-by: Cheng Pan <chengpan@apache.org> Signed-off-by: Cheng Pan <chengpan@apache.org>
- Loading branch information
Showing
6 changed files
with
300 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
kyuubi-common/src/main/scala/org/apache/kyuubi/util/JdbcUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kyuubi.util | ||
|
||
import java.sql.{Connection, PreparedStatement, ResultSet} | ||
import javax.sql.DataSource | ||
|
||
import scala.util.control.NonFatal | ||
|
||
import org.apache.kyuubi.Logging | ||
|
||
object JdbcUtils extends Logging { | ||
|
||
def close(c: AutoCloseable): Unit = { | ||
if (c != null) { | ||
try { | ||
c.close() | ||
} catch { | ||
case NonFatal(t) => warn(s"Error on closing", t) | ||
} | ||
} | ||
} | ||
|
||
def withCloseable[R, C <: AutoCloseable](c: C)(block: C => R): R = { | ||
try { | ||
block(c) | ||
} finally { | ||
close(c) | ||
} | ||
} | ||
|
||
def withConnection[R](block: Connection => R)(implicit ds: DataSource): R = { | ||
withCloseable(ds.getConnection)(block) | ||
} | ||
|
||
def execute( | ||
sqlTemplate: String)( | ||
setParameters: PreparedStatement => Unit = _ => {})( | ||
implicit ds: DataSource): Boolean = withConnection { conn => | ||
withCloseable(conn.prepareStatement(sqlTemplate)) { pStmt => | ||
setParameters(pStmt) | ||
pStmt.execute() | ||
} | ||
} | ||
|
||
def executeUpdate( | ||
sqlTemplate: String)( | ||
setParameters: PreparedStatement => Unit = _ => {})( | ||
implicit ds: DataSource): Int = withConnection { conn => | ||
withCloseable(conn.prepareStatement(sqlTemplate)) { pStmt => | ||
setParameters(pStmt) | ||
pStmt.executeUpdate() | ||
} | ||
} | ||
|
||
def executeQuery[R]( | ||
sqlTemplate: String)( | ||
setParameters: PreparedStatement => Unit = _ => {})( | ||
processResultSet: ResultSet => R)( | ||
implicit ds: DataSource): R = withConnection { conn => | ||
withCloseable(conn.prepareStatement(sqlTemplate)) { pStmt => | ||
setParameters(pStmt) | ||
withCloseable(pStmt.executeQuery()) { rs => | ||
processResultSet(rs) | ||
} | ||
} | ||
} | ||
|
||
def executeQueryWithRowMapper[R]( | ||
sqlTemplate: String)( | ||
setParameters: PreparedStatement => Unit = _ => {})( | ||
rowMapper: ResultSet => R)( | ||
implicit ds: DataSource): Seq[R] = withConnection { conn => | ||
withCloseable(conn.prepareStatement(sqlTemplate)) { pStmt => | ||
setParameters(pStmt) | ||
withCloseable(pStmt.executeQuery()) { rs => | ||
val builder = Seq.newBuilder[R] | ||
while (rs.next()) builder += rowMapper(rs) | ||
builder.result | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.