My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package org.apache.ibatis.session.defaults;

import org.apache.ibatis.exceptions.ExceptionFactory;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.ErrorContext;
import org.apache.ibatis.executor.result.DefaultMapResultHandler;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;

import java.sql.Connection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DefaultSqlSession implements SqlSession {

private Configuration configuration;
private Executor executor;

private boolean autoCommit;
private boolean dirty;

public DefaultSqlSession(Configuration configuration, Executor executor, boolean autoCommit) {
this.configuration = configuration;
this.executor = executor;
this.autoCommit = autoCommit;
this.dirty = false;
}

public Object selectOne(String statement) {
return selectOne(statement, null);
}

public Object selectOne(String statement, Object parameter) {
// Popular vote was to return null on 0 results and throw exception on too many.
List list = selectList(statement, parameter);
if (list.size() == 1) {
return list.get(0);
} else if (list.size() > 1) {
throw new TooManyResultsException("Expected one result (or null) to be returned by selectOne(), but found: " + list.size());
} else {
return null;
}
}

public Map selectMap(String statement, String mapKey) {
return selectMap(statement, null, mapKey, RowBounds.DEFAULT);
}

public Map selectMap(String statement, Object parameter, String mapKey) {
return selectMap(statement, parameter, mapKey, RowBounds.DEFAULT);
}

public Map selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds) {
final DefaultMapResultHandler mapResultHandler = new DefaultMapResultHandler(mapKey);
select(statement, parameter, rowBounds, mapResultHandler);
return mapResultHandler.getMappedResults();
}

public List selectList(String statement) {
return selectList(statement, null);
}

public List selectList(String statement, Object parameter) {
return selectList(statement, parameter, RowBounds.DEFAULT);
}

public List selectList(String statement, Object parameter, RowBounds rowBounds) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
return executor.query(ms, wrapCollection(parameter), rowBounds, Executor.NO_RESULT_HANDLER);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

public void select(String statement, Object parameter, ResultHandler handler) {
select(statement, parameter, RowBounds.DEFAULT, handler);
}

public void select(String statement, ResultHandler handler) {
select(statement, null, RowBounds.DEFAULT, handler);
}

public void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler) {
try {
MappedStatement ms = configuration.getMappedStatement(statement);
executor.query(ms, wrapCollection(parameter), rowBounds, handler);
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error querying database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

public int insert(String statement) {
return insert(statement, null);
}

public int insert(String statement, Object parameter) {
return update(statement, parameter);
}

public int update(String statement) {
return update(statement, null);
}

public int update(String statement, Object parameter) {
try {
dirty = true;
MappedStatement ms = configuration.getMappedStatement(statement);
return executor.update(ms, wrapCollection(parameter));
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error updating database. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

public int delete(String statement) {
return update(statement, null);
}

public int delete(String statement, Object parameter) {
return update(statement, wrapCollection(parameter));
}

public void commit() {
commit(false);
}

public void commit(boolean force) {
try {
executor.commit(isCommitOrRollbackRequired(force));
dirty = false;
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error committing transaction. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

public void rollback() {
rollback(false);
}

public void rollback(boolean force) {
try {
executor.rollback(isCommitOrRollbackRequired(force));
dirty = false;
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error rolling back transaction. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

public void close() {
try {
executor.close(isCommitOrRollbackRequired(false));
dirty = false;
} catch (Exception e) {
throw ExceptionFactory.wrapException("Error closing transaction. Cause: " + e, e);
} finally {
ErrorContext.instance().reset();
}
}

public Configuration getConfiguration() {
return configuration;
}

public <T> T getMapper(Class<T> type) {
return configuration.getMapper(type, this);
}

public Connection getConnection() {
return executor.getTransaction().getConnection();
}

public void clearCache() {
executor.clearLocalCache();
}

private boolean isCommitOrRollbackRequired(boolean force) {
return (!autoCommit && dirty) || force;
}

private Object wrapCollection(final Object object) {
if (object instanceof List) {
return new HashMap() {{
put("list", object);
}};
} else if (object != null && object.getClass().isArray()) {
return new HashMap() {{
put("array", object);
}};
}
return object;
}

}

Change log

r3491 by simone.tripodi on Dec 31, 2010   Diff
[maven-release-plugin]  copy for tag
mybatis-3.0.4
Go to: 

Older revisions

r2979 by clinton.begin on Oct 31, 2010   Diff
Added selectMap functionality for
selecting Maps of results keyed by a
property
r2160 by nathan.maves on May 30, 2010   Diff
added select method without parameters
per issue http://code.google.com/p/myb
atis/issues/detail?id=9
r1949 by clinton.begin on May 16, 2010   Diff
Moved
mirror/ibatis/java/ibatis-3/trunk to
trunk.
All revisions of this file

File info

Size: 6254 bytes, 208 lines
Powered by Google Project Hosting