001package fr.ifremer.adagio.synchro.dao;
002
003/*
004 * #%L
005 * SIH-Adagio :: Synchronization
006 * $Id:$
007 * $HeadURL:$
008 * %%
009 * Copyright (C) 2012 - 2014 Ifremer
010 * %%
011 * This program is free software: you can redistribute it and/or modify
012 * it under the terms of the GNU Affero General Public License as published by
013 * the Free Software Foundation, either version 3 of the License, or
014 * (at your option) any later version.
015 * 
016 * This program is distributed in the hope that it will be useful,
017 * but WITHOUT ANY WARRANTY; without even the implied warranty of
018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
019 * GNU General Public License for more details.
020 * 
021 * You should have received a copy of the GNU Affero General Public License
022 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
023 * #L%
024 */
025
026import java.io.Closeable;
027import java.sql.Connection;
028import java.sql.ResultSet;
029import java.sql.SQLException;
030import java.sql.Timestamp;
031import java.util.Date;
032import java.util.List;
033import java.util.Map;
034import java.util.Set;
035
036import fr.ifremer.adagio.synchro.meta.SynchroTableMetadata;
037import fr.ifremer.adagio.synchro.service.SynchroPendingOperationBuffer;
038
039public interface SynchroTableDao extends Closeable {
040
041    Connection getConnection();
042
043    SynchroTableMetadata getTable();
044
045    void setPendingOperationBuffer(SynchroPendingOperationBuffer pendingChangesBuffer);
046
047    SynchroPendingOperationBuffer getPendingOperationBuffer();
048
049    /**
050     * Obtains the max update date found in table's rows
051     * 
052     * @return
053     * @throws SQLException
054     */
055    Timestamp getLastUpdateDate() throws SQLException;
056
057    /**
058     * Count the number of rows in the table
059     * 
060     * @return
061     * @throws SQLException
062     */
063    long count() throws SQLException;
064
065    /**
066     * Count the number of rows updated since the given date
067     * 
068     * @param fromDate
069     * @return
070     * @throws SQLException
071     */
072    long countDataToUpdate(Date fromDate) throws SQLException;
073
074    /**
075     * Retrieve a id by a remote id. Only used for data synchronization (not used in referential synchronization)
076     * 
077     * @param tableName
078     * @param remoteId
079     * @return the local id, or null if remote_id not found
080     */
081    Integer getIdFromRemoteId(String tableName, Integer remoteId) throws SQLException;
082
083    /**
084     * Obtains existing remoteIds
085     * 
086     * @return
087     * @throws SQLException
088     */
089    Map<Integer, Integer> getExistingRemoteIdsMap() throws SQLException;
090
091    /**
092     * Getting all row depending of parent FK values
093     * 
094     * @param fkColumnName
095     *            the FK column name
096     * @param fkValues
097     *            values of the FK columns
098     * @return
099     * @throws SQLException
100     */
101    ResultSet getDataByFk(String fkColumnName, Set<Integer> fkValues) throws SQLException;
102
103    /**
104     * Obtains all data updated since the given date (or all data if the given date is null
105     * 
106     * @param fromDate
107     * @return A Resulset with expected data.
108     * @throws SQLException
109     */
110    ResultSet getDataToUpdate(Date fromDate) throws SQLException;
111
112    /**
113     * Obtains all primary keys of a table.
114     * If more than one key in tha table, use a serialization {@link SynchroTableMetadata#toPkStr}
115     * 
116     * @return
117     * @throws SQLException
118     */
119    Set<String> getExistingPrimaryKeys() throws SQLException;
120
121    /**
122     * Obtains a row, using values of each PK
123     * 
124     * @param pk
125     *            values of PK column
126     * @return the existing row in the table
127     * @throws SQLException
128     */
129    Object[] findByPk(List<Object> pk) throws SQLException;
130
131    /**
132     * Obtains a PK from the current ResultSet's row
133     * 
134     * @param incomingData
135     * @param tableBuffer
136     * @return
137     * @throws SQLException
138     */
139    List<Object> getPk(ResultSet incomingData) throws SQLException;
140
141    /**
142     * Delete all rows of a table
143     * 
144     * @throws SQLException
145     */
146    void deleteAll() throws SQLException;
147
148    /**
149     * Insert into a table the current row of the given {@link ResultSet}
150     * 
151     * @param pk
152     *            the PK of the current row (only need for logging)
153     * @param incomingData
154     *            a {@link ResultSet} with a row ready to be read ({@link ResultSet#next()} should have been call
155     *            before)
156     * @throws SQLException
157     */
158    void executeInsert(ResultSet incomingData) throws SQLException;
159
160    /**
161     * Insert into a table the current row of the given incoming data
162     * <p/>
163     * Same as {@link #executeInsert(List,ResultSet)}, but using a array instead of a ResultSet.
164     * 
165     * @param incomingData
166     *            a array of object
167     * @throws SQLException
168     */
169    void executeInsert(Object[] row) throws SQLException;
170
171    Integer executeInsertAndReturnId(ResultSet incomingData) throws SQLException;
172
173    /**
174     * Update one table's row, using the current row of the given {@link ResultSet}
175     * 
176     * @param pk
177     *            the PK of the row to update
178     * @param incomingData
179     *            a {@link ResultSet} with a row ready to be read ({@link ResultSet#next()} should have been call
180     *            before)
181     * @throws SQLException
182     */
183    void executeUpdate(List<Object> pk, ResultSet incomingData) throws SQLException;
184
185    /**
186     * Update one table's row, using the current row of the given array of values
187     * <p/>
188     * Same as {@link #executeUpdate(List,ResultSet)}, but using a array instead of a ResultSet.
189     * 
190     * @param pk
191     *            the PK of the row to update
192     * @param row
193     *            a array of object
194     * @throws SQLException
195     */
196    void executeUpdate(List<Object> pk, Object[] row) throws SQLException;
197
198    /**
199     * Flush all pending updates (i.e. pending batch statement)
200     * 
201     * @throws SQLException
202     */
203    void flush() throws SQLException;
204
205    /**
206     * Obtains the number of updated rows
207     * 
208     * @return the number of updated rows
209     */
210    int getUpdateCount();
211
212    /**
213     * Obtains the number of inserted rows
214     * 
215     * @return the number of inserted rows
216     */
217    int getInsertCount();
218
219}