| 
 | 1 | +/*  | 
 | 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more  | 
 | 3 | + * contributor license agreements.  See the NOTICE file distributed with  | 
 | 4 | + * this work for additional information regarding copyright ownership.  | 
 | 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0  | 
 | 6 | + * (the "License"); you may not use this file except in compliance with  | 
 | 7 | + * the License.  You may obtain a copy of the License at  | 
 | 8 | + *  | 
 | 9 | + *    http://www.apache.org/licenses/LICENSE-2.0  | 
 | 10 | + *  | 
 | 11 | + * Unless required by applicable law or agreed to in writing, software  | 
 | 12 | + * distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 14 | + * See the License for the specific language governing permissions and  | 
 | 15 | + * limitations under the License.  | 
 | 16 | + */  | 
 | 17 | + | 
 | 18 | +package org.apache.kyuubi.util  | 
 | 19 | + | 
 | 20 | +import java.util.Properties  | 
 | 21 | +import javax.sql.DataSource  | 
 | 22 | + | 
 | 23 | +import com.zaxxer.hikari.util.DriverDataSource  | 
 | 24 | + | 
 | 25 | +import org.apache.kyuubi.KyuubiFunSuite  | 
 | 26 | + | 
 | 27 | +class JdbcUtilsSuite extends KyuubiFunSuite {  | 
 | 28 | + | 
 | 29 | +  private val connUrl = s"jdbc:derby:memory:jdbc_utils_test;create=true"  | 
 | 30 | +  private val shutdownUrl = s"jdbc:derby:memory:jdbc_utils_test;shutdown=true"  | 
 | 31 | +  private val driverClz = "org.apache.derby.jdbc.AutoloadedDriver"  | 
 | 32 | + | 
 | 33 | +  implicit private val ds: DataSource =  | 
 | 34 | +    new DriverDataSource(connUrl, driverClz, new Properties, "test", "test")  | 
 | 35 | + | 
 | 36 | +  case class Person(id: Int, name: String)  | 
 | 37 | + | 
 | 38 | +  override def beforeAll(): Unit = {  | 
 | 39 | + | 
 | 40 | +    super.beforeAll()  | 
 | 41 | +  }  | 
 | 42 | + | 
 | 43 | +  test("JdbcUtils methods") {  | 
 | 44 | +    JdbcUtils.execute(  | 
 | 45 | +      """CREATE TABLE person(  | 
 | 46 | +        |  id   INT NOT NULL PRIMARY KEY,  | 
 | 47 | +        |  name VARCHAR(255)  | 
 | 48 | +        |)  | 
 | 49 | +        |""".stripMargin)()  | 
 | 50 | +    val affected = JdbcUtils.executeUpdate("INSERT INTO person VALUES (?, ?), (?, ?)") { stmt =>  | 
 | 51 | +      stmt.setInt(1, 3)  | 
 | 52 | +      stmt.setString(2, "Apache")  | 
 | 53 | +      stmt.setInt(3, 9)  | 
 | 54 | +      stmt.setString(4, "Kyuubi")  | 
 | 55 | +    }  | 
 | 56 | +    assert(affected == 2)  | 
 | 57 | + | 
 | 58 | +    val persons = JdbcUtils.executeQueryWithRowMapper(  | 
 | 59 | +      "SELECT * FROM person WHERE id=?") { stmt =>  | 
 | 60 | +      stmt.setInt(1, 9)  | 
 | 61 | +    } { rs =>  | 
 | 62 | +      Person(rs.getInt(1), rs.getString(2))  | 
 | 63 | +    }  | 
 | 64 | +    assert(persons.length == 1)  | 
 | 65 | +    assert(persons.head == Person(9, "Kyuubi"))  | 
 | 66 | + | 
 | 67 | +    JdbcUtils.executeQuery("SELECT count(*) FROM person")() { rs =>  | 
 | 68 | +      assert(rs.next())  | 
 | 69 | +      assert(!rs.next())  | 
 | 70 | +    }  | 
 | 71 | +  }  | 
 | 72 | +}  | 
0 commit comments