View Javadoc
1   /*
2    * Copyright 2004-2025 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.sqlmap.engine.execution;
17  
18  import com.ibatis.sqlmap.engine.config.SqlMapConfiguration;
19  import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
20  import com.ibatis.sqlmap.engine.scope.SessionScope;
21  import com.ibatis.sqlmap.engine.scope.StatementScope;
22  
23  import java.sql.Connection;
24  import java.sql.SQLException;
25  import java.util.List;
26  import java.util.Properties;
27  
28  /**
29   * Classes responsible for executing the SQL implement this interface Support for custom SQL Executors.
30   */
31  public interface SqlExecutor {
32    //
33    // Constants
34    //
35    /** Constant to let us know not to skip anything. */
36    int NO_SKIPPED_RESULTS = 0;
37  
38    /** Constant to let us know to include all records. */
39    int NO_MAXIMUM_RESULTS = -999999;
40  
41    /**
42     * Execute an update.
43     *
44     * @param statementScope
45     *          - the request scope
46     * @param conn
47     *          - the database connection
48     * @param sql
49     *          - the sql statement to execute
50     * @param parameters
51     *          - the parameters for the sql statement
52     *
53     * @return - the number of records changed
54     *
55     * @throws SQLException
56     *           - if the update fails
57     */
58    int executeUpdate(StatementScope statementScope, Connection conn, String sql, Object[] parameters)
59        throws SQLException;
60  
61    /**
62     * Adds a statement to a batch.
63     *
64     * @param statementScope
65     *          - the request scope
66     * @param conn
67     *          - the database connection
68     * @param sql
69     *          - the sql statement
70     * @param parameters
71     *          - the parameters for the statement
72     *
73     * @throws SQLException
74     *           - if the statement fails
75     */
76    void addBatch(StatementScope statementScope, Connection conn, String sql, Object[] parameters) throws SQLException;
77  
78    /**
79     * Execute a batch of statements.
80     *
81     * @param sessionScope
82     *          - the session scope
83     *
84     * @return - the number of rows impacted by the batch
85     *
86     * @throws SQLException
87     *           - if a statement fails
88     */
89    int executeBatch(SessionScope sessionScope) throws SQLException;
90  
91    /**
92     * Execute a batch of statements.
93     *
94     * @param sessionScope
95     *          - the session scope
96     *
97     * @return - a List of BatchResult objects (may be null if no batch has been initiated). There will be one BatchResult
98     *         object in the list for each sub-batch executed
99     *
100    * @throws SQLException
101    *           if a database access error occurs, or the drive does not support batch statements
102    * @throws BatchException
103    *           if the driver throws BatchUpdateException
104    */
105   List executeBatchDetailed(SessionScope sessionScope) throws SQLException, BatchException;
106 
107   /**
108    * Long form of the method to execute a query.
109    *
110    * @param statementScope
111    *          - the request scope
112    * @param conn
113    *          - the database connection
114    * @param sql
115    *          - the SQL statement to execute
116    * @param parameters
117    *          - the parameters for the statement
118    * @param skipResults
119    *          - the number of results to skip
120    * @param maxResults
121    *          - the maximum number of results to return
122    * @param callback
123    *          - the row handler for the query
124    *
125    * @throws SQLException
126    *           - if the query fails
127    */
128   void executeQuery(StatementScope statementScope, Connection conn, String sql, Object[] parameters, int skipResults,
129       int maxResults, RowHandlerCallback callback) throws SQLException;
130 
131   /**
132    * Execute a stored procedure that updates data.
133    *
134    * @param statementScope
135    *          - the request scope
136    * @param conn
137    *          - the database connection
138    * @param sql
139    *          - the SQL to call the procedure
140    * @param parameters
141    *          - the parameters for the procedure
142    *
143    * @return - the rows impacted by the procedure
144    *
145    * @throws SQLException
146    *           - if the procedure fails
147    */
148   int executeUpdateProcedure(StatementScope statementScope, Connection conn, String sql, Object[] parameters)
149       throws SQLException;
150 
151   /**
152    * Execute a stored procedure.
153    *
154    * @param statementScope
155    *          - the request scope
156    * @param conn
157    *          - the database connection
158    * @param sql
159    *          - the sql to call the procedure
160    * @param parameters
161    *          - the parameters for the procedure
162    * @param skipResults
163    *          - the number of results to skip
164    * @param maxResults
165    *          - the maximum number of results to return
166    * @param callback
167    *          - a row handler for processing the results
168    *
169    * @throws SQLException
170    *           - if the procedure fails
171    */
172   void executeQueryProcedure(StatementScope statementScope, Connection conn, String sql, Object[] parameters,
173       int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException;
174 
175   /**
176    * Clean up any batches on the session.
177    *
178    * @param sessionScope
179    *          - the session to clean up
180    */
181   void cleanup(SessionScope sessionScope);
182 
183   /**
184    * Initializing SQL Executor by passing configuration and global properties.
185    *
186    * @param config
187    *          - the configuration class, which contains complete configuration info
188    * @param globalProps
189    *          the global props
190    */
191   void init(SqlMapConfiguration config, Properties globalProps);
192 }