001
014
015 package com.liferay.portal.kernel.dao.db;
016
017 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.StringUtil;
021
022 import java.io.IOException;
023
024 import java.sql.Connection;
025 import java.sql.DatabaseMetaData;
026 import java.sql.PreparedStatement;
027 import java.sql.ResultSet;
028 import java.sql.ResultSetMetaData;
029 import java.sql.SQLException;
030
031 import javax.naming.NamingException;
032
033
037 public abstract class BaseDBProcess implements DBProcess {
038
039 public BaseDBProcess() {
040 }
041
042 @Override
043 public void runSQL(Connection connection, String template)
044 throws IOException, SQLException {
045
046 DB db = DBManagerUtil.getDB();
047
048 db.runSQL(connection, template);
049 }
050
051 @Override
052 public void runSQL(String template) throws IOException, SQLException {
053 DB db = DBManagerUtil.getDB();
054
055 if (connection == null) {
056 db.runSQL(template);
057 }
058 else {
059 db.runSQL(connection, template);
060 }
061 }
062
063 @Override
064 public void runSQL(String[] templates) throws IOException, SQLException {
065 DB db = DBManagerUtil.getDB();
066
067 if (connection == null) {
068 db.runSQL(templates);
069 }
070 else {
071 db.runSQL(connection, templates);
072 }
073 }
074
075 @Override
076 public void runSQLTemplate(String path)
077 throws IOException, NamingException, SQLException {
078
079 DB db = DBManagerUtil.getDB();
080
081 db.runSQLTemplate(path);
082 }
083
084 @Override
085 public void runSQLTemplate(String path, boolean failOnError)
086 throws IOException, NamingException, SQLException {
087
088 DB db = DBManagerUtil.getDB();
089
090 db.runSQLTemplate(path, failOnError);
091 }
092
093 @Override
094 public void runSQLTemplateString(
095 String template, boolean evaluate, boolean failOnError)
096 throws IOException, NamingException, SQLException {
097
098 DB db = DBManagerUtil.getDB();
099
100 if (connection == null) {
101 db.runSQLTemplateString(template, evaluate, failOnError);
102 }
103 else {
104 db.runSQLTemplateString(
105 connection, template, evaluate, failOnError);
106 }
107 }
108
109 protected boolean doHasTable(String tableName) throws Exception {
110 PreparedStatement ps = null;
111 ResultSet rs = null;
112
113 try {
114 DatabaseMetaData metadata = connection.getMetaData();
115
116 rs = metadata.getTables(null, null, tableName, null);
117
118 while (rs.next()) {
119 return true;
120 }
121 }
122 finally {
123 DataAccess.cleanUp(ps, rs);
124 }
125
126 return false;
127 }
128
129 protected boolean hasColumn(String tableName, String columnName)
130 throws Exception {
131
132 PreparedStatement ps = null;
133 ResultSet rs = null;
134
135 try {
136 ps = connection.prepareStatement("select * from " + tableName);
137
138 rs = ps.executeQuery();
139
140 ResultSetMetaData rsmd = rs.getMetaData();
141
142 for (int i = 0; i < rsmd.getColumnCount(); i++) {
143 String curColumnName = rsmd.getColumnName(i + 1);
144
145 if (StringUtil.equalsIgnoreCase(curColumnName, columnName)) {
146 return true;
147 }
148 }
149 }
150 catch (Exception e) {
151 _log.error(e, e);
152 }
153 finally {
154 DataAccess.cleanUp(ps, rs);
155 }
156
157 return false;
158 }
159
160 protected boolean hasRows(String tableName) throws Exception {
161 PreparedStatement ps = null;
162 ResultSet rs = null;
163
164 try {
165 ps = connection.prepareStatement(
166 "select count(*) from " + tableName);
167
168 rs = ps.executeQuery();
169
170 while (rs.next()) {
171 int count = rs.getInt(1);
172
173 if (count > 0) {
174 return true;
175 }
176 }
177 }
178 catch (Exception e) {
179 _log.error(e, e);
180 }
181 finally {
182 DataAccess.cleanUp(ps, rs);
183 }
184
185 return false;
186 }
187
188 protected boolean hasTable(String tableName) throws Exception {
189 if (doHasTable(StringUtil.toLowerCase(tableName)) ||
190 doHasTable(StringUtil.toUpperCase(tableName)) ||
191 doHasTable(tableName)) {
192
193 return true;
194 }
195
196 return false;
197 }
198
199 protected Connection connection;
200
201 private static final Log _log = LogFactoryUtil.getLog(BaseDBProcess.class);
202
203 }