summaryrefslogtreecommitdiffstats
path: root/ecomp-sdk/epsdk-analytics/src/main/java/org/openecomp/portalsdk/analytics/system/RemDbUtils.java
blob: 733a0a387d7bd94cbc3b5e1e634b5eb90ff5b26c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*-
 * ================================================================================
 * eCOMP Portal SDK
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property
 * ================================================================================
 * Licensed 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.openecomp.portalsdk.analytics.system;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.openecomp.portalsdk.analytics.error.RaptorException;
import org.openecomp.portalsdk.analytics.error.RaptorRuntimeException;
import org.openecomp.portalsdk.analytics.error.ReportSQLException;
import org.openecomp.portalsdk.analytics.util.DataSet;
import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;

public class RemDbUtils /* implements IDbUtils */{

	static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(RemDbUtils.class);

	

	public RemDbUtils() {
	}

	public static Connection getConnection(String dbKey) throws ReportSQLException {
		return Globals.getRDbUtils().getRemoteConnection(dbKey);
	} // getConnection

	public static void clearConnection(Connection con) throws ReportSQLException {
		try {
        if ((con != null) && !con.isClosed()) 
		 Globals.getRDbUtils().clearConnection(con);
		} catch (SQLException ex) { throw new ReportSQLException(ex.getMessage()); }
		
	} // clearConnection

	public static Connection startTransaction(String dbKey) throws RaptorException {
		Connection con = null;
		try { 
			con = getConnection(dbKey);
			con.setAutoCommit(false);
		} catch (SQLException ex) {
			throw new RaptorRuntimeException (ex.getMessage(), ex.getCause());
		} catch (Exception ex2 ) {
			throw new RaptorException (ex2.getMessage(), ex2.getCause());
		}
		return con;
	} // startTransaction

	public static void commitTransaction(Connection con) throws RaptorException {
		try {
			con.commit();
		} catch (SQLException ex) {
			throw new RaptorRuntimeException (ex.getMessage(), ex.getCause());
		} catch (Exception ex2 ) {
			throw new RaptorException (ex2.getMessage(), ex2.getCause());
		}
		//clearConnection(con);
	} // commitTransaction

	public static void rollbackTransaction(Connection con) throws RaptorException {
		try { 
			 con.rollback();
			 clearConnection(con);
			} catch (SQLException ex) {
				throw new RaptorRuntimeException (ex.getMessage(), ex.getCause());
			} catch (Exception ex2 ) {
				throw new RaptorException (ex2.getMessage(), ex2.getCause());
			}
	} // rollbackTransaction

//	public static String executeCall(Connection con, String sql, boolean expectResult)
//			throws ReportSQLException, Exception {
//		String result = null;
//
//		try {
//            if(con.isClosed()) con = getConnection();            
//			Log.write("[SQL Call] " + sql, 4);
//			CallableStatement stmt = con.prepareCall(sql);
//			if (expectResult)
//				stmt.registerOutParameter(1, Types.CHAR);
//			stmt.executeUpdate();
//			if (expectResult)
//				result = stmt.getString(1);
//			stmt.close();
//            con.commit();            
//		} catch (SQLException e) {
//			throw new ReportSQLException(e.getMessage(), sql);
//		} finally {
//            clearConnection(con);
//        }
//
//		return result;
//	} // executeCall
//
//	public static String executeCall(String sql, boolean expectResult)
//			throws ReportSQLException, Exception {
//		Connection con = null;
//		try {
//			con = getConnection();
//			String result = executeCall(con, sql, expectResult);
//			return result;
//		} catch (SQLException e) {
//			throw new ReportSQLException(e.getMessage(), sql);
//		} 
//	} // executeCall
//
//	public static int executeUpdate(Connection con, String sql) throws ReportSQLException, Exception {
//		try {
//            if(con.isClosed()) con = getConnection();            
//			Statement stmt = con.createStatement();
//
//			int rcode = -1;
//			try {
//				Log.write("[SQL Update] " + sql, 4);
//				rcode = stmt.executeUpdate(sql);
//                stmt.close();                
//                con.commit();                
//			} finally {
//                clearConnection(con);
//			}
//
//			return rcode;
//		} catch (SQLException e) {
//			throw new ReportSQLException(e.getMessage(), sql);
//		} finally {
//            clearConnection(con);
//        }
//	} // executeUpdate
//
//	public static int executeUpdate(String sql) throws ReportSQLException, Exception {
//		Connection con = null;
//		try {
//			con = getConnection();
//			int rcode = executeUpdate(con, sql);
//			return rcode;
//		} catch (SQLException e) {
//			throw new ReportSQLException(e.getMessage(), sql);
//		} 
//	} // executeUpdate

	public static DataSet executeQuery(Connection con, String sql, String dbKey) throws ReportSQLException, Exception {
		return executeQuery(con, sql, Integer.MAX_VALUE,dbKey);
	} // executeQuery

	public static DataSet executeQuery(Connection con, String sql, int maxRowLimit, String dbKey)
			throws ReportSQLException {
		try {
			if (con==null || con.isClosed())  con = getConnection(dbKey); 
			if(con==null) throw new ReportSQLException("Remote Connection not configured for "+ dbKey);
			Statement stmt = con.createStatement();
			logger.debug(EELFLoggerDelegate.debugLogger, ("[SQL CALL FROM RAPTOR] [SQL] " + sql));
			ResultSet rs = stmt.executeQuery(sql);
			DataSet ds = new DataSet(rs, maxRowLimit);
			   if(rs!=null) 
					rs.close();
			   if(stmt!=null)
				stmt.close();

			return ds;
		} catch (SQLException e) {
			throw new ReportSQLException(e.getMessage(), sql, e.getCause());
		} 
	} // executeQuery

	public static DataSet executeQuery(String sql,String dbKey) throws ReportSQLException {
		return executeQuery(sql, Integer.MAX_VALUE, dbKey);
	} // executeQuery

	public static DataSet executeQuery(String sql, int maxRowLimit, String dbKey) throws ReportSQLException{
		Connection con = null;
		try {
			con = getConnection(dbKey);
			return executeQuery(con, sql, maxRowLimit,dbKey);
		} catch (ReportSQLException e) {
                throw new ReportSQLException(e.getMessage(), sql, e.getCause());
        } finally {
            clearConnection(con);
        }
	} // executeQuery

} // DbUtils