View Javadoc
1   /*
2    * Copyright 2004-2022 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    public 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    public void addBatch(StatementScope statementScope, Connection conn, String sql, Object[] parameters)
77        throws SQLException;
78  
79    /**
80     * Execute a batch of statements.
81     *
82     * @param sessionScope
83     *          - the session scope
84     *
85     * @return - the number of rows impacted by the batch
86     *
87     * @throws SQLException
88     *           - if a statement fails
89     */
90    public int executeBatch(SessionScope sessionScope) throws SQLException;
91  
92    /**
93     * Execute a batch of statements.
94     *
95     * @param sessionScope
96     *          - the session scope
97     *
98     * @return - a List of BatchResult objects (may be null if no batch has been initiated). There will be one BatchResult
99     *         object in the list for each sub-batch executed
100    *
101    * @throws SQLException
102    *           if a database access error occurs, or the drive does not support batch statements
103    * @throws BatchException
104    *           if the driver throws BatchUpdateException
105    */
106   public List executeBatchDetailed(SessionScope sessionScope) throws SQLException, BatchException;
107 
108   /**
109    * Long form of the method to execute a query.
110    *
111    * @param statementScope
112    *          - the request scope
113    * @param conn
114    *          - the database connection
115    * @param sql
116    *          - the SQL statement to execute
117    * @param parameters
118    *          - the parameters for the statement
119    * @param skipResults
120    *          - the number of results to skip
121    * @param maxResults
122    *          - the maximum number of results to return
123    * @param callback
124    *          - the row handler for the query
125    *
126    * @throws SQLException
127    *           - if the query fails
128    */
129   public void executeQuery(StatementScope statementScope, Connection conn, String sql, Object[] parameters,
130       int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException;
131 
132   /**
133    * Execute a stored procedure that updates data.
134    *
135    * @param statementScope
136    *          - the request scope
137    * @param conn
138    *          - the database connection
139    * @param sql
140    *          - the SQL to call the procedure
141    * @param parameters
142    *          - the parameters for the procedure
143    *
144    * @return - the rows impacted by the procedure
145    *
146    * @throws SQLException
147    *           - if the procedure fails
148    */
149   public int executeUpdateProcedure(StatementScope statementScope, Connection conn, String sql, Object[] parameters)
150       throws SQLException;
151 
152   /**
153    * Execute a stored procedure.
154    *
155    * @param statementScope
156    *          - the request scope
157    * @param conn
158    *          - the database connection
159    * @param sql
160    *          - the sql to call the procedure
161    * @param parameters
162    *          - the parameters for the procedure
163    * @param skipResults
164    *          - the number of results to skip
165    * @param maxResults
166    *          - the maximum number of results to return
167    * @param callback
168    *          - a row handler for processing the results
169    *
170    * @throws SQLException
171    *           - if the procedure fails
172    */
173   public void executeQueryProcedure(StatementScope statementScope, Connection conn, String sql, Object[] parameters,
174       int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException;
175 
176   /**
177    * Clean up any batches on the session.
178    *
179    * @param sessionScope
180    *          - the session to clean up
181    */
182   public void cleanup(SessionScope sessionScope);
183 
184   /**
185    * Initializing SQL Executor by passing configuration and global properties.
186    *
187    * @param config
188    *          - the configuration class, which contains complete configuration info
189    * @param globalProps
190    *          the global props
191    */
192   public void init(SqlMapConfiguration config, Properties globalProps);
193 }