1 | package org.greenstone.gsdl3.sql.derby;
|
---|
2 |
|
---|
3 | import org.greenstone.gsdl3.sql.*;
|
---|
4 |
|
---|
5 |
|
---|
6 | import java.sql.Connection;
|
---|
7 | import java.sql.ResultSet;
|
---|
8 | import java.sql.ResultSetMetaData;
|
---|
9 | import java.sql.SQLException;
|
---|
10 | import java.sql.SQLWarning;
|
---|
11 | import java.sql.Statement;
|
---|
12 |
|
---|
13 | import java.util.ArrayList;
|
---|
14 | import java.util.HashMap;
|
---|
15 | import java.util.Iterator;
|
---|
16 |
|
---|
17 |
|
---|
18 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
19 |
|
---|
20 | import org.apache.log4j.*;
|
---|
21 |
|
---|
22 | public class DerbyDBWrapper implements MetadataDBWrapper{
|
---|
23 | protected SQLServer sqlServer = null;
|
---|
24 | protected SQLStatements sqlState = null;
|
---|
25 | protected Connection connection = null;
|
---|
26 | protected static Logger logger = Logger.getLogger(org.greenstone.gsdl3.sql.derby.DerbyDBWrapper.class.getName());
|
---|
27 |
|
---|
28 |
|
---|
29 | public DBInfo getInfo(String id){
|
---|
30 | if(sqlState == null) return null;
|
---|
31 | String dbInfoState = sqlState.getDBInfoStatement(id);
|
---|
32 | ArrayList<HashMap<String, Object>> result = executeQuery(dbInfoState);
|
---|
33 | if (result.size() == 0) return null;
|
---|
34 | DBInfo info = new DBInfo();
|
---|
35 | for(int i=0; i<result.size();i++){
|
---|
36 | HashMap arow = result.get(i);
|
---|
37 | Iterator ite = arow.keySet().iterator();
|
---|
38 | while(ite.hasNext()){
|
---|
39 | String key = (String)ite.next();
|
---|
40 | Object value = arow.get(key);
|
---|
41 | //logger.info(key + "=>" +value);
|
---|
42 | if (value == null || value.equals("")) continue;
|
---|
43 | info.addInfo(key.toLowerCase(),sqlState.undoDBSafe(value));
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | return info;
|
---|
48 | }
|
---|
49 |
|
---|
50 | public void setSQLServer(SQLServer server){
|
---|
51 | sqlServer = server;
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void setSQLStatements(SQLStatements state){
|
---|
55 | sqlState = state;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public boolean openConnection(String databasepath){
|
---|
59 | if(connection != null) return true;
|
---|
60 |
|
---|
61 | connection = sqlServer.connect(databasepath);
|
---|
62 | if (connection == null) return false;
|
---|
63 | try{
|
---|
64 | connection.setAutoCommit(true);
|
---|
65 | }
|
---|
66 | catch(SQLException sqle){
|
---|
67 | logger.debug("Database Error occured when creating a statement object",sqle);
|
---|
68 | return false;
|
---|
69 | }
|
---|
70 | return true;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public boolean openAndCreateConnection(String databasepath){
|
---|
74 | if(connection != null) return true;
|
---|
75 | connection = sqlServer.connectAndCreate(databasepath);
|
---|
76 |
|
---|
77 | if (connection == null) {
|
---|
78 | logger.error("sql connection is null. database path="+databasepath);
|
---|
79 | return false;
|
---|
80 | }
|
---|
81 |
|
---|
82 | try{
|
---|
83 | connection.setAutoCommit(true);
|
---|
84 | }
|
---|
85 | catch(SQLException sqle){
|
---|
86 | logger.debug("Database Error occured when creating a statement object",sqle);
|
---|
87 | return false;
|
---|
88 | }
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 |
|
---|
92 | public synchronized ArrayList<HashMap<String, Object>> executeQuery(String query_statement){
|
---|
93 | //the database hasn't been correct yet
|
---|
94 | ArrayList<HashMap<String, Object>> results = new ArrayList<HashMap<String, Object>>();
|
---|
95 | ResultSet rs = null;
|
---|
96 | try{
|
---|
97 | //by passing the two arguments, the ResultSet returned from executeQuery is updatable
|
---|
98 | Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
|
---|
99 | rs = statement.executeQuery(query_statement);
|
---|
100 | ResultSetMetaData rsmd = rs.getMetaData();
|
---|
101 | int numOfColumns = rsmd.getColumnCount();
|
---|
102 | while(rs.next()){
|
---|
103 | HashMap<String, Object> arow = new HashMap<String, Object>();
|
---|
104 | for(int i = 1; i <= numOfColumns ; i++){
|
---|
105 | arow.put(rsmd.getColumnName(i).toLowerCase(), rs.getObject(i));
|
---|
106 | }
|
---|
107 | results.add(arow);
|
---|
108 | }
|
---|
109 | statement.close();
|
---|
110 | }
|
---|
111 | catch(SQLException sqle){
|
---|
112 | logger.debug("Database Error occured when executeQuery " + query_statement,sqle);
|
---|
113 | return results;
|
---|
114 | }
|
---|
115 | catch(Exception e){
|
---|
116 | logger.debug(e);
|
---|
117 | return null;
|
---|
118 | }
|
---|
119 | return results;
|
---|
120 | }
|
---|
121 | /**
|
---|
122 | * Same as executeQuery except the return type is ResultSet
|
---|
123 | * @param stat
|
---|
124 | * @return ResultSet of querying the statement 'stat'
|
---|
125 | */
|
---|
126 | public synchronized ResultSet queryResultSet(String stat){
|
---|
127 | ResultSet rs = null;
|
---|
128 | //by passing the two arguments, the ResultSet returned from executeQuery is updatable
|
---|
129 |
|
---|
130 | try{
|
---|
131 | Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
|
---|
132 | if (statement == null){
|
---|
133 | logger.info("Null sql statement provided.");
|
---|
134 | return null;
|
---|
135 | }
|
---|
136 | logger.debug(stat);
|
---|
137 | rs = statement.executeQuery(stat);
|
---|
138 | logger.info("sql stat="+stat+ " result="+rs);
|
---|
139 | }
|
---|
140 | catch(SQLException sqle){
|
---|
141 | logger.info("Database Error occured when execute query " + stat, sqle);
|
---|
142 | return null;
|
---|
143 | }
|
---|
144 | catch(Exception e){
|
---|
145 | logger.info("Exception="+e);
|
---|
146 | return null;
|
---|
147 | }
|
---|
148 | logger.debug(" result="+rs);
|
---|
149 | return rs;
|
---|
150 | }
|
---|
151 | /**
|
---|
152 | * Used by create (table)
|
---|
153 | */
|
---|
154 | public synchronized boolean execute(String stat) {
|
---|
155 | boolean rs;
|
---|
156 |
|
---|
157 | try{
|
---|
158 | Statement statement = connection.createStatement();
|
---|
159 | if (statement == null){
|
---|
160 | logger.info("statement is null.");
|
---|
161 | return false;
|
---|
162 | }
|
---|
163 |
|
---|
164 | rs = statement.execute(stat);
|
---|
165 | statement.close();
|
---|
166 | }
|
---|
167 | catch(SQLException sqle){
|
---|
168 | logger.debug("Database Error occured when execute query " + stat, sqle);
|
---|
169 | return false;
|
---|
170 | }
|
---|
171 | catch(Exception e){
|
---|
172 | logger.debug(e);
|
---|
173 | return false;
|
---|
174 | }
|
---|
175 | return rs;
|
---|
176 | }
|
---|
177 | /**
|
---|
178 | * Used by insert, update, and delete
|
---|
179 | */
|
---|
180 | public synchronized boolean executeUpdate(String stat) {
|
---|
181 | int rs;
|
---|
182 | try{
|
---|
183 | Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
|
---|
184 | if (statement == null){
|
---|
185 | logger.info("statement is null.");
|
---|
186 | return false;
|
---|
187 | }
|
---|
188 |
|
---|
189 | //either the row count of INSERT, UPDATE OR DELETE statement, or 0 if the statement returns nothing
|
---|
190 | rs = statement.executeUpdate(stat);
|
---|
191 | statement.close();
|
---|
192 | logger.debug("sql stat="+stat+ " result="+rs);
|
---|
193 | }
|
---|
194 | catch(SQLException sqle){
|
---|
195 | logger.debug("Database Error occured when execute query " + stat, sqle);
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 | catch(Exception e){
|
---|
199 | logger.debug("Exception="+e);
|
---|
200 | return false;
|
---|
201 | }
|
---|
202 | logger.debug(" result="+rs);
|
---|
203 | return (rs==-1)? false : true;
|
---|
204 | }
|
---|
205 | /**
|
---|
206 | * Used by checking the existence of table
|
---|
207 | * Same as the method 'execute' except throws SQLException
|
---|
208 | */
|
---|
209 | public void check4Table(String stat) throws SQLException {
|
---|
210 | Statement statement = connection.createStatement();
|
---|
211 | statement.executeQuery(stat);
|
---|
212 | statement.close();
|
---|
213 | }
|
---|
214 |
|
---|
215 | public void closeConnection(String databasepath){
|
---|
216 | try{
|
---|
217 | if(connection != null){
|
---|
218 | connection.close();
|
---|
219 | connection = null;
|
---|
220 | sqlServer.disconnect(databasepath);
|
---|
221 | }
|
---|
222 | }
|
---|
223 | catch(SQLException sqle){
|
---|
224 | logger.debug("Database Error occured when close connection " + databasepath, sqle);
|
---|
225 | }
|
---|
226 | }
|
---|
227 |
|
---|
228 | } |
---|