1 16 17 package org.springframework.jdbc.object; 18 19 import java.sql.ResultSet ; 20 import java.sql.SQLException ; 21 import java.sql.Types ; 22 23 import javax.sql.DataSource ; 24 25 import org.springframework.dao.TypeMismatchDataAccessException; 26 import org.springframework.jdbc.core.SingleColumnRowMapper; 27 import org.springframework.jdbc.support.JdbcUtils; 28 29 53 public class SqlFunction extends MappingSqlQuery { 54 55 private final SingleColumnRowMapper rowMapper = new SingleColumnRowMapper(); 56 57 58 66 public SqlFunction() { 67 setRowsExpected(1); 68 } 69 70 76 public SqlFunction(DataSource ds, String sql) { 77 setRowsExpected(1); 78 setDataSource(ds); 79 setSql(sql); 80 } 81 82 90 public SqlFunction(DataSource ds, String sql, int[] types) { 91 setRowsExpected(1); 92 setDataSource(ds); 93 setSql(sql); 94 setTypes(types); 95 } 96 97 107 public SqlFunction(DataSource ds, String sql, int[] types, Class resultType) { 108 setRowsExpected(1); 109 setDataSource(ds); 110 setSql(sql); 111 setTypes(types); 112 setResultType(resultType); 113 } 114 115 116 121 public void setResultType(Class resultType) { 122 this.rowMapper.setRequiredType(resultType); 123 } 124 125 126 131 protected Object mapRow(ResultSet rs, int rowNum) throws SQLException { 132 return this.rowMapper.mapRow(rs, rowNum); 133 } 134 135 136 140 public int run() { 141 return run(null); 142 } 143 144 149 public int run(int parameter) { 150 return run(new Object [] {new Integer (parameter)}); 151 } 152 153 160 public int run(Object [] parameters) { 161 Object obj = super.findObject(parameters); 162 if (!(obj instanceof Number )) { 163 throw new TypeMismatchDataAccessException("Couldn't convert result object [" + obj + "] to int"); 164 } 165 return ((Number ) obj).intValue(); 166 } 167 168 173 public Object runGeneric() { 174 return findObject((Object []) null); 175 } 176 177 182 public Object runGeneric(int parameter) { 183 return findObject(parameter); 184 } 185 186 194 public Object runGeneric(Object [] parameters) { 195 return findObject(parameters); 196 } 197 198 } 199 | Popular Tags |