1 package org.skife.jdbi.v2;
2
3 import org.skife.jdbi.v2.tweak.Argument;
4 import org.skife.jdbi.v2.tweak.SQLLog;
5 import org.skife.jdbi.v2.tweak.StatementBuilder;
6 import org.skife.jdbi.v2.tweak.StatementCustomizer;
7 import org.skife.jdbi.v2.tweak.StatementLocator;
8 import org.skife.jdbi.v2.tweak.StatementRewriter;
9
10 import java.sql.CallableStatement;
11 import java.sql.Connection;
12 import java.sql.PreparedStatement;
13 import java.sql.SQLException;
14 import java.sql.Statement;
15 import java.sql.Types;
16 import java.util.ArrayList;
17 import java.util.Collection;
18 import java.util.List;
19
20
21
22
23 public class Call extends SQLStatement<Call>
24 {
25 private final List<OutParamArgument> params = new ArrayList<OutParamArgument>();
26
27 Call(Connection connection,
28 StatementLocator locator,
29 StatementRewriter rewriter,
30 StatementBuilder cache,
31 String sql,
32 StatementContext ctx,
33 SQLLog log,
34 TimingCollector timingCollector,
35 Collection<StatementCustomizer> customizers)
36 {
37 super(new Binding(), locator, rewriter, connection, cache, sql, ctx, log, timingCollector, customizers);
38 }
39
40
41
42
43
44
45
46 public Call registerOutParameter(int position, int sqlType)
47 {
48 return registerOutParameter(position, sqlType, null);
49 }
50
51 public Call registerOutParameter(int position, int sqlType, CallableStatementMapper mapper)
52 {
53 getParams().addPositional(position, new OutParamArgument(sqlType, mapper, null));
54 return this;
55 }
56
57
58
59
60
61
62
63 public Call registerOutParameter(String name, int sqlType)
64 {
65 return registerOutParameter(name, sqlType, null);
66 }
67
68 public Call registerOutParameter(String name, int sqlType, CallableStatementMapper mapper)
69 {
70 getParams().addNamed(name, new OutParamArgument(sqlType, mapper, name));
71 return this;
72 }
73
74
75
76
77
78 public OutParameters invoke()
79 {
80 return this.internalExecute(QueryPreperator.NO_OP, new QueryResultMunger<OutParameters>(){
81 public OutParameters munge(Statement results) throws SQLException
82 {
83 OutParameters out = new OutParameters();
84 for ( OutParamArgument param : params ) {
85 Object obj = param.map((CallableStatement)results);
86 out.getMap().put(param.position, obj);
87 if ( param.name != null ) {
88 out.getMap().put(param.name, obj);
89 }
90 }
91 return out;
92 }
93 }, QueryPostMungeCleanup.CLOSE_RESOURCES_QUIETLY);
94 }
95
96 private class OutParamArgument implements Argument
97 {
98 private final int sqlType;
99 private final CallableStatementMapper mapper;
100 private final String name;
101 private int position ;
102
103 public OutParamArgument(int sqlType, CallableStatementMapper mapper, String name)
104 {
105 this.sqlType = sqlType;
106 this.mapper = mapper;
107 this.name = name;
108 params.add(this);
109 }
110
111 public void apply(int position, PreparedStatement statement, StatementContext ctx) throws SQLException
112 {
113 ((CallableStatement)statement).registerOutParameter(position, sqlType);
114 this.position = position ;
115 }
116
117 public Object map(CallableStatement stmt) throws SQLException
118 {
119 if ( mapper != null ) {
120 return mapper.map(position, stmt);
121 }
122 switch ( sqlType ) {
123 case Types.CLOB : case Types.VARCHAR :
124 case Types.LONGNVARCHAR :
125 case Types.LONGVARCHAR :
126 case Types.NCLOB :
127 case Types.NVARCHAR :
128 return stmt.getString(position) ;
129 case Types.BLOB :
130 case Types.VARBINARY :
131 return stmt.getBytes(position) ;
132 case Types.SMALLINT :
133 return stmt.getShort(position);
134 case Types.INTEGER :
135 return stmt.getInt(position);
136 case Types.BIGINT :
137 return stmt.getLong(position);
138 case Types.TIMESTAMP : case Types.TIME :
139 return stmt.getTimestamp(position) ;
140 case Types.DATE :
141 return stmt.getDate(position) ;
142 case Types.FLOAT :
143 return stmt.getFloat(position);
144 case Types.DECIMAL : case Types.DOUBLE :
145 return stmt.getDouble(position);
146 default :
147 return stmt.getObject(position);
148
149 }
150 }
151 }
152 }