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 | import org.greenstone.gsdl3.util.DBInfo;
|
---|
18 |
|
---|
19 | import org.apache.log4j.*;
|
---|
20 |
|
---|
21 | public class DerbyDBWrapper implements MetadataDBWrapper{
|
---|
22 | protected SQLServer sqlServer = null;
|
---|
23 | protected SQLStatements sqlState = null;
|
---|
24 | protected Connection connection = null;
|
---|
25 | protected Statement statement = 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 | connection = sqlServer.connect(databasepath);
|
---|
60 | if (connection == null) return false;
|
---|
61 |
|
---|
62 | try{
|
---|
63 | statement = connection.createStatement();
|
---|
64 | }
|
---|
65 | catch(SQLException sqle){
|
---|
66 | logger.error("Database Error occured when creating a statement object",sqle);
|
---|
67 | return false;
|
---|
68 | }
|
---|
69 | return true;
|
---|
70 | }
|
---|
71 |
|
---|
72 | //return a list of rows array->hashmap
|
---|
73 | public ArrayList executeQuery(String query_statement){
|
---|
74 | //the database hasn't been correct yet
|
---|
75 | if (statement == null){
|
---|
76 | logger.info("the database hasn't been correct yet");
|
---|
77 | return new ArrayList();
|
---|
78 | }
|
---|
79 |
|
---|
80 | ArrayList results = new ArrayList();
|
---|
81 |
|
---|
82 | try{
|
---|
83 | ResultSet rs = statement.executeQuery(query_statement);
|
---|
84 | ResultSetMetaData rsmd = rs.getMetaData();
|
---|
85 | int numOfColumns = rsmd.getColumnCount();
|
---|
86 | while(rs.next()){
|
---|
87 | HashMap arow = new HashMap();
|
---|
88 | for(int i = 1; i < numOfColumns ; i++){
|
---|
89 | arow.put(rsmd.getColumnName(i), rs.getObject(i));
|
---|
90 | }
|
---|
91 | results.add(arow);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | catch(SQLException sqle){
|
---|
95 | logger.error("Database Error occured when executeQuery " + query_statement,sqle);
|
---|
96 | return results;
|
---|
97 | }
|
---|
98 | return results;
|
---|
99 | }
|
---|
100 |
|
---|
101 | /** check for the existence of a table**/
|
---|
102 | public boolean check4Table (String check_table) {
|
---|
103 | try {
|
---|
104 | statement.execute(check_table);
|
---|
105 | } catch (SQLException sqle) {
|
---|
106 | String theError = (sqle).getSQLState();
|
---|
107 | if (theError.equals("42X05")) // Table does not exist
|
---|
108 | {
|
---|
109 | return false;
|
---|
110 | }
|
---|
111 | else{
|
---|
112 | return true;
|
---|
113 | }
|
---|
114 | }
|
---|
115 | // System.out.println("Just got the warning - table exists OK ");
|
---|
116 | return true;
|
---|
117 | }
|
---|
118 |
|
---|
119 | public void closeConnection(String databasepath){
|
---|
120 | sqlServer.disconnect(databasepath);
|
---|
121 | }
|
---|
122 |
|
---|
123 | } |
---|