Permalink
Browse files

Remove some dupplication.

  • Loading branch information...
1 parent a7af68f commit 2b00a9ca6d222dc5e0ec4fe5448f423fc653de0b @detinho committed Apr 10, 2012
Showing with 17 additions and 29 deletions.
  1. +17 −29 src/main/java/br/com/detinho/sqlbuilder/Select.java
@@ -3,6 +3,7 @@
import static br.com.detinho.sqlbuilder.SqlBuilder.col;
import static br.com.detinho.sqlbuilder.StringUtils.removeTrailingComma;
+import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
@@ -79,17 +80,7 @@ private String generateColumnsSql(String sql) {
}
private String generateTablesSql(String sql) {
- String tabelas = "";
- for (Table t : tables) {
- tabelas += t.write() + ", ";
- }
-
- if (! "".equals(tabelas)) {
- tabelas = removeTrailingComma(tabelas);
- sql += " FROM ";
- sql += tabelas;
- }
- return sql;
+ return sql + writeSql("FROM", tables);
}
private String generateJoinsSql(String sql) {
@@ -103,31 +94,16 @@ private String generateWhereSql(String sql) {
if (criteria != null) {
sql += " WHERE " + criteria.write();
}
+
return sql;
}
private String generateOrderSql(String sql) {
- String orderSql = "";
- for (OrderBy order : orders) {
- orderSql += order.write() + ", ";
- }
-
- if (!orderSql.equals("")) {
- sql += " ORDER BY " + removeTrailingComma(orderSql);
- }
- return sql;
+ return sql + writeSql("ORDER BY", orders);
}
private String generateGroupSql(String sql) {
- String groupSql = "";
- for (GroupBy group : groups) {
- groupSql += group.write() + ", ";
- }
-
- if (!groupSql.equals("")) {
- sql += " GROUP BY " + removeTrailingComma(groupSql);
- }
- return sql;
+ return sql + writeSql("GROUP BY", groups);
}
public void where(String columnAlias, String operator, Scalar value) {
@@ -202,4 +178,16 @@ public void orderBy(String alias, OrderType type) {
public void groupBy(String alias) {
groups.add(new GroupBy(alias));
}
+
+ private static String writeSql(String clause, Collection<? extends Writable> writables) {
+ String finalSql = "";
+ for (Writable writable : writables) {
+ finalSql += writable.write() + ", ";
+ }
+
+ if (!finalSql.equals("")) {
+ finalSql = String.format(" %s %s", clause, removeTrailingComma(finalSql));
+ }
+ return finalSql;
+ }
}

0 comments on commit 2b00a9c

Please sign in to comment.