source: main/branches/stable_gs3/greenstone3/src/java/org/greenstone/gsdl3/sql/derby/DerbyDBWrapper.java@ 21960

Last change on this file since 21960 was 21960, checked in by xiao, 14 years ago

changed all logger.debug to logger.error

File size: 6.4 KB
Line 
1package org.greenstone.gsdl3.sql.derby;
2
3import org.greenstone.gsdl3.sql.*;
4
5
6import java.sql.Connection;
7import java.sql.ResultSet;
8import java.sql.ResultSetMetaData;
9import java.sql.SQLException;
10import java.sql.SQLWarning;
11import java.sql.Statement;
12
13import java.util.ArrayList;
14import java.util.HashMap;
15import java.util.Iterator;
16
17
18import org.greenstone.gsdl3.util.DBInfo;
19
20import org.apache.log4j.*;
21
22public 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 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 = (HashMap)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.error("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.error("Database Error occured when creating a statement object",sqle);
87 return false;
88 }
89 return true;
90 }
91
92 public synchronized ArrayList executeQuery(String query_statement){
93 //the database hasn't been correct yet
94 ArrayList results = new ArrayList();
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 arow = new HashMap();
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.error("Database Error occured when executeQuery " + query_statement,sqle);
113 return results;
114 }
115 catch(Exception e){
116 logger.error(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.error(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.error(" 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.error("Database Error occured when execute query " + stat, sqle);
169 return false;
170 }
171 catch(Exception e){
172 logger.error(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.error("sql stat="+stat+ " result="+rs);
193 }
194 catch(SQLException sqle){
195 logger.error("Database Error occured when execute query " + stat, sqle);
196 return false;
197 }
198 catch(Exception e){
199 logger.error("Exception="+e);
200 return false;
201 }
202 logger.error(" 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.error("Database Error occured when close connection " + databasepath, sqle);
225 }
226 }
227
228 }
Note: See TracBrowser for help on using the repository browser.