68a7e680a097abef790987630a488bc8dbfdb887
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.sql / src / net / sourceforge / phpdt / sql / sql / FilterSort.java
1 package net.sourceforge.phpdt.sql.sql;
2
3 import java.util.ArrayList;
4
5 public class FilterSort {
6         private ArrayList filterList = new ArrayList();
7         private ArrayList sortList = new ArrayList();
8         public void addFilter(String column, String operator, String value, boolean isString) {
9                 FilterRow row = new FilterRow();
10                 row.column = column;
11                 row.operator = operator;
12                 row.value = value;
13                 row.isString = isString;
14                 filterList.add(row);
15         } 
16         public void clearFilters() {
17                 filterList.clear();
18         }
19         public void addSort(String column) {
20                 sortList.add(column);
21         }
22         public void removeSort(String column) {
23                 sortList.remove(column);
24         }
25         public String toString() {
26                 StringBuffer text = new StringBuffer();
27                 if (filterList.size() > 0) {
28                         text.append(" WHERE ");
29                         for (int i = 0; i < filterList.size(); i++) {
30                                 FilterRow row = (FilterRow) filterList.get(i);
31                                 text.append(row.column);
32                                 text.append(" ");
33                                 text.append(row.operator);
34                                 text.append(" ");
35                                 if (row.isString) {
36                                         text.append(escape(row.value));
37                                 } else {
38                                         text.append(row.value);
39                                 }
40                                 text.append(" ");
41                                 if (i < filterList.size() - 1) {
42                                         text.append("AND ");
43                                 }
44                         }
45                 }
46                 if (sortList.size() > 0) {
47                         text.append(" ORDER BY ");
48                         for (int i = 0; i < sortList.size(); i++) {
49                                 String value = (String) sortList.get(i);
50                                 text.append(value);
51                                 if (i < sortList.size() - 1) {
52                                         text.append(",");
53                                 }
54                         }
55                 }
56                 return text.toString();
57         }
58         public static String escape(String original) {
59                 return '\'' + original + '\'';
60         }
61 }
62
63 class FilterRow {
64         public String column;
65         public String operator;
66         public String value;
67         public boolean isString;
68 }