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  package org.waarp.common.database.model;
21  
22  import org.waarp.common.database.DbSession;
23  import org.waarp.common.database.exception.WaarpDatabaseNoConnectionException;
24  import org.waarp.common.database.exception.WaarpDatabaseNoDataException;
25  import org.waarp.common.database.exception.WaarpDatabaseSqlException;
26  import org.waarp.common.database.model.DbModelAbstract.DbTypeResolver;
27  
28  import java.sql.Connection;
29  import java.sql.SQLException;
30  
31  /**
32   * Interface for Database Model
33   * <p>
34   * This class is an interface for special functions that needs special
35   * implementations according to the
36   * database model used.
37   */
38  public interface DbModel {
39    /**
40     * @param server
41     * @param user
42     * @param passwd
43     *
44     * @return a connection according to the underlying Database Model
45     *
46     * @throws SQLException
47     */
48    Connection getDbConnection(String server, String user, String passwd)
49        throws SQLException;
50  
51    /**
52     * Release any internal resources if needed
53     */
54    void releaseResources();
55  
56    /**
57     * @return the number of Pooled Connections if any
58     */
59    int currentNumberOfPooledConnections();
60  
61    /**
62     * @return the current DbType used
63     */
64    DbType getDbType();
65  
66    /**
67     * @return the current DbTypeResolver
68     */
69    DbTypeResolver getDbTypeResolver();
70  
71    /**
72     * Create all necessary tables into the database
73     *
74     * @param session SQL session
75     *
76     * @throws WaarpDatabaseNoConnectionException
77     */
78    void createTables(DbSession session)
79        throws WaarpDatabaseNoConnectionException;
80  
81    /**
82     * Reset the sequence (example)
83     *
84     * @param session SQL session
85     *
86     * @throws WaarpDatabaseNoConnectionException
87     */
88    void resetSequence(DbSession session, long newvalue)
89        throws WaarpDatabaseNoConnectionException;
90  
91    /**
92     * @param dbSession
93     *
94     * @return The next unique specialId
95     */
96    long nextSequence(DbSession dbSession)
97        throws WaarpDatabaseNoConnectionException, WaarpDatabaseSqlException,
98               WaarpDatabaseNoDataException;
99  
100   /**
101    * Validate connection
102    *
103    * @param dbSession
104    *
105    * @throws WaarpDatabaseNoConnectionException
106    */
107   void validConnection(DbSession dbSession)
108       throws WaarpDatabaseNoConnectionException;
109 
110   /**
111    * Add a limit on the request to get the "limit" first rows. Note that it
112    * must
113    * be compatible to add the
114    * "limit" condition.<br>
115    * <b>DO NOT CHANGE (add) ANYTHING to the request</b><br>
116    * <p>
117    * On Oracle: select allfield from (request) where rownnum <= limit<br>
118    * On others: request LIMIT limit<br>
119    *
120    * @param allfields string representing the equivalent to "*" in
121    *     "select
122    *     *" but more precisely as "field1,
123    *     field2" in "select field1, field2"
124    * @param request
125    * @param limit
126    *
127    * @return the new request String which will limit the result to the
128    *     specified
129    *     number of rows
130    */
131   String limitRequest(String allfields, String request, int limit);
132 
133   /**
134    * Upgrade Database from version
135    *
136    * @param session
137    * @param version
138    *
139    * @return True if the Database is upgraded
140    *
141    * @throws WaarpDatabaseNoConnectionException
142    */
143   boolean upgradeDb(DbSession session, String version)
144       throws WaarpDatabaseNoConnectionException;
145 
146   /**
147    * Check if Database is ok from version
148    *
149    * @param session
150    * @param version
151    * @param tryFix True will imply a try to fix if possible
152    *
153    * @return True if the Database needs an upgrade
154    *
155    * @throws WaarpDatabaseNoConnectionException
156    */
157   boolean needUpgradeDb(DbSession session, String version, boolean tryFix)
158       throws WaarpDatabaseNoConnectionException;
159 }