1
2 package org.hibernate.impl;
3
4 import java.util.HashMap;
5 import java.util.Iterator;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9 import org.hibernate.FlushMode;
10 import org.hibernate.HibernateException;
11 import org.hibernate.LockMode;
12 import org.hibernate.Query;
13 import org.hibernate.ScrollMode;
14 import org.hibernate.ScrollableResults;
15 import org.hibernate.Session;
16 import org.hibernate.engine.QueryParameters;
17 import org.hibernate.engine.SessionImplementor;
18 import org.hibernate.engine.TypedValue;
19 import org.hibernate.engine.query.ParameterMetadata;
20 import org.hibernate.type.Type;
21
22
23
24
25
26
27
28
29 public class QueryImpl extends AbstractQueryImpl {
30
31 private Map lockModes = new HashMap(2);
32
33
34 public QueryImpl(String queryString, FlushMode flushMode, SessionImplementor session, ParameterMetadata parameterMetadata) {
35 super(queryString, flushMode, session, parameterMetadata);
36 }
37
38 public QueryImpl(String queryString, SessionImplementor session, ParameterMetadata parameterMetadata) {
39 this(queryString, null, session, parameterMetadata);
40 }
41
42 public Iterator iterate() throws HibernateException {
43 verifyParameters();
44 Map namedParams = getNamedParams();
45 before();
46 try {
47 return getSession().iterate(expandParameterLists(namedParams), getQueryParameters(namedParams));
48 } finally {
49 after();
50 }
51 }
52
53 public ScrollableResults scroll() throws HibernateException {
54 return scroll(ScrollMode.SCROLL_INSENSITIVE);
55 }
56
57 public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException {
58 verifyParameters();
59 Map namedParams = getNamedParams();
60 before();
61 QueryParameters qp = getQueryParameters(namedParams);
62 qp.setScrollMode(scrollMode);
63 try {
64 return getSession().scroll(expandParameterLists(namedParams), qp);
65 } finally {
66 after();
67 }
68 }
69
70 public List list() throws HibernateException {
71 verifyParameters();
72 Map namedParams = getNamedParams();
73 before();
74 try {
75 return getSession().list(expandParameterLists(namedParams), getQueryParameters(namedParams));
76 } finally {
77 after();
78 }
79 }
80
81 public int executeUpdate() throws HibernateException {
82 verifyParameters();
83 Map namedParams = getNamedParams();
84 before();
85 try {
86 return getSession().executeUpdate(expandParameterLists(namedParams), getQueryParameters(namedParams));
87 } finally {
88 after();
89 }
90 }
91
92 public Query setLockMode(String alias, LockMode lockMode) {
93 lockModes.put(alias, lockMode);
94 return this;
95 }
96
97 protected Map getLockModes() {
98 return lockModes;
99 }
100
101
102
103
104
105
106 public int count() {
107 StringBuffer hqlQuery = new StringBuffer(this.getQueryString().toUpperCase());
108 StringBuffer hqlQueryBuffer = new StringBuffer();
109 hqlQueryBuffer.append("SELECT COUNT(*) ");
110 int start = hqlQuery.indexOf("FROM");
111 int stop = hqlQuery.indexOf("ORDER BY");
112 if (stop >= 0) {
113 hqlQueryBuffer.append(this.getQueryString().substring(start, stop));
114 } else {
115 hqlQueryBuffer.append(this.getQueryString().substring(start));
116 }
117 Query countQuery = ((Session)this.session).createQuery(hqlQueryBuffer.toString());
118
119 Map parameters = this.getNamedParams();
120 Set parameterNames = parameters.keySet();
121 Iterator iter = parameterNames.iterator();
122 while (iter.hasNext()) {
123 String name = (String)iter.next();
124 TypedValue typedValue = (TypedValue)parameters.get(name);
125 countQuery.setParameter(name, typedValue.getValue(), typedValue.getType());
126 }
127
128 List paramTypes = this.getTypes();
129 List paramValues = this.getValues();
130 if (paramTypes != null && paramTypes.size() > 0) {
131 for (int i = 0; i < paramTypes.size(); i++) {
132 countQuery.setParameter(i, paramValues.get(i), (Type)paramTypes.get(i));
133 }
134 }
135 int count = ((Long)countQuery.uniqueResult()).intValue();
136 return count;
137 }
138 }