|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
DynamicSelect.java | - | 62.5% | 64.7% | 63.3% |
|
1 |
/*
|
|
2 |
* Joey and its relative products are published under the terms
|
|
3 |
* of the Apache Software License.
|
|
4 |
*/
|
|
5 |
package org.asyrinx.brownie.core.sql;
|
|
6 |
|
|
7 |
import java.text.DateFormat;
|
|
8 |
import java.text.SimpleDateFormat;
|
|
9 |
|
|
10 |
/**
|
|
11 |
* 動的にSELECT文を生成するためのクラスです。 <br>
|
|
12 |
* 検索条件が動的に変更される場合には 動的にSQLを生成する必要がありますが、 PreparedStatementでは使用しにくい場合が多いです。
|
|
13 |
* そのような場合に、このクラスを使用してください。 <br>
|
|
14 |
*
|
|
15 |
* @author Akima
|
|
16 |
*/
|
|
17 |
public class DynamicSelect { |
|
18 |
|
|
19 |
/**
|
|
20 |
* Constructor for DynamicSql.
|
|
21 |
*/
|
|
22 | 3 |
public DynamicSelect() {
|
23 | 3 |
this(new BasicSqlBuilder()); |
24 |
} |
|
25 |
|
|
26 |
/**
|
|
27 |
* Constructor for DynamicSql.
|
|
28 |
*/
|
|
29 | 3 |
public DynamicSelect(IBuilder builder) {
|
30 | 3 |
super();
|
31 | 3 |
this.builder = builder;
|
32 | 3 |
this.selectFields = new Fields(builder); |
33 | 3 |
this.fromTables = new Tables(builder); |
34 | 3 |
this.whereConditions = new Conditions(builder); |
35 | 3 |
this.groupByFields = new Fields(builder); |
36 | 3 |
this.havingConditions = new Conditions(builder); |
37 | 3 |
this.orderByFields = new Fields(builder); |
38 |
} |
|
39 |
|
|
40 |
protected final IBuilder builder;
|
|
41 |
|
|
42 |
private final Fields selectFields;
|
|
43 |
|
|
44 |
private final Tables fromTables;
|
|
45 |
|
|
46 |
private final Conditions whereConditions;
|
|
47 |
|
|
48 |
private final Fields groupByFields;
|
|
49 |
|
|
50 |
private final Conditions havingConditions;
|
|
51 |
|
|
52 |
private final Fields orderByFields;
|
|
53 |
|
|
54 |
private boolean distinct = false; |
|
55 |
|
|
56 |
private DateFormat dateFormat = new SimpleDateFormat("yyyy/mm/dd"); |
|
57 |
|
|
58 | 0 |
public Conditions newConditions() {
|
59 | 0 |
return new Conditions(this.builder); |
60 |
} |
|
61 |
|
|
62 | 0 |
public Fields newFields() {
|
63 | 0 |
return new Fields(this.builder); |
64 |
} |
|
65 |
|
|
66 |
/**
|
|
67 |
* @return
|
|
68 |
*/
|
|
69 | 0 |
public DateFormat getDateFormat() {
|
70 | 0 |
return dateFormat;
|
71 |
} |
|
72 |
|
|
73 |
/**
|
|
74 |
* @param format
|
|
75 |
*/
|
|
76 | 0 |
public void setDateFormat(DateFormat format) { |
77 | 0 |
dateFormat = format; |
78 |
} |
|
79 |
|
|
80 |
/**
|
|
81 |
* SQLを生成します。 <br>
|
|
82 |
*
|
|
83 |
* @param buffer
|
|
84 |
* 生成されたSQLを保持するバッファ
|
|
85 |
*/
|
|
86 | 3 |
public void build(StringBuffer buffer) { |
87 | 3 |
builder.build(this, buffer);
|
88 |
} |
|
89 |
|
|
90 |
/**
|
|
91 |
* SQLを生成します。 <br>
|
|
92 |
*
|
|
93 |
* @return 生成されたSQL文字列
|
|
94 |
*/
|
|
95 | 3 |
public String build() {
|
96 | 3 |
StringBuffer dest = new StringBuffer();
|
97 | 3 |
build(dest); |
98 | 3 |
return dest.toString();
|
99 |
} |
|
100 |
|
|
101 |
/**
|
|
102 |
* 全てのフィールドやテーブルなど、追加されているものをクリアします。 <br>
|
|
103 |
* フィールドだけクリアしたいという場合には、このメソッドを使用せずに、 以下のように記述してください。
|
|
104 |
*
|
|
105 |
* <pre>
|
|
106 |
* DynamicSql sql = new DynamicSql();
|
|
107 |
* sql.addField("XXX");
|
|
108 |
* sql.addFrom("AAA");
|
|
109 |
* //・・・・・
|
|
110 |
* sql.getSelectFields.clear(); //フィールドだけ削除
|
|
111 |
*
|
|
112 |
* </pre>
|
|
113 |
*/
|
|
114 | 0 |
public void clear() { |
115 | 0 |
selectFields.clear(); |
116 | 0 |
fromTables.clear(); |
117 | 0 |
whereConditions.clear(); |
118 | 0 |
orderByFields.clear(); |
119 | 0 |
groupByFields.clear(); |
120 | 0 |
havingConditions.clear(); |
121 | 0 |
distinct = false;
|
122 |
} |
|
123 |
|
|
124 |
/**
|
|
125 |
* distinctするSQLを生成するかどうかを表すフラグです。
|
|
126 |
*
|
|
127 |
* @return
|
|
128 |
*/
|
|
129 | 2 |
public boolean isDistinct() { |
130 | 2 |
return distinct;
|
131 |
} |
|
132 |
|
|
133 |
/**
|
|
134 |
* distinctするSQLを生成するかどうかを指定できます。
|
|
135 |
*
|
|
136 |
* @param b
|
|
137 |
*/
|
|
138 | 0 |
public void setDistinct(boolean b) { |
139 | 0 |
distinct = b; |
140 |
} |
|
141 |
|
|
142 |
/**
|
|
143 |
* @return
|
|
144 |
*/
|
|
145 | 6 |
public Tables getFromTables() {
|
146 | 6 |
return fromTables;
|
147 |
} |
|
148 |
|
|
149 |
/**
|
|
150 |
* @return
|
|
151 |
*/
|
|
152 | 3 |
public Fields getGroupByFields() {
|
153 | 3 |
return groupByFields;
|
154 |
} |
|
155 |
|
|
156 |
/**
|
|
157 |
* @return
|
|
158 |
*/
|
|
159 | 3 |
public Conditions getHavingConditions() {
|
160 | 3 |
return havingConditions;
|
161 |
} |
|
162 |
|
|
163 |
/**
|
|
164 |
* @return
|
|
165 |
*/
|
|
166 | 3 |
public Fields getOrderByFields() {
|
167 | 3 |
return orderByFields;
|
168 |
} |
|
169 |
|
|
170 |
/**
|
|
171 |
* @return
|
|
172 |
*/
|
|
173 | 9 |
public Fields getSelectFields() {
|
174 | 9 |
return selectFields;
|
175 |
} |
|
176 |
|
|
177 |
/**
|
|
178 |
* @return
|
|
179 |
*/
|
|
180 | 7 |
public Conditions getWhereConditions() {
|
181 | 7 |
return whereConditions;
|
182 |
} |
|
183 |
|
|
184 |
} |
|