1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.efaps.db;
23
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.sql.Statement;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.Map.Entry;
30 import java.util.UUID;
31
32 import org.efaps.admin.datamodel.Attribute;
33 import org.efaps.admin.datamodel.SQLTable;
34 import org.efaps.admin.datamodel.Type;
35 import org.efaps.db.transaction.ConnectionResource;
36 import org.efaps.db.wrapper.SQLSelect;
37 import org.efaps.util.EFapsException;
38 import org.efaps.util.cache.CacheReloadException;
39
40
41
42
43
44
45
46
47 public class AttributeQuery
48 extends AbstractObjectQuery<Object>
49 {
50
51
52
53
54 private final Attribute attribute;
55
56
57
58
59
60
61 public AttributeQuery(final UUID _typeUUI)
62 throws CacheReloadException
63 {
64 this(Type.get(_typeUUI));
65 }
66
67
68
69
70
71
72 public AttributeQuery(final Type _type)
73 {
74 this(_type, _type.getAttribute("ID"));
75 }
76
77
78
79
80
81
82
83
84 public AttributeQuery(final UUID _typeUUI,
85 final Attribute _attribute)
86 throws CacheReloadException
87 {
88 this(Type.get(_typeUUI), _attribute);
89 }
90
91
92
93
94
95
96
97
98 public AttributeQuery(final UUID _typeUUI,
99 final String _attributeName)
100 throws CacheReloadException
101 {
102 this(Type.get(_typeUUI), Type.get(_typeUUI).getAttribute(_attributeName));
103 }
104
105
106
107
108
109
110 public AttributeQuery(final Type _type,
111 final Attribute _attribute)
112 {
113 super(_type);
114 this.attribute = _attribute == null ? _type.getAttribute("ID") : _attribute;
115 }
116
117
118
119
120
121
122 public Attribute getAttribute()
123 {
124 return this.attribute;
125 }
126
127
128
129
130
131
132
133 @Override
134 public List<Object> execute()
135 throws EFapsException
136 {
137 return executeWithoutAccessCheck();
138 }
139
140
141
142
143
144
145
146 @Override
147 public List<Object> executeWithoutAccessCheck()
148 throws EFapsException
149 {
150 executeOneCompleteStmt(getSQLStatement());
151 return getValues();
152 }
153
154
155
156
157
158
159 public String getSQLStatement()
160 throws EFapsException
161 {
162 prepareQuery();
163 final SQLSelect select = new SQLSelect()
164 .column(getSqlTable2Index().get(this.attribute.getTable()), this.attribute.getSqlColNames().get(0))
165 .from(getBaseType().getMainTable().getSqlTable(), 0);
166
167
168 if (getSqlTable2Index().size() > 0) {
169 for (final Entry<SQLTable, Integer> entry : getSqlTable2Index().entrySet()) {
170 if (entry.getValue() > 0) {
171 select.leftJoin(entry.getKey().getSqlTable(), entry.getValue(), "ID", 0, "ID");
172 }
173 }
174 }
175 select.addSection(getWhere());
176 select.addSection(getOrderBy());
177 select.addSection(getLimit());
178
179 if (AbstractObjectQuery.LOG.isDebugEnabled()) {
180 AbstractObjectQuery.LOG.debug(select.getSQL());
181 }
182 return select.getSQL();
183 }
184
185
186
187
188 @Override
189 protected void prepareQuery()
190 throws EFapsException
191 {
192 super.prepareQuery();
193
194 getIndex4SqlTable(this.attribute.getTable());
195 }
196
197
198
199
200
201
202
203 protected boolean executeOneCompleteStmt(final String _complStmt)
204 throws EFapsException
205 {
206 final boolean ret = false;
207 ConnectionResource con = null;
208 try {
209 con = Context.getThreadContext().getConnectionResource();
210 if (AbstractObjectQuery.LOG.isDebugEnabled()) {
211 AbstractObjectQuery.LOG.debug(_complStmt.toString());
212 }
213
214 final Statement stmt = con.getConnection().createStatement();
215
216 final ResultSet rs = stmt.executeQuery(_complStmt.toString());
217 new ArrayList<Instance>();
218 while (rs.next()) {
219 getValues().add(rs.getObject(1));
220 }
221 rs.close();
222 stmt.close();
223 con.commit();
224 } catch (final SQLException e) {
225 throw new EFapsException(AttributeQuery.class, "executeOneCompleteStmt", e);
226 } finally {
227 if (con != null && con.isOpened()) {
228 con.abort();
229 }
230 }
231 return ret;
232 }
233 }