View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.database;
17  
18  import java.io.UnsupportedEncodingException;
19  import java.sql.Connection;
20  import java.sql.SQLException;
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  
24  import javax.sql.DataSource;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.hibernate.util.StringHelper;
29  import org.seasar.framework.container.factory.SingletonS2ContainerFactory;
30  import org.seasar.tuigwaa.util.TgwResource;
31  
32  import com.isenshi.util.extlib.HSQLDBUtil;
33  
34  public class DatabaseUtils {
35  
36  	private static final String BACKUP_SQL_PREFIX = "SELECT * FROM ";
37  
38  	private static final String CLEAN_SQL_PREFIX = "DELETE FROM ";
39  
40  	private static final String RESTORE_SQL_PREFIX = "INSERT INTO ";
41  
42  	private static Log log_ = LogFactory.getLog(DatabaseUtils.class);
43  	
44  	private static final SimpleDateFormat SQL_TIMESTAMP_FORMAT = 
45  		new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
46  
47  	private static final SimpleDateFormat SQL_DATA_FORMAT = 
48  		new SimpleDateFormat("yyyy-MM-dd");	
49  
50  	public static String buildBackupQuery(String schema, String table) {
51  		StringBuffer buf = new StringBuffer(BACKUP_SQL_PREFIX);
52  		String fullTableName = (schema != null && schema.length() > 0) ? schema
53  				+ "." + table
54  				: table;
55  		buf.append(fullTableName);
56  		return buf.toString();
57  	}
58  
59  	public static String buildCleanQuery(String schema, String table) {
60  		StringBuffer buf = new StringBuffer(CLEAN_SQL_PREFIX);
61  		String fullTableName = (schema != null && schema.length() > 0) ? schema
62  				+ "." + table
63  				: table;
64  		buf.append(fullTableName);
65  		return buf.toString();
66  	}
67  
68  	public static String buildRestoreQuery(String schema, String table,
69  			String[] columns) {
70  		StringBuffer buf = new StringBuffer(RESTORE_SQL_PREFIX);
71  		String fullTableName = (schema != null && schema.length() > 0) ? schema
72  				+ "." + table
73  				: table;
74  		buf.append(fullTableName);
75  
76  		int size = 0;
77  		if (columns != null && (size = columns.length) > 0) {
78  			buf.append(" (");
79  			buf.append(StringHelper.join(",", columns));
80  			buf.append(" ) VALUES (");
81  			for (int i = 0; i < size; i++) {
82  				buf.append("?");
83  				if (i != size - 1 && size != 1)
84  					buf.append(",");
85  			}
86  			buf.append(")");
87  		}
88  		return buf.toString();
89  	}
90  
91  	
92  	public static void closeQueitly(Connection con){
93  		if(con != null){
94  			try {
95  				con.close();
96  			} catch (SQLException e) {
97  				//do nothing
98  			}
99  		}
100 	}
101 	
102 	public static String getJDBCURL() {
103 		if (isEmbededHsql()) {
104 			try {
105 				return HSQLDBUtil.getJDBCURL();
106 			} catch (UnsupportedEncodingException e) {
107 				e.printStackTrace();
108 				throw new RuntimeException(e);
109 			}
110 		} else {
111 			return TgwResource.getProperty("database.url");
112 		}
113 	}
114 
115 	public static void shutdown() {
116 		if (isEmbededHsql()) {
117 			DataSource dataSource = (DataSource) SingletonS2ContainerFactory
118 					.getContainer().getComponent("basedb.dataSource");
119 			try {
120 				HSQLDBUtil.shutdown(dataSource);
121 			} catch (SQLException e) {
122 				e.printStackTrace();
123 			}
124 		}
125 	}
126 
127 	public static boolean isEscapeClauseNeeded(){ // temporary
128 		
129 		String driver = getBasedbDriver();
130 		boolean flag = false;
131 		if("org.hsqldb.jdbcDriver".equals(driver) ||
132 			"com.ibm.db2.jcc.DB2Driver".equals(driver)){
133 			flag = true;
134 		}		
135 		return flag;		
136 	}
137 	
138 	private static boolean isEmbededHsql() {
139 		return "org.hsqldb.jdbcDriver".equals(getBasedbDriver());
140 	}	
141 	
142 	private static String getBasedbDriver(){
143 		return TgwResource.getProperty("database.driver");
144 	}
145 	
146 	public static Object[] bindArgs(String[] data, String[] type) {
147 
148 		if (data.length != type.length)
149 			return null;
150 
151 		int size = type.length;
152 		Object[] objs = new Object[size];
153 
154 		for (int i = 0; i < size; i++) {
155 			String className = type[i];
156 			Object o = null;
157 
158 			if (data[i] != null && !data[i].equals("")) {
159 
160 				if (className.equals("java.lang.Long")) {
161 					o = new Long(data[i]);
162 				} else if (className.equals("java.lang.Integer")) {
163 					o = new Integer(data[i]);
164 				} else if (className.equals("java.lang.Double")) {
165 					o = new Double(data[i]);
166 				} else if (className.equals("java.lang.String")) {
167 					o = data[i];
168 				} else if (className.equals("java.lang.Boolean")) {
169 					o = new Boolean(data[i]);
170 				} else if (className.equals("java.sql.Date")) {
171 					try {
172 						o = SQL_DATA_FORMAT.parse(data[i]);
173 					} catch (ParseException pe) {
174 						log_.error("failed to parse. " + pe.getMessage());
175 					}
176 				} else if (className.equals("java.sql.Timestamp")) {
177 					try {
178 						o = SQL_TIMESTAMP_FORMAT.parse(data[i]);
179 					} catch (ParseException pe) {
180 						log_.error("failed to parse. " + pe.getMessage());
181 					}
182 				} else {
183 					log_.error("unknown types " + className);
184 					// throws some exception...
185 				}
186 			}			
187 			objs[i] = o;
188 		}
189 		return objs;
190 	}	
191 	
192 }