Newer
Older
* </i></p>
*
* @param stmt {@link PreparedStatement} in which the update query has been prepared.
* @param indRow Index of the row in the whole update process. It is used only for error management purpose.
*
* @throws SQLException If {@link PreparedStatement#executeUpdate()} fails.</i>
* @throws DBException If {@link PreparedStatement#addBatch()} fails and this update does not concern the first row, or if the number of updated rows is different from 1.
*/
protected final void executeUpdate(final PreparedStatement stmt, int indRow) throws SQLException, DBException{
// BATCH INSERTION: (the query is queued and will be executed later)
if (supportsBatchUpdates){
// Add the prepared query in the batch queue of the statement:
try{
stmt.addBatch();
}catch(SQLException se){
if (!isCancelled())
supportsBatchUpdates = false;
/*
* If the error happens for the first row, it is still possible to insert all rows
* with the non-batch function - executeUpdate().
*
* Otherwise, it is impossible to insert the previous batched rows ; an exception must be thrown
* and must stop the whole TAP_SCHEMA initialization.
*/
if (indRow == 1){
if (!isCancelled() && logger != null)
logger.logDB(LogLevel.WARNING, this, "EXEC_UPDATE", "BATCH query impossible => TRYING AGAIN IN A NORMAL EXECUTION (executeUpdate())!", se);
}else{
if (!isCancelled() && logger != null)
logger.logDB(LogLevel.ERROR, this, "EXEC_UPDATE", "BATCH query impossible!", se);
throw new DBException("BATCH query impossible!", se);
}
}
}
// NORMAL INSERTION: (immediate insertion)
if (!supportsBatchUpdates){
// Insert the row prepared in the given statement:
int nbRowsWritten = stmt.executeUpdate();
// Check the row has been inserted with success:
if (nbRowsWritten != 1){
if (logger != null)
logger.logDB(LogLevel.ERROR, this, "EXEC_UPDATE", "ROW " + indRow + " not inserted!", null);
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
throw new DBException("ROW " + indRow + " not inserted!");
}
}
}
/**
* <p>Execute all batched queries.</p>
*
* <p>To do so, {@link PreparedStatement#executeBatch()} and then, if the first was successful, {@link PreparedStatement#clearBatch()} is called.</p>
*
* <p>
* Before returning, this function is ensuring that exactly the given number of rows has been updated.
* If it is not the case, a {@link DBException} is thrown.
* </p>
*
* <p><i>Note:
* This function has no effect if batch queries are not supported.
* </i></p>
*
* <p><i><b>Important note:</b>
* In case {@link PreparedStatement#executeBatch()} fails by throwing an {@link SQLException},
* batch update queries will be afterwards considered as not supported by this connection.
* </i></p>
*
* @param stmt {@link PreparedStatement} in which the update query has been prepared.
* @param nbRows Number of rows that should be updated.
*
* @throws DBException If {@link PreparedStatement#executeBatch()} fails, or if the number of updated rows is different from the given one.
*/
protected final void executeBatchUpdates(final PreparedStatement stmt, int nbRows) throws DBException{
if (supportsBatchUpdates){
// Execute all the batch queries:
int[] rows;
try{
rows = stmt.executeBatch();
}catch(SQLException se){
if (!isCancelled()){
supportsBatchUpdates = false;
if (logger != null)
logger.logDB(LogLevel.ERROR, this, "EXEC_UPDATE", "BATCH execution impossible!", se);
}
throw new DBException("BATCH execution impossible!", se);
}
// Remove executed queries from the statement:
try{
stmt.clearBatch();
}catch(SQLException se){
if (!isCancelled() && logger != null)
logger.logDB(LogLevel.WARNING, this, "EXEC_UPDATE", "CLEAR BATCH impossible!", se);
}
// Count the updated rows:
int nbRowsUpdated = 0;
for(int i = 0; i < rows.length; i++)
nbRowsUpdated += rows[i];
// Check all given rows have been inserted with success:
if (nbRowsUpdated != nbRows){
if (logger != null)
logger.logDB(LogLevel.ERROR, this, "EXEC_UPDATE", "ROWS not all update (" + nbRows + " to update ; " + nbRowsUpdated + " updated)!", null);
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
throw new DBException("ROWS not all updated (" + nbRows + " to update ; " + nbRowsUpdated + " updated)!");
}
}
}
/**
* Append all items of the iterator inside the given list.
*
* @param lst List to update.
* @param it All items to append inside the list.
*/
private < T > void appendAllInto(final List<T> lst, final Iterator<T> it){
while(it.hasNext())
lst.add(it.next());
}
/**
* <p>Tell whether the given DB name is equals (case sensitively or not, in function of the given parameter)
* to the given name coming from a {@link TAPMetadata} object.</p>
*
* <p>If at least one of the given name is NULL, <i>false</i> is returned.</p>
*
* <p><i>Note:
* The comparison will be done in function of the specified case sensitivity BUT ALSO of the case supported and stored by the DBMS.
* For instance, if it has been specified a case insensitivity and that mixed case is not supported by unquoted identifier,
* the comparison must be done, surprisingly, by considering the case if unquoted identifiers are stored in lower or upper case.
* Thus, this special way to evaluate equality should be as closed as possible to the identifier storage and research policies of the used DBMS.
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
*
* @param dbName Name provided by the database.
* @param metaName Name provided by a {@link TAPMetadata} object.
* @param caseSensitive <i>true</i> if the equality test must be done case sensitively, <i>false</i> otherwise.
*
* @return <i>true</i> if both names are equal, <i>false</i> otherwise.
*/
protected final boolean equals(final String dbName, final String metaName, final boolean caseSensitive){
if (dbName == null || metaName == null)
return false;
if (caseSensitive){
if (supportsMixedCaseQuotedIdentifier || mixedCaseQuoted)
return dbName.equals(metaName);
else if (lowerCaseQuoted)
return dbName.equals(metaName.toLowerCase());
else if (upperCaseQuoted)
return dbName.equals(metaName.toUpperCase());
else
return dbName.equalsIgnoreCase(metaName);
}else{
if (supportsMixedCaseUnquotedIdentifier)
return dbName.equalsIgnoreCase(metaName);
else if (lowerCaseUnquoted)
return dbName.equals(metaName.toLowerCase());
else if (upperCaseUnquoted)
return dbName.equals(metaName.toUpperCase());
else
return dbName.equalsIgnoreCase(metaName);
@Override
public void setFetchSize(final int size){
supportsFetchSize = true;
fetchSize = (size > 0) ? size : IGNORE_FETCH_SIZE;
}