001package com.avaje.ebean; 002 003import java.sql.CallableStatement; 004import java.sql.SQLException; 005 006/** 007 * For making calls to stored procedures. Refer to the Ebean execute() method. 008 * <p> 009 * Note that UpdateSql is designed for general DML sql and CallableSql is 010 * designed for use with stored procedures. Also note that when using this in 011 * batch mode the out parameters are not read. 012 * </p> 013 * <p> 014 * Example 1: 015 * </p> 016 * 017 * <pre class="code"> 018 * String sql = "{call sp_order_mod(?,?)}"; 019 * 020 * CallableSql cs = Ebean.createCallableSql(sql); 021 * cs.setParameter(1, "turbo"); 022 * cs.registerOut(2, Types.INTEGER); 023 * 024 * Ebean.execute(cs); 025 * 026 * // read the out parameter 027 * Integer returnValue = (Integer) cs.getObject(2); 028 * </pre> 029 * 030 * <p> 031 * Example 2:<br> 032 * Includes batch mode, table modification information and label. Note that the 033 * label is really only to help people reading the transaction logs to identify 034 * the procedure called etc. 035 * </p> 036 * 037 * <pre class="code"> 038 * String sql = "{call sp_insert_order(?,?)}"; 039 * 040 * CallableSql cs = Ebean.createCallableSql(sql); 041 * 042 * // Inform Ebean this stored procedure inserts into the 043 * // oe_order table and inserts + updates the oe_order_detail table. 044 * // this is used to invalidate objects in the cache 045 * cs.addModification("oe_order", true, false, false); 046 * cs.addModification("oe_order_detail", true, true, false); 047 * 048 * Transaction t = Ebean.startTransaction(); 049 * 050 * // execute using JDBC batching 10 statements at a time 051 * t.setBatchMode(true); 052 * t.setBatchSize(10); 053 * try { 054 * cs.setParameter(1, "Was"); 055 * cs.setParameter(2, "Banana"); 056 * Ebean.execute(cs); 057 * 058 * cs.setParameter(1, "Here"); 059 * cs.setParameter(2, "Kumera"); 060 * Ebean.execute(cs); 061 * 062 * cs.setParameter(1, "More"); 063 * cs.setParameter(2, "Apple"); 064 * Ebean.execute(cs); 065 * 066 * // Ebean.externalModification("oe_order",true,false,false); 067 * // Ebean.externalModification("oe_order_detail",true,true,false); 068 * Ebean.commitTransaction(); 069 * 070 * } finally { 071 * Ebean.endTransaction(); 072 * } 073 * </pre> 074 * 075 * @see com.avaje.ebean.SqlUpdate 076 * @see com.avaje.ebean.Ebean#execute(CallableSql) 077 */ 078public interface CallableSql { 079 080 /** 081 * Return the label that is put into the transaction log. 082 */ 083 String getLabel(); 084 085 /** 086 * Set the label that is put in the transaction log. 087 */ 088 CallableSql setLabel(String label); 089 090 /** 091 * Return the statement execution timeout. 092 */ 093 int getTimeout(); 094 095 /** 096 * Return the callable sql. 097 */ 098 String getSql(); 099 100 /** 101 * Set the statement execution timeout. Zero implies unlimited time. 102 * <p> 103 * This is set to the underlying CallableStatement. 104 * </p> 105 */ 106 CallableSql setTimeout(int secs); 107 108 /** 109 * Set the callable sql. 110 */ 111 CallableSql setSql(String sql); 112 113 /** 114 * Bind a parameter that is bound as a IN parameter. 115 * <p> 116 * position starts at value 1 (not 0) to be consistent with CallableStatement. 117 * </p> 118 * <p> 119 * This is designed so that you do not need to set params in index order. You 120 * can set/register param 2 before param 1 etc. 121 * </p> 122 * 123 * @param position 124 * the index position of the parameter. 125 * @param value 126 * the value of the parameter. 127 */ 128 CallableSql bind(int position, Object value); 129 130 /** 131 * Bind a positioned parameter (same as bind method). 132 * 133 * @param position 134 * the index position of the parameter. 135 * @param value 136 * the value of the parameter. 137 */ 138 CallableSql setParameter(int position, Object value); 139 140 /** 141 * Register an OUT parameter. 142 * <p> 143 * Note that position starts at value 1 (not 0) to be consistent with 144 * CallableStatement. 145 * </p> 146 * <p> 147 * This is designed so that you do not need to register params in index order. 148 * You can set/register param 2 before param 1 etc. 149 * </p> 150 * 151 * @param position 152 * the index position of the parameter (starts with 1). 153 * @param type 154 * the jdbc type of the OUT parameter that will be read. 155 */ 156 CallableSql registerOut(int position, int type); 157 158 /** 159 * Return an OUT parameter value. 160 * <p> 161 * position starts at value 1 (not 0) to be consistent with CallableStatement. 162 * </p> 163 * <p> 164 * This can only be called after the CallableSql has been executed. When run 165 * in batch mode you effectively can't use this method. 166 * </p> 167 */ 168 Object getObject(int position); 169 170 /** 171 * 172 * You can extend this object and override this method for more advanced 173 * stored procedure calls. This would be the case when ResultSets are returned 174 * etc. 175 */ 176 boolean executeOverride(CallableStatement cstmt) throws SQLException; 177 178 /** 179 * Add table modification information to the TransactionEvent. 180 * <p> 181 * This would be similar to using the 182 * <code>Ebean.externalModification()</code> method. It may be easier and make 183 * more sense to set it here with the CallableSql. 184 * </p> 185 * <p> 186 * For UpdateSql the table modification information is derived by parsing the 187 * sql to determine the table name and whether it was an insert, update or 188 * delete. 189 * </p> 190 */ 191 CallableSql addModification(String tableName, boolean inserts, boolean updates, boolean deletes); 192 193}