View Javadoc
1   /*
2    * This file is part of Waarp Project (named also Waarp or GG).
3    *
4    *  Copyright (c) 2019, Waarp SAS, and individual contributors by the @author
5    *  tags. See the COPYRIGHT.txt in the distribution for a full listing of
6    * individual contributors.
7    *
8    *  All Waarp Project is free software: you can redistribute it and/or
9    * modify it under the terms of the GNU General Public License as published by
10   * the Free Software Foundation, either version 3 of the License, or (at your
11   * option) any later version.
12   *
13   * Waarp is distributed in the hope that it will be useful, but WITHOUT ANY
14   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15   * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License along with
18   * Waarp . If not, see <http://www.gnu.org/licenses/>.
19   */
20  
21  package org.waarp.openr66.dao.database;
22  
23  import org.waarp.common.database.ConnectionFactory;
24  import org.waarp.common.database.properties.DbProperties;
25  import org.waarp.common.database.properties.H2Properties;
26  import org.waarp.common.database.properties.MariaDBProperties;
27  import org.waarp.common.database.properties.MySQLProperties;
28  import org.waarp.common.database.properties.OracleProperties;
29  import org.waarp.common.database.properties.PostgreSQLProperties;
30  import org.waarp.common.logging.WaarpLogger;
31  import org.waarp.common.logging.WaarpLoggerFactory;
32  import org.waarp.openr66.dao.DAOFactory;
33  import org.waarp.openr66.dao.database.h2.H2TransferDAO;
34  import org.waarp.openr66.dao.database.mariadb.MariaDBTransferDAO;
35  import org.waarp.openr66.dao.database.mysql.MySqlDBTransferDAO;
36  import org.waarp.openr66.dao.database.oracle.OracleTransferDAO;
37  import org.waarp.openr66.dao.database.postgres.PostgreSQLTransferDAO;
38  import org.waarp.openr66.dao.exception.DAOConnectionException;
39  
40  import java.sql.Array;
41  import java.sql.Blob;
42  import java.sql.CallableStatement;
43  import java.sql.Clob;
44  import java.sql.Connection;
45  import java.sql.DatabaseMetaData;
46  import java.sql.NClob;
47  import java.sql.PreparedStatement;
48  import java.sql.SQLClientInfoException;
49  import java.sql.SQLException;
50  import java.sql.SQLWarning;
51  import java.sql.SQLXML;
52  import java.sql.Savepoint;
53  import java.sql.Statement;
54  import java.sql.Struct;
55  import java.util.Map;
56  import java.util.Properties;
57  import java.util.concurrent.Executor;
58  
59  /**
60   * DAOFactory for standard SQL databases
61   */
62  public class DBDAOFactory extends DAOFactory {
63  
64    private static final String DATA_ACCESS_ERROR = "data access error";
65  
66    private static final WaarpLogger logger =
67        WaarpLoggerFactory.getLogger(DBDAOFactory.class);
68  
69    private final ConnectionFactory connectionFactory;
70  
71    private final FakeConnection fakeConnection;
72  
73    public DBDAOFactory(final ConnectionFactory factory) {
74      connectionFactory = factory;
75      fakeConnection = new FakeConnection(connectionFactory);
76    }
77  
78    public ConnectionFactory getConnectionFactory() {
79      return connectionFactory;
80    }
81  
82    @Override
83    public int getMaxConnections() {
84      return connectionFactory.getMaxConnections();
85    }
86  
87    @Override
88    public DBBusinessDAO getBusinessDAO(final boolean isCacheable)
89        throws DAOConnectionException {
90      try {
91        if (isCacheable) {
92          return new DBBusinessDAO(fakeConnection);
93        }
94        return new DBBusinessDAO(connectionFactory.getConnection());
95      } catch (final SQLException e) {
96        throw new DAOConnectionException(DATA_ACCESS_ERROR, e);
97      }
98    }
99  
100   @Override
101   public DBHostDAO getHostDAO(final boolean isCacheable)
102       throws DAOConnectionException {
103     try {
104       if (isCacheable) {
105         return new DBHostDAO(fakeConnection);
106       }
107       return new DBHostDAO(connectionFactory.getConnection());
108     } catch (final SQLException e) {
109       throw new DAOConnectionException(DATA_ACCESS_ERROR, e);
110     }
111   }
112 
113   @Override
114   public DBLimitDAO getLimitDAO(final boolean isCacheable)
115       throws DAOConnectionException {
116     try {
117       if (isCacheable) {
118         return new DBLimitDAO(fakeConnection);
119       }
120       return new DBLimitDAO(connectionFactory.getConnection());
121     } catch (final SQLException e) {
122       throw new DAOConnectionException(DATA_ACCESS_ERROR, e);
123     }
124   }
125 
126   @Override
127   public DBMultipleMonitorDAO getMultipleMonitorDAO(final boolean isCacheable)
128       throws DAOConnectionException {
129     try {
130       if (isCacheable) {
131         return new DBMultipleMonitorDAO(fakeConnection);
132       }
133       return new DBMultipleMonitorDAO(connectionFactory.getConnection());
134     } catch (final SQLException e) {
135       throw new DAOConnectionException(DATA_ACCESS_ERROR, e);
136     }
137   }
138 
139   @Override
140   public DBRuleDAO getRuleDAO(final boolean isCacheable)
141       throws DAOConnectionException {
142     try {
143       if (isCacheable) {
144         return new DBRuleDAO(fakeConnection);
145       }
146       return new DBRuleDAO(connectionFactory.getConnection());
147     } catch (final SQLException e) {
148       throw new DAOConnectionException(DATA_ACCESS_ERROR, e);
149     }
150   }
151 
152   @Override
153   public DBTransferDAO getTransferDAO() throws DAOConnectionException {
154     try {
155       final DbProperties prop = connectionFactory.getProperties();
156       if (prop instanceof H2Properties) {
157         return new H2TransferDAO(connectionFactory.getConnection());
158       } else if (prop instanceof MariaDBProperties) {
159         return new MariaDBTransferDAO(connectionFactory.getConnection());
160       } else if (prop instanceof MySQLProperties) {
161         return new MySqlDBTransferDAO(connectionFactory.getConnection());
162       } else if (prop instanceof OracleProperties) {
163         return new OracleTransferDAO(connectionFactory.getConnection());
164       } else if (prop instanceof PostgreSQLProperties) {
165         return new PostgreSQLTransferDAO(connectionFactory.getConnection());
166       } else {
167         throw new DAOConnectionException("Unsupported database");
168       }
169     } catch (final SQLException e) {
170       throw new DAOConnectionException(DATA_ACCESS_ERROR, e);
171     }
172   }
173 
174   @Override
175   public String getLimitRequest(final String request, final int limit,
176                                 final int offset) {
177     final DbProperties prop = connectionFactory.getProperties();
178     String result = request;
179     final boolean isOracle = prop instanceof OracleProperties;
180     if (offset > 0) {
181       if (isOracle) {
182         result += " OFFSET " + limit + " ROWS";
183       } else {
184         result += " OFFSET " + limit;
185       }
186     }
187     if (limit > 0) {
188       if (isOracle) {
189         result += " FETCH NEXT " + limit + " ROWS ONLY";
190       } else {
191         result += " LIMIT " + limit;
192       }
193     }
194     return result;
195   }
196 
197   /**
198    * Close the DBDAOFactory and close the ConnectionFactory Warning: You need
199    * to close the Connection yourself!
200    */
201   public void close() {
202     logger.debug("Closing DAOFactory.");
203     logger.debug("Closing factory ConnectionFactory.");
204     connectionFactory.close();
205   }
206 
207   /**
208    * Fake Connection to allow to setup a real connection for cached capability
209    * but not using cache due to lack of data in it
210    */
211   static class FakeConnection implements Connection {
212     private final ConnectionFactory connectionFactory;
213 
214     public FakeConnection(final ConnectionFactory connectionFactory) {
215       this.connectionFactory = connectionFactory;
216     }
217 
218     /**
219      * @return a newly created real connection
220      *
221      * @throws SQLException
222      */
223     public Connection getRealConnection() throws SQLException {
224       return connectionFactory.getConnection();
225     }
226 
227     @Override
228     public Statement createStatement() throws SQLException {
229       return null;
230     }
231 
232     @Override
233     public PreparedStatement prepareStatement(final String s)
234         throws SQLException {
235       return null;
236     }
237 
238     @Override
239     public CallableStatement prepareCall(final String s) throws SQLException {
240       return null;
241     }
242 
243     @Override
244     public String nativeSQL(final String s) throws SQLException {
245       return null;
246     }
247 
248     @Override
249     public void setAutoCommit(final boolean b) throws SQLException {
250       // Emoty
251     }
252 
253     @Override
254     public boolean getAutoCommit() throws SQLException {
255       return false;
256     }
257 
258     @Override
259     public void commit() throws SQLException {
260       // Emoty
261     }
262 
263     @Override
264     public void rollback() throws SQLException {
265       // Emoty
266     }
267 
268     @Override
269     public void close() throws SQLException {
270       // Emoty
271     }
272 
273     @Override
274     public boolean isClosed() throws SQLException {
275       return false;
276     }
277 
278     @Override
279     public DatabaseMetaData getMetaData() throws SQLException {
280       return null;
281     }
282 
283     @Override
284     public void setReadOnly(final boolean b) throws SQLException {
285       // Emoty
286     }
287 
288     @Override
289     public boolean isReadOnly() throws SQLException {
290       return false;
291     }
292 
293     @Override
294     public void setCatalog(final String s) throws SQLException {
295       // Emoty
296     }
297 
298     @Override
299     public String getCatalog() throws SQLException {
300       return null;
301     }
302 
303     @Override
304     public void setTransactionIsolation(final int i) throws SQLException {
305       // Emoty
306     }
307 
308     @Override
309     public int getTransactionIsolation() throws SQLException {
310       return 0;
311     }
312 
313     @Override
314     public SQLWarning getWarnings() throws SQLException {
315       return null;
316     }
317 
318     @Override
319     public void clearWarnings() throws SQLException {
320       // Emoty
321     }
322 
323     @Override
324     public Statement createStatement(final int i, final int i1)
325         throws SQLException {
326       return null;
327     }
328 
329     @Override
330     public PreparedStatement prepareStatement(final String s, final int i,
331                                               final int i1)
332         throws SQLException {
333       return null;
334     }
335 
336     @Override
337     public CallableStatement prepareCall(final String s, final int i,
338                                          final int i1) throws SQLException {
339       return null;
340     }
341 
342     @Override
343     public Map<String, Class<?>> getTypeMap() throws SQLException {
344       return null;
345     }
346 
347     @Override
348     public void setTypeMap(final Map<String, Class<?>> map)
349         throws SQLException {
350       // Emoty
351     }
352 
353     @Override
354     public void setHoldability(final int i) throws SQLException {
355       // Emoty
356     }
357 
358     @Override
359     public int getHoldability() throws SQLException {
360       return 0;
361     }
362 
363     @Override
364     public Savepoint setSavepoint() throws SQLException {
365       return null;
366     }
367 
368     @Override
369     public Savepoint setSavepoint(final String s) throws SQLException {
370       return null;
371     }
372 
373     @Override
374     public void rollback(final Savepoint savepoint) throws SQLException {
375       // Emoty
376     }
377 
378     @Override
379     public void releaseSavepoint(final Savepoint savepoint)
380         throws SQLException {
381       // Emoty
382     }
383 
384     @Override
385     public Statement createStatement(final int i, final int i1, final int i2)
386         throws SQLException {
387       return null;
388     }
389 
390     @Override
391     public PreparedStatement prepareStatement(final String s, final int i,
392                                               final int i1, final int i2)
393         throws SQLException {
394       return null;
395     }
396 
397     @Override
398     public CallableStatement prepareCall(final String s, final int i,
399                                          final int i1, final int i2)
400         throws SQLException {
401       return null;
402     }
403 
404     @Override
405     public PreparedStatement prepareStatement(final String s, final int i)
406         throws SQLException {
407       return null;
408     }
409 
410     @Override
411     public PreparedStatement prepareStatement(final String s, final int[] ints)
412         throws SQLException {
413       return null;
414     }
415 
416     @Override
417     public PreparedStatement prepareStatement(final String s,
418                                               final String[] strings)
419         throws SQLException {
420       return null;
421     }
422 
423     @Override
424     public Clob createClob() throws SQLException {
425       return null;
426     }
427 
428     @Override
429     public Blob createBlob() throws SQLException {
430       return null;
431     }
432 
433     @Override
434     public NClob createNClob() throws SQLException {
435       return null;
436     }
437 
438     @Override
439     public SQLXML createSQLXML() throws SQLException {
440       return null;
441     }
442 
443     @Override
444     public boolean isValid(final int i) throws SQLException {
445       return false;
446     }
447 
448     @Override
449     public void setClientInfo(final String s, final String s1)
450         throws SQLClientInfoException {
451       // Emoty
452     }
453 
454     @Override
455     public void setClientInfo(final Properties properties)
456         throws SQLClientInfoException {
457       // Emoty
458     }
459 
460     @Override
461     public String getClientInfo(final String s) throws SQLException {
462       return null;
463     }
464 
465     @Override
466     public Properties getClientInfo() throws SQLException {
467       return null;
468     }
469 
470     @Override
471     public Array createArrayOf(final String s, final Object[] objects)
472         throws SQLException {
473       return null;
474     }
475 
476     @Override
477     public Struct createStruct(final String s, final Object[] objects)
478         throws SQLException {
479       return null;
480     }
481 
482     @Override
483     public void setSchema(final String s) throws SQLException {
484       // Emoty
485     }
486 
487     @Override
488     public String getSchema() throws SQLException {
489       return null;
490     }
491 
492     @Override
493     public void abort(final Executor executor) throws SQLException {
494       // Emoty
495     }
496 
497     @Override
498     public void setNetworkTimeout(final Executor executor, final int i)
499         throws SQLException {
500       // Emoty
501     }
502 
503     @Override
504     public int getNetworkTimeout() throws SQLException {
505       return 0;
506     }
507 
508     @Override
509     public <T> T unwrap(final Class<T> aClass) throws SQLException {
510       return null;
511     }
512 
513     @Override
514     public boolean isWrapperFor(final Class<?> aClass) throws SQLException {
515       return false;
516     }
517   }
518 }