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