1 /*******************************************************************************
2 * Copyright (c) 2017 hangum.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Lesser Public License v2.1
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
9 * hangum - initial API and implementation
10 ******************************************************************************/
11 package com
.hangum
.tadpole
.engine
.query
.surface
;
13 import java
.sql
.Connection
;
14 import java
.sql
.DatabaseMetaData
;
15 import java
.sql
.PreparedStatement
;
16 import java
.sql
.ResultSet
;
17 import java
.sql
.Statement
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
23 import org
.apache
.log4j
.Logger
;
25 import com
.hangum
.tadpole
.commons
.libs
.core
.define
.PublicTadpoleDefine
.SQL_STATEMENT_TYPE
;
26 import com
.hangum
.tadpole
.engine
.query
.dao
.mysql
.TableColumnDAO
;
27 import com
.hangum
.tadpole
.engine
.query
.dao
.mysql
.TableDAO
;
28 import com
.hangum
.tadpole
.engine
.query
.dao
.system
.UserDBDAO
;
29 import com
.hangum
.tadpole
.engine
.sql
.util
.PartQueryUtil
;
30 import com
.hangum
.tadpole
.engine
.sql
.util
.resultset
.QueryExecuteResultDTO
;
33 * JDBC 메타데이터를 이용하여 스키마, 테이블, 컬럼 정보를 요청합니다,
38 public abstract class BasicDBInfo
implements ConnectionInterfact
{
39 private static final Logger logger
= Logger
.getLogger(BasicDBInfo
.class);
41 public abstract Connection
getConnection(final UserDBDAO userDB
) throws Exception
;
48 public String
getKeyworkd(final UserDBDAO userDB
) throws Exception
{
49 String strKeyWord
= "";
51 Connection javaConn
= null;
53 javaConn
= getConnection(userDB
);
54 strKeyWord
= javaConn
.getMetaData().getSQLKeywords();
56 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
){}
69 public int executeUpdate(UserDBDAO userDB
, String sqlQuery
) throws Exception
{
70 Statement statement
= null;
72 Connection javaConn
= null;
74 javaConn
= getConnection(userDB
);
75 statement
= javaConn
.createStatement();
76 return statement
.executeUpdate(sqlQuery
);
78 try { if(statement
!= null) statement
.close(); } catch(Exception e
) {}
79 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
){}
87 * @param sql_STATEMENT_TYPE
88 * @param statementParameter
93 public QueryExecuteResultDTO
executeQueryPlan(UserDBDAO userDB
, String strQuery
, SQL_STATEMENT_TYPE sql_STATEMENT_TYPE
, Object
[] statementParameter
) throws Exception
{
94 return select(userDB
, PartQueryUtil
.makeExplainQuery(userDB
, strQuery
), statementParameter
, 1000);
106 public int executeUpdate(UserDBDAO userDB
, String string
, String name
) throws Exception
{
107 Statement statement
= null;
109 Connection javaConn
= null;
111 javaConn
= getConnection(userDB
);
112 String quoteString
= javaConn
.getMetaData().getIdentifierQuoteString();
114 statement
= javaConn
.createStatement();
115 return statement
.executeUpdate(String
.format(string
, quoteString
+ name
+ quoteString
));
117 try { if(statement
!= null) statement
.close(); } catch(Exception e
) {}
118 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
){}
127 public void connectionCheck(UserDBDAO userDB
) throws Exception
{
129 Connection javaConn
= null;
131 javaConn
= getConnection(userDB
);
132 DatabaseMetaData dbmd
= javaConn
.getMetaData();
134 dbmd
.getCatalogSeparator();
136 } catch(Exception e
) {
137 logger
.error("connection check", e
);
140 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
) {}
150 public List
<TableDAO
> tableList(UserDBDAO userDB
) throws Exception
{
151 List
<TableDAO
> showTables
= new ArrayList
<TableDAO
>();
154 Connection javaConn
= null;
156 javaConn
= getConnection(userDB
);
157 DatabaseMetaData dbmd
= javaConn
.getMetaData();
158 rs
= dbmd
.getTables(null, null, "%", null);
161 String strTBName
= rs
.getString("TABLE_NAME");
162 String strComment
= rs
.getString("REMARKS");
163 // logger.debug( rs.getString("TABLE_CAT") );
164 // logger.debug( rs.getString("TABLE_SCHEM") );
165 // logger.debug( rs.getString("TABLE_NAME") );
166 // logger.debug( rs.getString("TABLE_TYPE") );
167 // logger.debug( rs.getString("REMARKS") );
169 TableDAO tdao
= new TableDAO(strTBName
, strComment
);
170 showTables
.add(tdao
);
174 } catch(Exception e
) {
175 logger
.error("table list", e
);
178 try { if(rs
!= null) rs
.close(); } catch(Exception e
) {}
179 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
) {}
185 * table의 컬컴 정보를 리턴합니다.
191 public List
<TableColumnDAO
> tableColumnList(UserDBDAO userDB
, Map
<String
, String
> mapParam
) throws Exception
{
192 List
<TableColumnDAO
> showTableColumns
= new ArrayList
<TableColumnDAO
>();
194 ResultSet rsPrimaryKey
= null;
196 Map
<String
, String
> mapPrimaryKey
= new HashMap
<String
, String
>();
198 Connection javaConn
= null;
200 javaConn
= getConnection(userDB
);
201 DatabaseMetaData dbmd
= javaConn
.getMetaData();
202 rs
= dbmd
.getColumns(null, null, mapParam
.get("table"), null);
204 rsPrimaryKey
= dbmd
.getPrimaryKeys(null, null, mapParam
.get("table"));
205 while(rsPrimaryKey
.next()) {
206 mapPrimaryKey
.put(rsPrimaryKey
.getString("COLUMN_NAME"), "PRI");
210 TableColumnDAO tcDAO
= new TableColumnDAO();
211 tcDAO
.setName(rs
.getString("COLUMN_NAME"));
212 if(mapPrimaryKey
.containsKey(tcDAO
.getName())) {
215 tcDAO
.setType(rs
.getString("TYPE_NAME"));
216 tcDAO
.setNull(rs
.getString("IS_NULLABLE"));
217 tcDAO
.setComment(rs
.getString("REMARKS"));
219 showTableColumns
.add(tcDAO
);
222 return showTableColumns
;
223 } catch(Exception e
) {
224 logger
.error(mapParam
.get("table") + " table column", e
);
227 try { if(rsPrimaryKey
!= null) rsPrimaryKey
.close(); } catch(Exception e
) {}
228 try { if(rs
!= null) rs
.close(); } catch(Exception e
) {}
229 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
) {}
238 * @param requestQuery
239 * @param statementParameter
244 public QueryExecuteResultDTO
select(UserDBDAO userDB
, String requestQuery
, Object
[] statementParameter
, int limitCount
) throws Exception
{
245 if(logger
.isDebugEnabled()) logger
.debug("\t * Query is [ " + requestQuery
);
247 PreparedStatement pstmt
= null;
250 Connection javaConn
= null;
252 javaConn
= getConnection(userDB
);
253 pstmt
= javaConn
.prepareStatement(requestQuery
);
254 if(statementParameter
!= null) {
255 for (int i
=1; i
<=statementParameter
.length
; i
++) {
256 pstmt
.setObject(i
, statementParameter
[i
-1]);
259 rs
= pstmt
.executeQuery();
261 return new QueryExecuteResultDTO(userDB
, requestQuery
, true, rs
, limitCount
);
262 } catch(Exception e
) {
267 try { if(pstmt
!= null) pstmt
.close(); } catch(Exception e
) {}
268 try { if(rs
!= null) rs
.close(); } catch(Exception e
) {}
269 try { if(javaConn
!= null) javaConn
.close(); } catch(Exception e
){}