2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package jtest.util.org.openecomp.sdnc.ra;
24 import org.springframework.jdbc.core.JdbcTemplate;
25 import org.springframework.jdbc.support.rowset.SqlRowSet;
27 public class TestTable {
29 private String tableName;
30 private String[] columnList;
31 private String idName;
33 private String insertSql;
35 private JdbcTemplate jdbcTemplate;
37 public TestTable(JdbcTemplate jdbcTemplate, String tableName, String idName, String... columnList) {
38 this.jdbcTemplate = jdbcTemplate;
39 this.tableName = tableName;
41 this.columnList = columnList;
45 private void createInsertSql() {
46 StringBuilder ss = new StringBuilder();
47 ss.append("INSERT INTO ").append(tableName).append(" (");
48 for (String s : columnList)
49 ss.append(s).append(", ");
50 ss.setLength(ss.length() - 2);
51 ss.append(") VALUES (");
52 for (int i = 0; i < columnList.length; i++)
54 ss.setLength(ss.length() - 2);
56 insertSql = ss.toString();
59 public void add(Object... values) {
60 jdbcTemplate.update(insertSql, values);
63 public long getLastId() {
64 return jdbcTemplate.queryForObject("SELECT max(" + idName + ") FROM " + tableName, Long.class);
67 public Long getId(String where) {
68 String selectSql = "SELECT " + idName + " FROM " + tableName + " WHERE " + where;
69 SqlRowSet rs = jdbcTemplate.queryForRowSet(selectSql);
71 return rs.getLong(idName);
75 public boolean exists(String where) {
76 String selectSql = "SELECT * FROM " + tableName + " WHERE " + where;
77 SqlRowSet rs = jdbcTemplate.queryForRowSet(selectSql);
81 public void delete(String where) {
82 jdbcTemplate.update("DELETE FROM " + tableName + " WHERE " + where);